-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Sema] Always look through optionals for unresolved member lookup #34715
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// RUN: %target-typecheck-verify-swift -dump-ast > %t.dump | ||
// RUN: %FileCheck %s < %t.dump | ||
|
||
// SR-13815 | ||
extension Optional { | ||
func sr13815() -> SR13815? { SR13815() } | ||
static func sr13815_2() -> SR13815? { SR13815() } | ||
static func sr13815_3() -> SR13815? { SR13815() } | ||
static var sr13815_wrongType: Int { 0 } | ||
static var sr13815_overload: SR13815 { SR13815() } | ||
init(overloaded: Void) { self = nil } | ||
} | ||
|
||
struct SR13815 { | ||
static var sr13815: SR13815? = SR13815() | ||
static var sr13815_2: SR13815? = SR13815() | ||
static var sr13815_wrongType: SR13815? { SR13815() } | ||
static var p_SR13815: SR13815? { SR13815() } | ||
static func sr13815_3() -> SR13815? { SR13815() } | ||
static var sr13815_overload: SR13815? { SR13815() } | ||
init(overloaded: Void) {} | ||
init?(failable: Void) {} | ||
init() {} | ||
} | ||
|
||
protocol P_SR13815 {} | ||
extension Optional: P_SR13815 where Wrapped: Equatable { | ||
static func p_SR13815() {} | ||
} | ||
|
||
let _: SR13815? = .sr13815 | ||
let _: SR13815? = .sr13815_wrongType | ||
let _: SR13815? = .init() | ||
let _: SR13815? = .sr13815() // expected-error {{instance member 'sr13815' cannot be used on type 'SR13815?'}} | ||
let _: SR13815? = .sr13815_2() | ||
let _: SR13815? = .init(SR13815()) | ||
let _: SR13815? = .init(overloaded: ()) | ||
// If members exist on Optional and Wrapped, always choose the one on optional | ||
// CHECK: declref_expr {{.*}} location={{.*}}optional_overload.swift:37 | ||
// CHECK-SAME: decl=optional_overload.(file).Optional extension.init(overloaded:) | ||
let _: SR13815? = .sr13815_overload | ||
// Should choose the overload from Optional even if the Wrapped overload would otherwise have a better score | ||
// CHECK: member_ref_expr {{.*}} location={{.*}}optional_overload.swift:41 | ||
// CHECK-SAME: decl=optional_overload.(file).Optional extension.sr13815_overload | ||
let _: SR13815? = .init(failable: ()) | ||
let _: SR13815? = .sr13815_3() | ||
let _: SR13815? = .p_SR13815 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//===--- UnresolvedMemberLookupTests.cpp --------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "SemaFixture.h" | ||
#include "swift/Sema/ConstraintSystem.h" | ||
|
||
using namespace swift; | ||
using namespace swift::unittest; | ||
using namespace swift::constraints; | ||
|
||
TEST_F(SemaTest, TestLookupAlwaysLooksThroughOptionalBase) { | ||
auto *intTypeDecl = getStdlibNominalTypeDecl("Int"); | ||
auto *optTypeDecl = getStdlibNominalTypeDecl("Optional"); | ||
auto intType = intTypeDecl->getDeclaredType(); | ||
auto intOptType = OptionalType::get(intType); | ||
auto stringType = getStdlibType("String"); | ||
|
||
auto *intMember = addExtensionVarMember(intTypeDecl, "test", intOptType); | ||
addExtensionVarMember(optTypeDecl, "test", stringType); | ||
|
||
auto *UME = new (Context) | ||
UnresolvedMemberExpr(SourceLoc(), DeclNameLoc(), | ||
DeclNameRef(Context.getIdentifier("test")), true); | ||
auto *UMCRE = new (Context) UnresolvedMemberChainResultExpr(UME, UME); | ||
|
||
ConstraintSystem cs(DC, ConstraintSystemOptions()); | ||
cs.generateConstraints(UMCRE, DC); | ||
cs.addConstraint( | ||
ConstraintKind::Conversion, cs.getType(UMCRE), intOptType, | ||
cs.getConstraintLocator(UMCRE, ConstraintLocator::ContextualType)); | ||
SmallVector<Solution, 2> solutions; | ||
cs.solve(solutions); | ||
|
||
// We should have a solution. | ||
ASSERT_EQ(solutions.size(), 1); | ||
|
||
auto &solution = solutions[0]; | ||
auto *locator = cs.getConstraintLocator(UME, | ||
ConstraintLocator::UnresolvedMember); | ||
auto choice = solution.getOverloadChoice(locator).choice; | ||
|
||
// The `test` member on `Int` should be selected. | ||
ASSERT_EQ(choice.getDecl(), intMember); | ||
} | ||
|
||
TEST_F(SemaTest, TestLookupPrefersResultsOnOptionalRatherThanBase) { | ||
auto *intTypeDecl = getStdlibNominalTypeDecl("Int"); | ||
auto *optTypeDecl = getStdlibNominalTypeDecl("Optional"); | ||
auto intType = intTypeDecl->getDeclaredType(); | ||
auto intOptType = OptionalType::get(intType); | ||
|
||
addExtensionVarMember(intTypeDecl, "test", intOptType); | ||
auto *optMember = addExtensionVarMember(optTypeDecl, "test", intType); | ||
|
||
auto *UME = new (Context) | ||
UnresolvedMemberExpr(SourceLoc(), DeclNameLoc(), | ||
DeclNameRef(Context.getIdentifier("test")), true); | ||
auto *UMCRE = new (Context) UnresolvedMemberChainResultExpr(UME, UME); | ||
|
||
ConstraintSystem cs(DC, ConstraintSystemOptions()); | ||
cs.generateConstraints(UMCRE, DC); | ||
cs.addConstraint( | ||
ConstraintKind::Conversion, cs.getType(UMCRE), intOptType, | ||
cs.getConstraintLocator(UMCRE, ConstraintLocator::ContextualType)); | ||
SmallVector<Solution, 2> solutions; | ||
cs.solve(solutions); | ||
|
||
// We should have a solution. | ||
ASSERT_EQ(solutions.size(), 1); | ||
|
||
auto &solution = solutions[0]; | ||
auto *locator = cs.getConstraintLocator(UME, | ||
ConstraintLocator::UnresolvedMember); | ||
auto choice = solution.getOverloadChoice(locator).choice; | ||
auto score = solution.getFixedScore(); | ||
|
||
// The `test` member on `Optional` should be chosen over the member on `Int`, | ||
// even though the score is otherwise worse. | ||
ASSERT_EQ(score.Data[SK_ValueToOptional], 1); | ||
ASSERT_EQ(choice.getDecl(), optMember); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would break source compatibility when score of something found on
Wrapped
be better than score onOptional
itself...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we already score
Decl
better thanDeclViaUnwrappedOptional
, the score onWrapped
would have to outweigh the disadvantage from theOverloadChoiceKind
mismatch. Do you think such situations are likely to occur in practice?Is there any way to unconditionally prefer the non-optional choices, in the case where we've found multiple solutions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solutions are first ranked on the score produced by the solver (https://github.com/apple/swift/blob/main/lib/Sema/CSRanking.cpp#L758) and then their declaration diff is compared if scores match. It's quite possible that choices could have different optionality of require other implicit conversion which would change the behavior here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xedin Ah, thank you for pointing that out. The situation where we have identically-named static members on both
Optional<Wrapped>
andWrapped
where both properly type-check seems pretty unlikely to me, but if it's above the risk level for being considered source-compatible (as opposed to just a bug) then so be it.Is there a way to add the
Wrapped
overloads but not skip over them whenever anyOptional
overloads successfully type check?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One way to implement this without changing existing behavior would be to lift
DeclViaUnwrappedOptional
into the solver score to make sure regular declarations are always preferred over the ones found on wrapped type.