Skip to content

Commit 92fed81

Browse files
committed
[Macros] Ban extension macros attached to local types.
(cherry picked from commit 095542a)
1 parent 90a3187 commit 92fed81

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7148,6 +7148,9 @@ ERROR(global_arbitrary_name,none,
71487148
"'%0' macros are not allowed to introduce arbitrary names "
71497149
"at global scope",
71507150
(StringRef))
7151+
ERROR(local_extension_macro,none,
7152+
"local type cannot have attached extension macro",
7153+
())
71517154

71527155
ERROR(macro_resolve_circular_reference, none,
71537156
"circular reference resolving %select{freestanding|attached}0 macro %1",

lib/Sema/TypeCheckMacros.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,11 @@ ExpandExtensionMacros::evaluate(Evaluator &evaluator,
14881488
Optional<unsigned>
14891489
swift::expandExtensions(CustomAttr *attr, MacroDecl *macro,
14901490
MacroRole role, NominalTypeDecl *nominal) {
1491+
if (nominal->getDeclContext()->isLocalContext()) {
1492+
nominal->diagnose(diag::local_extension_macro);
1493+
return llvm::None;
1494+
}
1495+
14911496
auto macroSourceFile = ::evaluateAttachedMacro(macro, nominal, attr,
14921497
/*passParentContext=*/false,
14931498
role);

test/Macros/macro_expand_extensions.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,11 @@ struct Outer {
5454

5555
// CHECK: Wrapped.requirement
5656
requiresP(Outer.Nested<Wrapped>.self)
57+
58+
#if TEST_DIAGNOSTICS
59+
func testLocal() {
60+
@DelegatedConformance
61+
struct Local<Element> {}
62+
// expected-error@-1{{local type cannot have attached extension macro}}
63+
}
64+
#endif

0 commit comments

Comments
 (0)