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
6 changes: 3 additions & 3 deletions lib/AST/RequirementMachine/GenericSignatureQueries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,16 +755,16 @@ RequirementMachine::getReducedShapeTerm(Type type) const {
// Get the term T', which is the reduced shape of T.
if (term.size() != 2 ||
term[0].getKind() != Symbol::Kind::GenericParam ||
term[1].getKind() != Symbol::Kind::Shape) {
!term.hasShape()) {
ABORT([&](auto &out) {
out << "Invalid reduced shape\n";
out << "Type: " << type << "\n";
out << "Term: " << term;
});
}

MutableTerm reducedTerm(term.begin(), term.end() - 1);
return reducedTerm;
term.removeShape();
return term;
}

Type RequirementMachine::getReducedShape(Type type,
Expand Down
18 changes: 12 additions & 6 deletions lib/AST/RequirementMachine/InterfaceType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,12 @@ RewriteContext::getRelativeTermForType(CanType typeWitness,
// Get the substitution S corresponding to τ_0_n.
unsigned index = getGenericParamIndex(typeWitness->getRootGenericParam());
result = MutableTerm(substitutions[index]);
ASSERT(result.back().getKind() != Symbol::Kind::Shape);
bool hasShape = false;

if (result.hasShape()) {
hasShape = true;
result.removeShape();
}

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

if (hasShape)
result.add(Symbol::forShape(*this));

return result;
}

Expand All @@ -485,7 +493,7 @@ Type PropertyMap::getTypeFromSubstitutionSchema(
auto substitution = substitutions[index];

bool isShapePosition = (pos == TypePosition::Shape);
bool isShapeTerm = (substitution.back() == Symbol::forShape(Context));
bool isShapeTerm = substitution.hasShape();
if (isShapePosition != isShapeTerm) {
ABORT([&](auto &out) {
out << "Shape vs. type mixup\n\n";
Expand All @@ -504,8 +512,8 @@ Type PropertyMap::getTypeFromSubstitutionSchema(
// Undo the thing where the count type of a PackExpansionType
// becomes a shape term.
if (isShapeTerm) {
MutableTerm mutTerm(substitution.begin(), substitution.end() - 1);
substitution = Term::get(mutTerm, Context);
MutableTerm noShape = substitution.termWithoutShape();
substitution = Term::get(noShape, Context);
}

// Prepend the prefix of the lookup key to the substitution.
Expand Down Expand Up @@ -571,8 +579,6 @@ RewriteContext::getRelativeSubstitutionSchemaFromType(
//
// τ_0_0 := T
// τ_0_1 := U.[shape]
ASSERT(pos != TypePosition::Shape && "Not implemented");

unsigned index = result.size();

result.push_back(Term::get(term, *this));
Expand Down
11 changes: 5 additions & 6 deletions lib/AST/RequirementMachine/RequirementBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,10 @@ void RequirementBuilder::addRequirementRules(ArrayRef<unsigned> rules) {

ASSERT(rule.getLHS().back().getKind() != Symbol::Kind::Protocol);

if (constraintTerm.back().getKind() == Symbol::Kind::Shape) {
ASSERT(rule.getRHS().back().getKind() == Symbol::Kind::Shape);
if (constraintTerm.hasShape()) {
ASSERT(rule.getRHS().hasShape());
// Strip off the shape symbol from the constraint term.
constraintTerm = MutableTerm(constraintTerm.begin(),
constraintTerm.end() - 1);
constraintTerm.removeShape();
}

if (constraintTerm.front().getKind() == Symbol::Kind::PackElement) {
Expand Down Expand Up @@ -332,10 +331,10 @@ void RequirementBuilder::processConnectedComponents() {
for (auto &pair : Components) {
MutableTerm subjectTerm(pair.first);
RequirementKind kind;
if (subjectTerm.back().getKind() == Symbol::Kind::Shape) {
if (subjectTerm.hasShape()) {
kind = RequirementKind::SameShape;
// Strip off the shape symbol from the subject term.
subjectTerm = MutableTerm(subjectTerm.begin(), subjectTerm.end() - 1);
subjectTerm.removeShape();
} else {
kind = RequirementKind::SameType;
if (subjectTerm.front().getKind() == Symbol::Kind::PackElement) {
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/RequirementMachine/RewriteSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ void RewriteSystem::verifyRewriteRules(ValidityPolicy policy) const {

if (rhs.size() == 1 && rhs[0].getKind() == Symbol::Kind::Shape) {
// We can have a rule like T.[shape] => [shape].
ASSERT_RULE(lhs.back().getKind() == Symbol::Kind::Shape);
ASSERT_RULE(lhs.hasShape());
} else {
// Otherwise, LHS and RHS must have the same domain.
auto lhsDomain = lhs.getRootProtocol();
Expand Down
20 changes: 20 additions & 0 deletions lib/AST/RequirementMachine/Term.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ Symbol Term::back() const {
return Ptr->getElements().back();
}

bool Term::hasShape() const {
return back().getKind() == Symbol::Kind::Shape;
}

MutableTerm Term::termWithoutShape() const {
if (hasShape())
return MutableTerm(begin(), end() - 1);
else
return MutableTerm(begin(), end());
}

Symbol Term::operator[](size_t index) const {
return Ptr->getElements()[index];
}
Expand Down Expand Up @@ -224,6 +235,15 @@ std::optional<int> MutableTerm::compare(const MutableTerm &other,
return compareImpl(begin(), end(), other.begin(), other.end(), ctx);
}

bool MutableTerm::hasShape() const {
return back().getKind() == Symbol::Kind::Shape;
}

void MutableTerm::removeShape() {
if (hasShape())
Symbols.pop_back();
}

/// Replace the subterm in the range [from,to) of this term with \p rhs.
void MutableTerm::rewriteSubTerm(Symbol *from, Symbol *to, Term rhs) {
auto oldSize = size();
Expand Down
6 changes: 6 additions & 0 deletions lib/AST/RequirementMachine/Term.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class Term final {

Symbol back() const;

bool hasShape() const;
MutableTerm termWithoutShape() const;

Symbol operator[](size_t index) const;

/// Returns an opaque pointer that uniquely identifies this term.
Expand Down Expand Up @@ -184,6 +187,9 @@ class MutableTerm final {
return Symbols.back();
}

bool hasShape() const;
void removeShape();

Symbol operator[](size_t index) const {
return Symbols[index];
}
Expand Down
12 changes: 11 additions & 1 deletion test/Generics/infinite_concrete_type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ protocol TooManyDifferences {
associatedtype A2
associatedtype B
associatedtype C
}
}

struct G2<T> {
func f2<each A>()
// expected-error@-1 {{cannot build rewrite system for generic signature; concrete type nesting limit exceeded}}
// expected-note@-2 {{failed rewrite rule is }}
// expected-error@-3 {{generic parameter 'A' is not used in function signature}}
where (repeat each A, T) == T {}
// expected-error@-1 {{tuple with noncopyable element type 'repeat each A' is not supported}}

}
6 changes: 6 additions & 0 deletions test/Generics/parameter-pack-requirements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,9 @@ func sameTypeMatch1<T: PP, each U: PP, each V: PP>(t: T, u: repeat each U, v: re
// 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 ())>
func sameTypeMatch2<T: PP, each U: PP, each V: PP>(t: T, u: repeat each U, v: repeat each V)
where T.A == Shape<repeat each U>, T.A == Shape<repeat each V> {}

// CHECK-LABEL: PackTupleNesting
// CHECK-NEXT: <T, U, V, each W where T == ((repeat each W), (repeat each W)), U == (repeat each W), V == (repeat each W)>
struct PackTupleNesting<T, U, V, each W>
where T == ((repeat each W), U),
T == (V, (repeat each W)) {} // expected-warning 3{{same-type requirement makes generic parameter}}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// RUN: not %target-swift-frontend -typecheck %s
extension Result {
func a<each b>() where Success == (Result) -> (repeat each b)> {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: not %target-swift-frontend -typecheck %s
// https://github.com/swiftlang/swift/issues/84490
struct a < b > {
func
c < each d where (repeat each d , b) == b>()
}