Skip to content

Commit d61205b

Browse files
authored
Merge pull request swiftlang#25370 from xedin/rdar-51587755-5.1
[5.1][Diagnostics] Clarify requirement failure source when it's anchored a…
2 parents 6e65e44 + c2c80d6 commit d61205b

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
@@ -131,9 +131,17 @@ Optional<SelectedOverload> FailureDiagnostic::getChoiceFor(Expr *expr) {
131131
}
132132

133133
Type RequirementFailure::getOwnerType() const {
134-
return getType(getRawAnchor())
135-
->getInOutObjectType()
136-
->getMetatypeInstanceType();
134+
auto *anchor = getRawAnchor();
135+
136+
// If diagnostic is anchored at assignment expression
137+
// it means that requirement failure happend while trying
138+
// to convert source to destination, which means that
139+
// owner type is actually not an assignment expression
140+
// itself but its source.
141+
if (auto *assignment = dyn_cast<AssignExpr>(anchor))
142+
anchor = assignment->getSrc();
143+
144+
return getType(anchor)->getInOutObjectType()->getMetatypeInstanceType();
137145
}
138146

139147
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)