Skip to content

[Type checker] Use proper lookup type in substitutions for #keyPath. #4914

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
Sep 22, 2016
Merged
Show file tree
Hide file tree
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
22 changes: 12 additions & 10 deletions lib/Sema/TypeCheckExprObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,

// Local function to perform name lookup for the current index.
auto performLookup = [&](unsigned idx, Identifier componentName,
SourceLoc componentNameLoc) -> LookupResult {
SourceLoc componentNameLoc,
Type &lookupType) -> LookupResult {
if (state == Beginning)
return lookupUnqualified(dc, componentName, componentNameLoc);

Expand All @@ -170,7 +171,6 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
// Determine the type in which the lookup should occur. If we have
// a bridged value type, this will be the Objective-C class to
// which it is bridged.
Type lookupType;
if (auto bridgedClass = Context.getBridgedToObjC(dc, currentType))
lookupType = bridgedClass;
else
Expand Down Expand Up @@ -210,15 +210,17 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
}

// Look for this component.
LookupResult lookup = performLookup(idx, componentName, componentNameLoc);
Type lookupType;
LookupResult lookup = performLookup(idx, componentName, componentNameLoc,
lookupType);

// If we didn't find anything, try to apply typo-correction.
bool resultsAreFromTypoCorrection = false;
if (!lookup) {
performTypoCorrection(dc, DeclRefKind::Ordinary, currentType,
performTypoCorrection(dc, DeclRefKind::Ordinary, lookupType,
componentName, componentNameLoc,
(currentType ? defaultMemberTypeLookupOptions
: defaultUnqualifiedLookupOptions),
(lookupType ? defaultMemberTypeLookupOptions
: defaultUnqualifiedLookupOptions),
lookup);

if (currentType)
Expand Down Expand Up @@ -263,7 +265,7 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
if (resultsAreFromTypoCorrection)
break;

if (currentType)
if (lookupType)
diagnose(componentNameLoc, diag::ambiguous_member_overload_set,
componentName);
else
Expand Down Expand Up @@ -325,9 +327,9 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
}

Type newType;
if (currentType && !currentType->isAnyObject()) {
newType = currentType->getTypeOfMember(dc->getParentModule(), type,
this);
if (lookupType && !lookupType->isAnyObject()) {
newType = lookupType->getTypeOfMember(dc->getParentModule(), type,
this);
} else {
newType = type->getDeclaredInterfaceType();
}
Expand Down
14 changes: 14 additions & 0 deletions test/expr/unary/keypath/keypath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ class C {
var nonObjC: String? // expected-note{{add '@objc' to expose this var to Objective-C}}{{3-3=@objc }}
}

extension NSArray {
@objc class Foo : NSObject {
@objc var propString: String = ""
}
}

extension Array {
typealias Foo = NSArray.Foo
}

func testKeyPath(a: A, b: B) {
// Property
let _: String = #keyPath(A.propB)
Expand Down Expand Up @@ -84,6 +94,10 @@ func testKeyPath(a: A, b: B) {

// Property with keyword name.
let _: String = #keyPath(A.repeat)

// Nested type of a bridged type (rdar://problem/28061409).
typealias IntArray = [Int]
let _: String = #keyPath(IntArray.Foo.propString)
}

func testAsStaticString() {
Expand Down