Skip to content

Commit 6492cc3

Browse files
committed
[GenericSigBuilder] Eliminate some redundancy in same-type-to-concrete checking.
1 parent ba81b66 commit 6492cc3

File tree

2 files changed

+17
-36
lines changed

2 files changed

+17
-36
lines changed

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,50 +1911,36 @@ bool GenericSignatureBuilder::addSameTypeRequirementToConcrete(
19111911
PotentialArchetype *T,
19121912
Type Concrete,
19131913
const RequirementSource *Source) {
1914-
auto rep = T->getRepresentative();
1914+
// Record the concrete type and its source.
1915+
if (!T->ConcreteType) {
1916+
// FIXME: Always record this.
1917+
T->ConcreteType = Concrete;
1918+
T->ConcreteTypeSource = Source;
1919+
}
19151920

1916-
// If there is an existing source on this potential archetype, make sure
1917-
// we have the same type.
1918-
// FIXME: Delay until finalize().
1919-
if (auto existingSource = T->ConcreteTypeSource) {
1921+
// If we've already been bound to a type, match that type.
1922+
if (auto oldConcrete = T->getConcreteType()) {
19201923
bool mismatch = addSameTypeRequirement(
1921-
T->ConcreteType, Concrete, Source, [&](Type type1, Type type2) {
1924+
oldConcrete, Concrete, Source, [&](Type type1, Type type2) {
19221925
Diags.diagnose(Source->getLoc(),
19231926
diag::requires_same_type_conflict,
19241927
T->isGenericParam(),
19251928
T->getDependentType(/*FIXME: */{ }, true), type1,
19261929
type2);
1930+
19271931
});
19281932

19291933
if (mismatch) return true;
19301934

1931-
// If this is a better source, record it.
1932-
updateRequirementSource(T->ConcreteTypeSource, Source);
1933-
1935+
// Nothing more to do; the types matched.
19341936
return false;
19351937
}
19361938

1937-
// If we've already been bound to a type, match that type.
1938-
if (T != rep) {
1939-
if (auto oldConcrete = rep->getConcreteType()) {
1940-
bool mismatch = addSameTypeRequirement(
1941-
oldConcrete, Concrete, Source, [&](Type type1, Type type2) {
1942-
Diags.diagnose(Source->getLoc(),
1943-
diag::requires_same_type_conflict,
1944-
T->isGenericParam(),
1945-
T->getDependentType(/*FIXME: */{ }, true), type1,
1946-
type2);
1947-
1948-
});
1949-
1950-
if (mismatch) return true;
1951-
return false;
1952-
}
1953-
}
1954-
1955-
// Record the concrete type and its source.
1956-
T->ConcreteType = Concrete;
1957-
T->ConcreteTypeSource = Source;
1939+
// Record the requirement.
1940+
auto rep = T->getRepresentative();
1941+
auto equivClass = rep->getOrCreateEquivalenceClass();
1942+
if (!equivClass->concreteType)
1943+
equivClass->concreteType = Concrete;
19581944

19591945
// Make sure the concrete type fulfills the requirements on the archetype.
19601946
// FIXME: Move later...
@@ -1988,11 +1974,6 @@ bool GenericSignatureBuilder::addSameTypeRequirementToConcrete(
19881974
updateRequirementSource(conforms.second, concreteSource);
19891975
}
19901976

1991-
// Record the requirement.
1992-
auto equivClass = rep->getOrCreateEquivalenceClass();
1993-
if (!equivClass->concreteType)
1994-
equivClass->concreteType = Concrete;
1995-
19961977
// Make sure the concrete type fulfills the superclass requirement
19971978
// of the archetype.
19981979
if (rep->Superclass) {

test/Constraints/same_types.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func test4<T: Barrable>(_ t: T) -> Y where T.Bar == Y {
7575

7676
func fail3<T: Barrable>(_ t: T) -> X
7777
where T.Bar == X { // expected-error {{'X' does not conform to required protocol 'Fooable'}}
78-
return t.bar // expected-error{{cannot convert return expression of type 'T.Bar' to return type 'X'}}
78+
return t.bar
7979
}
8080

8181
func test5<T: Barrable>(_ t: T) -> X where T.Bar.Foo == X {

0 commit comments

Comments
 (0)