Skip to content

Allow leading dot syntax for StaticParserError #951

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

Merged
merged 3 commits into from
Oct 17, 2022

Conversation

hamishknight
Copy link
Contributor

@hamishknight hamishknight commented Oct 15, 2022

Convert StaticParserError into a struct, and define the diagnostics in a DiagnosticMessage where Self == StaticParserError extension. This allows us to use leading dot syntax to pass a StaticParserError anywhere a DiagnosticMessage is expected. Also, do the same for StaticParserFixIt.

To compute the diagnostic ID, we can (ab)use #function to pick up the name of the property

@hamishknight
Copy link
Contributor Author

/Users/ec2-user/jenkins/workspace/swift-syntax-PR-macOS/branch-main/swift-syntax/Sources/SwiftParser/Diagnostics/ParseDiagnosticsGenerator.swift:442:5: error: type of expression is ambiguous without more context
    exchangeTokens(
    ^~~~~~~~~~~~~~~

:( wonder what swift version CI is using

@hamishknight
Copy link
Contributor Author

Looks like the CI is using Swift 5.5, which should be fine as it should have SE-0299, but it seems the 5.5 constraint system is barfing when a closure is involved (works on 5.6 though). Adding a generic parameter to exchangeTokens & removeToken should work around the issue.

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.

Neat idea 🤩 I’ve got a couple of additional suggestion/ideas:

  • Could you add a test case that checks the diagnostic ID for one StaticParserError? I would like to see how the diagnostic ID looks like (basically making sure it contains no parentheses)
  • I find it harder to find diagnostic messages in the new extension but I think that’s resolved by formatting the diagnostic messages as single lines, e.g.
public static var allStatmentsInSwitchMustBeCoveredByCase: Self { .init("all statements inside a switch must be covered by a 'case' or 'default' label") }
  • I think we can do the same for StaticParserFixIt.

@hamishknight
Copy link
Contributor Author

Could you add a test case that checks the diagnostic ID for one StaticParserError? I would like to see how the diagnostic ID looks like (basically making sure it contains no parentheses)

We already have one :)

I find it harder to find diagnostic messages in the new extension but I think that’s resolved by formatting the diagnostic messages as single lines, e.g.

Hmm, I definitely prefer the current formatting. Putting them on a single line means that each diagnostic message starts at a different column (depending on how long the var name is), which make it hard to read IMO. It also makes it extra hard for people with smaller screens, e.g:

Screen Shot 2022-10-17 at 10 55 39

I think we can do the same for StaticParserFixIt

👍

@hamishknight
Copy link
Contributor Author

Bleh, the 5.5 compiler really seems to struggle with these:

/Users/ec2-user/jenkins/workspace/swift-syntax-PR-macOS/branch-main/swift-syntax/Sources/SwiftParser/Diagnostics/ParseDiagnosticsGenerator.swift:192:43: error: missing argument label 'fromProtocol:' in call
              newNode: Syntax(TokenSyntax.identifier("`\(invalidIdentifier.text)`", leadingTrivia: invalidIdentifier.leadingTrivia, trailingTrivia: invalidIdentifier.trailingTrivia))
                                          ^
                              fromProtocol: 
/Users/ec2-user/jenkins/workspace/swift-syntax-PR-macOS/branch-main/swift-syntax/Sources/SwiftParser/Diagnostics/ParseDiagnosticsGenerator.swift:268:12: error: type 'FixIt.Change' has no member 'remove'
          .remove(unexpected: unexpected)
          ~^~~~~~

going to add back the StaticParserFixIt overloads on FixIt, which we can drop once 5.6 is the minimum required Swift version.

@hamishknight hamishknight force-pushed the static-dot branch 3 times, most recently from 5d6201f to 1d11fc6 Compare October 17, 2022 11:17
Convert StaticParserError into a struct, and
define the diagnostics in a
`DiagnosticMessage where Self == StaticParserError`
extension. This allows us to use leading dot syntax
to pass a StaticParserError anywhere a DiagnosticMessage
is expected.

To compute the diagnostic ID, we can (ab)use
`#function` to pick up the name of the property.
Following a similar pattern to StaticParserError,
convert StaticParserFixIt into a struct with
static members in a constrained protocol extension.
@hamishknight
Copy link
Contributor Author

@swift-ci please test

@hamishknight hamishknight merged commit 98a260b into swiftlang:main Oct 17, 2022
@hamishknight hamishknight deleted the static-dot branch October 17, 2022 15:21
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