Skip to content

Construct AutoClosureExpr With Deeper Parent Contexts #38244

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 1 commit into from
Jul 6, 2021
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
16 changes: 16 additions & 0 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4433,7 +4433,23 @@ class ConstraintSystem {

/// Build implicit autoclosure expression wrapping a given expression.
/// Given expression represents computed result of the closure.
///
/// The \p ClosureDC must be the deepest possible context that
/// contains this autoclosure expression. For example,
///
/// func foo() {
/// _ = { $0 || $1 || $2 }
/// }
///
/// Even though the decl context of $1 (after solution application) is
/// `||`'s autoclosure parameter, we cannot know this until solution
/// application has finished because autoclosure expressions are expanded in
/// depth-first order then \c ContextualizeClosures comes around to clean up.
/// All that is required is that the explicit closure be the context since it
/// is the innermost context that can introduce potential new capturable
/// declarations.
Expr *buildAutoClosureExpr(Expr *expr, FunctionType *closureType,
DeclContext *ClosureDC,
bool isDefaultWrappedValue = false,
bool isAsyncLetWrapper = false);

Expand Down
22 changes: 11 additions & 11 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1092,8 +1092,8 @@ namespace {
FunctionType::ExtInfo closureInfo;
auto *autoClosureType =
FunctionType::get(outerParamTypes, fnType->getResult(), closureInfo);
auto *autoClosure = new (context) AutoClosureExpr(fnCall, autoClosureType,
discriminator, cs.DC);
auto *autoClosure = new (context)
AutoClosureExpr(fnCall, autoClosureType, discriminator, dc);
autoClosure->setParameterList(outerParams);
autoClosure->setThunkKind(AutoClosureExpr::Kind::SingleCurryThunk);
cs.cacheType(autoClosure);
Expand Down Expand Up @@ -1126,9 +1126,8 @@ namespace {

auto resultTy = selfFnTy->getResult();
auto discriminator = AutoClosureExpr::InvalidDiscriminator;
auto closure =
new (context) AutoClosureExpr(/*set body later*/nullptr, resultTy,
discriminator, cs.DC);
auto closure = new (context) AutoClosureExpr(/*set body later*/ nullptr,
resultTy, discriminator, dc);
closure->setParameterList(params);
closure->setType(selfFnTy);
closure->setThunkKind(AutoClosureExpr::Kind::SingleCurryThunk);
Expand Down Expand Up @@ -1693,8 +1692,7 @@ namespace {
memberLocator);

auto outerClosure =
new (context) AutoClosureExpr(closure, selfFnTy,
discriminator, cs.DC);
new (context) AutoClosureExpr(closure, selfFnTy, discriminator, dc);
outerClosure->setThunkKind(AutoClosureExpr::Kind::DoubleCurryThunk);

outerClosure->setParameterList(outerParams);
Expand Down Expand Up @@ -6056,14 +6054,15 @@ Expr *ExprRewriter::coerceCallArguments(
bool isDefaultWrappedValue =
target->propertyWrapperHasInitialWrappedValue();
auto *placeholder = injectWrappedValuePlaceholder(
cs.buildAutoClosureExpr(arg, closureType, isDefaultWrappedValue),
cs.buildAutoClosureExpr(arg, closureType, dc,
isDefaultWrappedValue),
/*isAutoClosure=*/true);
arg = CallExpr::createImplicit(ctx, placeholder, {}, {});
arg->setType(closureType->getResult());
cs.cacheType(arg);
}

convertedArg = cs.buildAutoClosureExpr(arg, closureType);
convertedArg = cs.buildAutoClosureExpr(arg, closureType, dc);
} else {
convertedArg = coerceToType(
arg, paramType,
Expand Down Expand Up @@ -8324,7 +8323,7 @@ static Expr *wrapAsyncLetInitializer(
auto closureType = FunctionType::get({ }, initializerType, extInfo);
ASTContext &ctx = dc->getASTContext();
Expr *autoclosureExpr = cs.buildAutoClosureExpr(
initializer, closureType, /*isDefaultWrappedValue=*/false,
initializer, closureType, dc, /*isDefaultWrappedValue=*/false,
/*isAsyncLetWrapper=*/true);

// Call the autoclosure so that the AST types line up. SILGen will ignore the
Expand Down Expand Up @@ -8792,7 +8791,8 @@ ExprWalker::rewriteTarget(SolutionApplicationTarget target) {
// conversion.
if (FunctionType *autoclosureParamType =
target.getAsAutoclosureParamType()) {
resultExpr = cs.buildAutoClosureExpr(resultExpr, autoclosureParamType);
resultExpr = cs.buildAutoClosureExpr(resultExpr, autoclosureParamType,
target.getDeclContext());
}

solution.setExprTypes(resultExpr);
Expand Down
6 changes: 4 additions & 2 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5098,6 +5098,7 @@ ConstraintSystem::isConversionEphemeral(ConversionRestrictionKind conversion,

Expr *ConstraintSystem::buildAutoClosureExpr(Expr *expr,
FunctionType *closureType,
DeclContext *ClosureContext,
bool isDefaultWrappedValue,
bool isAsyncLetWrapper) {
auto &Context = DC->getASTContext();
Expand All @@ -5116,8 +5117,9 @@ Expr *ConstraintSystem::buildAutoClosureExpr(Expr *expr,
newClosureType = closureType->withExtInfo(info.withNoEscape(false))
->castTo<FunctionType>();

auto *closure = new (Context) AutoClosureExpr(
expr, newClosureType, AutoClosureExpr::InvalidDiscriminator, DC);
auto *closure = new (Context)
AutoClosureExpr(expr, newClosureType,
AutoClosureExpr::InvalidDiscriminator, ClosureContext);

closure->setParameterList(ParameterList::createEmpty(Context));

Expand Down
29 changes: 29 additions & 0 deletions test/expr/capture/local_lazy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %target-swift-frontend -emit-silgen -verify %s > /dev/null
// RUN: %target-swift-frontend -dump-ast %s | %FileCheck %s

struct S {
func foo() -> Int {
// Make sure the decl context for the autoclosure passed to ?? is deep
// enough that it can 'see' the capture of $0 from the outer closure.
// CHECK-LABEL: (lazy_initializer_expr
// CHECK: (closure_expr
// CHECK: location={{.*}}local_lazy.swift:[[@LINE+3]]
// CHECK: (autoclosure_expr implicit
// CHECK: captures=($0<direct>
lazy var nest: (Int) -> Int = { Optional<Int>.none ?? $0 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you FileCheck the SIL function definitions to ensure they have capture arguments too, just in case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followed the other tests and checked the AST dump. The SILGen test is now being used to make sure we don't crash.

return nest(1)
}
}

extension S {
func bar() -> Int {
// CHECK-LABEL: (lazy_initializer_expr
// CHECK: (closure_expr
// CHECK: location={{.*}}local_lazy.swift:[[@LINE+3]]
// CHECK: (autoclosure_expr implicit
// CHECK: captures=($0<direct>
lazy var nest: (Int) -> Int = { Optional<Int>.none ?? $0 }
return nest(1)
}
}