Skip to content

[Macros] Don't visit macro-generated extensions in visitAuxiliaryDecls. #66914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,20 +407,6 @@ void Decl::visitAuxiliaryDecls(
}
}

if (auto *nominal = dyn_cast<NominalTypeDecl>(mutableThis)) {
auto conformanceBuffers =
evaluateOrDefault(ctx.evaluator,
ExpandConformanceMacros{nominal},
{});
for (auto bufferID : conformanceBuffers) {
auto startLoc = sourceMgr.getLocForBufferStart(bufferID);
auto *sourceFile = moduleDecl->getSourceFileContainingLocation(startLoc);
for (auto *extension : sourceFile->getTopLevelDecls()) {
callback(extension);
}
}
}

if (visitFreestandingExpanded) {
if (auto *med = dyn_cast<MacroExpansionDecl>(mutableThis)) {
if (auto bufferID = evaluateOrDefault(
Expand Down
3 changes: 1 addition & 2 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4104,8 +4104,7 @@ void FileUnit::getTopLevelDeclsWithAuxiliaryDecls(
getTopLevelDecls(nonExpandedDecls);
for (auto *decl : nonExpandedDecls) {
decl->visitAuxiliaryDecls([&](Decl *auxDecl) {
if (!isa<ExtensionDecl>(auxDecl))
results.push_back(auxDecl);
results.push_back(auxDecl);
});
results.push_back(decl);
}
Expand Down
5 changes: 0 additions & 5 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5578,11 +5578,6 @@ void IRGenModule::emitNestedTypeDecls(DeclRange members) {
continue;

member->visitAuxiliaryDecls([&](Decl *decl) {
// FIXME: Conformance macros can generate extension decls. These
// are visited as top-level decls; skip them here.
if (isa<ExtensionDecl>(decl))
return;

emitNestedTypeDecls({decl, nullptr});
});
switch (member->getKind()) {
Expand Down
3 changes: 3 additions & 0 deletions lib/Index/Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,9 @@ void IndexSwiftASTWalker::visitModule(ModuleDecl &Mod) {
if (!handleSourceOrModuleFile(*SrcFile))
return;
walk(*SrcFile);
if (auto *synthesizedSF = SrcFile->getSynthesizedFile()) {
walk(synthesizedSF);
}
} else {
IsModuleFile = true;
isSystemModule = Mod.isNonUserModule();
Expand Down
4 changes: 0 additions & 4 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2201,10 +2201,6 @@ class SILGenModuleRAII {
for (auto *D : sf->getTopLevelDecls()) {
// Emit auxiliary decls.
D->visitAuxiliaryDecls([&](Decl *auxiliaryDecl) {
// Skip extensions decls; they are visited below.
if (isa<ExtensionDecl>(auxiliaryDecl))
return;

FrontendStatsTracer StatsTracer(SGM.getASTContext().Stats,
"SILgen-decl", auxiliaryDecl);
SGM.visit(auxiliaryDecl);
Expand Down
5 changes: 0 additions & 5 deletions lib/SILGen/SILGenType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1101,11 +1101,6 @@ class SILGenType : public TypeMemberVisitor<SILGenType> {
SGM.emitLazyConformancesForType(theType);

forEachMemberToLower(theType, [&](Decl *member) {
// FIXME: Conformance macros can generate extension decls. These
// are visited as top-level decls; skip them here.
if (isa<ExtensionDecl>(member))
return;

visit(member);
});

Expand Down
8 changes: 7 additions & 1 deletion lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,13 @@ swift::expandConformances(CustomAttr *attr, MacroDecl *macro,
extension->setExtendedNominal(nominal);
nominal->addExtension(extension);

// Make it accessible to getTopLevelDecls()
// Most other macro-generated declarations are visited through calling
// 'visitAuxiliaryDecls' on the original declaration the macro is attached
// to. We don't do this for macro-generated extensions, because the
// extension is not a peer of the original declaration. Instead of
// requiring all callers of 'visitAuxiliaryDecls' to understand the
// hoisting behavior of macro-generated extensions, we make the
// extension accessible through 'getTopLevelDecls()'.
if (auto file = dyn_cast<FileUnit>(
decl->getDeclContext()->getModuleScopeContext()))
file->getOrCreateSynthesizedFile().addTopLevelDecl(extension);
Expand Down
10 changes: 10 additions & 0 deletions lib/Sema/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,16 @@ TypeCheckSourceFileRequest::evaluate(Evaluator &eval, SourceFile *SF) const {
}
}

// Type-check macro-generated extensions.
if (auto *synthesizedSF = SF->getSynthesizedFile()) {
for (auto *decl : synthesizedSF->getTopLevelDecls()) {
if (!decl->isImplicit()) {
assert(isa<ExtensionDecl>(decl));
TypeChecker::typeCheckDecl(decl);
}
}
}

typeCheckDelayedFunctions(*SF);
}

Expand Down
28 changes: 16 additions & 12 deletions test/Index/index_macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ struct AddOne {
// CHECK: [[@LINE-5]]:1 | function/Swift | freeLog() | [[FREE_LOG_USR]] | Ref,Call,Impl,RelCall,RelCont
// CHECK-NEXT: RelCall,RelCont | instance-method/Swift | freeFunc() | [[FREE_FUNC_USR]]

func testExpr() {
#freestandingExpr
// CHECK: [[@LINE-1]]:3 | function/Swift | exprLog() | [[EXPR_LOG_USR]] | Ref,Call,Impl,RelCall,RelCont
// CHECK-NEXT: RelCall,RelCont | function/Swift | testExpr()
}

// CHECK: [[@LINE+4]]:40 | macro/Swift | Peer() | [[PEER_USR]] | Ref
// CHECK: [[@LINE+3]]:23 | macro/Swift | MemberAttribute() | [[MEMBER_ATTRIBUTE_USR]] | Ref
// CHECK: [[@LINE+2]]:15 | macro/Swift | Member() | [[MEMBER_USR]] | Ref
Expand Down Expand Up @@ -112,11 +118,6 @@ struct TestAttached {
// CHECK: [[@LINE-24]]:39 | function/Swift | peerLog() | [[PEER_LOG_USR]] | Ref,Call,Impl,RelCall,RelCont
// CHECK-NEXT: RelCall,RelCont | instance-method/Swift | peerFunc() | [[PEER_FUNC_USR]]

// `Conformance` adds `TestProto` as a conformance on an extension of `TestAttached`
// CHECK: [[@LINE-28]]:1 | extension/ext-struct/Swift | TestAttached | {{.*}} | Def,Impl
// CHECK: [[@LINE-29]]:1 | protocol/Swift | TestProto | [[PROTO_USR]] | Ref,Impl,RelBase
// CHECK-NEXT: RelBase | extension/ext-struct/Swift | TestAttached

// CHECK: [[@LINE+1]]:8 | struct/Swift | Outer | [[OUTER_USR:.*]] | Def
struct Outer {
// CHECK: [[@LINE+1]]:4 | macro/Swift | PeerMember() | [[PEER_MEMBER_USR]] | Ref
Expand All @@ -137,16 +138,19 @@ struct Outer {
// CHECK: [[@LINE-6]]:16 | function/Swift | memberLog() | [[MEMBER_LOG_USR]] | Ref,Call,Impl,RelCall,RelCont
// CHECK-NEXT: RelCall,RelCont | instance-method/Swift | memberFunc() | [[INNER_FUNC_USR]]


// Expanded extensions are visited last

// `Conformance` adds `TestProto` as a conformance on an extension of `TestAttached`
// CHECK: [[@LINE-51]]:1 | extension/ext-struct/Swift | TestAttached | {{.*}} | Def,Impl
// CHECK: [[@LINE-52]]:1 | protocol/Swift | TestProto | [[PROTO_USR]] | Ref,Impl,RelBase
// CHECK-NEXT: RelBase | extension/ext-struct/Swift | TestAttached

// `Conformance` adds `TestProto` as a conformance on an extension of `TestInner`
// CHECK: [[@LINE-10]]:3 | extension/ext-struct/Swift | TestInner | {{.*}} | Def,Impl
// CHECK: [[@LINE-11]]:3 | protocol/Swift | TestProto | [[PROTO_USR]] | Ref,Impl,RelBase
// CHECK: [[@LINE-18]]:3 | extension/ext-struct/Swift | TestInner | {{.*}} | Def,Impl
// CHECK: [[@LINE-19]]:3 | protocol/Swift | TestProto | [[PROTO_USR]] | Ref,Impl,RelBase
// CHECK-NEXT: RelBase | extension/ext-struct/Swift | TestInner

func testExpr() {
#freestandingExpr
// CHECK: [[@LINE-1]]:3 | function/Swift | exprLog() | [[EXPR_LOG_USR]] | Ref,Call,Impl,RelCall,RelCont
// CHECK-NEXT: RelCall,RelCont | function/Swift | testExpr()
}

//--- IndexMacros.swift
import SwiftSyntax
Expand Down
6 changes: 6 additions & 0 deletions test/Macros/macro_expand_conformances.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ requireHashable(S2())

requireEquatable(E.Nested())

extension E {
@Equatable struct NestedInExtension {}
}

requireEquatable(E.NestedInExtension())

#if TEST_DIAGNOSTICS
requireEquatable(PublicEquatable())

Expand Down