Skip to content

Commit 3d47e1c

Browse files
authored
Merge pull request #76336 from tshortli/member-import-visibility-ranking
ConstraintSystem: Use scoring to implement MemberImportVisibility
2 parents 863d2e4 + 58a7720 commit 3d47e1c

20 files changed

+128
-61
lines changed

include/swift/AST/DeclContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,9 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
680680
/// Looks up a precedence group with a given \p name.
681681
PrecedenceGroupLookupResult lookupPrecedenceGroup(Identifier name) const;
682682

683+
/// Returns true if the parent module of \p decl is imported by this context.
684+
bool isDeclImported(const Decl *decl) const;
685+
683686
/// Return the ASTContext for a specified DeclContext by
684687
/// walking up to the enclosing module and returning its ASTContext.
685688
LLVM_READONLY

include/swift/AST/SourceFile.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,6 @@ class SourceFile final : public FileUnit {
507507
MissingImportForMemberDiagnostics[decl].push_back(loc);
508508
}
509509

510-
/// Returns true if there is a pending missing import diagnostic for \p decl.
511-
bool hasDelayedMissingImportForMemberDiagnostic(const ValueDecl *decl) const {
512-
return MissingImportForMemberDiagnostics.contains(decl);
513-
}
514-
515510
DelayedMissingImportForMemberDiags
516511
takeDelayedMissingImportForMemberDiagnostics() {
517512
DelayedMissingImportForMemberDiags diags;

include/swift/Sema/CSFix.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,14 +1926,11 @@ class MoveOutOfOrderArgument final : public ConstraintFix {
19261926
};
19271927

19281928
class AllowInaccessibleMember final : public AllowInvalidMemberRef {
1929-
bool IsMissingImport;
1930-
19311929
AllowInaccessibleMember(ConstraintSystem &cs, Type baseType,
19321930
ValueDecl *member, DeclNameRef name,
1933-
ConstraintLocator *locator, bool isMissingImport)
1931+
ConstraintLocator *locator)
19341932
: AllowInvalidMemberRef(cs, FixKind::AllowInaccessibleMember, baseType,
1935-
member, name, locator),
1936-
IsMissingImport(isMissingImport) {}
1933+
member, name, locator) {}
19371934

19381935
public:
19391936
std::string getName() const override {
@@ -1948,8 +1945,7 @@ class AllowInaccessibleMember final : public AllowInvalidMemberRef {
19481945

19491946
static AllowInaccessibleMember *create(ConstraintSystem &cs, Type baseType,
19501947
ValueDecl *member, DeclNameRef name,
1951-
ConstraintLocator *locator,
1952-
bool isMissingImport);
1948+
ConstraintLocator *locator);
19531949

19541950
static bool classof(const ConstraintFix *fix) {
19551951
return fix->getKind() == FixKind::AllowInaccessibleMember;

include/swift/Sema/ConstraintSystem.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,8 @@ enum ScoreKind: unsigned int {
960960
SK_Hole,
961961
/// A reference to an @unavailable declaration.
962962
SK_Unavailable,
963+
/// A reference to a declaration from a module that has not been imported.
964+
SK_MissingImport,
963965
/// A reference to an async function in a synchronous context.
964966
///
965967
/// \note Any score kind after this is considered a conversion that doesn't
@@ -1124,6 +1126,9 @@ struct Score {
11241126
case SK_Unavailable:
11251127
return "use of an unavailable declaration";
11261128

1129+
case SK_MissingImport:
1130+
return "use of a declaration that has not been imported";
1131+
11271132
case SK_AsyncInSyncMismatch:
11281133
return "async-in-synchronous mismatch";
11291134

@@ -1975,10 +1980,6 @@ struct MemberLookupResult {
19751980
/// The member is inaccessible (e.g. a private member in another file).
19761981
UR_Inaccessible,
19771982

1978-
/// The member is not visible because it comes from a module that was not
1979-
/// imported.
1980-
UR_MissingImport,
1981-
19821983
/// This is a `WritableKeyPath` being used to look up read-only member,
19831984
/// used in situations involving dynamic member lookup via keypath,
19841985
/// because it's not known upfront what access capability would the

lib/AST/NameLookup.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,8 +2325,7 @@ static bool missingImportForMemberDecl(const DeclContext *dc, ValueDecl *decl) {
23252325
if (!ctx.LangOpts.hasFeature(Feature::MemberImportVisibility))
23262326
return false;
23272327

2328-
auto declModule = decl->getDeclContext()->getParentModule();
2329-
return !ctx.getImportCache().isImportedBy(declModule, dc);
2328+
return !dc->isDeclImported(decl);
23302329
}
23312330

23322331
/// Determine whether the given declaration is an acceptable lookup
@@ -2504,6 +2503,11 @@ bool DeclContext::lookupQualified(Type type,
25042503
loc, options, decls);
25052504
}
25062505

2506+
bool DeclContext::isDeclImported(const Decl *decl) const {
2507+
auto declModule = decl->getDeclContext()->getParentModule();
2508+
return getASTContext().getImportCache().isImportedBy(declModule, this);
2509+
}
2510+
25072511
static void installPropertyWrapperMembersIfNeeded(NominalTypeDecl *target,
25082512
DeclNameRef member) {
25092513
auto &Context = target->getASTContext();

lib/Sema/CSDiagnostics.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6250,11 +6250,6 @@ bool InaccessibleMemberFailure::diagnoseAsError() {
62506250
}
62516251

62526252
auto loc = nameLoc.isValid() ? nameLoc.getStartLoc() : ::getLoc(anchor);
6253-
if (IsMissingImport) {
6254-
maybeDiagnoseMissingImportForMember(Member, getDC(), loc);
6255-
return true;
6256-
}
6257-
62586253
auto accessLevel = Member->getFormalAccessScope().accessLevelForDiagnostics();
62596254
if (auto *CD = dyn_cast<ConstructorDecl>(Member)) {
62606255
emitDiagnosticAt(loc, diag::init_candidate_inaccessible,

lib/Sema/CSDiagnostics.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,13 +1640,11 @@ class ClosureParamDestructuringFailure final : public FailureDiagnostic {
16401640
/// ```
16411641
class InaccessibleMemberFailure final : public FailureDiagnostic {
16421642
ValueDecl *Member;
1643-
bool IsMissingImport;
16441643

16451644
public:
16461645
InaccessibleMemberFailure(const Solution &solution, ValueDecl *member,
1647-
ConstraintLocator *locator, bool isMissingImport)
1648-
: FailureDiagnostic(solution, locator), Member(member),
1649-
IsMissingImport(isMissingImport) {}
1646+
ConstraintLocator *locator)
1647+
: FailureDiagnostic(solution, locator), Member(member) {}
16501648

16511649
bool diagnoseAsError() override;
16521650
};

lib/Sema/CSFix.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,16 +1153,16 @@ MoveOutOfOrderArgument *MoveOutOfOrderArgument::create(
11531153

11541154
bool AllowInaccessibleMember::diagnose(const Solution &solution,
11551155
bool asNote) const {
1156-
InaccessibleMemberFailure failure(solution, getMember(), getLocator(),
1157-
IsMissingImport);
1156+
InaccessibleMemberFailure failure(solution, getMember(), getLocator());
11581157
return failure.diagnose(asNote);
11591158
}
11601159

1161-
AllowInaccessibleMember *AllowInaccessibleMember::create(
1162-
ConstraintSystem &cs, Type baseType, ValueDecl *member, DeclNameRef name,
1163-
ConstraintLocator *locator, bool isMissingImport) {
1164-
return new (cs.getAllocator()) AllowInaccessibleMember(
1165-
cs, baseType, member, name, locator, isMissingImport);
1160+
AllowInaccessibleMember *
1161+
AllowInaccessibleMember::create(ConstraintSystem &cs, Type baseType,
1162+
ValueDecl *member, DeclNameRef name,
1163+
ConstraintLocator *locator) {
1164+
return new (cs.getAllocator())
1165+
AllowInaccessibleMember(cs, baseType, member, name, locator);
11661166
}
11671167

11681168
bool AllowAnyObjectKeyPathRoot::diagnose(const Solution &solution,

lib/Sema/CSRanking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
963963
? SolutionCompareResult::Better
964964
: SolutionCompareResult::Worse;
965965
}
966-
966+
967967
// Compute relative score.
968968
unsigned score1 = 0;
969969
unsigned score2 = 0;

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10346,7 +10346,8 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
1034610346
// include them in the unviable candidates list.
1034710347
if (result.ViableCandidates.empty() && result.UnviableCandidates.empty() &&
1034810348
includeInaccessibleMembers) {
10349-
NameLookupOptions lookupOptions = defaultMemberLookupOptions;
10349+
NameLookupOptions lookupOptions =
10350+
defaultConstraintSolverMemberLookupOptions;
1035010351

1035110352
// Local function that looks up additional candidates using the given lookup
1035210353
// options, recording the results as unviable candidates.
@@ -10380,12 +10381,6 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
1038010381
if (lookupUnviable(lookupOptions | NameLookupFlags::IgnoreAccessControl,
1038110382
MemberLookupResult::UR_Inaccessible))
1038210383
return result;
10383-
10384-
// Ignore missing import statements in order to find more candidates that
10385-
// might have been missed before.
10386-
if (lookupUnviable(lookupOptions | NameLookupFlags::IgnoreMissingImports,
10387-
MemberLookupResult::UR_MissingImport))
10388-
return result;
1038910384
}
1039010385

1039110386
return result;
@@ -10586,11 +10581,9 @@ static ConstraintFix *fixMemberRef(
1058610581
}
1058710582

1058810583
case MemberLookupResult::UR_Inaccessible:
10589-
case MemberLookupResult::UR_MissingImport:
1059010584
assert(choice.isDecl());
10591-
return AllowInaccessibleMember::create(
10592-
cs, baseTy, choice.getDecl(), memberName, locator,
10593-
*reason == MemberLookupResult::UR_MissingImport);
10585+
return AllowInaccessibleMember::create(cs, baseTy, choice.getDecl(),
10586+
memberName, locator);
1059410587

1059510588
case MemberLookupResult::UR_UnavailableInExistential: {
1059610589
return choice.isDecl()

0 commit comments

Comments
 (0)