Skip to content

Improve sorting of diagnostics at MissingDeclaration #836

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 1 commit into from
Sep 26, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ extension UnexpectedNodesSyntax {
}
}

extension Syntax {
func hasParent(_ expectedParent: Syntax) -> Bool {
var walk = self.parent
while walk != nil {
if walk == expectedParent {
return true
}
walk = walk?.parent
}
return false
}
}

fileprivate extension FixIt.Change {
/// Replaced a present node with a missing node
static func makeMissing(node: TokenSyntax) -> FixIt.Change {
Expand Down Expand Up @@ -59,7 +72,20 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
let diagProducer = ParseDiagnosticsGenerator()
diagProducer.walk(tree)
diagProducer.diagnostics.sort {
return $0.node.id.indexInTree < $1.node.id.indexInTree
if $0.position != $1.position {
return $0.position < $1.position
}

// Emit children diagnostics before parent diagnostics.
// This makes sure that for missing declarations with attributes, we emit diagnostics on the attribute before we complain about the missing declaration.
if $0.node.hasParent($1.node) {
return true
} else if $1.node.hasParent($0.node) {
return false
} else {
// If multiple tokens are missing at the same location, emit diagnostics about nodes that occur earlier in the tree first.
return $0.node.id.indexInTree < $1.node.id.indexInTree
}
}
return diagProducer.diagnostics
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/SwiftParserTest/Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ final class AttributeTests: XCTestCase {
}
""",
diagnostics: [
DiagnosticSpec(locationMarker: "DIAG_2", message: "Expected declaration after attribute"),
DiagnosticSpec(locationMarker: "DIAG_1", message: "Expected 'for' in attribute argument"),
DiagnosticSpec(locationMarker: "DIAG_1", message: "Expected ':' in attribute argument"),
DiagnosticSpec(locationMarker: "DIAG_2", message: "Expected ')' to end attribute"),
DiagnosticSpec(locationMarker: "DIAG_2", message: "Expected declaration after attribute"),
]
)
}
Expand Down Expand Up @@ -42,9 +42,9 @@ final class AttributeTests: XCTestCase {
@_specialize(e#^DIAG^#
""",
diagnostics: [
DiagnosticSpec(message: "Expected declaration after attribute"),
DiagnosticSpec(message: "Expected ':' in attribute argument"),
DiagnosticSpec(message: "Expected ')' to end attribute"),
DiagnosticSpec(message: "Expected declaration after attribute"),
]
)
}
Expand All @@ -55,10 +55,10 @@ final class AttributeTests: XCTestCase {
@_specialize(e#^DIAG_1^#, exported#^DIAG_2^#)#^DIAG_3^#
""",
diagnostics: [
DiagnosticSpec(locationMarker: "DIAG_3", message: "Expected declaration after attribute"),
DiagnosticSpec(locationMarker: "DIAG_1", message: "Expected ':' in attribute argument"),
DiagnosticSpec(locationMarker: "DIAG_2", message: "Expected ':' in attribute argument"),
DiagnosticSpec(locationMarker: "DIAG_2", message: "Expected 'false' in attribute argument"),
DiagnosticSpec(locationMarker: "DIAG_3", message: "Expected declaration after attribute"),
]
)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftParserTest/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -904,8 +904,8 @@ final class DeclarationTests: XCTestCase {
diagnostics: [
DiagnosticSpec(locationMarker: "OPENING_BRACE", message: "Expected '{' to start struct"),
DiagnosticSpec(locationMarker: "AFTER_POUND_IF", message: "Expected condition of conditional compilation clause"),
DiagnosticSpec(locationMarker: "END", message: "Expected declaration after attribute in conditional compilation clause"),
DiagnosticSpec(locationMarker: "END", message: "Expected name of attribute"),
DiagnosticSpec(locationMarker: "END", message: "Expected declaration after attribute in conditional compilation clause"),
DiagnosticSpec(locationMarker: "END", message: "Expected '#endif' in conditional compilation block"),
DiagnosticSpec(locationMarker: "END", message: "Expected '}' to end struct")
]
Expand Down