Detect and add a fix-it for using : instead of -> in a function#3276
Detect and add a fix-it for using : instead of -> in a function#3276
Conversation
|
@swift-ci please test |
d566fa0 to
c1fcd35
Compare
rintaro
left a comment
There was a problem hiding this comment.
Thanks! This is a very nice diagnostic that was missing in the new parser.
| let unexpectedBeforeArrow: RawUnexpectedNodesSyntax? | ||
| let arrow: RawTokenSyntax | ||
| if let unexpectedColon { | ||
| let diagnostic = TokenDiagnostic( | ||
| .expectedArrowBeforeReturnType, | ||
| byteOffset: unexpectedColon.leadingTriviaByteLength | ||
| ) | ||
| unexpectedBeforeArrow = nil | ||
| arrow = unexpectedColon.tokenView.withTokenDiagnostic( | ||
| tokenDiagnostic: diagnostic, | ||
| arena: arena | ||
| ) | ||
| } else { | ||
| (unexpectedBeforeArrow, arrow) = self.expect(.arrow) | ||
| } |
There was a problem hiding this comment.
TokenDiagnostic is mainly for Lexer errors (and some white space errors).
For cases like this, we prefer to put : token to unexpectedBeforeArrow and create missing -> token, then handle that pattern in ParseDiagnosticsGenerator.
I.e.
let (unexpectedBeforeArrow, arrow) = if let colon = self.consume(if: .colon) {
(RawUnexpectedNodesSyntax([colon], arena: self.arena), self.missingToken(.arrow))
} else {
self.expect(.arrow)
}There was a problem hiding this comment.
I tried with that instead, but the fix-it produced this. How do you suggest I get it to output just the arrow?
-func foo() : [String] {}
+func foo() : -> [String] {}There was a problem hiding this comment.
Sorry I accidentally removed the first part of the comment.
You'd need to handle that pattern in ParseDiagnosticsGenerator. visit(_: ReturnClauseSyntax) and generate appropriate diagnostics
| effectSpecifiers: inout (some RawMisplacedEffectSpecifiersTrait)?, | ||
| allowNamedOpaqueResultType: Bool | ||
| allowNamedOpaqueResultType: Bool, | ||
| unexpectedColon: RawTokenSyntax? = nil |
There was a problem hiding this comment.
Instead of passing-in the unexpectedColon, let's consume it in this function:
| unexpectedColon: RawTokenSyntax? = nil | |
| acceptColon: Bool = false |
So we can use this for subscript too. This is a pretty common mistake
subscript(idx: Index): Element { ... }
I accidentally do this quite regularly when changing a computed property to a method. This change adds a fix-it (including adding a space before the inserted
->) to make it a little easier to fix.