Skip to content

Commit 23701bb

Browse files
authored
Merge pull request swiftlang#69174 from xedin/rdar-116122902
[CSDiagnostics] Adjust how requirement failures anchor type is computed
2 parents e1fdf50 + 4c38d72 commit 23701bb

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ bool FailureDiagnostic::conformsToKnownProtocol(
217217
}
218218

219219
Type RequirementFailure::getOwnerType() const {
220-
auto anchor = getRawAnchor();
221-
220+
auto anchor = getAnchor();
222221
// If diagnostic is anchored at assignment expression
223222
// it means that requirement failure happened while trying
224223
// to convert source to destination, which means that

lib/Sema/ConstraintSystem.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5729,6 +5729,13 @@ void constraints::simplifyLocator(ASTNode &anchor,
57295729
// Extract tuple element.
57305730
auto elt = path[0].castTo<LocatorPathElt::AnyTupleElement>();
57315731
unsigned index = elt.getIndex();
5732+
5733+
if (auto *AE = getAsExpr<AssignExpr>(anchor)) {
5734+
if (isa<TupleExpr>(AE->getSrc())) {
5735+
anchor = AE->getSrc();
5736+
}
5737+
}
5738+
57325739
if (auto tupleExpr = getAsExpr<TupleExpr>(anchor)) {
57335740
if (index < tupleExpr->getNumElements()) {
57345741
anchor = tupleExpr->getElement(index);
@@ -5744,6 +5751,7 @@ void constraints::simplifyLocator(ASTNode &anchor,
57445751
continue;
57455752
}
57465753
}
5754+
57475755
break;
57485756
}
57495757

test/Generics/conditional_conformances.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func existential_good<T: P1>(_: T.Type) {
355355
}
356356

357357
func existential_bad<T>(_: T.Type) {
358-
_ = Free<T>() as P2 // expected-error{{protocol 'P2' requires that 'T' conform to 'P1'}}
358+
_ = Free<T>() as P2 // expected-error{{generic struct 'Free' requires that 'T' conform to 'P1'}}
359359
}
360360

361361
// rdar://problem/35837054

test/Generics/conditional_conformances_literals.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func arraySameType() {
4545

4646
let _: SameType = arrayWorks as SameType
4747
let _: SameType = arrayFails as SameType
48-
// expected-error@-1 {{protocol 'SameType' requires the types 'Fails' and 'Works' be equivalent}}
48+
// expected-error@-1 {{generic struct 'Array' requires the types 'Fails' and 'Works' be equivalent}}
4949
}
5050

5151
func dictionarySameType() {
@@ -70,7 +70,7 @@ func dictionarySameType() {
7070

7171
let _: SameType = dictWorks as SameType
7272
let _: SameType = dictFails as SameType
73-
// expected-error@-1 {{protocol 'SameType' requires the types 'Fails' and 'Works' be equivalent}}
73+
// expected-error@-1 {{generic struct 'Dictionary' requires the types 'Fails' and 'Works' be equivalent}}
7474
}
7575

7676
func arrayConforms() {
@@ -91,11 +91,11 @@ func arrayConforms() {
9191

9292
let _: Conforms = [works] as Conforms
9393
let _: Conforms = [fails] as Conforms
94-
// expected-error@-1 {{protocol 'Conforms' requires that 'Fails' conform to 'Conforms'}}
94+
// expected-error@-1 {{generic struct 'Array' requires that 'Fails' conform to 'Conforms'}}
9595

9696
let _: Conforms = arrayWorks as Conforms
9797
let _: Conforms = arrayFails as Conforms
98-
// expected-error@-1 {{protocol 'Conforms' requires that 'Fails' conform to 'Conforms'}}
98+
// expected-error@-1 {{generic struct 'Array' requires that 'Fails' conform to 'Conforms'}}
9999
}
100100

101101
func dictionaryConforms() {
@@ -116,11 +116,11 @@ func dictionaryConforms() {
116116

117117
let _: Conforms = [0 : works] as Conforms
118118
let _: Conforms = [0 : fails] as Conforms
119-
// expected-error@-1 {{protocol 'Conforms' requires that 'Fails' conform to 'Conforms'}}
119+
// expected-error@-1 {{generic struct 'Dictionary' requires that 'Fails' conform to 'Conforms'}}
120120

121121
let _: Conforms = dictWorks as Conforms
122122
let _: Conforms = dictFails as Conforms
123-
// expected-error@-1 {{protocol 'Conforms' requires that 'Fails' conform to 'Conforms'}}
123+
// expected-error@-1 {{generic struct 'Dictionary' requires that 'Fails' conform to 'Conforms'}}
124124
}
125125

126126
func combined() {
@@ -133,6 +133,6 @@ func combined() {
133133
// expected-error@-1 {{type 'any Conforms' cannot conform to 'Conforms'}} expected-note@-1 {{only concrete types such as structs, enums and classes can conform to protocols}}
134134

135135
let _: Conforms = [[0: [1 : [fails]] as Conforms]]
136-
// expected-error@-1 {{protocol 'Conforms' requires that 'Fails' conform to 'Conforms'}}
136+
// expected-error@-1 {{generic struct 'Dictionary' requires that 'Fails' conform to 'Conforms'}}
137137
// expected-error@-2 {{type 'any Conforms' cannot conform to 'Conforms'}} expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}}
138138
}
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 AnyValue {
4+
}
5+
6+
struct Value<T> {}
7+
8+
extension Value: AnyValue where T = { // expected-error {{use '==' for same-type requirements rather than '='}} expected-error {{expected type}}
9+
// expected-note@-1 {{requirement from conditional conformance of 'Value<T>' to 'AnyValue'}}
10+
}
11+
12+
struct Test {
13+
var tuple: (value: any AnyValue, id: Int)?
14+
15+
mutating func test<T>(v: Value<T>) {
16+
_ = {
17+
// FIXME(diagnostics): We need to figure out how to avoid mentioning <<error type>> in the second diagnostic
18+
self.tuple = (v, 42)
19+
// expected-error@-1 {{cannot assign value of type '(Value<T>, Int)' to type '(value: any AnyValue, id: Int)'}}
20+
// expected-error@-2 {{generic struct 'Value' requires the types 'T' and '<<error type>>' be equivalent}}
21+
return 0
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)