Skip to content

Commit a0369c5

Browse files
authored
Merge pull request #36325 from slavapestov/simplify-contextualize-closures
Sema: Fix crash with local 'lazy' variables that contain a closure
2 parents 0f7f060 + ddb8ed8 commit a0369c5

File tree

2 files changed

+8
-35
lines changed

2 files changed

+8
-35
lines changed

lib/Sema/TypeCheckStmt.cpp

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,6 @@ using namespace swift;
5858

5959
#define DEBUG_TYPE "TypeCheckStmt"
6060

61-
#ifndef NDEBUG
62-
/// Determine whether the given context is for the backing property of a
63-
/// property wrapper.
64-
static bool isPropertyWrapperBackingInitContext(DeclContext *dc) {
65-
auto initContext = dyn_cast<Initializer>(dc);
66-
if (!initContext) return false;
67-
68-
auto patternInitContext = dyn_cast<PatternBindingInitializer>(initContext);
69-
if (!patternInitContext) return false;
70-
71-
auto binding = patternInitContext->getBinding();
72-
if (!binding) return false;
73-
74-
auto singleVar = binding->getSingleVar();
75-
if (!singleVar) return false;
76-
77-
return singleVar->getOriginalWrappedProperty() != nullptr;
78-
}
79-
#endif
80-
8161
namespace {
8262
class ContextualizeClosures : public ASTWalker {
8363
DeclContext *ParentDC;
@@ -129,20 +109,7 @@ namespace {
129109

130110
// Explicit closures start their own sequence.
131111
if (auto CE = dyn_cast<ClosureExpr>(E)) {
132-
// In the repl, the parent top-level context may have been re-written.
133-
if (CE->getParent() != ParentDC) {
134-
if ((CE->getParent()->getContextKind() !=
135-
ParentDC->getContextKind()) ||
136-
ParentDC->getContextKind() != DeclContextKind::TopLevelCodeDecl) {
137-
// If a closure is nested within an auto closure, we'll need to update
138-
// its parent to the auto closure parent.
139-
assert((ParentDC->getContextKind() ==
140-
DeclContextKind::AbstractClosureExpr ||
141-
isPropertyWrapperBackingInitContext(ParentDC)) &&
142-
"Incorrect parent decl context for closure");
143-
CE->setParent(ParentDC);
144-
}
145-
}
112+
CE->setParent(ParentDC);
146113

147114
// If the closure was type checked within its enclosing context,
148115
// we need to walk into it with a new sequence.

test/SILGen/lazy_locals.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@ func generic<T>(x: T) -> T {
3636
return z
3737
}
3838

39-
// 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 {
39+
// 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 {
40+
41+
func lazyLocalWithNestedClosure() {
42+
lazy var x = {
43+
return 3
44+
}()
45+
}

0 commit comments

Comments
 (0)