Skip to content

Detect/prevent type variables from leaking across constraint systems. #5624

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 1 commit into from
Nov 3, 2016
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
3 changes: 3 additions & 0 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6793,6 +6793,9 @@ Expr *TypeChecker::callWitness(Expr *base, DeclContext *dc,

// Find the witness we need to use.
auto type = base->getType();
assert(!type->hasTypeVariable() &&
"Building call to witness with unresolved base type!");

if (auto metaType = type->getAs<AnyMetatypeType>())
type = metaType->getInstanceType();

Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3101,6 +3101,11 @@ bool swift::isExtensionApplied(DeclContext &DC, Type BaseTy,
bool Failed = false;
SmallVector<Type, 3> TypeScratch;

// Don't allow type variables from an existing constraint system to
// leak into the new constraint system created below.
assert(!BaseTy->hasTypeVariable() &&
"Unexpected type variable in base type!");

// Prepare type substitution map.
TypeSubstitutionMap Substitutions = BaseTy->getMemberSubstitutions(ED);
auto resolveType = [&](Type Ty) {
Expand Down Expand Up @@ -3144,6 +3149,9 @@ bool swift::isExtensionApplied(DeclContext &DC, Type BaseTy,
static bool canSatisfy(Type T1, Type T2, DeclContext &DC, ConstraintKind Kind,
bool ReplaceArchetypeWithVariables,
bool AllowFreeVariables) {
assert(!T1->hasTypeVariable() && !T2->hasTypeVariable() &&
"Unexpected type variable in constraint satisfaction testing");

std::unique_ptr<TypeChecker> CreatedTC;
// If the current ast context has no type checker, create one for it.
auto *TC = static_cast<TypeChecker*>(DC.getASTContext().getLazyResolver());
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5445,7 +5445,7 @@ bool TypeChecker::isProtocolExtensionUsable(DeclContext *dc, Type type,

// If the type still has parameters, the constrained extension is considered
// unusable.
if (type->hasTypeParameter())
if (type->hasTypeParameter() || type->hasTypeVariable())
return false;

// Set up a constraint system where we open the generic parameters of the
Expand Down