Skip to content

Commit

Permalink
[GSB] Diagnose all same-type-to-concrete conflicts consistently.
Browse files Browse the repository at this point in the history
  • Loading branch information
DougGregor committed Jun 24, 2017
1 parent bf730ff commit b51529f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Expand Up @@ -1690,6 +1690,9 @@ NOTE(redundant_conformance_here,none,
"inferred from type here}0",
(unsigned, Type, ProtocolDecl *))

ERROR(same_type_conflict,none,
"%select{generic parameter |protocol |}0%1 cannot be equal to both "
"%2 and %3", (unsigned, Type, Type, Type))
WARNING(redundant_same_type_to_concrete,none,
"redundant same-type constraint %0 == %1", (Type, Type))
NOTE(same_type_redundancy_here,none,
Expand Down
23 changes: 10 additions & 13 deletions lib/AST/GenericSignatureBuilder.cpp
Expand Up @@ -1728,6 +1728,7 @@ namespace {
PotentialArchetype *pa;

void operator()(Type type1, Type type2) const {
// FIXME: Shouldn't need this!
if (pa->getParent() && pa->getConcreteTypeDecl() &&
source->getLoc().isInvalid()) {
diags.diagnose(pa->getConcreteTypeDecl()->getLoc(),
Expand All @@ -1736,14 +1737,6 @@ namespace {
type1, type2);
return;
}

if (source->getLoc().isValid()) {
diags.diagnose(source->getLoc(),
diag::requires_same_type_conflict,
pa->isGenericParam(),
pa->getDependentType(/*FIXME: */{ }, true),
type1, type2);
}
}
};
} // end anonymous namespace
Expand Down Expand Up @@ -5010,8 +5003,8 @@ void GenericSignatureBuilder::checkConcreteTypeConstraints(

checkConstraintList<Type>(
genericParams, equivClass->concreteTypeConstraints,
[](const ConcreteConstraint &constraint) {
return true;
[&](const ConcreteConstraint &constraint) {
return constraint.value->isEqual(equivClass->concreteType);
},
[&](Type concreteType) {
// If the concrete type is equivalent, the constraint is redundant.
Expand All @@ -5020,10 +5013,14 @@ void GenericSignatureBuilder::checkConcreteTypeConstraints(
if (concreteType->isEqual(equivClass->concreteType))
return ConstraintRelation::Redundant;

// Call this unrelated.
return ConstraintRelation::Unrelated;
// If either has a type parameter, call them unrelated.
if (concreteType->hasTypeParameter() ||
equivClass->concreteType->hasTypeParameter())
return ConstraintRelation::Unrelated;

return ConstraintRelation::Conflicting;
},
None,
diag::same_type_conflict,
diag::redundant_same_type_to_concrete,
diag::same_type_redundancy_here);

Expand Down
41 changes: 32 additions & 9 deletions test/Constraints/same_types.swift
Expand Up @@ -58,14 +58,16 @@ func test3<T: Fooable, U: Fooable>(_ t: T, u: U) -> (X, X)
func fail1<
T: Fooable, U: Fooable
>(_ t: T, u: U) -> (X, Y)
where T.Foo == X, U.Foo == Y, T.Foo == U.Foo { // expected-error{{associated type 'T.Foo' cannot be equal to both 'X' and 'Y'}}
where T.Foo == X, U.Foo == Y, T.Foo == U.Foo { // expected-error{{'U.Foo' cannot be equal to both 'Y' and 'X'}}
// expected-note@-1{{same-type constraint 'T.Foo' == 'X' written here}}
return (t.foo, u.foo) // expected-error{{cannot convert return expression of type 'X' to return type 'Y'}}
}

func fail2<
T: Fooable, U: Fooable
>(_ t: T, u: U) -> (X, Y)
where T.Foo == U.Foo, T.Foo == X, U.Foo == Y { // expected-error{{associated type 'U.Foo' cannot be equal to both 'X' and 'Y'}}
where T.Foo == U.Foo, T.Foo == X, U.Foo == Y { // expected-error{{'U.Foo' cannot be equal to both 'Y' and 'X'}}
// expected-note@-1{{same-type constraint 'T.Foo' == 'X' written here}}
return (t.foo, u.foo) // expected-error{{cannot convert return expression of type 'X' to return type 'Y'}}
}

Expand Down Expand Up @@ -94,20 +96,22 @@ func test7<T: Barrable>(_ t: T) -> (Y, X) where T.Bar == Y, T.Bar.Foo == X {

func fail4<T: Barrable>(_ t: T) -> (Y, Z)
where
T.Bar == Y,
T.Bar.Foo == Z { // expected-error{{associated type 'T.Bar.Foo' cannot be equal to both 'Y.Foo' (aka 'X') and 'Z'}}
T.Bar == Y, // expected-note{{same-type constraint 'T.Bar.Foo' == 'Y.Foo' (aka 'X') implied here}}
T.Bar.Foo == Z { // expected-error{{'T.Bar.Foo' cannot be equal to both 'Z' and 'Y.Foo' (aka 'X')}}
return (t.bar, t.bar.foo) // expected-error{{cannot convert return expression of type 'X' to return type 'Z'}}
}

func fail5<T: Barrable>(_ t: T) -> (Y, Z)
where
T.Bar.Foo == Z, // expected-warning{{redundant same-type constraint 'T.Bar.Foo' == 'Z'}}
T.Bar == Y { // expected-error{{associated type 'T.Bar.Foo' cannot be equal to both 'Z' and 'X'}}
// expected-note@-1{{same-type constraint 'T.Bar.Foo' == 'Y.Foo' (aka 'X') implied here}}
T.Bar.Foo == Z, // expected-note{{same-type constraint 'T.Bar.Foo' == 'Z' written here}}
T.Bar == Y { // expected-error{{'T.Bar.Foo' cannot be equal to both 'Y.Foo' (aka 'X') and 'Z'}}
return (t.bar, t.bar.foo) // expected-error{{cannot convert return expression of type 'X' to return type 'Z'}}
}

func test8<T: Fooable>(_ t: T) where T.Foo == X, T.Foo == Y {} // expected-error{{associated type 'T.Foo' cannot be equal to both 'X' and 'Y'}}
func test8<T: Fooable>(_ t: T)
where T.Foo == X, // expected-note{{same-type constraint 'T.Foo' == 'X' written here}}
T.Foo == Y {} // expected-error{{'T.Foo' cannot be equal to both 'Y' and 'X'}}


func testAssocTypeEquivalence<T: Fooable>(_ fooable: T) -> X.Type
where T.Foo == X {
Expand Down Expand Up @@ -246,7 +250,6 @@ func structuralSameTypeRecursive1<T: P2, U>(_: T, _: U)
where T.Assoc1 == Tuple2<T.Assoc1, U> // expected-error{{same-type constraint 'T.Assoc1' == '(T.Assoc1, U)' is recursive}}
{ }


protocol P3 {
}

Expand All @@ -256,5 +259,25 @@ protocol P4 {

func test9<T>(_: T) where T.A == X, T: P4, T.A: P3 { } // expected-error{{same-type constraint type 'X' does not conform to required protocol 'P3'}}

// Same-type constraint conflict through protocol where clauses.
protocol P5 where Foo1 == Foo2 {
associatedtype Foo1
associatedtype Foo2
}

protocol P6 {
associatedtype Bar: P5
}

struct X5a {}

struct X5b { }

func test9<T: P6, U: P6>(_ t: T, u: U)
where T.Bar.Foo1 == X5a, // expected-note{{same-type constraint 'T.Bar.Foo1' == 'X5a' written here}}
U.Bar.Foo2 == X5b, // expected-error{{'U.Bar.Foo2' cannot be equal to both 'X5b' and 'X5a'}}
T.Bar == U.Bar {
}

// FIXME: Remove -verify-ignore-unknown.
// <unknown>:0: error: unexpected error produced: generic parameter τ_0_0.Bar.Foo cannot be equal to both 'Y.Foo' (aka 'X') and 'Z'

0 comments on commit b51529f

Please sign in to comment.