Skip to content

Commit

Permalink
Merge pull request #36325 from slavapestov/simplify-contextualize-clo…
Browse files Browse the repository at this point in the history
…sures

Sema: Fix crash with local 'lazy' variables that contain a closure
  • Loading branch information
slavapestov committed Mar 6, 2021
2 parents 0f7f060 + ddb8ed8 commit a0369c5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 35 deletions.
35 changes: 1 addition & 34 deletions lib/Sema/TypeCheckStmt.cpp
Expand Up @@ -58,26 +58,6 @@ using namespace swift;

#define DEBUG_TYPE "TypeCheckStmt"

#ifndef NDEBUG
/// Determine whether the given context is for the backing property of a
/// property wrapper.
static bool isPropertyWrapperBackingInitContext(DeclContext *dc) {
auto initContext = dyn_cast<Initializer>(dc);
if (!initContext) return false;

auto patternInitContext = dyn_cast<PatternBindingInitializer>(initContext);
if (!patternInitContext) return false;

auto binding = patternInitContext->getBinding();
if (!binding) return false;

auto singleVar = binding->getSingleVar();
if (!singleVar) return false;

return singleVar->getOriginalWrappedProperty() != nullptr;
}
#endif

namespace {
class ContextualizeClosures : public ASTWalker {
DeclContext *ParentDC;
Expand Down Expand Up @@ -129,20 +109,7 @@ namespace {

// Explicit closures start their own sequence.
if (auto CE = dyn_cast<ClosureExpr>(E)) {
// In the repl, the parent top-level context may have been re-written.
if (CE->getParent() != ParentDC) {
if ((CE->getParent()->getContextKind() !=
ParentDC->getContextKind()) ||
ParentDC->getContextKind() != DeclContextKind::TopLevelCodeDecl) {
// If a closure is nested within an auto closure, we'll need to update
// its parent to the auto closure parent.
assert((ParentDC->getContextKind() ==
DeclContextKind::AbstractClosureExpr ||
isPropertyWrapperBackingInitContext(ParentDC)) &&
"Incorrect parent decl context for closure");
CE->setParent(ParentDC);
}
}
CE->setParent(ParentDC);

// If the closure was type checked within its enclosing context,
// we need to walk into it with a new sequence.
Expand Down
8 changes: 7 additions & 1 deletion test/SILGen/lazy_locals.swift
Expand Up @@ -36,4 +36,10 @@ func generic<T>(x: T) -> T {
return z
}

// CHECK-LABEL: sil private [lazy_getter] [noinline] [ossa] @$s11lazy_locals7generic1xxx_tlF1zL_xvg : $@convention(thin) <T> (@guaranteed <τ_0_0> { var Optional<τ_0_0> } <T>, @in_guaranteed T) -> @out T {
// CHECK-LABEL: sil private [lazy_getter] [noinline] [ossa] @$s11lazy_locals7generic1xxx_tlF1zL_xvg : $@convention(thin) <T> (@guaranteed <τ_0_0> { var Optional<τ_0_0> } <T>, @in_guaranteed T) -> @out T {

func lazyLocalWithNestedClosure() {
lazy var x = {
return 3
}()
}

0 comments on commit a0369c5

Please sign in to comment.