fix(parser): prefer the arrow reading for (args): t => body in expres… - #1
Open
seprov wants to merge 1 commit into
Open
fix(parser): prefer the arrow reading for (args): t => body in expres…#1seprov wants to merge 1 commit into
seprov wants to merge 1 commit into
Conversation
…sion position The lexer injects ES6_FUN when a parenthesized group is followed by => or :, and the multi-parser forks: one parser consumes the token (the arrow-function reading) and one ignores it (e.g. the expression reading). When the return annotation's type is followed by => and the arrow body is itself valid type syntax -- e.g. f((x: int): int => x), where int => x is also an arrow *type* -- both parsers run to completion, and the winner was whichever came first in the fork list: the expression reading, f((x: int) : (int => x)). That preference also breaks the printer round-trip: printing hoists a function body's type constraint into return-annotation position ((module M: SIG) => (module M: SUB) prints as (module M: SIG): (module SUB) => (module M)), and re-parsing that output under the expression preference yields an ill-typed constraint instead of the original function -- refmt could turn a compiling program into a non-compiling one. Put the forked (arrow) parser first instead. This matches how the same text already parses in let-binding position, where the bare-constraint reading is not grammatical. Constraining an expression against an arrow type remains expressible with a parenthesized type: (e: ((a, b) => t)); the one fixture relying on the bare form (wrapping-re.t, df_locallyAbstractFuncAnnotated: 'figureMeOut) is updated to that spelling and formats back to byte-identical output. Fixes reasonml#2925 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…sion position
The lexer injects ES6_FUN when a parenthesized group is followed by => or :, and the multi-parser forks: one parser consumes the token (the arrow-function reading) and one ignores it (e.g. the expression reading). When the return annotation's type is followed by => and the arrow body is itself valid type syntax -- e.g. f((x: int): int => x), where int => x is also an arrow type -- both parsers run to completion, and the winner was whichever came first in the fork list: the expression reading, f((x: int) : (int => x)).
That preference also breaks the printer round-trip: printing hoists a function body's type constraint into return-annotation position ((module M: SIG) => (module M: SUB) prints as
(module M: SIG): (module SUB) => (module M)), and re-parsing that output under the expression preference yields an ill-typed constraint instead of the original function -- refmt could turn a compiling program into a non-compiling one.
Put the forked (arrow) parser first instead. This matches how the same text already parses in let-binding position, where the bare-constraint reading is not grammatical. Constraining an expression against an arrow type remains expressible with a parenthesized type: (e: ((a, b) => t)); the one fixture relying on the bare form (wrapping-re.t, df_locallyAbstractFuncAnnotated: 'figureMeOut) is updated to that spelling and formats back to byte-identical output.
Fixes reasonml#2925