Skip to content

Commit 8048549

Browse files
authored
Merge pull request #69249 from xedin/rdar-116122902-5.10
[5.10][CSDiagnostics] Adjust how requirement failures anchor type is computed
2 parents 0c55704 + 3cdb2a1 commit 8048549

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
@@ -216,8 +216,7 @@ bool FailureDiagnostic::conformsToKnownProtocol(
216216
}
217217

218218
Type RequirementFailure::getOwnerType() const {
219-
auto anchor = getRawAnchor();
220-
219+
auto anchor = getAnchor();
221220
// If diagnostic is anchored at assignment expression
222221
// it means that requirement failure happened while trying
223222
// 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
@@ -5705,6 +5705,13 @@ void constraints::simplifyLocator(ASTNode &anchor,
57055705
// Extract tuple element.
57065706
auto elt = path[0].castTo<LocatorPathElt::AnyTupleElement>();
57075707
unsigned index = elt.getIndex();
5708+
5709+
if (auto *AE = getAsExpr<AssignExpr>(anchor)) {
5710+
if (isa<TupleExpr>(AE->getSrc())) {
5711+
anchor = AE->getSrc();
5712+
}
5713+
}
5714+
57085715
if (auto tupleExpr = getAsExpr<TupleExpr>(anchor)) {
57095716
if (index < tupleExpr->getNumElements()) {
57105717
anchor = tupleExpr->getElement(index);
@@ -5720,6 +5727,7 @@ void constraints::simplifyLocator(ASTNode &anchor,
57205727
continue;
57215728
}
57225729
}
5730+
57235731
break;
57245732
}
57255733

test/Generics/conditional_conformances.swift

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

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

359359
// 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)