Skip to content

Commit 1c28684

Browse files
committed
AST: Add module visibility shadowing rule to removeShadowedDecls()
This simulates the shadowing done by ModuleNameLookup, which is about to be removed. The basic idea is that if a module A imports a module B, and B imports C, then from A's point of view, top-level declarations from B will shadow top-level declarations from C.
1 parent 962f358 commit 1c28684

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lib/AST/NameLookup.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,35 @@ static void recordShadowedDeclsAfterSignatureMatch(
187187
// all have the same signature, we expect n to remain small.
188188
auto *curModule = dc->getParentModule();
189189
ASTContext &ctx = curModule->getASTContext();
190+
auto &imports = ctx.getImportCache();
191+
190192
for (unsigned firstIdx : indices(decls)) {
191193
auto firstDecl = decls[firstIdx];
192194
auto firstModule = firstDecl->getModuleContext();
195+
auto name = firstDecl->getBaseName();
193196
for (unsigned secondIdx : range(firstIdx + 1, decls.size())) {
194197
// Determine whether one module takes precedence over another.
195198
auto secondDecl = decls[secondIdx];
196199
auto secondModule = secondDecl->getModuleContext();
197200

201+
// Top-level type declarations in a module shadow other declarations
202+
// visible through the module's imports.
203+
//
204+
// [Backward compatibility] Note that members of types have the same
205+
// shadowing check, but we do it after dropping unavailable members.
206+
if (firstModule != secondModule &&
207+
firstDecl->getDeclContext()->isModuleScopeContext() &&
208+
secondDecl->getDeclContext()->isModuleScopeContext()) {
209+
// Now check if one module shadows the other.
210+
if (imports.isShadowedBy(firstModule, secondModule, name, dc)) {
211+
shadowed.insert(firstDecl);
212+
break;
213+
} else if (imports.isShadowedBy(secondModule, firstModule, name, dc)) {
214+
shadowed.insert(secondDecl);
215+
continue;
216+
}
217+
}
218+
198219
// Swift 4 compatibility hack: Don't shadow properties defined in
199220
// extensions of generic types with properties defined elsewhere.
200221
// This is due to the fact that in Swift 4, we only gave custom overload

0 commit comments

Comments
 (0)