Skip to content
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
30 changes: 21 additions & 9 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1770,7 +1770,7 @@ Type Solution::simplifyType(Type type, bool wantInterfaceType) const {
if (wantInterfaceType)
type = type->mapTypeOutOfContext();

if (!(type->hasTypeVariable() || type->hasPlaceholder()))
if (!type->hasTypeVariableOrPlaceholder())
return type;

// Map type variables to fixed types from bindings.
Expand All @@ -1788,16 +1788,28 @@ Type Solution::simplifyType(Type type, bool wantInterfaceType) const {
});
ASSERT(!(wantInterfaceType && resolvedType->hasPrimaryArchetype()));

// Placeholders shouldn't be reachable through a solution, they are only
// useful to determine what went wrong exactly.
if (resolvedType->hasPlaceholder()) {
return resolvedType.transformRec([&](Type type) -> std::optional<Type> {
if (type->isPlaceholder())
return Type(cs.getASTContext().TheUnresolvedType);
return std::nullopt;
});
// We may have type variables and placeholders left over. These are solver
// allocated so cannot escape this function. Turn them into UnresolvedType.
// - Type variables may still be present from unresolved pack expansions where
// e.g the count type is a hole, so the pattern may never become a
// concrete type.
// - Placeholders may be present for any holes.
if (resolvedType->hasTypeVariableOrPlaceholder()) {
auto &ctx = cs.getASTContext();
resolvedType =
resolvedType.transformRec([&](Type type) -> std::optional<Type> {
if (!type->hasTypeVariableOrPlaceholder())
return type;

auto *typePtr = type.getPointer();
if (isa<TypeVariableType>(typePtr) || isa<PlaceholderType>(typePtr))
return Type(ctx.TheUnresolvedType);

return std::nullopt;
});
}

CONDITIONAL_ASSERT(!resolvedType->getRecursiveProperties().isSolverAllocated());
return resolvedType;
}

Expand Down
5 changes: 5 additions & 0 deletions test/Constraints/pack-expansion-expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -798,3 +798,8 @@ do {
}
}
}

func testInvalidDecomposition() {
func id<T>(_ x: T) -> T {}
let (a, b) = id((repeat each undefined)) // expected-error {{cannot find 'undefined' in scope}}
}
4 changes: 4 additions & 0 deletions validation-test/IDE/crashers_fixed/6cef534b98d6b512.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// {"kind":"complete","signature":"swift::constraints::Solution::simplifyType(swift::Type, bool) const"}
// RUN: %target-swift-ide-test -code-completion --code-completion-token=COMPLETE -code-completion-diagnostics -source-filename %s
for (a(b, d)) [
#^COMPLETE^#, 0, (repeat.c)].enumerated(
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// {"kind":"typecheck","signature":"swift::constraints::Solution::simplifyType(swift::Type, bool) const"}
// RUN: not %target-swift-frontend -typecheck %s
for (a(b, d)) in [(repeat .c)].enumerated(<#expression#>) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// {"signature":"(anonymous namespace)::SetExprTypes::walkToExprPost(swift::Expr*)"}
// RUN: not %target-swift-frontend -typecheck %s
typealias a<b, c> = c
struct d < each b {
typealias e< c > =
(repeat a< each b, c >)
typealias f< each c > = (repeat e< each c >
func 1 {
typealias g = h
(g<
i >
typealias g< each c > = f< repeat each c >