Skip to content

[CS] Connect arg-to-param constraint to function builder #33005

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 3 commits into from
Jul 21, 2020
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
19 changes: 0 additions & 19 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,6 @@ class BuilderClosureVisitor
DeclContext *dc, Type builderType,
Type bodyResultType)
: cs(cs), dc(dc), ctx(ctx), builderType(builderType) {
assert((cs || !builderType->hasTypeVariable()) &&
"cannot handle builder type with type variables without "
"constraint system");
builder = builderType->getAnyNominal();
applied.builderType = builderType;
applied.bodyResultType = bodyResultType;
Expand Down Expand Up @@ -1635,22 +1632,6 @@ ConstraintSystem::matchFunctionBuilder(
}
}

// If the builder type has a type parameter, substitute in the type
// variables.
if (builderType->hasTypeParameter()) {
// Find the opened type for this callee and substitute in the type
// parametes.
for (const auto &opened : OpenedTypes) {
if (opened.first == calleeLocator) {
OpenedTypeMap replacements(opened.second.begin(),
opened.second.end());
builderType = openType(builderType, replacements);
break;
}
}
assert(!builderType->hasTypeParameter());
}

BuilderClosureVisitor visitor(getASTContext(), this, dc, builderType,
bodyResultType);

Expand Down
81 changes: 67 additions & 14 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7274,8 +7274,17 @@ ConstraintSystem::simplifyOneWayConstraint(
secondSimplified, first, ConstraintKind::BindParam, flags, locator);
}

static Type getFunctionBuilderTypeFor(ConstraintSystem &cs, unsigned paramIdx,
ConstraintLocator *calleeLocator) {
static Type getOpenedFunctionBuilderTypeFor(ConstraintSystem &cs,
ConstraintLocatorBuilder locator) {
auto lastElt = locator.last();
if (!lastElt)
return Type();

auto argToParamElt = lastElt->getAs<LocatorPathElt::ApplyArgToParam>();
if (!argToParamElt)
return Type();

auto *calleeLocator = cs.getCalleeLocator(cs.getConstraintLocator(locator));
auto selectedOverload = cs.findSelectedOverloadFor(calleeLocator);
if (!(selectedOverload &&
selectedOverload->choice.getKind() == OverloadChoiceKind::Decl))
Expand All @@ -7290,8 +7299,27 @@ static Type getFunctionBuilderTypeFor(ConstraintSystem &cs, unsigned paramIdx,
if (!choice->hasParameterList())
return Type();

auto *PD = getParameterAt(choice, paramIdx);
return PD->getFunctionBuilderType();
auto *PD = getParameterAt(choice, argToParamElt->getParamIdx());
auto builderType = PD->getFunctionBuilderType();
if (!builderType)
return Type();

// If the builder type has a type parameter, substitute in the type
// variables.
if (builderType->hasTypeParameter()) {
// Find the opened type for this callee and substitute in the type
// parametes.
// FIXME: We should consider changing OpenedTypes to a MapVector.
for (const auto &opened : cs.getOpenedTypes()) {
if (opened.first == calleeLocator) {
OpenedTypeMap replacements(opened.second.begin(), opened.second.end());
builderType = cs.openType(builderType, replacements);
break;
}
}
assert(!builderType->hasTypeParameter());
}
return builderType;
}

bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
Expand All @@ -7310,15 +7338,7 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
auto *inferredClosureType = getClosureType(closure);

// Determine whether a function builder will be applied.
Type functionBuilderType;
ConstraintLocator *calleeLocator = nullptr;
if (auto last = locator.last()) {
if (auto argToParam = last->getAs<LocatorPathElt::ApplyArgToParam>()) {
calleeLocator = getCalleeLocator(getConstraintLocator(locator));
functionBuilderType = getFunctionBuilderTypeFor(
*this, argToParam->getParamIdx(), calleeLocator);
}
}
auto functionBuilderType = getOpenedFunctionBuilderTypeFor(*this, locator);

// Determine whether to introduce one-way constraints between the parameter's
// type as seen in the body of the closure and the external parameter
Expand Down Expand Up @@ -7392,6 +7412,7 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,

// If there is a function builder to apply, do so now.
if (functionBuilderType) {
auto *calleeLocator = getCalleeLocator(getConstraintLocator(locator));
if (auto result = matchFunctionBuilder(
closure, functionBuilderType, closureType->getResult(),
ConstraintKind::Conversion, calleeLocator, locator)) {
Expand Down Expand Up @@ -9884,9 +9905,11 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
case ConstraintKind::BindToPointerType:
case ConstraintKind::Subtype:
case ConstraintKind::Conversion:
return matchTypes(first, second, kind, subflags, locator);

case ConstraintKind::ArgumentConversion:
case ConstraintKind::OperatorArgumentConversion:
return matchTypes(first, second, kind, subflags, locator);
return addArgumentConversionConstraintImpl(kind, first, second, locator);

case ConstraintKind::OpaqueUnderlyingType:
return simplifyOpaqueUnderlyingTypeConstraint(first, second,
Expand Down Expand Up @@ -9956,6 +9979,36 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
llvm_unreachable("Unhandled ConstraintKind in switch.");
}

ConstraintSystem::SolutionKind
ConstraintSystem::addArgumentConversionConstraintImpl(
ConstraintKind kind, Type first, Type second,
ConstraintLocatorBuilder locator) {
assert(kind == ConstraintKind::ArgumentConversion ||
kind == ConstraintKind::OperatorArgumentConversion);

// If we have an unresolved closure argument, form an unsolved argument
// conversion constraint, making sure to reference the type variables for
// a function builder if applicable. This ensures we properly connect the
// closure type variable with any type variables in the function builder, as
// such type variables will be accessible within the body of the closure when
// we open it.
first = getFixedTypeRecursive(first, /*rvalue*/ false);
if (auto *argTypeVar = first->getAs<TypeVariableType>()) {
if (argTypeVar->getImpl().isClosureType()) {
// Extract any type variables present in the parameter's function builder.
SmallVector<TypeVariableType *, 4> typeVars;
if (auto builderTy = getOpenedFunctionBuilderTypeFor(*this, locator))
builderTy->getTypeVariables(typeVars);

auto *loc = getConstraintLocator(locator);
addUnsolvedConstraint(
Constraint::create(*this, kind, first, second, loc, typeVars));
return SolutionKind::Solved;
}
}
return matchTypes(first, second, kind, TMF_GenerateConstraints, locator);
}

void
ConstraintSystem::addKeyPathApplicationRootConstraint(Type root, ConstraintLocatorBuilder locator) {
// If this is a subscript with a KeyPath expression, add a constraint that
Expand Down
7 changes: 7 additions & 0 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4686,6 +4686,13 @@ class ConstraintSystem {
ConstraintLocatorBuilder locator,
bool isFavored);

/// Adds a constraint for the conversion of an argument to a parameter. Do not
/// call directly, use \c addConstraint instead.
SolutionKind
addArgumentConversionConstraintImpl(ConstraintKind kind, Type first,
Type second,
ConstraintLocatorBuilder locator);

/// Collect the current inactive disjunction constraints.
void collectDisjunctions(SmallVectorImpl<Constraint *> &disjunctions);

Expand Down
34 changes: 34 additions & 0 deletions test/Constraints/sr13183.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %target-typecheck-verify-swift

// SR-13183: Make sure we don't incorrectly split the constraint system without
// considering that a function builder type var may connect the inside of a
// closure body with the enclosing expression.

struct New<Value> {
init(value: Value, @ScopeBuilder<Value> scope: () -> Component) { }
}

struct Component {}

struct Map<Value, Transformed> {
let transform: (Value) -> Transformed
}

@_functionBuilder
struct ScopeBuilder<Value> {
static func buildExpression<T>(_ map: Map<Value, T>) -> Component {
Component()
}

static func buildBlock(_ components: Component...) -> Component {
Component()
}
}

let new1 = New(value: 42) {
Map { $0.description }
}

let new2 = New<Int>(value: 42) {
Map { $0.description }
}