Skip to content

Sema: Fix crash with local 'lazy' variables that contain a closure #36325

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
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
35 changes: 1 addition & 34 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}()
}