Skip to content

Commit ece05c2

Browse files
committed
Sema: Don't expand macros when binding extensions
Fixes rdar://149798059.
1 parent 2e368ce commit ece05c2

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/AST/NameLookup.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,9 @@ enum class DirectlyReferencedTypeLookupFlags {
11251125
/// Include members that would normally be excluded because they come from
11261126
/// modules that have not been imported directly.
11271127
IgnoreMissingImports = 1 << 3,
1128+
1129+
/// Whenther we should exclude macro expansions.
1130+
ExcludeMacroExpansions = 1 << 4,
11281131
};
11291132

11301133
using DirectlyReferencedTypeLookupOptions =
@@ -3065,6 +3068,10 @@ static DirectlyReferencedTypeDecls directReferencesForUnqualifiedTypeLookup(
30653068
DirectlyReferencedTypeLookupFlags::IgnoreMissingImports))
30663069
options |= UnqualifiedLookupFlags::IgnoreMissingImports;
30673070

3071+
if (typeLookupOptions.contains(
3072+
DirectlyReferencedTypeLookupFlags::ExcludeMacroExpansions))
3073+
options |= UnqualifiedLookupFlags::ExcludeMacroExpansions;
3074+
30683075
// Manually exclude macro expansions here since the source location
30693076
// is overridden below.
30703077
if (namelookup::isInMacroArgument(dc->getParentSourceFile(), loc))
@@ -3147,6 +3154,10 @@ static llvm::TinyPtrVector<TypeDecl *> directReferencesForQualifiedTypeLookup(
31473154
DirectlyReferencedTypeLookupFlags::IgnoreMissingImports))
31483155
options |= NL_IgnoreMissingImports;
31493156

3157+
if (typeLookupOptions.contains(
3158+
DirectlyReferencedTypeLookupFlags::ExcludeMacroExpansions))
3159+
options |= NL_ExcludeMacroExpansions;
3160+
31503161
// Look through the type declarations we were given, resolving them down
31513162
// to nominal type declarations, module declarations, and
31523163
SmallVector<ModuleDecl *, 2> moduleDecls;
@@ -3583,9 +3594,13 @@ ExtendedNominalRequest::evaluate(Evaluator &evaluator,
35833594

35843595
ASTContext &ctx = ext->getASTContext();
35853596
auto options = defaultDirectlyReferencedTypeLookupOptions;
3597+
35863598
if (ext->isInSpecializeExtensionContext()) {
35873599
options |= DirectlyReferencedTypeLookupFlags::AllowUsableFromInline;
35883600
}
3601+
3602+
options |= DirectlyReferencedTypeLookupFlags::ExcludeMacroExpansions;
3603+
35893604
DirectlyReferencedTypeDecls referenced = directReferencesForTypeRepr(
35903605
evaluator, ctx, typeRepr, ext->getParent(), options);
35913606

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %target-typecheck-verify-swift %s
2+
3+
@attached(member) @attached(peer)
4+
public macro Foo(_: any P) = #externalMacro(module: "FooMacros", type: "FooMacro")
5+
// expected-warning@-1 {{external macro implementation type 'FooMacros.FooMacro' could not be found for macro 'Foo'; plugin for module 'FooMacros' not found}}
6+
// expected-note@-2 2 {{'Foo' declared here}}
7+
8+
public protocol P {}
9+
10+
@Foo(S.s)
11+
struct A {
12+
// expected-error@-1 2 {{external macro implementation type 'FooMacros.FooMacro' could not be found for macro 'Foo'; plugin for module 'FooMacros' not found}}
13+
func a() {}
14+
}
15+
16+
extension A {
17+
struct Nested {}
18+
}
19+
20+
// Binding this extension must not trigger macro expansion, because that
21+
// performs a qualified lookup of S.s, which fails because the extension
22+
// of P declared below has not been bound yet.
23+
extension A.Nested {}
24+
25+
struct S: P {}
26+
27+
extension P where Self == S {
28+
static var s: Self { Self() }
29+
}

0 commit comments

Comments
 (0)