Skip to content

Commit 6133ffe

Browse files
authored
Merge pull request #11274 from slavapestov/bogus-generic-signature-diagnostic
AST: Fix bogus diagnostic with bad conformance requirements in generic signature
2 parents 699510c + eb46696 commit 6133ffe

File tree

8 files changed

+27
-23
lines changed

8 files changed

+27
-23
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,11 +1659,12 @@ ERROR(protocol_composition_one_class,none,
16591659
"contains class %1", (Type, Type))
16601660

16611661
ERROR(requires_conformance_nonprotocol,none,
1662-
"type %0 constrained to non-protocol type %1", (TypeLoc, TypeLoc))
1662+
"type %0 constrained to non-protocol, non-class type %1",
1663+
(TypeLoc, TypeLoc))
16631664
ERROR(requires_not_suitable_archetype,none,
1664-
"%select{|first |second }0type %1 in %select{conformance|same-type}2 "
1665-
"requirement does not refer to a generic parameter or associated type",
1666-
(int, TypeLoc, int))
1665+
"type %0 in conformance requirement does not refer to a "
1666+
"generic parameter or associated type",
1667+
(TypeLoc))
16671668
ERROR(requires_no_same_type_archetype,none,
16681669
"neither type in same-type refers to a generic parameter or "
16691670
"associated type",

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2952,7 +2952,7 @@ ConstraintResult GenericSignatureBuilder::addLayoutRequirement(
29522952
// complain.
29532953
if (source.isExplicit() && source.getLoc().isValid()) {
29542954
Diags.diagnose(source.getLoc(), diag::requires_not_suitable_archetype,
2955-
0, TypeLoc::withoutLoc(resolvedSubject->getType()), 0);
2955+
TypeLoc::withoutLoc(resolvedSubject->getType()));
29562956
return ConstraintResult::Concrete;
29572957
}
29582958

@@ -3078,20 +3078,14 @@ ConstraintResult GenericSignatureBuilder::addTypeRequirement(
30783078
}
30793079

30803080
// The right-hand side needs to be concrete.
3081+
Type constraintType;
30813082
if (auto constraintPA = resolvedConstraint->getPotentialArchetype()) {
3082-
// The constraint type isn't a statically-known constraint.
3083-
if (source.getLoc().isValid()) {
3084-
auto constraintType = constraintPA->getDependentType(Impl->GenericParams);
3085-
Diags.diagnose(source.getLoc(), diag::requires_not_suitable_archetype,
3086-
1, TypeLoc::withoutLoc(constraintType), 0);
3087-
}
3088-
3089-
return ConstraintResult::Concrete;
3083+
constraintType = constraintPA->getDependentType(Impl->GenericParams);
3084+
} else {
3085+
constraintType = resolvedConstraint->getType();
30903086
}
30913087

30923088
// Check whether we have a reasonable constraint type at all.
3093-
auto constraintType = resolvedConstraint->getType();
3094-
assert(constraintType && "Missing constraint type?");
30953089
if (!constraintType->isExistentialType() &&
30963090
!constraintType->getClassOrBoundGenericClass()) {
30973091
if (source.getLoc().isValid() && !constraintType->hasError()) {
@@ -3129,7 +3123,7 @@ ConstraintResult GenericSignatureBuilder::addTypeRequirement(
31293123
if (source.isExplicit()) {
31303124
if (source.getLoc().isValid()) {
31313125
Diags.diagnose(source.getLoc(), diag::requires_not_suitable_archetype,
3132-
0, TypeLoc::withoutLoc(resolvedSubject->getType()), 0);
3126+
TypeLoc::withoutLoc(resolvedSubject->getType()));
31333127
}
31343128

31353129
return ConstraintResult::Concrete;

test/Generics/function_defs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func beginsWith3<S0: P2, S1: P2>(_ seq1: S0, _ seq2: S1) -> Bool
284284
// Bogus requirements
285285
//===----------------------------------------------------------------------===//
286286
func nonTypeReq<T>(_: T) where T : Wibble {} // expected-error{{use of undeclared type 'Wibble'}}
287-
func badProtocolReq<T>(_: T) where T : Int {} // expected-error{{type 'T' constrained to non-protocol type 'Int'}}
287+
func badProtocolReq<T>(_: T) where T : Int {} // expected-error{{type 'T' constrained to non-protocol, non-class type 'Int'}}
288288

289289
func nonTypeSameType<T>(_: T) where T == Wibble {} // expected-error{{use of undeclared type 'Wibble'}}
290290
func nonTypeSameType2<T>(_: T) where Wibble == T {} // expected-error{{use of undeclared type 'Wibble'}}

test/Generics/invalid.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,12 @@ protocol P1 {
101101
associatedtype C where ThisTypeDoesNotExist == ThisTypeDoesNotExist
102102
// expected-error@-1 2{{use of undeclared type 'ThisTypeDoesNotExist'}}
103103
}
104+
105+
// Diagnostic referred to the wrong type - <rdar://problem/33604221>
106+
107+
protocol E { associatedtype XYZ }
108+
109+
class P<N> {
110+
func q<A>(b:A) where A:E, N : A.XYZ { return }
111+
// expected-error@-1 {{type 'N' constrained to non-protocol, non-class type 'A.XYZ'}}
112+
}

test/Sema/circular_decl_checking.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var TopLevelVar: TopLevelVar? { return nil } // expected-error 2 {{use of undecl
4545

4646
// FIXME: The first error is redundant, isn't correct in what it states, and
4747
// also should be emitted on the inheritance clause.
48-
protocol AProtocol { // expected-error {{first type 'Self.e' in conformance requirement does not refer to a generic parameter or associated type}}
48+
protocol AProtocol { // expected-error {{type 'Self.e' constrained to non-protocol, non-class type 'Self.e'}}
4949
associatedtype e : e // expected-error {{inheritance from non-protocol, non-class type 'Self.e'}}
5050
}
5151

test/attr/attr_specialize.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ public func anotherFuncWithTwoGenericParameters<X: P, Y>(x: X, y: Y) {
151151

152152
@_specialize(where T: P) // expected-error{{Only same-type and layout requirements are supported by '_specialize' attribute}}
153153
@_specialize(where T: Int) // expected-error{{Only conformances to protocol types are supported by '_specialize' attribute}} expected-error{{Only same-type and layout requirements are supported by '_specialize' attribute}}
154-
// expected-error@-1{{type 'T' constrained to non-protocol type 'Int'}}
154+
// expected-error@-1{{type 'T' constrained to non-protocol, non-class type 'Int'}}
155155

156156
@_specialize(where T: S1) // expected-error{{Only conformances to protocol types are supported by '_specialize' attribute}} expected-error{{Only same-type and layout requirements are supported by '_specialize' attribute}}
157-
// expected-error@-1{{type 'T' constrained to non-protocol type 'S1'}}
157+
// expected-error@-1{{type 'T' constrained to non-protocol, non-class type 'S1'}}
158158
@_specialize(where T: C1) // expected-error{{Only conformances to protocol types are supported by '_specialize' attribute}} expected-error{{Only same-type and layout requirements are supported by '_specialize' attribute}}
159159
@_specialize(where Int: P) // expected-error{{type 'Int' in conformance requirement does not refer to a generic parameter or associated type}} expected-error{{Only same-type and layout requirements are supported by '_specialize' attribute}} expected-error{{too few type parameters are specified in '_specialize' attribute (got 0, but expected 1)}} expected-error{{Missing constraint for 'T' in '_specialize' attribute}}
160160
func funcWithForbiddenSpecializeRequirement<T>(_ t: T) {

test/decl/protocol/protocols.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func test1() {
4040

4141
protocol Bogus : Int {}
4242
// expected-error@-1{{inheritance from non-protocol type 'Int'}}
43-
// expected-error@-2{{type 'Self' constrained to non-protocol type 'Int'}}
43+
// expected-error@-2{{type 'Self' constrained to non-protocol, non-class type 'Int'}}
4444

4545
// Explicit conformance checks (successful).
4646

test/decl/protocol/req/unsatisfiable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ protocol Base {
4040
// FIXME: This used to /not/ error in Swift 3. It didn't impose any statically-
4141
// enforced requirements, but the compiler crashed if you used anything but the
4242
// same type.
43-
protocol Sub1: Base { // expected-error {{first type 'Self.Assoc' in conformance requirement does not refer to a generic parameter or associated type}}
43+
protocol Sub1: Base { // expected-error {{type 'Self.SubAssoc' constrained to non-protocol, non-class type 'Self.Assoc'}}
4444
associatedtype SubAssoc: Assoc // expected-error {{inheritance from non-protocol, non-class type 'Self.Assoc'}}
4545
}
4646
// FIXME: This error is incorrect in what it states and should be emitted on
4747
// the where-clause.
48-
protocol Sub2: Base { // expected-error {{first type 'Self.Assoc' in conformance requirement does not refer to a generic parameter or associated type}}
48+
protocol Sub2: Base { // expected-error {{type 'Self.SubAssoc' constrained to non-protocol, non-class type 'Self.Assoc'}}
4949
associatedtype SubAssoc where SubAssoc: Assoc
5050
}

0 commit comments

Comments
 (0)