Skip to content

Commit e417205

Browse files
committed
[Macros] Allow any decl names in macro expansions when the macro covers arbitrary
names.
1 parent c9a86ac commit e417205

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

include/swift/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8463,6 +8463,11 @@ class MacroDecl : public GenericContext, public ValueDecl {
84638463
void getIntroducedNames(MacroRole role, ValueDecl *attachedTo,
84648464
SmallVectorImpl<DeclName> &names) const;
84658465

8466+
/// Returns a DeclName that represents arbitrary names.
8467+
static DeclName getArbitraryName() {
8468+
return DeclName();
8469+
}
8470+
84668471
/// Retrieve the definition of this macro.
84678472
MacroDefinition getDefinition() const;
84688473

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10110,7 +10110,7 @@ void MacroDecl::getIntroducedNames(MacroRole role, ValueDecl *attachedTo,
1011010110
}
1011110111

1011210112
case MacroIntroducedDeclNameKind::Arbitrary:
10113-
// FIXME: Indicate that the macro covers arbitrary names.
10113+
names.push_back(MacroDecl::getArbitraryName());
1011410114
break;
1011510115
}
1011610116
}

lib/Sema/TypeCheckMacros.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,13 @@ static void validateMacroExpansion(SourceFile *expansionBuffer,
538538
continue;
539539
}
540540

541-
if (!coversName.count(baseName)) {
542-
value->diagnose(diag::invalid_macro_introduced_name,
543-
baseName, macro->getBaseName());
541+
if (coversName.count(baseName) ||
542+
coversName.count(MacroDecl::getArbitraryName())) {
543+
continue;
544544
}
545+
546+
value->diagnose(diag::invalid_macro_introduced_name,
547+
baseName, macro->getBaseName());
545548
}
546549
}
547550
}

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,25 @@ public struct AddMembers: MemberMacro {
494494
}
495495
}
496496

497+
public struct AddArbitraryMembers: MemberMacro {
498+
public static func expansion(
499+
of node: AttributeSyntax,
500+
providingMembersOf decl: some DeclGroupSyntax,
501+
in context: some MacroExpansionContext
502+
) throws -> [DeclSyntax] {
503+
guard let identified = decl.asProtocol(IdentifiedDeclSyntax.self) else {
504+
return []
505+
}
506+
507+
let parentName = identified.identifier.trimmed
508+
return [
509+
"struct \(parentName)1 {}",
510+
"struct \(parentName)2 {}",
511+
"struct \(parentName)3 {}",
512+
]
513+
}
514+
}
515+
497516
/// Implementation of the `wrapStoredProperties` macro, which can be
498517
/// used to apply an attribute to all of the stored properties of a type.
499518
///

test/Macros/macro_expand_synthesized_members.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ let s = S()
2929
// CHECK: Storage
3030
s.useSynthesized()
3131

32+
@attached(member, names: arbitrary)
33+
macro addArbitraryMembers() = #externalMacro(module: "MacroDefinition", type: "AddArbitraryMembers")
34+
35+
@addArbitraryMembers
36+
struct MyType {}
37+
38+
// CHECK: MyType1
39+
// CHECK: MyType2
40+
// CHECK: MyType3
41+
print(MyType.MyType1.self)
42+
print(MyType.MyType2.self)
43+
print(MyType.MyType3.self)
44+
3245
@attached(
3346
member,
3447
names: named(RawValue), named(rawValue), named(`init`)

0 commit comments

Comments
 (0)