Skip to content

[5.9][ConstraintSystem] Reverse direction of ShapeOf constraint #65657

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 2 commits into from
May 4, 2023
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: 2 additions & 1 deletion include/swift/Sema/Constraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ enum class ConstraintKind : char {
/// Binds the RHS type to a tuple of the params of a function typed LHS. Note
/// this discards function parameter flags.
BindTupleOfFunctionParams,
/// The first type is a type pack, and the second type is its reduced shape.
/// The first type is a reduced shape of the second type (represented as a
/// pack type).
ShapeOf,
/// Represents explicit generic arguments provided for a reference to
/// a declaration.
Expand Down
6 changes: 3 additions & 3 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4858,10 +4858,10 @@ class ConstraintSystem {
ASTNode element, ContextualTypeInfo context, bool isDiscarded,
TypeMatchOptions flags, ConstraintLocatorBuilder locator);

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

/// Simplify an explicit generic argument constraint by equating the
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3103,7 +3103,8 @@ namespace {
llvm_unreachable("unsupported pack reference ASTNode");
}

CS.addConstraint(ConstraintKind::ShapeOf, packType, shapeTypeVar,
CS.addConstraint(
ConstraintKind::ShapeOf, shapeTypeVar, packType,
CS.getConstraintLocator(expr, ConstraintLocator::PackShape));
}

Expand Down
26 changes: 13 additions & 13 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13234,17 +13234,17 @@ static bool hasUnresolvedPackVars(Type type) {
}

ConstraintSystem::SolutionKind ConstraintSystem::simplifyShapeOfConstraint(
Type type1, Type type2, TypeMatchOptions flags,
Type shapeTy, Type packTy, TypeMatchOptions flags,
ConstraintLocatorBuilder locator) {
// Recursively replace all type variables with fixed bindings if
// possible.
type1 = simplifyType(type1, flags);
packTy = simplifyType(packTy, flags);

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

addUnsolvedConstraint(shapeOf);
Expand All @@ -13255,30 +13255,30 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyShapeOfConstraint(
};

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

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

if (type1->hasPlaceholder()) {
if (packTy->hasPlaceholder()) {
if (!shouldAttemptFixes())
return SolutionKind::Error;

recordTypeVariablesAsHoles(type2);
recordTypeVariablesAsHoles(shapeTy);
return SolutionKind::Solved;
}

auto shape = type1->getReducedShape();
addConstraint(ConstraintKind::Bind, shape, type2, locator);
auto shape = packTy->getReducedShape();
addConstraint(ConstraintKind::Bind, shapeTy, shape, locator);
return SolutionKind::Solved;
}

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

addUnsolvedConstraint(shapeOf);
addUnsolvedConstraint(explictGenericArgs);
return SolutionKind::Solved;
}

Expand Down
6 changes: 3 additions & 3 deletions test/Constraints/pack-expansion-expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ func typeReprPacks<each T: ExpressibleByIntegerLiteral>(_ t: repeat each T) {
}

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

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}}
Expand Down