Skip to content

Commit d68a7a7

Browse files
committed
Requirement Machine: Fix an issue with pack expansion + tuple type crashes
Support pack expansion types in term rewriting, maintaining shape invariants and not throwing assertions unnecessarily. Additional tests added for an inifinite case and a concrete case.
1 parent fcb7d76 commit d68a7a7

File tree

7 files changed

+36
-16
lines changed

7 files changed

+36
-16
lines changed

lib/AST/RequirementMachine/InterfaceType.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,12 @@ RewriteContext::getRelativeTermForType(CanType typeWitness,
424424
// Get the substitution S corresponding to τ_0_n.
425425
unsigned index = getGenericParamIndex(typeWitness->getRootGenericParam());
426426
result = MutableTerm(substitutions[index]);
427-
ASSERT(!result.hasShape());
427+
bool hasShape = false;
428+
429+
if (result.hasShape()) {
430+
hasShape = true;
431+
result.removeShape();
432+
}
428433

429434
// If the substitution is a term consisting of a single protocol symbol
430435
// [P], save P for later.
@@ -463,6 +468,9 @@ RewriteContext::getRelativeTermForType(CanType typeWitness,
463468
for (auto iter = symbols.rbegin(), end = symbols.rend(); iter != end; ++iter)
464469
result.add(*iter);
465470

471+
if (hasShape)
472+
result.add(Symbol::forShape(*this));
473+
466474
return result;
467475
}
468476

@@ -571,8 +579,6 @@ RewriteContext::getRelativeSubstitutionSchemaFromType(
571579
//
572580
// τ_0_0 := T
573581
// τ_0_1 := U.[shape]
574-
ASSERT(pos != TypePosition::Shape && "Not implemented");
575-
576582
unsigned index = result.size();
577583

578584
result.push_back(Term::get(term, *this));

test/Generics/infinite_concrete_type.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,14 @@ protocol TooManyDifferences {
5555
associatedtype A2
5656
associatedtype B
5757
associatedtype C
58-
}
58+
}
59+
60+
struct G2<T> {
61+
func f2<each A>()
62+
// expected-error@-1 {{cannot build rewrite system for generic signature; concrete type nesting limit exceeded}}
63+
// expected-note@-2 {{failed rewrite rule is }}
64+
// expected-error@-3 {{generic parameter 'A' is not used in function signature}}
65+
where (repeat each A, T) == T {}
66+
// expected-error@-1 {{tuple with noncopyable element type 'repeat each A' is not supported}}
67+
68+
}

test/Generics/parameter-pack-requirements.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,9 @@ func sameTypeMatch1<T: PP, each U: PP, each V: PP>(t: T, u: repeat each U, v: re
126126
// CHECK-NEXT: <T, each U, each V where T : PP, repeat each U : PP, (repeat (each U, each V)) : Any, repeat each V : PP, T.[PP]A == (/* shape: each U */ repeat ())>
127127
func sameTypeMatch2<T: PP, each U: PP, each V: PP>(t: T, u: repeat each U, v: repeat each V)
128128
where T.A == Shape<repeat each U>, T.A == Shape<repeat each V> {}
129+
130+
// CHECK-LABEL: PackTupleNesting
131+
// CHECK-NEXT: <T, U, V, each W where T == ((repeat each W), (repeat each W)), U == (repeat each W), V == (repeat each W)>
132+
struct PackTupleNesting<T, U, V, each W>
133+
where T == ((repeat each W), U),
134+
T == (V, (repeat each W)) {} // expected-warning 3{{same-type requirement makes generic parameter}}

validation-test/compiler_crashers/RewriteContext-getRelativeTermForType-5e95d5.swift

Lines changed: 0 additions & 5 deletions
This file was deleted.

validation-test/compiler_crashers/RewriteContext-getRelativeTermForType-6235b4.swift

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: not %target-swift-frontend -typecheck %s
2+
extension Result {
3+
func a<each b>() where Success == (Result) -> (repeat each b)> {}
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: not %target-swift-frontend -typecheck %s
2+
// https://github.com/swiftlang/swift/issues/84490
3+
struct a < b > {
4+
func
5+
c < each d where (repeat each d , b) == b>()
6+
}

0 commit comments

Comments
 (0)