Skip to content

Commit 4ef35c5

Browse files
committed
Embrace the swift-syntax notion of Fix-Its as separate notes.
The swift-syntax diagnostic system always treats Fix-Its as separate notes, which are never attached to the primary diagnostic. Embrace this module in the mapping over to the existing C++ diagnostic engine.
1 parent f8e98ca commit 4ef35c5

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
lines changed

lib/ASTGen/Sources/ASTGen/Diagnostics.swift

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,47 +91,34 @@ 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-
99-
// Collect all of the Fix-It changes based on their Fix-It ID.
100-
var fixItChangesByID: [MessageID : [FixIt.Change]] = [:]
101-
for fixIt in diagnostic.fixIts {
102-
let id = knownNoteIDs.contains(fixIt.message.fixItID)
103-
? fixIt.message.fixItID
104-
: diagnostic.diagnosticID
105-
fixItChangesByID[id, default: []]
106-
.append(contentsOf: fixIt.changes.changes)
107-
}
108-
10994
// Emit the main diagnostic
11095
emitDiagnosticParts(
11196
diagEnginePtr: diagEnginePtr,
11297
sourceFileBuffer: sourceFileBuffer,
11398
message: diagnostic.diagMessage.message + (messageSuffix ?? ""),
11499
severity: diagnostic.diagMessage.severity,
115100
position: diagnostic.position,
116-
highlights: diagnostic.highlights,
117-
fixItChanges: fixItChangesByID[diagnostic.diagnosticID] ?? []
101+
highlights: diagnostic.highlights
118102
)
119103

120-
fixItChangesByID.removeValue(forKey: diagnostic.diagnosticID)
104+
// Emit Fix-Its.
105+
for fixIt in diagnostic.fixIts {
106+
emitDiagnosticParts(
107+
diagEnginePtr: diagEnginePtr,
108+
sourceFileBuffer: sourceFileBuffer,
109+
message: fixIt.message.message,
110+
severity: .note, position: diagnostic.position,
111+
fixItChanges: fixIt.changes.changes
112+
)
113+
}
121114

122115
// Emit any notes as follow-ons.
123116
for note in diagnostic.notes {
124117
emitDiagnosticParts(
125118
diagEnginePtr: diagEnginePtr,
126119
sourceFileBuffer: sourceFileBuffer,
127120
message: note.message,
128-
severity: .note, position: note.position,
129-
fixItChanges: fixItChangesByID[note.noteMessage.fixItID] ?? []
121+
severity: .note, position: note.position
130122
)
131-
132-
fixItChangesByID.removeValue(forKey: note.noteMessage.fixItID)
133123
}
134-
135-
// All Fix-Its must have been removed by the code above.
136-
assert(fixItChangesByID.isEmpty)
137124
}

test/Macros/macro_expand.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ struct OnlyAdds {
5757
func testAddBlocker(a: Int, b: Int, c: Int, oa: OnlyAdds) {
5858
_ = #addBlocker(a * b * c)
5959
#if TEST_DIAGNOSTICS
60-
_ = #addBlocker(a + b * c) // expected-error{{blocked an add; did you mean to subtract? (from macro 'addBlocker')}}{{21-22=-}}
61-
_ = #addBlocker(oa + oa) // expected-error{{blocked an add; did you mean to subtract? (from macro 'addBlocker')}}{{22-23=-}}
62-
// expected-note@-1{{in expansion of macro 'addBlocker' here}}
60+
_ = #addBlocker(a + b * c) // expected-error{{blocked an add; did you mean to subtract? (from macro 'addBlocker')}}
61+
// expected-note@-1{{use '-'}}{{21-22=-}}
62+
_ = #addBlocker(oa + oa) // expected-error{{blocked an add; did you mean to subtract? (from macro 'addBlocker')}}
63+
// expected-note@-1{{in expansion of macro 'addBlocker' here}}
64+
// expected-note@-2{{use '-'}}{{22-23=-}}
6365
#endif
6466
}

test/Parse/new_parser_diagnostics.swift

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

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

0 commit comments

Comments
 (0)