Skip to content

Commit 0f09b3c

Browse files
committed
Sema: Look through missing imports during unqualified type lookup.
To improve knock-on diagnostics, resolve unqualified types to their matching member type declarations regardless of whether an import is missing when the `MemberImportVisibility` features is enabled. Part of rdar://126637855.
1 parent bbea88e commit 0f09b3c

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,20 +1381,6 @@ static Type diagnoseUnknownType(const TypeResolution &resolution,
13811381
return ErrorType::get(ctx);
13821382
}
13831383

1384-
// Try ignoring missing imports.
1385-
relookupOptions |= NameLookupFlags::IgnoreMissingImports;
1386-
auto nonImportedResults = TypeChecker::lookupUnqualifiedType(
1387-
dc, repr->getNameRef(), repr->getLoc(), relookupOptions);
1388-
if (!nonImportedResults.empty()) {
1389-
auto first = cast<TypeDecl>(nonImportedResults.front().getValueDecl());
1390-
auto nameLoc = repr->getNameLoc();
1391-
maybeDiagnoseMissingImportForMember(first, dc, nameLoc.getStartLoc());
1392-
1393-
// Don't try to recover here; we'll get more access-related diagnostics
1394-
// downstream if we do.
1395-
return ErrorType::get(ctx);
1396-
}
1397-
13981384
// Fallback.
13991385
auto L = repr->getNameLoc();
14001386
SourceRange R = repr->getNameLoc().getSourceRange();
@@ -1656,6 +1642,16 @@ resolveUnqualifiedIdentTypeRepr(const TypeResolution &resolution,
16561642
auto globals =
16571643
TypeChecker::lookupUnqualifiedType(DC, id, repr->getLoc(), lookupOptions);
16581644

1645+
// If the look up did not yield any results, try again but allow members from
1646+
// modules that are not directly imported to be accessible.
1647+
bool didIgnoreMissingImports = false;
1648+
if (!globals && ctx.LangOpts.hasFeature(Feature::MemberImportVisibility)) {
1649+
lookupOptions |= NameLookupFlags::IgnoreMissingImports;
1650+
globals = TypeChecker::lookupUnqualifiedType(DC, id, repr->getLoc(),
1651+
lookupOptions);
1652+
didIgnoreMissingImports = true;
1653+
}
1654+
16591655
// If we're doing structural resolution and one of the results is an
16601656
// associated type, ignore any other results found from the same
16611657
// DeclContext; they are going to be protocol typealiases, possibly
@@ -1730,6 +1726,11 @@ resolveUnqualifiedIdentTypeRepr(const TypeResolution &resolution,
17301726

17311727
// If we found a type declaration with the given name, return it now.
17321728
if (current) {
1729+
if (didIgnoreMissingImports &&
1730+
maybeDiagnoseMissingImportForMember(currentDecl, DC, repr->getLoc())) {
1731+
repr->setInvalid();
1732+
return ErrorType::get(ctx);
1733+
}
17331734
repr->setValue(currentDecl, currentDC);
17341735
return current;
17351736
}

test/NameLookup/members_transitive_multifile_access_level.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,20 @@ func internalFunc(_ x: Int) {
4747

4848
//--- function_signatures.swift
4949

50-
// FIXME: The access level is wrong on many of these fix-its.
5150
import Swift // Just here to anchor the fix-its
5251
// expected-note 2 {{add import of module 'InternalUsesOnly'}}{{1-1=internal import InternalUsesOnly\n}}
53-
// expected-note@-1 {{add import of module 'PackageUsesOnly'}}{{1-1=internal import PackageUsesOnly\n}}
54-
// expected-note@-2 {{add import of module 'PublicUsesOnly'}}{{1-1=internal import PublicUsesOnly\n}}
55-
// expected-note@-3 2 {{add import of module 'MixedUses'}}{{1-1=internal import MixedUses\n}}
52+
// expected-note@-1 {{add import of module 'PackageUsesOnly'}}{{1-1=package import PackageUsesOnly\n}}
53+
// expected-note@-2 {{add import of module 'PublicUsesOnly'}}{{1-1=public import PublicUsesOnly\n}}
54+
// expected-note@-3 2 {{add import of module 'MixedUses'}}{{1-1=public import MixedUses\n}}
5655

5756
extension Int {
5857
private func usesTypealiasInInternalUsesOnly_Private(x: TypealiasInInternalUsesOnly) {} // expected-error {{type alias 'TypealiasInInternalUsesOnly' is not available due to missing import of defining module 'InternalUsesOnly'}}
5958
internal func usesTypealiasInInternalUsesOnly(x: TypealiasInInternalUsesOnly) {} // expected-error {{type alias 'TypealiasInInternalUsesOnly' is not available due to missing import of defining module 'InternalUsesOnly'}}
6059
package func usesTypealiasInPackageUsesOnly(x: TypealiasInPackageUsesOnly) {} // expected-error {{type alias 'TypealiasInPackageUsesOnly' is not available due to missing import of defining module 'PackageUsesOnly'}}
6160
public func usesTypealiasInPublicUsesOnly(x: TypealiasInPublicUsesOnly) {} // expected-error {{type alias 'TypealiasInPublicUsesOnly' is not available due to missing import of defining module 'PublicUsesOnly'}}
61+
// expected-warning@-1 {{cannot use type alias 'TypealiasInPublicUsesOnly' here; 'PublicUsesOnly' was not imported by this file}}
6262
public func usesTypealiasInMixedUses(x: TypealiasInMixedUses) {} // expected-error {{type alias 'TypealiasInMixedUses' is not available due to missing import of defining module 'MixedUses'}}
63+
// expected-warning@-1 {{cannot use type alias 'TypealiasInMixedUses' here; 'MixedUses' was not imported by this file}}
6364
internal func usesTypealiasInMixedUses_Internal(x: TypealiasInMixedUses) {} // expected-error {{type alias 'TypealiasInMixedUses' is not available due to missing import of defining module 'MixedUses'}}
6465
}
6566

0 commit comments

Comments
 (0)