Skip to content

Commit 3d0cf98

Browse files
committed
AST: Look through missing imports in ExtendedNominalRequest.
If an `ExtendedNominalRequest`'s initial type lookup yields no results, query again ignoring missing imports to find nominals that were excluded due to the `MemberImportVisibility` feature being enabled. The missing import will be diagnosed during type resolution and allowing the request to succeed enables better diagnostics. Part of rdar://126637855.
1 parent 0f09b3c commit 3d0cf98

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

lib/AST/NameLookup.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,10 @@ enum class DirectlyReferencedTypeLookupFlags {
10921092
/// Include results that are members of protocols to which the contextual
10931093
/// type conforms.
10941094
AllowProtocolMembers = 1 << 2,
1095+
1096+
/// Include members that would normally be excluded because they come from
1097+
/// modules that have not been imported directly.
1098+
IgnoreMissingImports = 1 << 3,
10951099
};
10961100

10971101
using DirectlyReferencedTypeLookupOptions =
@@ -2917,6 +2921,10 @@ static DirectlyReferencedTypeDecls directReferencesForUnqualifiedTypeLookup(
29172921
DirectlyReferencedTypeLookupFlags::AllowUsableFromInline))
29182922
options |= UnqualifiedLookupFlags::IncludeUsableFromInline;
29192923

2924+
if (typeLookupOptions.contains(
2925+
DirectlyReferencedTypeLookupFlags::IgnoreMissingImports))
2926+
options |= UnqualifiedLookupFlags::IgnoreMissingImports;
2927+
29202928
// Manually exclude macro expansions here since the source location
29212929
// is overridden below.
29222930
if (namelookup::isInMacroArgument(dc->getParentSourceFile(), loc))
@@ -2968,14 +2976,10 @@ static DirectlyReferencedTypeDecls directReferencesForUnqualifiedTypeLookup(
29682976
}
29692977

29702978
/// Perform qualified name lookup for types.
2971-
static llvm::TinyPtrVector<TypeDecl *>
2972-
directReferencesForQualifiedTypeLookup(Evaluator &evaluator,
2973-
ASTContext &ctx,
2974-
ArrayRef<TypeDecl *> baseTypes,
2975-
DeclNameRef name,
2976-
DeclContext *dc,
2977-
SourceLoc loc,
2978-
bool allowUsableFromInline=false) {
2979+
static llvm::TinyPtrVector<TypeDecl *> directReferencesForQualifiedTypeLookup(
2980+
Evaluator &evaluator, ASTContext &ctx, ArrayRef<TypeDecl *> baseTypes,
2981+
DeclNameRef name, DeclContext *dc, SourceLoc loc,
2982+
DirectlyReferencedTypeLookupOptions typeLookupOptions) {
29792983
llvm::TinyPtrVector<TypeDecl *> result;
29802984
auto addResults = [&result](ArrayRef<ValueDecl *> found){
29812985
for (auto decl : found){
@@ -2990,9 +2994,14 @@ directReferencesForQualifiedTypeLookup(Evaluator &evaluator,
29902994
SmallVector<ValueDecl *, 4> members;
29912995
auto options = NL_RemoveNonVisible | NL_OnlyTypes;
29922996

2993-
if (allowUsableFromInline)
2997+
if (typeLookupOptions.contains(
2998+
DirectlyReferencedTypeLookupFlags::AllowUsableFromInline))
29942999
options |= NL_IncludeUsableFromInline;
29953000

3001+
if (typeLookupOptions.contains(
3002+
DirectlyReferencedTypeLookupFlags::IgnoreMissingImports))
3003+
options |= NL_IgnoreMissingImports;
3004+
29963005
// Look through the type declarations we were given, resolving them down
29973006
// to nominal type declarations, module declarations, and
29983007
SmallVector<ModuleDecl *, 2> moduleDecls;
@@ -3031,8 +3040,7 @@ static DirectlyReferencedTypeDecls directReferencesForDeclRefTypeRepr(
30313040
// For a qualified identifier, perform qualified name lookup.
30323041
result.first = directReferencesForQualifiedTypeLookup(
30333042
evaluator, ctx, result.first, repr->getNameRef(), dc, repr->getLoc(),
3034-
options.contains(
3035-
DirectlyReferencedTypeLookupFlags::AllowUsableFromInline));
3043+
options);
30363044

30373045
return result;
30383046
}
@@ -3419,6 +3427,16 @@ ExtendedNominalRequest::evaluate(Evaluator &evaluator,
34193427
DirectlyReferencedTypeDecls referenced = directReferencesForTypeRepr(
34203428
evaluator, ctx, typeRepr, ext->getParent(), options);
34213429

3430+
// If there were no results, expand the lookup to include members that are
3431+
// inaccessible due to missing imports. The missing imports will be diagnosed
3432+
// elsewhere.
3433+
if (referenced.first.empty() &&
3434+
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility)) {
3435+
options |= DirectlyReferencedTypeLookupFlags::IgnoreMissingImports;
3436+
referenced = directReferencesForTypeRepr(evaluator, ctx, typeRepr,
3437+
ext->getParent(), options);
3438+
}
3439+
34223440
// Resolve those type declarations to nominal type declarations.
34233441
SmallVector<ModuleDecl *, 2> modulesFound;
34243442
bool anyObject = false;

0 commit comments

Comments
 (0)