Skip to content

Commit ae32673

Browse files
committed
[Macros] Fix visiting nested conformance macro declarations in SIL and IRGen.
When expanding a conformance macro, the generated extension decls are added to the TopLevelDecls vector of the synthesized file unit to expose them to sf->getSynthesizedFile()->getTopLevelDecls(). There are two problems with this: 1. These decls are also visited via visitAuxiliaryDecls() for the purpose of type checking. This causes problems in code generation because the extensions are visited separately, and because SIL and IRGen assume nested auxiliary decls are members. 2. SILGen only emits top-level decls directly from the source file rather than its synthesized file. Auxiliary decls are visited here, but this doesn't work for nested conformance macros because the attached-to decl is not at the top-level, so macro-generated conformances for nested types never emit their descriptor. To fix this in the short term, visit top-level decls in the synthesized file that are generated by conformance macros, and skip auxiliary extension decls when emitting type members. This fix is narrowly scoped to only impact macros, but in the future this warrants a more structural fix to better handle top-level decls in the synthesized file.
1 parent e48a9b5 commit ae32673

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5501,6 +5501,11 @@ void IRGenModule::emitNestedTypeDecls(DeclRange members) {
55015501
continue;
55025502

55035503
member->visitAuxiliaryDecls([&](Decl *decl) {
5504+
// FIXME: Conformance macros can generate extension decls. These
5505+
// are visited as top-level decls; skip them here.
5506+
if (isa<ExtensionDecl>(decl))
5507+
return;
5508+
55045509
emitNestedTypeDecls({decl, nullptr});
55055510
});
55065511
switch (member->getKind()) {

lib/SILGen/SILGen.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,10 @@ class SILGenModuleRAII {
21962196
for (auto *D : sf->getTopLevelDecls()) {
21972197
// Emit auxiliary decls.
21982198
D->visitAuxiliaryDecls([&](Decl *auxiliaryDecl) {
2199+
// Skip extensions decls; they are visited below.
2200+
if (isa<ExtensionDecl>(auxiliaryDecl))
2201+
return;
2202+
21992203
FrontendStatsTracer StatsTracer(SGM.getASTContext().Stats,
22002204
"SILgen-decl", auxiliaryDecl);
22012205
SGM.visit(auxiliaryDecl);
@@ -2206,6 +2210,34 @@ class SILGenModuleRAII {
22062210
SGM.visit(D);
22072211
}
22082212

2213+
// FIXME: Visit macro-generated extensions separately.
2214+
//
2215+
// The code below that visits auxiliary decls of the top-level
2216+
// decls in the source file does not work for nested types with
2217+
// attached conformance macros:
2218+
// ```
2219+
// struct Outer {
2220+
// @AddConformance struct Inner {}
2221+
// }
2222+
// ```
2223+
// Because the attached-to decl is not at the top-level. To fix this,
2224+
// visit the macro-generated conformances that are recorded in the
2225+
// synthesized file unit to cover all macro-generated extension decls.
2226+
if (auto *synthesizedFile = sf->getSynthesizedFile()) {
2227+
for (auto *D : synthesizedFile->getTopLevelDecls()) {
2228+
if (!isa<ExtensionDecl>(D))
2229+
continue;
2230+
2231+
auto *sf = D->getInnermostDeclContext()->getParentSourceFile();
2232+
if (sf->getFulfilledMacroRole() != MacroRole::Conformance)
2233+
continue;
2234+
2235+
FrontendStatsTracer StatsTracer(SGM.getASTContext().Stats,
2236+
"SILgen-decl", D);
2237+
SGM.visit(D);
2238+
}
2239+
}
2240+
22092241
for (Decl *D : sf->getHoistedDecls()) {
22102242
FrontendStatsTracer StatsTracer(SGM.getASTContext().Stats,
22112243
"SILgen-decl", D);

lib/SILGen/SILGenType.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,11 @@ class SILGenType : public TypeMemberVisitor<SILGenType> {
10861086
SGM.emitLazyConformancesForType(theType);
10871087

10881088
forEachMemberToLower(theType, [&](Decl *member) {
1089+
// FIXME: Conformance macros can generate extension decls. These
1090+
// are visited as top-level decls; skip them here.
1091+
if (isa<ExtensionDecl>(member))
1092+
return;
1093+
10891094
visit(member);
10901095
});
10911096

test/Macros/macro_expand_conformances.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ struct S {}
2929
@Hashable
3030
struct S2 {}
3131

32+
enum E {
33+
@Equatable struct Nested {}
34+
}
35+
3236
// CHECK-DUMP: @__swiftmacro_25macro_expand_conformances1S9EquatablefMc_.swift
3337
// CHECK-DUMP: extension S : Equatable {}
3438

@@ -38,6 +42,8 @@ requireEquatable(S())
3842
requireEquatable(S2())
3943
requireHashable(S2())
4044

45+
requireEquatable(E.Nested())
46+
4147
@attached(conformance)
4248
@attached(member, names: named(requirement))
4349
macro DelegatedConformance() = #externalMacro(module: "MacroDefinition", type: "DelegatedConformanceMacro")

0 commit comments

Comments
 (0)