Skip to content

Commit 81156b2

Browse files
authored
Merge pull request #25357 from xedin/rdar-51587755
[Diagnostics] Clarify requirement failure source when it's anchored a…
2 parents 9298944 + 4bd5a1e commit 81156b2

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,17 @@ Optional<SelectedOverload> FailureDiagnostic::getChoiceFor(Expr *expr) const {
111111
}
112112

113113
Type RequirementFailure::getOwnerType() const {
114-
return getType(getRawAnchor())
115-
->getInOutObjectType()
116-
->getMetatypeInstanceType();
114+
auto *anchor = getRawAnchor();
115+
116+
// If diagnostic is anchored at assignment expression
117+
// it means that requirement failure happend while trying
118+
// to convert source to destination, which means that
119+
// owner type is actually not an assignment expression
120+
// itself but its source.
121+
if (auto *assignment = dyn_cast<AssignExpr>(anchor))
122+
anchor = assignment->getSrc();
123+
124+
return getType(anchor)->getInOutObjectType()->getMetatypeInstanceType();
117125
}
118126

119127
const GenericContext *RequirementFailure::getGenericContext() const {

test/Constraints/sr10906.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
protocol ViewDataSource: class {
4+
func foo<T>() -> [T]
5+
}
6+
7+
class View {
8+
weak var delegate: ViewDataSource?
9+
}
10+
11+
final class ViewController<T> {
12+
let view = View()
13+
init() {
14+
view.delegate = self
15+
// expected-error@-1 {{generic class 'ViewController' requires the types 'T' and 'String' be equivalent}}
16+
}
17+
}
18+
19+
extension ViewController: ViewDataSource where T == String {
20+
// expected-note@-1 {{requirement from conditional conformance of 'ViewController<T>' to 'ViewDataSource'}}
21+
func foo<T>() -> [T] {
22+
return []
23+
}
24+
}

0 commit comments

Comments
 (0)