Skip to content

Improve diagnostic if where clause is missing a requirement #1281

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,20 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
return .visitChildren
}

public override func visit(_ node: SameTypeRequirementSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
}
if node.equalityToken.presence == .missing && node.rightTypeIdentifier.isMissingAllTokens {
addDiagnostic(
node.equalityToken,
.missingConformanceRequirement,
handledNodes: [node.equalityToken.id, node.rightTypeIdentifier.id]
)
}
return .visitChildren
}

public override func visit(_ node: SourceFileSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ extension DiagnosticMessage where Self == StaticParserError {
public static var missingColonInTernaryExpr: Self {
.init("expected ':' after '? ...' in ternary expression")
}
public static var missingConformanceRequirement: Self {
.init("expected ':' or '==' to indicate a conformance or same-type requirement")
}
public static var misspelledAsync: Self {
.init("expected async specifier; did you mean 'async'?")
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftParserTest/AttributeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ final class AttributeTests: XCTestCase {
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ':' and parameters in '@differentiable' argument", fixIts: ["insert ':' and parameters"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '=' and right-hand type in same type requirement", fixIts: ["insert '=' and right-hand type"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ':' or '==' to indicate a conformance or same-type requirement"),
Copy link
Contributor

Choose a reason for hiding this comment

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

We could suggest both as fix-its, but otherwise LGTM

DiagnosticSpec(locationMarker: "2️⃣", message: "expected ')' to end attribute", fixIts: ["insert ')'"]),
],
fixedSource: """
@differentiable(reverse wrt: <#identifier#>,where T = <#type#>)
@differentiable(reverse wrt: <#identifier#>,where T)
func podcastPlaybackSpeed() {
}
"""
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftParserTest/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ final class DeclarationTests: XCTestCase {
AssertParse(
"class T where t1️⃣",
diagnostics: [
DiagnosticSpec(message: "expected '=' and right-hand type in same type requirement"),
DiagnosticSpec(message: "expected ':' or '==' to indicate a conformance or same-type requirement"),
DiagnosticSpec(message: "expected member block in class"),
]
)
AssertParse(
"class B<where g1️⃣",
diagnostics: [
DiagnosticSpec(message: "expected '=' and right-hand type in same type requirement"),
DiagnosticSpec(message: "expected ':' or '==' to indicate a conformance or same-type requirement"),
DiagnosticSpec(message: "expected '>' to end generic parameter clause"),
DiagnosticSpec(message: "expected member block in class"),
]
Expand Down