Skip to content

Commit c2a59bf

Browse files
committed
Tweak redecl checking’s handling of generated code
Macro expansions are now treated like a part of the source file they belong to, for purposes of the “second declaration is the one that’s diagnosed” rule. This helps stabilize a behavior that was easy to perturb.
1 parent 8a97ed9 commit c2a59bf

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,15 +618,15 @@ static void checkGenericParams(GenericContext *ownerCtx) {
618618
/// Returns \c true if \p current and \p other are in the same source file
619619
/// \em and \c current appears before \p other in that file.
620620
static bool isBeforeInSameFile(Decl *current, Decl *other) {
621-
if (current->getDeclContext()->getParentSourceFile() !=
622-
other->getDeclContext()->getParentSourceFile())
621+
if (current->getDeclContext()->getOutermostParentSourceFile() !=
622+
other->getDeclContext()->getOutermostParentSourceFile())
623623
return false;
624624

625-
if (!current->getLoc().isValid())
625+
if (current->getLoc().isInvalid() || other->getLoc().isInvalid())
626626
return false;
627627

628628
return current->getASTContext().SourceMgr
629-
.isBeforeInBuffer(current->getLoc(), other->getLoc());
629+
.isBefore(current->getLoc(), other->getLoc());
630630
}
631631

632632
template <typename T>

test/Macros/macro_expand.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ struct MemberNotCovered {
7272
// expected-note@-1 {{in expansion of macro 'NotCovered' here}}
7373

7474
// CHECK-DIAGS: error: declaration name 'value' is not covered by macro 'NotCovered'
75-
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX70_2_33_4361AD9339943F52AE6186DD51E04E91Ll10NotCoveredfMf_.swift
75+
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-5]]_2_33_4361AD9339943F52AE6186DD51E04E91Ll10NotCoveredfMf_.swift
7676
// CHECK-DIAGS: var value: Int
7777
// CHECK-DIAGS: END CONTENTS OF FILE
7878
}
@@ -126,6 +126,24 @@ struct Bad {}
126126
// CHECK-DIAGS: typealias _ImageLiteralType = Void
127127
// CHECK-DIAGS: typealias _FileReferenceLiteralType = Void
128128
// CHECK-DIAGS: END CONTENTS OF FILE
129+
130+
// Redeclaration checking should behave as though expansions are part of the
131+
// source file.
132+
struct RedeclChecking {
133+
#varValue
134+
135+
// expected-error@+1 {{invalid redeclaration of 'value'}}
136+
var value: Int { 0 }
137+
}
138+
139+
// CHECK-DIAGS: macro_expand.swift:[[@LINE-3]]:7: error: invalid redeclaration of 'value'
140+
// CHECK-DIAGS: @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-8]]_2_33_4361AD9339943F52AE6186DD51E04E91Ll8varValuefMf_.swift:1:5: note: 'value' previously declared here
141+
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-9]]_2_33_4361AD9339943F52AE6186DD51E04E91Ll8varValuefMf_.swift:
142+
// CHECK-DIAGS: var value: Int {
143+
// CHECK-DIAGS: 1
144+
// CHECK-DIAGS: }
145+
// CHECK-DIAGS: END CONTENTS OF FILE
146+
129147
#endif
130148

131149
@freestanding(declaration)
@@ -138,7 +156,7 @@ macro AccidentalCodeItem() = #externalMacro(module: "MacroDefinition", type: "Fa
138156
func invalidDeclarationMacro() {
139157
#accidentalCodeItem
140158
// expected-note@-1 {{in expansion of macro 'accidentalCodeItem' here}}
141-
// CHECK-DIAGS: @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX138_2_18accidentalCodeItemfMf_.swift:1:1: error: expected macro expansion to produce a declaration
159+
// CHECK-DIAGS: @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-3]]_2_18accidentalCodeItemfMf_.swift:1:1: error: expected macro expansion to produce a declaration
142160

143161
@AccidentalCodeItem struct S {}
144162
// expected-note@-1 {{in expansion of macro 'AccidentalCodeItem' on struct 'S' here}}

0 commit comments

Comments
 (0)