Parens: handle special parsing of new T…
& inherit T…
#16239
Merged
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.
Followup to #16079.
new
orinherit
keywords.Constructor applications are handled differently in the parser after the
new
andinherit
keywords than they are elsewhere: only a subset of expressions are allowed as an argument (as specified inatomicExprAfterType
in pars.fsy). This means that any expression outside of that blessed subset must be parenthesized, even if the exact same ctor application would not require parentheses withoutnew
orinherit
.E.g.,
This is due to the following in pars.fsy:
fsharp/src/Compiler/pars.fsy
Line 4841 in 24ef671
fsharp/src/Compiler/pars.fsy
Line 5336 in 24ef671
fsharp/src/Compiler/pars.fsy
Lines 5042 to 5076 in 24ef671
These are required to support the use of postfix generic notation in those contexts (as noted in #16239 (comment)) and possibly other things. Even though the use of postfix notation in such contexts seems likely to be quite rare on the whole, removing support for it would be a breaking change.
Edit: click to see the question that was answered in the comments
Question for those in the know
(I can open a new issue for this if it makes sense.)
I've run into this inconsistency repeatedly over the years and it has always bothered me. Is the
atomicExprAfterType
restriction here actually still needed? Would that suddenly cause more problems like those addressed by #15923? Or could we just replace it withargExpr
in the parser fornew
andinherit
and get rid of the inconsistency? Making that change locally seems to work and doesn't seem to cause any immediate problems in this codebase......Ideally
new ctor expr
andctor expr
would be completely interchangeable (except, probably, in object expressions), but there is one additional inconsistency that would require more changes to the compiler to resolve. Explicit type arguments are required when the application is preceded bynew
but can be inferred whennew
is not present:ResizeArray 3
is allowed, butnew ResizeArray (3)
is not (regardless of parentheses around the arg).