Skip to content

Commit 23a7a59

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 10d49aa commit 23a7a59

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,23 @@ class HasStoredPropertyClassInvalid {
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
}
137137

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+
138155
@attached(body)
139156
public macro ThrowCancellation() = #externalMacro(module: "MacroDefinition", type: "ThrowCancellationMacro")
140157

0 commit comments

Comments
 (0)