-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Parse][Sema] Improve interpolation parsing and construction #24464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@swift-ci please smoke test |
a52ffd8
to
d77ea1a
Compare
@swift-ci please smoke test |
Thank you for looking into this! |
There were a number of mistakes in this test: • The whole thing was indented one space. • Some fix-it tests were malformed and therefore not being tested. • The output checking could in theory allow content before or after the intended content.
d77ea1a
to
d253999
Compare
@rintaro I've moved it to |
|
@swift-ci please smoke test |
Ah, I see that was caused by invalid |
|
||
virtual std::pair<bool, Expr *> walkToExprPre(Expr *E) { | ||
assert(!isa<InterpolatedStringLiteralExpr>(E) && | ||
"StrangeInterpolationRewriter found nested interpolation?"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some nested test cases? like:
"[\(foo: "=\(bar: 0)=")]"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do. (But it's safe because the only place nested interpolations can appear is as one of the parameters of an appendInterpolation(_:)
call, and we stop recursing when we see CallExpr
.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@swift-ci please build toolchain |
So…I accidentally made Cool. |
This defers diagnosis until a stage where #if conditions have definitely been evaluated, at the cost of a slightly more complex implementation. We’ll gain some of that complexity back in a subsequent refactoring. Fixes SR-9937.
Now that we manipulate the argument list to correct strange interpolations in Sema, we can parse interpolations directly as argument lists, simplifying the parser. By itself, this refactoring causes a code completion regression; a subsequent commit will fix that.
This change permits UnresolvedDotExpr to have both a name and a base that are implicit, but a valid DotLoc, and to treat that DotLoc as the node’s location. It then changes the generation of string interpolation code so that `$stringInterpolation.appendInterpolation` references have a DotLoc corresponding to the backslash in the string literal. This makes it possible for `ExprContextAnalyzer` in IDE to correctly detect when you are code-completing in a string interpolation and treat it as an `appendInterpolation` call.
0857b44
to
4f1e05c
Compare
Squashed away the intermediate state. |
@swift-ci please smoke test |
@swift-ci please smoke test and merge |
In Swift 4.2 mode, we currently diagnose and correct multi-argument and labeled interpolations during parsing. This isn't great because there are various modes where we parse code that won't actually compile in Swift 4.2 mode, like code in
#if swift(>=5)
blocks.This PR moves this diagnosis to Sema, specifically to
PreCheckExpression
. This then allows us to parse string interpolations literally as argument lists, which (with another small fix) is enough to let code completion offerappendInterpolation
overload signatures in a string interpolation segment.While I was there, I also cleaned up some issues with the strange interpolation warning test, such as fixits that weren't being checked properly and accidental indentation. It's best to review the commits separately so you can see which parts of the test changed.
Resolves SR-9937.