Skip to content

Commit 92c20be

Browse files
AlexDenisovredsun82
authored andcommitted
Swift: change indexing for extension declarations to make them more stable
1 parent 6c954ea commit 92c20be

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

swift/extractor/mangler/SwiftMangler.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,26 @@ SwiftMangledName SwiftMangler::visitExtensionDecl(const swift::ExtensionDecl* de
9191
if (decl->getDeclContext()->isLocalContext()) {
9292
return {};
9393
}
94-
auto extended = decl->getExtendedNominal();
95-
if (!extended) {
96-
// may happen in incomplete ASTs
97-
return {};
94+
95+
auto parent = getParent(decl);
96+
unsigned index = 0;
97+
if (auto parentModule = llvm::dyn_cast<swift::ModuleDecl>(parent)) {
98+
llvm::SmallVector<swift::Decl*> parentDecls;
99+
parentModule->getTopLevelDecls(parentDecls);
100+
auto found = std::find(std::begin(parentDecls), std::end(parentDecls), decl);
101+
assert(found != std::end(parentDecls));
102+
index = found - std::begin(parentDecls);
103+
} else if (auto iterableParent = llvm::dyn_cast<swift::IterableDeclContext>(parent)) {
104+
auto parentDecls = iterableParent->getAllMembers();
105+
auto found = std::find(std::begin(parentDecls), std::end(parentDecls), decl);
106+
assert(found != std::end(parentDecls));
107+
index = found - std::begin(parentDecls);
108+
} else {
109+
assert(false && "non-local context must be module or iterable decl context");
98110
}
111+
99112
auto ret = initMangled(decl);
100-
ret << dispatcher.fetchLabel(extended);
101-
// get the index of all extensions of the same nominal type within this decl's module
102-
auto index = 0u;
103-
bool found = false;
104-
for (auto ext : extended->getExtensions()) {
105-
if (ext == decl) {
106-
found = true;
107-
break;
108-
}
109-
if (ext->getModuleContext() == decl->getModuleContext()) ++index;
110-
}
111-
assert(found && "extension not found within extended nominal type decl");
113+
ret << dispatcher.fetchLabel(parent);
112114
ret << index;
113115
return ret;
114116
}

0 commit comments

Comments
 (0)