Skip to content

Commit b4a2d89

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 87f891c commit b4a2d89

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,24 @@ class HasStoredPropertyClassInvalid {
134134
#AddStoredProperty((Self.self, 0).1) // expected-note {{in expansion of macro 'AddStoredProperty' here}}
135135
// CHECK-DIAGS: @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-2]]_2_33_{{.*}}AddStoredPropertyfMf_.swift:1:22: error: covariant 'Self' type cannot be referenced from a stored property initializer
136136
}
137+
138+
// Redeclaration checking should behave as though expansions are part of the
139+
// source file.
140+
struct RedeclChecking {
141+
#varValue
142+
143+
// expected-error@+1 {{invalid redeclaration of 'value'}}
144+
var value: Int { 0 }
145+
}
146+
147+
// CHECK-DIAGS: macro_expand.swift:[[@LINE-3]]:7: error: invalid redeclaration of 'value'
148+
// CHECK-DIAGS: @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-8]]_2_33_4361AD9339943F52AE6186DD51E04E91Ll8varValuefMf_.swift:1:5: note: 'value' previously declared here
149+
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-9]]_2_33_4361AD9339943F52AE6186DD51E04E91Ll8varValuefMf_.swift:
150+
// CHECK-DIAGS: var value: Int {
151+
// CHECK-DIAGS: 1
152+
// CHECK-DIAGS: }
153+
// CHECK-DIAGS: END CONTENTS OF FILE
154+
137155
#endif
138156

139157
@freestanding(declaration)

0 commit comments

Comments
 (0)