[Parser] Fix String interpolation accepts invalid argument label syntax without expression #80928
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.
Description
resolves: #80927
The interpolated string "(1, f:)" compiles without error, despite f: being an argument label with no following expression.
It seems to be regression of SE-0439 Allow trailing comma in comma-separated lists. #74522.
Approach
In this PR, I fixed the bug by aligning the approach for trailing comma in StringInterpolation with the approach in other contexts.
When we think about valid case:
"\(10,)"
, In #74522,parseListItem
returnsParseListItemResult::Continue
when parser found right paren with trailing comma,)
in StringInterpolation thenparseExprPrimary
(triggered by the next iteration ofparseListItem
) will parse)
next to"\(10,
. Therefore, we wrote the specific logic for trailing comma in String Interpolation atparseExprPrimary
in order to enable trailing comma in String Interpolation.However, this logic has problem that incorrectly allow the pattern invalid argument label syntax without expression:
\(10, f:)
(since tokensf:
are consumed as optional before we callparseExprPrimary
.)Regarding the approach for trailing comma in other than String Interpolation such as function call,
parseListItem
returnsParseListItemResult::Finished
when parser found right paren with trailing comma,)
.. in this case we don't have to write additional logic inparseExprPrimary
and correctly emit error on the pattern:\(1, f:)
as well. (parseExprPrimary
originally correctly emit error on that pattern without the specific logic for trailing comma in String Interpolation)I this PR, to align the approach for trailing comma,
parseListItem
returnsParseListItemResult::FinishedInStringInterpolation
when parser found right paren with trailing comma,)
in String Interpolation.Alternative Considered
Add additional validation after calling
parseExpr
(and subsequentparseExprPrimary
).swift/lib/Parse/ParseExpr.cpp
Lines 3249 to 3253 in f31c89a
It's a good thing that the change is purely additive, but in the first place, writing StringInterpolation-specific logic directly in
parseExpr
may harm scalability, considering the possibility of future grammar changes and modifications toparseExpr
. Ensuring scalability in the long term requires aligning the approach for StringInterpolation with that of other contexts such as function calls..