Skip to content
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

improve diagnostic for unnamed closure parameters #2727

Conversation

AppAppWorks
Copy link
Contributor

fix #2340

@AppAppWorks
Copy link
Contributor Author

@bnbarham would you mind reviewing this PR too? :)

Sources/SwiftParser/Parameters.swift Outdated Show resolved Hide resolved
Sources/SwiftParser/Parameters.swift Outdated Show resolved Hide resolved
Tests/SwiftParserTest/translated/RecoveryTests.swift Outdated Show resolved Hide resolved
@AppAppWorks AppAppWorks force-pushed the improve-diagnostic-for-unnamed-closure-parameter branch from e0533a8 to 3d54574 Compare July 16, 2024 02:01
@AppAppWorks
Copy link
Contributor Author

Updated, please have a look.

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thank you @AppAppWorks. Looks great!

@ahoppen
Copy link
Member

ahoppen commented Jul 16, 2024

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge July 16, 2024 16:34
@AppAppWorks
Copy link
Contributor Author

AppAppWorks commented Jul 16, 2024

@ahoppen I've found that some of my code comments are misleading and the test case for missing both identifier and type is missing.

I've rewritten the comments and added a new test case as follows, if you also think they're necessary and good, I'll force-commit again.

let names = self.parseParameterNames()
var colon = self.consume(if: .colon)
// try to parse the type regardless of the presence of the preceding colon
// to tackle any unnamed parameter or missing colon
// e.g. [X], (:[X]) or (x [X])
let canParseType = withLookahead { $0.canParseType() }
let type: RawTypeSyntax?
if canParseType {
  type = self.parseType(misplacedSpecifiers: misplacedSpecifiers)
  if colon == nil {
    // mark the preceding colon as missing if the type is present
    // e.g. [X] or (x [X])
    colon = missingToken(.colon)
  }
} else if colon != nil {
  // mark the type as missing if the preceding colon is present
  // e.g. (:) or (_:)
  type = RawTypeSyntax(RawMissingTypeSyntax(arena: self.arena))
} else {
  type = nil
}
assertParse(
  """
  test { (1️⃣:2️⃣) in }
  """,
  diagnostics: [
    DiagnosticSpec(
      locationMarker: "1️⃣",
      message: "expected identifier in parameter",
      fixIts: ["insert identifier"]
    ),
    DiagnosticSpec(
      locationMarker: "2️⃣",
      message: "expected type in parameter",
      fixIts: ["insert type"]
    ),
  ],
  fixedSource: """
    test { (<#identifier#>: <#type#>) in }
    """
)

@ahoppen
Copy link
Member

ahoppen commented Jul 16, 2024

Force push your improved changes and I’ll review them again.

- instead of treating as unrecognized, try to parse the remaining tokens as a type even if the preceding colon is missing
auto-merge was automatically disabled July 16, 2024 22:17

Head branch was pushed to by a user without write access

@AppAppWorks AppAppWorks force-pushed the improve-diagnostic-for-unnamed-closure-parameter branch from 3d54574 to 55cd304 Compare July 16, 2024 22:17
@AppAppWorks
Copy link
Contributor Author

Pushed, please check.

@ahoppen ahoppen enabled auto-merge July 16, 2024 22:24
@ahoppen
Copy link
Member

ahoppen commented Jul 16, 2024

@swift-ci Please test

2 similar comments
@ahoppen
Copy link
Member

ahoppen commented Jul 17, 2024

@swift-ci Please test

@ahoppen
Copy link
Member

ahoppen commented Jul 18, 2024

@swift-ci Please test

@ahoppen ahoppen merged commit 5f8e664 into swiftlang:main Jul 18, 2024
3 checks passed
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.

Improve diagnostic for unnamed closure parameter
4 participants