Skip to content

Commit dbbda56

Browse files
authored
Merge pull request #65657 from xedin/reverse-shape-of-constraint-5.9
[5.9][ConstraintSystem] Reverse direction of `ShapeOf` constraint
2 parents 26e3d7d + 0dac85d commit dbbda56

File tree

5 files changed

+23
-21
lines changed

5 files changed

+23
-21
lines changed

include/swift/Sema/Constraint.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ enum class ConstraintKind : char {
224224
/// Binds the RHS type to a tuple of the params of a function typed LHS. Note
225225
/// this discards function parameter flags.
226226
BindTupleOfFunctionParams,
227-
/// The first type is a type pack, and the second type is its reduced shape.
227+
/// The first type is a reduced shape of the second type (represented as a
228+
/// pack type).
228229
ShapeOf,
229230
/// Represents explicit generic arguments provided for a reference to
230231
/// a declaration.

include/swift/Sema/ConstraintSystem.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4858,10 +4858,10 @@ class ConstraintSystem {
48584858
ASTNode element, ContextualTypeInfo context, bool isDiscarded,
48594859
TypeMatchOptions flags, ConstraintLocatorBuilder locator);
48604860

4861-
/// Simplify a shape constraint by binding the reduced shape of the
4862-
/// left hand side to the right hand side.
4861+
/// Simplify a shape constraint by binding the left-hand side to the
4862+
/// reduced shape of the right-hand side.
48634863
SolutionKind simplifyShapeOfConstraint(
4864-
Type type1, Type type2, TypeMatchOptions flags,
4864+
Type shapeTy, Type packTy, TypeMatchOptions flags,
48654865
ConstraintLocatorBuilder locator);
48664866

48674867
/// Simplify an explicit generic argument constraint by equating the

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3103,7 +3103,8 @@ namespace {
31033103
llvm_unreachable("unsupported pack reference ASTNode");
31043104
}
31053105

3106-
CS.addConstraint(ConstraintKind::ShapeOf, packType, shapeTypeVar,
3106+
CS.addConstraint(
3107+
ConstraintKind::ShapeOf, shapeTypeVar, packType,
31073108
CS.getConstraintLocator(expr, ConstraintLocator::PackShape));
31083109
}
31093110

lib/Sema/CSSimplify.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13234,17 +13234,17 @@ static bool hasUnresolvedPackVars(Type type) {
1323413234
}
1323513235

1323613236
ConstraintSystem::SolutionKind ConstraintSystem::simplifyShapeOfConstraint(
13237-
Type type1, Type type2, TypeMatchOptions flags,
13237+
Type shapeTy, Type packTy, TypeMatchOptions flags,
1323813238
ConstraintLocatorBuilder locator) {
1323913239
// Recursively replace all type variables with fixed bindings if
1324013240
// possible.
13241-
type1 = simplifyType(type1, flags);
13241+
packTy = simplifyType(packTy, flags);
1324213242

1324313243
auto formUnsolved = [&]() {
1324413244
// If we're supposed to generate constraints, do so.
1324513245
if (flags.contains(TMF_GenerateConstraints)) {
1324613246
auto *shapeOf = Constraint::create(
13247-
*this, ConstraintKind::ShapeOf, type1, type2,
13247+
*this, ConstraintKind::ShapeOf, shapeTy, packTy,
1324813248
getConstraintLocator(locator));
1324913249

1325013250
addUnsolvedConstraint(shapeOf);
@@ -13255,30 +13255,30 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyShapeOfConstraint(
1325513255
};
1325613256

1325713257
// Don't try computing the shape of a type variable.
13258-
if (type1->isTypeVariableOrMember())
13258+
if (packTy->isTypeVariableOrMember())
1325913259
return formUnsolved();
1326013260

1326113261
// We can't compute a reduced shape if the input type still
1326213262
// contains type variables that might bind to pack archetypes
1326313263
// or pack expansions.
1326413264
SmallPtrSet<TypeVariableType *, 2> typeVars;
13265-
type1->getTypeVariables(typeVars);
13265+
packTy->getTypeVariables(typeVars);
1326613266
for (auto *typeVar : typeVars) {
1326713267
if (typeVar->getImpl().canBindToPack() ||
1326813268
typeVar->getImpl().isPackExpansion())
1326913269
return formUnsolved();
1327013270
}
1327113271

13272-
if (type1->hasPlaceholder()) {
13272+
if (packTy->hasPlaceholder()) {
1327313273
if (!shouldAttemptFixes())
1327413274
return SolutionKind::Error;
1327513275

13276-
recordTypeVariablesAsHoles(type2);
13276+
recordTypeVariablesAsHoles(shapeTy);
1327713277
return SolutionKind::Solved;
1327813278
}
1327913279

13280-
auto shape = type1->getReducedShape();
13281-
addConstraint(ConstraintKind::Bind, shape, type2, locator);
13280+
auto shape = packTy->getReducedShape();
13281+
addConstraint(ConstraintKind::Bind, shapeTy, shape, locator);
1328213282
return SolutionKind::Solved;
1328313283
}
1328413284

@@ -13420,11 +13420,11 @@ ConstraintSystem::simplifyExplicitGenericArgumentsConstraint(
1342013420
auto formUnsolved = [&]() {
1342113421
// If we're supposed to generate constraints, do so.
1342213422
if (flags.contains(TMF_GenerateConstraints)) {
13423-
auto *shapeOf = Constraint::create(
13424-
*this, ConstraintKind::ShapeOf, type1, type2,
13425-
getConstraintLocator(locator));
13423+
auto *explictGenericArgs =
13424+
Constraint::create(*this, ConstraintKind::ExplicitGenericArguments,
13425+
type1, type2, getConstraintLocator(locator));
1342613426

13427-
addUnsolvedConstraint(shapeOf);
13427+
addUnsolvedConstraint(explictGenericArgs);
1342813428
return SolutionKind::Solved;
1342913429
}
1343013430

test/Constraints/pack-expansion-expressions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ func typeReprPacks<each T: ExpressibleByIntegerLiteral>(_ t: repeat each T) {
9090
}
9191

9292
func sameShapeDiagnostics<each T, each U>(t: repeat each T, u: repeat each U) {
93-
_ = (repeat (each t, each u)) // expected-error {{pack expansion requires that 'each U' and 'each T' have the same shape}}
94-
_ = (repeat Array<(each T, each U)>()) // expected-error {{pack expansion requires that 'each U' and 'each T' have the same shape}}
95-
_ = (repeat (Array<each T>(), each u)) // expected-error {{pack expansion requires that 'each U' and 'each T' have the same shape}}
93+
_ = (repeat (each t, each u)) // expected-error {{pack expansion requires that 'each T' and 'each U' have the same shape}}
94+
_ = (repeat Array<(each T, each U)>()) // expected-error {{pack expansion requires that 'each T' and 'each U' have the same shape}}
95+
_ = (repeat (Array<each T>(), each u)) // expected-error {{pack expansion requires that 'each T' and 'each U' have the same shape}}
9696
}
9797

9898
func returnPackExpansionType<each T>(_ t: repeat each T) -> repeat each T { // expected-error {{pack expansion 'repeat each T' can only appear in a function parameter list, tuple element, or generic argument list}}

0 commit comments

Comments
 (0)