Skip to content

Commit 7e8a330

Browse files
authored
Merge pull request #67488 from bnbarham/better-note-locations
[Diagnostics] Improve diagnostics involving macros
2 parents 8c82b6b + 9691060 commit 7e8a330

11 files changed

+111
-97
lines changed

include/swift/AST/DiagnosticsCommon.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ ERROR(unknown_attribute,none,
221221
// MARK: macro diagnostics
222222
//------------------------------------------------------------------------------
223223
NOTE(in_macro_expansion,none,
224-
"in expansion of macro %0 here", (DeclName))
224+
"in expansion of macro %0%select{| on %kind1}1 here", (DeclName, const Decl *))
225225
ERROR(macro_experimental,none,
226226
"%0 macros are an experimental feature that is not enabled %select{|(%1)}1",
227227
(StringRef, StringRef))

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7243,8 +7243,8 @@ ERROR(extension_macro_invalid_conformance,none,
72437243
"invalid protocol conformance %0 in extension macro",
72447244
(Type))
72457245
ERROR(macro_attached_to_invalid_decl,none,
7246-
"'%0' macro cannot be attached to %1",
7247-
(StringRef, DescriptiveDeclKind))
7246+
"'%0' macro cannot be attached to %1 (%base2)",
7247+
(StringRef, DescriptiveDeclKind, const Decl *))
72487248
ERROR(conformance_macro,none,
72497249
"conformance macros are replaced by extension macros",
72507250
())

lib/AST/DiagnosticEngine.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,11 +1406,25 @@ DiagnosticEngine::getGeneratedSourceBufferNotes(SourceLoc loc) {
14061406
case GeneratedSourceInfo::PeerMacroExpansion:
14071407
case GeneratedSourceInfo::ConformanceMacroExpansion:
14081408
case GeneratedSourceInfo::ExtensionMacroExpansion: {
1409-
SourceRange origRange = expansionNode.getSourceRange();
14101409
DeclName macroName = getGeneratedSourceInfoMacroName(*generatedInfo);
14111410

1412-
Diagnostic expansionNote(diag::in_macro_expansion, macroName);
1413-
expansionNote.setLoc(origRange.Start);
1411+
// If it was an expansion of an attached macro, increase the range to
1412+
// include the decl's attributes. Also add the name of the decl the macro
1413+
// is attached to.
1414+
CustomAttr *attachedAttr = generatedInfo->attachedMacroCustomAttr;
1415+
Decl *attachedDecl =
1416+
attachedAttr ? expansionNode.dyn_cast<Decl *>() : nullptr;
1417+
SourceRange origRange = attachedDecl
1418+
? attachedDecl->getSourceRangeIncludingAttrs()
1419+
: expansionNode.getSourceRange();
1420+
1421+
Diagnostic expansionNote(diag::in_macro_expansion, macroName,
1422+
attachedDecl);
1423+
if (attachedAttr) {
1424+
expansionNote.setLoc(attachedAttr->getLocation());
1425+
} else {
1426+
expansionNote.setLoc(origRange.Start);
1427+
}
14141428
expansionNote.addRange(
14151429
Lexer::getCharSourceRangeFromSourceRange(SourceMgr, origRange));
14161430
expansionNote.setIsChildNote(true);

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3690,10 +3690,9 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
36903690
for (auto *roleAttr : macro->getAttrs().getAttributes<MacroRoleAttr>()) {
36913691
auto role = roleAttr->getMacroRole();
36923692
if (isInvalidAttachedMacro(role, D)) {
3693-
diagnoseAndRemoveAttr(attr,
3694-
diag::macro_attached_to_invalid_decl,
3693+
diagnoseAndRemoveAttr(attr, diag::macro_attached_to_invalid_decl,
36953694
getMacroRoleString(role),
3696-
D->getDescriptiveKind());
3695+
D->getDescriptiveKind(), D);
36973696
}
36983697
}
36993698

test/Macros/accessor_macros.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,5 @@ struct MyBrokenStruct {
114114

115115
@myPropertyWrapper
116116
struct CannotHaveAccessors {}
117-
// CHECK-DIAGS: 'accessor' macro cannot be attached to struct
117+
// CHECK-DIAGS: 'accessor' macro cannot be attached to struct ('CannotHaveAccessors')
118118
#endif

test/Macros/macro_expand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ macro Invalid() = #externalMacro(module: "MacroDefinition", type: "InvalidMacro"
7676

7777
@Invalid
7878
struct Bad {}
79-
// expected-note@-1 18 {{in expansion of macro 'Invalid' here}}
79+
// expected-note@-2 18 {{in expansion of macro 'Invalid' on struct 'Bad' here}}
8080

8181
// CHECK-DIAGS: error: macro expansion cannot introduce import
8282
// CHECK-DIAGS: error: macro expansion cannot introduce precedence group

test/Macros/macro_expand_attributes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ expansionOrder.originalMember = 28
144144
#if TEST_DIAGNOSTICS
145145
@wrapAllProperties
146146
typealias A = Int
147-
// expected-error@-2{{'memberAttribute' macro cannot be attached to type alias}}
147+
// expected-error@-2{{'memberAttribute' macro cannot be attached to type alias ('A')}}
148148

149149
@wrapAllProperties
150150
func noMembers() {}
151-
// expected-error@-2{{'memberAttribute' macro cannot be attached to global function}}
151+
// expected-error@-2{{'memberAttribute' macro cannot be attached to global function ('noMembers')}}
152152
#endif

test/Macros/macro_expand_extensions.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,18 @@ func testLocal() {
113113

114114
@DelegatedConformance
115115
typealias A = Int
116-
// expected-error@-2 {{'extension' macro cannot be attached to type alias}}
116+
// expected-error@-2 {{'extension' macro cannot be attached to type alias ('A')}}
117117

118118
@DelegatedConformance
119119
extension Int {}
120-
// expected-error@-2 {{'extension' macro cannot be attached to extension}}
120+
// expected-error@-2 {{'extension' macro cannot be attached to extension (extension of 'Int')}}
121121

122122
@attached(extension, conformances: P)
123123
macro UndocumentedNamesInExtension() = #externalMacro(module: "MacroDefinition", type: "DelegatedConformanceViaExtensionMacro")
124124

125125
@UndocumentedNamesInExtension
126126
struct S<Element> {}
127-
// expected-note@-1 {{in expansion of macro 'UndocumentedNamesInExtension' here}}
127+
// expected-note@-2 {{in expansion of macro 'UndocumentedNamesInExtension' on generic struct 'S' here}}
128128

129129
// CHECK-DIAGS: error: declaration name 'requirement()' is not covered by macro 'UndocumentedNamesInExtension'
130130

@@ -133,7 +133,7 @@ macro UndocumentedConformanceInExtension() = #externalMacro(module: "MacroDefini
133133

134134
@UndocumentedConformanceInExtension
135135
struct InvalidConformance<Element> {}
136-
// expected-note@-1 {{in expansion of macro 'UndocumentedConformanceInExtension' here}}
136+
// expected-note@-2 {{in expansion of macro 'UndocumentedConformanceInExtension' on generic struct 'InvalidConformance' here}}
137137

138138
// CHECK-DIAGS: error: conformance to 'P' is not covered by macro 'UndocumentedConformanceInExtension'
139139

@@ -142,7 +142,7 @@ macro UndocumentedCodable() = #externalMacro(module: "MacroDefinition", type: "A
142142

143143
@UndocumentedCodable
144144
struct TestUndocumentedCodable {}
145-
// expected-note@-1 {{in expansion of macro 'UndocumentedCodable' here}}
145+
// expected-note@-2 {{in expansion of macro 'UndocumentedCodable' on struct 'TestUndocumentedCodable' here}}
146146

147147
// CHECK-DIAGS: error: conformance to 'Codable' (aka 'Decodable & Encodable') is not covered by macro 'UndocumentedCodable'
148148

@@ -151,7 +151,7 @@ macro UndocumentedEncodable() = #externalMacro(module: "MacroDefinition", type:
151151

152152
@UndocumentedEncodable
153153
struct TestUndocumentedEncodable {}
154-
// expected-note@-1 {{in expansion of macro 'UndocumentedEncodable' here}}
154+
// expected-note@-2 {{in expansion of macro 'UndocumentedEncodable' on struct 'TestUndocumentedEncodable' here}}
155155

156156
// CHECK-DIAGS: error: conformance to 'Codable' (aka 'Decodable & Encodable') is not covered by macro 'UndocumentedEncodable'
157157

test/Macros/macro_expand_peers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func testVarPeer() {
217217
#if TEST_DIAGNOSTICS
218218
// Macros cannot be attached to function parameters
219219

220-
// expected-error@+1{{'peer' macro cannot be attached to parameter}}
220+
// expected-error@+1{{'peer' macro cannot be attached to parameter ('x')}}
221221
func test(@declareVarValuePeer x: Int) {}
222222
#endif
223223

test/Macros/macro_expand_synthesized_members.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ print(ElementType.paper.unknown())
104104

105105
#if TEST_DIAGNOSTICS
106106
@addMembersQuotedInit
107-
struct S2 { // expected-note{{in expansion of macro 'addMembersQuotedInit' here}}
107+
struct S2 {
108+
// expected-note@-2 {{in expansion of macro 'addMembersQuotedInit' on struct 'S2' here}}
108109
func useSynthesized() {
109110
S.method()
110111
print(type(of: getStorage()))

0 commit comments

Comments
 (0)