Skip to content

Commit 7ac1f8b

Browse files
authored
Merge pull request #4123 from jrose-apple/fix-ambiguity-for-generic-param-members
Always resolve overrides when doing member lookups.
2 parents f2443f4 + 7f84faa commit 7ac1f8b

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ namespace {
3939
DeclContext *DC;
4040
NameLookupOptions Options;
4141
bool ConsiderProtocolMembers;
42-
bool SearchingFromProtoExt = false;
42+
bool SearchingFromProtoExt;
43+
bool IsMemberLookup;
4344

4445
/// The vector of found declarations.
4546
SmallVector<ValueDecl *, 4> FoundDecls;
@@ -50,10 +51,11 @@ namespace {
5051
public:
5152
LookupResultBuilder(TypeChecker &tc, LookupResult &result, DeclContext *dc,
5253
NameLookupOptions options, bool considerProtocolMembers,
53-
bool searchingFromProtoExt)
54+
bool searchingFromProtoExt, bool isMemberLookup)
5455
: TC(tc), Result(result), DC(dc), Options(options),
5556
ConsiderProtocolMembers(considerProtocolMembers),
56-
SearchingFromProtoExt(searchingFromProtoExt) { }
57+
SearchingFromProtoExt(searchingFromProtoExt),
58+
IsMemberLookup(isMemberLookup) { }
5759

5860
~LookupResultBuilder() {
5961
// If any of the results have a base, we need to remove
@@ -62,7 +64,8 @@ namespace {
6264
// but there are weird assumptions about the results of unqualified
6365
// name lookup, e.g., that a local variable not having a type indicates
6466
// that it hasn't been seen yet.
65-
if (std::find_if(Result.begin(), Result.end(),
67+
if (!IsMemberLookup &&
68+
std::find_if(Result.begin(), Result.end(),
6669
[](const LookupResult::Result &found) {
6770
return found.Base != nullptr;
6871
}) == Result.end())
@@ -207,7 +210,8 @@ LookupResult TypeChecker::lookupUnqualified(DeclContext *dc, DeclName name,
207210
= options.contains(NameLookupFlags::ProtocolMembers);
208211
LookupResultBuilder builder(*this, result, dc, options,
209212
considerProtocolMembers,
210-
searchingFromProtoExt);
213+
searchingFromProtoExt,
214+
/*memberLookup*/false);
211215
for (const auto &found : lookup.Results) {
212216
// Determine which type we looked through to find this result.
213217
Type foundInType;
@@ -279,7 +283,8 @@ LookupResult TypeChecker::lookupMember(DeclContext *dc,
279283

280284
LookupResultBuilder builder(*this, result, dc, options,
281285
considerProtocolMembers,
282-
false);
286+
/*protocol extension*/false,
287+
/*member lookup*/true);
283288
SmallVector<ValueDecl *, 4> lookupResults;
284289
dc->lookupQualified(type, name, subOptions, this, lookupResults);
285290

test/Constraints/override.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %target-parse-verify-swift
2+
3+
class Base {
4+
func foo() {}
5+
}
6+
7+
class Sub: Base {
8+
override func foo() {}
9+
}
10+
11+
func removeOverrides<SomeSub: Sub>(concrete: Sub, generic: SomeSub) {
12+
_ = concrete.foo()
13+
_ = generic.foo()
14+
}

0 commit comments

Comments
 (0)