Skip to content

lookup: prefer Swift types over Clang ones when multiple types are present in lookup #40440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,21 @@ static void diagnoseAmbiguousMemberType(Type baseTy, SourceRange baseRange,
}
}

static LookupTypeResult preferSwiftTypes(LookupTypeResult results) {
if (results.size() == 1)
return results;
LookupTypeResult newResults;
for (auto cur: results) {
if (cur.Member->getClangNode()) {
continue;
}
newResults.addResult(cur);
}
if (newResults.size() == 1)
return newResults;
return results;
}

/// Resolve the given identifier type representation as a qualified
/// lookup within the given parent type, returning the type it
/// references.
Expand Down Expand Up @@ -1533,6 +1548,9 @@ static Type resolveNestedIdentTypeComponent(TypeResolution resolution,
memberTypes = TypeChecker::lookupMemberType(
DC, parentTy, comp->getNameRef(), lookupOptions);

// Prefer Swift types over Clang types.
memberTypes = preferSwiftTypes(memberTypes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test case? Should this go inside lookupMemberType() instead, since there are other callers of lookupMemberType() elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add a test case. I'm not sure about adding this to lookupMemberType() because code completion may show less results.


// Name lookup was ambiguous. Complain.
// FIXME: Could try to apply generic arguments first, and see whether
// that resolves things. But do we really want that to succeed?
Expand Down