Skip to content

Comments

Detect and add a fix-it for using : instead of -> in a function#3276

Open
j-f1 wants to merge 1 commit intomainfrom
jed/colon-to-arrow
Open

Detect and add a fix-it for using : instead of -> in a function#3276
j-f1 wants to merge 1 commit intomainfrom
jed/colon-to-arrow

Conversation

@j-f1
Copy link
Contributor

@j-f1 j-f1 commented Feb 18, 2026

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.

@j-f1
Copy link
Contributor Author

j-f1 commented Feb 18, 2026

@swift-ci please test

@j-f1 j-f1 force-pushed the jed/colon-to-arrow branch from d566fa0 to c1fcd35 Compare February 18, 2026 19:29
Copy link
Member

@rintaro rintaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is a very nice diagnostic that was missing in the new parser.

Comment on lines +1343 to +1357
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)
}
Copy link
Member

@rintaro rintaro Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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] {}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing-in the unexpectedColon, let's consume it in this function:

Suggested change
unexpectedColon: RawTokenSyntax? = nil
acceptColon: Bool = false

So we can use this for subscript too. This is a pretty common mistake

subscript(idx: Index): Element { ... }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants