Skip to content

Commit 095542a

Browse files
committed
[Macros] Ban extension macros attached to local types.
1 parent d3e2562 commit 095542a

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
@@ -7220,6 +7220,9 @@ ERROR(global_arbitrary_name,none,
72207220
"'%0' macros are not allowed to introduce arbitrary names "
72217221
"at global scope",
72227222
(StringRef))
7223+
ERROR(local_extension_macro,none,
7224+
"local type cannot have attached extension macro",
7225+
())
72237226

72247227
ERROR(macro_resolve_circular_reference, none,
72257228
"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
@@ -1487,6 +1487,11 @@ ExpandExtensionMacros::evaluate(Evaluator &evaluator,
14871487
llvm::Optional<unsigned>
14881488
swift::expandExtensions(CustomAttr *attr, MacroDecl *macro,
14891489
MacroRole role, NominalTypeDecl *nominal) {
1490+
if (nominal->getDeclContext()->isLocalContext()) {
1491+
nominal->diagnose(diag::local_extension_macro);
1492+
return llvm::None;
1493+
}
1494+
14901495
auto macroSourceFile = ::evaluateAttachedMacro(macro, nominal, attr,
14911496
/*passParentContext=*/false,
14921497
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)