Skip to content

Sema: Prefer class properties to protocol properties when ranking overloads #18206

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

Closed
Closed
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
20 changes: 20 additions & 0 deletions lib/Sema/CSRanking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,26 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
}
}

// If both are properties and one is in a protocol, prefer the one not in
// a protocol.
//
// This is a Swift 4.1 compatibility hack. Changes elsewhere in the compiler
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we conditionalize it on language version?

Copy link
Contributor

Choose a reason for hiding this comment

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

Arguably this is in line with preferring concrete implementations over protocol implementations. It is a little funny when the concrete implementation is a superclass, but…

// made this case ambiguous. Perhaps it was never meant to be unambiguous in
// the first place, because it only seemed to work for properties, and not
// methods, so I'm going to keep this intentionally narrow.
if (isa<VarDecl>(decl1) && isa<VarDecl>(decl2)) {
auto *nominal1 = dc1->getAsNominalTypeOrNominalTypeExtensionContext();
auto *nominal2 = dc2->getAsNominalTypeOrNominalTypeExtensionContext();

if (nominal1 && nominal2 && nominal1 != nominal2) {
if (isa<ProtocolDecl>(nominal1))
score2 += weight;

if (isa<ProtocolDecl>(nominal2))
score1 += weight;
}
}

// If we haven't found a refinement, record whether one overload is in
// any way more constrained than another. We'll only utilize this
// information in the case of a potential ambiguity.
Expand Down
20 changes: 20 additions & 0 deletions test/Constraints/ranking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,23 @@ func testDerived(b: B) {
f0(f1(b))
// CHECK: end sil function '$S7ranking11testDerived1byAA1BC_tF'
}

protocol X {
var z: Int { get }
}

class Y {
var z: Int = 0
}

// CHECK-LABEL: sil hidden @$S7ranking32testGenericPropertyProtocolClassyyxAA1YCRbzAA1XRzlF
func testGenericPropertyProtocolClass<T : X & Y>(_ t: T) {
// CHECK: class_method {{%.*}} : $Y, #Y.z!getter.1
_ = t.z
}

// CHECK-LABEL: sil hidden @$S7ranking36testExistentialPropertyProtocolClassyyAA1X_AA1YCXcF
func testExistentialPropertyProtocolClass(_ t: X & Y) {
// CHECK: class_method {{%.*}} : $Y, #Y.z!getter.1
_ = t.z
}