Skip to content

Commit ff25108

Browse files
committed
[CSDiagnostics] Use correct overload locator when requirement belongs to explicit init ref
Previously the logic to determine path to the selected overload in such case was simplistic and only checked the name to be of constructor, but `ConstructorMember` path is formed only if member reference is a constructor delegation e.g. `self.init` or `super.init` in an initializer context.
1 parent 69ff99f commit ff25108

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ ProtocolConformance *RequirementFailure::getConformanceForConditionalReq(
134134

135135
ValueDecl *RequirementFailure::getDeclRef() const {
136136
auto &cs = getConstraintSystem();
137+
auto &TC = getTypeChecker();
137138

138139
auto *anchor = getRawAnchor();
139140
auto *locator = cs.getConstraintLocator(anchor);
@@ -156,7 +157,7 @@ ValueDecl *RequirementFailure::getDeclRef() const {
156157
} else if (auto *UDE = dyn_cast<UnresolvedDotExpr>(anchor)) {
157158
ConstraintLocatorBuilder member(locator);
158159

159-
if (UDE->getName().isSimpleName(DeclBaseName::createConstructor())) {
160+
if (TC.getSelfForInitDelegationInConstructor(getDC(), UDE)) {
160161
member = member.withPathElement(PathEltKind::ConstructorMember);
161162
} else {
162163
member = member.withPathElement(PathEltKind::Member);

test/Constraints/generics.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,3 +677,13 @@ protocol C {
677677
protocol D {
678678
associatedtype Foo where Foo: String // expected-error {{type 'Self.Foo' constrained to non-protocol, non-class type 'String'}}
679679
}
680+
681+
func member_ref_with_explicit_init() {
682+
struct S<T: P> { // expected-note {{where 'T' = 'Int'}}
683+
init(_: T) {}
684+
init(_: T, _ other: Int = 42) {}
685+
}
686+
687+
_ = S.init(42)
688+
// expected-error@-1 {{generic struct 'S' requires that 'Int' conform to 'P'}}
689+
}

0 commit comments

Comments
 (0)