Skip to content

[Generics] Canonicalization in GenericSignatureBuilder and SubstitutionMap #16806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6988,17 +6988,24 @@ void GenericSignatureBuilder::checkSameTypeConstraints(
void GenericSignatureBuilder::checkConcreteTypeConstraints(
TypeArrayView<GenericTypeParamType> genericParams,
EquivalenceClass *equivClass) {
// Resolve any thus-far-unresolved dependent types.
Type resolvedConcreteType =
resolveDependentMemberTypes(*this, equivClass->concreteType);

checkConstraintList<Type>(
genericParams, equivClass->concreteTypeConstraints,
[&](const ConcreteConstraint &constraint) {
return constraint.value->isEqual(equivClass->concreteType);
if (constraint.value->isEqual(resolvedConcreteType))
return true;

auto resolvedType =
resolveDependentMemberTypes(*this, constraint.value);
return resolvedType->isEqual(resolvedConcreteType);
},
[&](const Constraint<Type> &constraint) {
Type concreteType = constraint.value;

// If the concrete type is equivalent, the constraint is redundant.
// FIXME: Should check this constraint after substituting in the
// archetype anchors for each dependent type.
if (concreteType->isEqual(equivClass->concreteType))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this comparing to the resolved one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EEEP! Nice catch; I'll fix.

return ConstraintRelation::Redundant;

Expand All @@ -7013,33 +7020,28 @@ void GenericSignatureBuilder::checkConcreteTypeConstraints(
diag::redundant_same_type_to_concrete,
diag::same_type_redundancy_here);

// Resolve any thus-far-unresolved dependent types.
equivClass->concreteType =
resolveDependentMemberTypes(*this, equivClass->concreteType);
equivClass->concreteType = resolvedConcreteType;
}

void GenericSignatureBuilder::checkSuperclassConstraints(
TypeArrayView<GenericTypeParamType> genericParams,
EquivalenceClass *equivClass) {
assert(equivClass->superclass && "No superclass constraint?");

// FIXME: We should be substituting in the canonical type in context so
// we can resolve superclass requirements, e.g., if you had:
//
// class Foo<T>
// class Bar: Foo<Int>
//
// func foo<T, U where U: Bar, U: Foo<T>>(...) { ... }
//
// then the second `U: Foo<T>` constraint introduces a `T == Int`
// constraint, and we will need to perform that substitution for this final
// check.
// Resolve any this-far-unresolved dependent types.
Type resolvedSuperclass =
resolveDependentMemberTypes(*this, equivClass->superclass);

auto representativeConstraint =
checkConstraintList<Type>(
genericParams, equivClass->superclassConstraints,
[&](const ConcreteConstraint &constraint) {
return constraint.value->isEqual(equivClass->superclass);
if (constraint.value->isEqual(resolvedSuperclass))
return true;

Type resolvedType =
resolveDependentMemberTypes(*this, constraint.value);
return resolvedType->isEqual(equivClass->superclass);
},
[&](const Constraint<Type> &constraint) {
Type superclass = constraint.value;
Expand All @@ -7056,15 +7058,16 @@ void GenericSignatureBuilder::checkSuperclassConstraints(
diag::superclass_redundancy_here);

// Resolve any this-far-unresolved dependent types.
equivClass->superclass =
resolveDependentMemberTypes(*this, equivClass->superclass);
equivClass->superclass = resolvedSuperclass;

// If we have a concrete type, check it.
// FIXME: Substitute into the concrete type.
if (equivClass->concreteType) {
Type resolvedConcreteType =
resolveDependentMemberTypes(*this, equivClass->concreteType);
auto existing = equivClass->findAnyConcreteConstraintAsWritten();
// Make sure the concrete type fulfills the superclass requirement.
if (!equivClass->superclass->isExactSuperclassOf(equivClass->concreteType)){
if (!equivClass->superclass->isExactSuperclassOf(resolvedConcreteType)){
Impl->HadAnyError = true;
if (existing) {
Diags.diagnose(existing->source->getLoc(), diag::type_does_not_inherit,
Expand All @@ -7084,7 +7087,7 @@ void GenericSignatureBuilder::checkSuperclassConstraints(
diag::type_does_not_inherit,
representativeConstraint.getSubjectDependentType(
genericParams),
equivClass->concreteType, equivClass->superclass);
resolvedConcreteType, equivClass->superclass);
}
} else if (representativeConstraint.source->shouldDiagnoseRedundancy(true)
&& existing &&
Expand Down
10 changes: 10 additions & 0 deletions lib/AST/SubstitutionMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ Type SubstitutionMap::lookupSubstitution(CanSubstitutableType type) const {

// Substitute into the replacement type.
replacementType = concreteType.subst(*this);

// If the generic signature is canonical, canonicalize the replacement type.
if (getGenericSignature()->isCanonical())
replacementType = replacementType->getCanonicalType();

return replacementType;
}

Expand All @@ -290,6 +295,11 @@ Type SubstitutionMap::lookupSubstitution(CanSubstitutableType type) const {
replacementType = ErrorType::get(type);

replacementType = lookupSubstitution(cast<SubstitutableType>(canonicalType));

// If the generic signature is canonical, canonicalize the replacement type.
if (getGenericSignature()->isCanonical())
replacementType = replacementType->getCanonicalType();

return replacementType;
}

Expand Down
16 changes: 16 additions & 0 deletions test/Generics/same_type_constraints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,19 @@ typealias NotAnInt = Double
extension X11 where NotAnInt == Int { }
// expected-warning@-1{{neither type in same-type constraint ('NotAnInt' (aka 'Double') or 'Int') refers to a generic parameter or associated type}}
// expected-error@-2{{generic signature requires types 'NotAnInt' (aka 'Double') and 'Int' to be the same}}


struct X12<T> { }

protocol P12 {
associatedtype A
associatedtype B
}

func testP12a<T: P12>(_: T) where T.A == X12<Int>, T.A == X12<T.B>, T.B == Int { }
// expected-warning@-1{{redundant same-type constraint 'T.B' == 'Int'}}
// expected-note@-2{{same-type constraint 'T.B' == 'Int' written here}}

func testP12b<T: P12>(_: T) where T.B == Int, T.A == X12<T.B>, X12<T.B> == T.A { }
// expected-warning@-1{{redundant same-type constraint 'T.A' == 'X12<Int>'}}
// expected-note@-2{{same-type constraint 'T.A' == 'X12<Int>' written here}}