Skip to content

Commit f8e98ca

Browse files
committed
[ASTGen] Make sure we handle Fix-Its without a matching note.
Any Fix-Its not associated with a note are automatically associated with the main diagnostic.
1 parent 33589de commit f8e98ca

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/ASTGen/Sources/ASTGen/Diagnostics.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,18 @@ func emitDiagnostic(
9191
diagnostic: Diagnostic,
9292
messageSuffix: String? = nil
9393
) {
94+
// Determine the set of note IDs.
95+
let knownNoteIDs: Set<MessageID> = .init(
96+
diagnostic.notes.map { $0.noteMessage.fixItID }
97+
)
98+
9499
// Collect all of the Fix-It changes based on their Fix-It ID.
95100
var fixItChangesByID: [MessageID : [FixIt.Change]] = [:]
96101
for fixIt in diagnostic.fixIts {
97-
fixItChangesByID[fixIt.message.fixItID, default: []]
102+
let id = knownNoteIDs.contains(fixIt.message.fixItID)
103+
? fixIt.message.fixItID
104+
: diagnostic.diagnosticID
105+
fixItChangesByID[id, default: []]
98106
.append(contentsOf: fixIt.changes.changes)
99107
}
100108

@@ -109,6 +117,8 @@ func emitDiagnostic(
109117
fixItChanges: fixItChangesByID[diagnostic.diagnosticID] ?? []
110118
)
111119

120+
fixItChangesByID.removeValue(forKey: diagnostic.diagnosticID)
121+
112122
// Emit any notes as follow-ons.
113123
for note in diagnostic.notes {
114124
emitDiagnosticParts(
@@ -118,5 +128,10 @@ func emitDiagnostic(
118128
severity: .note, position: note.position,
119129
fixItChanges: fixItChangesByID[note.noteMessage.fixItID] ?? []
120130
)
131+
132+
fixItChangesByID.removeValue(forKey: note.noteMessage.fixItID)
121133
}
134+
135+
// All Fix-Its must have been removed by the code above.
136+
assert(fixItChangesByID.isEmpty)
122137
}

test/Parse/new_parser_diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
// REQUIRES: asserts
66

77
_ = [(Int) -> async throws Int]()
8-
// expected-error@-1{{'async throws' may only occur before '->'}}
8+
// expected-error@-1{{'async throws' may only occur before '->'}}{{15-21=}} {{21-28=}} {{20-21= }} {{12-12=async }} {{12-12=throws }}

0 commit comments

Comments
 (0)