Skip to content

Commit 002c04e

Browse files
committed
Modified diagnostics a bit given feedback
1 parent 782f620 commit 002c04e

File tree

8 files changed

+66
-15
lines changed

8 files changed

+66
-15
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1782,9 +1782,18 @@ NOTE(bad_associated_type_deduction,none,
17821782
"unable to infer associated type %0 for protocol %1",
17831783
(DeclName, DeclName))
17841784
NOTE(associated_type_deduction_witness_failed,none,
1785-
"candidate would match and infer %0=%1 if %1 "
1785+
"candidate would match and infer %0 = %1 if %1 "
17861786
"%select{inherited from|conformed to}3 %2",
17871787
(DeclName, Type, Type, bool))
1788+
NOTE(associated_type_witness_conform_impossible,none,
1789+
"candidate can not infer %0 = %1 because %1 "
1790+
"is not a nominal type and so can't conform to %2",
1791+
(DeclName, Type, Type))
1792+
NOTE(associated_type_witness_inherit_impossible,none,
1793+
"candidate can not infer %0 = %1 because %1 "
1794+
"is not a class type and so can't inherit from %2",
1795+
(DeclName, Type, Type))
1796+
17881797
NOTE(ambiguous_associated_type_deduction,none,
17891798
"ambiguous inference of associated type %0: %1 vs. %2",
17901799
(DeclName, Type, Type))

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,8 +861,8 @@ swift::matchWitness(TypeChecker &tc,
861861
return RequirementMatch(witness, MatchKind::MissingConformance,
862862
assocType, protocolType);
863863
if (type->isEqual(conformance->getType())) {
864-
if (auto bgt = type->getAs<BoundGenericType>())
865-
type = bgt->getDecl()->getDeclaredType();
864+
if (auto agt = type->getAs<AnyGenericType>())
865+
type = agt->getDecl()->getDeclaredInterfaceType();
866866
return RequirementMatch(witness, MatchKind::MissingConformance, type,
867867
protocolType);
868868
}

lib/Sema/TypeCheckProtocolInference.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,23 @@ bool AssociatedTypeInference::diagnoseNoSolutions(
17471747
if (failed.Result.isError())
17481748
continue;
17491749

1750+
if (!failed.TypeWitness->is<NominalType>() &&
1751+
failed.Result.isConformanceRequirement()) {
1752+
diags.diagnose(failed.Witness,
1753+
diag::associated_type_witness_conform_impossible,
1754+
assocType->getName(), failed.TypeWitness,
1755+
failed.Result.getRequirement());
1756+
continue;
1757+
}
1758+
if (!failed.TypeWitness->is<ClassType>() &&
1759+
!failed.Result.isConformanceRequirement()) {
1760+
diags.diagnose(failed.Witness,
1761+
diag::associated_type_witness_inherit_impossible,
1762+
assocType->getName(), failed.TypeWitness,
1763+
failed.Result.getRequirement());
1764+
continue;
1765+
}
1766+
17501767
diags.diagnose(failed.Witness,
17511768
diag::associated_type_deduction_witness_failed,
17521769
assocType->getName(),

test/Generics/associated_types_inherit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct X1 : P {
1919
}
2020

2121
struct X2 : P { // expected-error{{type 'X2' does not conform to protocol 'P'}}
22-
func getAssoc() -> E { return E() } // expected-note{{candidate would match and infer 'Assoc'='E' if 'E' inherited from 'C'}}
22+
func getAssoc() -> E { return E() } // expected-note{{candidate would match and infer 'Assoc' = 'E' if 'E' inherited from 'C'}}
2323
}
2424

2525
func testP<T:P>(_ t: T) {

test/decl/protocol/req/associated_type_inference.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ struct X0f : P0 { // okay: Assoc1 = Int because Float doesn't conform to PSimple
5050
}
5151

5252
struct X0g : P0 { // expected-error{{type 'X0g' does not conform to protocol 'P0'}}
53-
func f0(_: Float) { } // expected-note{{candidate would match and infer 'Assoc1'='Float' if 'Float' conformed to 'PSimple'}}
54-
func g0(_: Float) { } // expected-note{{candidate would match and infer 'Assoc1'='Float' if 'Float' conformed to 'PSimple'}}
53+
func f0(_: Float) { } // expected-note{{candidate would match and infer 'Assoc1' = 'Float' if 'Float' conformed to 'PSimple'}}
54+
func g0(_: Float) { } // expected-note{{candidate would match and infer 'Assoc1' = 'Float' if 'Float' conformed to 'PSimple'}}
5555
}
5656

5757
struct X0h<T : PSimple> : P0 {
@@ -93,8 +93,8 @@ protocol P2 {
9393
}
9494

9595
extension P2 where Self.P2Assoc : PSimple {
96-
func f0(_ x: P2Assoc) { } // expected-note{{candidate would match and infer 'Assoc1'='Float' if 'Float' conformed to 'PSimple'}}
97-
func g0(_ x: P2Assoc) { } // expected-note{{candidate would match and infer 'Assoc1'='Float' if 'Float' conformed to 'PSimple'}}
96+
func f0(_ x: P2Assoc) { } // expected-note{{candidate would match and infer 'Assoc1' = 'Float' if 'Float' conformed to 'PSimple'}}
97+
func g0(_ x: P2Assoc) { } // expected-note{{candidate would match and infer 'Assoc1' = 'Float' if 'Float' conformed to 'PSimple'}}
9898
}
9999

100100
struct X0k : P0, P2 {
@@ -123,7 +123,7 @@ struct XProp0a : PropertyP0 { // okay PropType = Int
123123
}
124124

125125
struct XProp0b : PropertyP0 { // expected-error{{type 'XProp0b' does not conform to protocol 'PropertyP0'}}
126-
var property: Float // expected-note{{candidate would match and infer 'Prop'='Float' if 'Float' conformed to 'PSimple'}}
126+
var property: Float // expected-note{{candidate would match and infer 'Prop' = 'Float' if 'Float' conformed to 'PSimple'}}
127127
}
128128

129129
// Inference from subscripts
@@ -144,7 +144,7 @@ struct XSubP0a : SubscriptP0 {
144144

145145
struct XSubP0b : SubscriptP0 {
146146
// expected-error@-1{{type 'XSubP0b' does not conform to protocol 'SubscriptP0'}}
147-
subscript (i: Int) -> Float { get { return Float(i) } } // expected-note{{candidate would match and infer 'Element'='Float' if 'Float' conformed to 'PSimple'}}
147+
subscript (i: Int) -> Float { get { return Float(i) } } // expected-note{{candidate would match and infer 'Element' = 'Float' if 'Float' conformed to 'PSimple'}}
148148
}
149149

150150
struct XSubP0c : SubscriptP0 {

test/decl/protocol/req/func.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ protocol P12 {
266266
struct XIndexType : P11 { }
267267

268268
struct X12 : P12 { // expected-error{{type 'X12' does not conform to protocol 'P12'}}
269-
func getIndex() -> XIndexType { return XIndexType() } // expected-note{{candidate would match and infer 'Index'='XIndexType' if 'XIndexType' conformed to 'P1'}}
269+
func getIndex() -> XIndexType { return XIndexType() } // expected-note{{candidate would match and infer 'Index' = 'XIndexType' if 'XIndexType' conformed to 'P1'}}
270270
}
271271

272272
func ==(x: X12.Index, y: X12.Index) -> Bool { return true }

test/decl/protocol/req/missing_conformance.swift

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protocol LikeSequence {
2424
func makeIter() -> Iter
2525
}
2626
extension LikeSequence where Self == Self.Iter {
27-
func makeIter() -> Self { return self } // expected-note {{candidate would match and infer 'Iter'='Y' if 'Y' conformed to 'IterProtocol'}}
27+
func makeIter() -> Self { return self } // expected-note {{candidate would match and infer 'Iter' = 'Y' if 'Y' conformed to 'IterProtocol'}}
2828
}
2929

3030
struct Y : LikeSequence {} // expected-error {{type 'Y' does not conform to protocol 'LikeSequence'}}
@@ -42,16 +42,41 @@ extension P1 where Result : P2 {
4242
}
4343
protocol P3 {}
4444
extension P1 where Self : P3 {
45-
func got() {} // expected-note {{candidate would match if 'Z' conformed to 'P3'}}
45+
func got() {} // expected-note {{candidate would match if 'Z<T1, T2, T3, Result, T4>' conformed to 'P3'}}
4646
}
4747

4848
struct Z<T1, T2, T3, Result, T4> : P1 {} // expected-error {{type 'Z<T1, T2, T3, Result, T4>' does not conform to protocol 'P1'}}
4949

5050
protocol P4 {
51-
func this() // expected-note {{protocol requires function 'this()' with type '() -> ()'; do you want to add a stub?}}
51+
func this() // expected-note 2 {{protocol requires function 'this()' with type '() -> ()'; do you want to add a stub?}}
5252
}
5353
protocol P5 {}
5454
extension P4 where Self : P5 {
5555
func this() {} // expected-note {{candidate would match if 'W' conformed to 'P5'}}
56+
//// expected-note@-1 {{candidate would match if 'S<T>.SS' conformed to 'P5'}}
5657
}
5758
struct W : P4 {} // expected-error {{type 'W' does not conform to protocol 'P4'}}
59+
60+
struct S<T> {
61+
struct SS : P4 {} // expected-error {{type 'S<T>.SS' does not conform to protocol 'P4'}}
62+
}
63+
64+
class C {}
65+
protocol P6 {
66+
associatedtype T : C // expected-note {{unable to infer associated type 'T' for protocol 'P6'}}
67+
func f(t: T)
68+
}
69+
70+
struct A : P6 { // expected-error {{type 'A' does not conform to protocol 'P6'}}
71+
func f(t: Int) {} // expected-note {{candidate can not infer 'T' = 'Int' because 'Int' is not a class type and so can't inherit from 'C'}}
72+
}
73+
74+
protocol P7 {}
75+
protocol P8 {
76+
associatedtype T : P7 // expected-note {{unable to infer associated type 'T' for protocol 'P8'}}
77+
func g(t: T)
78+
}
79+
80+
struct B : P8 { // expected-error {{type 'B' does not conform to protocol 'P8'}}
81+
func g(t: (Int, String)) {} // expected-note {{candidate can not infer 'T' = '(Int, String)' because '(Int, String)' is not a nominal type and so can't conform to 'P7'}}
82+
}

test/stmt/foreach.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func bad_containers_2(bc: BadContainer2) {
1717
}
1818

1919
struct BadContainer3 : Sequence { // expected-error{{type 'BadContainer3' does not conform to protocol 'Sequence'}}
20-
func makeIterator() { } // expected-note{{candidate would match and infer 'Iterator'='()' if '()' conformed to 'IteratorProtocol'}}
20+
func makeIterator() { } // expected-note{{candidate can not infer 'Iterator' = '()' because '()' is not a nominal type and so can't conform to 'IteratorProtocol'}}
2121
}
2222

2323
func bad_containers_3(bc: BadContainer3) {

0 commit comments

Comments
 (0)