Skip to content

Commit 2bcad91

Browse files
authored
Merge pull request #58956 from hank121314/fix/sr-15940
[Sema] SR-15940: Assert crash in TypeCheckStmt.cpp
2 parents b2e1dab + c05afe2 commit 2bcad91

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

Diff for: lib/Sema/TypeCheckStorage.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -2689,7 +2689,8 @@ static VarDecl *synthesizePropertyWrapperProjectionVar(
26892689
}
26902690

26912691
static void typeCheckSynthesizedWrapperInitializer(VarDecl *wrappedVar,
2692-
Expr *&initializer) {
2692+
Expr *&initializer,
2693+
bool contextualize) {
26932694
auto *dc = wrappedVar->getInnermostDeclContext();
26942695
auto &ctx = wrappedVar->getASTContext();
26952696
auto *initContext = new (ctx) PropertyWrapperInitializer(
@@ -2705,6 +2706,11 @@ static void typeCheckSynthesizedWrapperInitializer(VarDecl *wrappedVar,
27052706

27062707
initializer = result->getAsExpr();
27072708

2709+
// Contextualize the initializer which is a local variable with defaultInit or
2710+
// gets an independent initializer. The rest of initializer contextualizing
2711+
// will be done in visitPatternBindingDecl.
2712+
if (!contextualize)
2713+
return;
27082714
TypeChecker::contextualizeInitializer(initContext, initializer);
27092715
checkPropertyWrapperActorIsolation(wrappedVar, initializer);
27102716
TypeChecker::checkInitializerEffects(initContext, initializer);
@@ -2979,7 +2985,8 @@ PropertyWrapperInitializerInfoRequest::evaluate(Evaluator &evaluator,
29792985
&& !wrapperInfo.defaultInit) {
29802986
auto ty = parentPBD->getPattern(patternNumber)->getType();
29812987
if (auto defaultInit = TypeChecker::buildDefaultInitializer(ty)) {
2982-
typeCheckSynthesizedWrapperInitializer(var, defaultInit);
2988+
typeCheckSynthesizedWrapperInitializer(var, defaultInit,
2989+
/*contextualize=*/false);
29832990
parentPBD->setInit(0, defaultInit);
29842991
parentPBD->setInitializerChecked(0);
29852992
}
@@ -2997,7 +3004,9 @@ PropertyWrapperInitializerInfoRequest::evaluate(Evaluator &evaluator,
29973004
// FIXME: Record this expression somewhere so that DI can perform the
29983005
// initialization itself.
29993006
Expr *defaultInit = nullptr;
3000-
typeCheckSynthesizedWrapperInitializer(var, defaultInit);
3007+
// Only contextualize local wrapped property, the rest of wrapped
3008+
// property will be contextualized in visitPatternBindingDecl.
3009+
typeCheckSynthesizedWrapperInitializer(var, defaultInit, dc->isLocalContext());
30013010
pbd->setInit(0, defaultInit);
30023011
pbd->setInitializerChecked(0);
30033012

@@ -3067,7 +3076,8 @@ PropertyWrapperInitializerInfoRequest::evaluate(Evaluator &evaluator,
30673076
!var->getName().hasDollarPrefix()) {
30683077
wrappedValueInit = PropertyWrapperValuePlaceholderExpr::create(
30693078
ctx, var->getSourceRange(), var->getType(), /*wrappedValue=*/nullptr);
3070-
typeCheckSynthesizedWrapperInitializer(var, wrappedValueInit);
3079+
typeCheckSynthesizedWrapperInitializer(var, wrappedValueInit,
3080+
/*contextualize=*/true);
30713081
}
30723082

30733083
return PropertyWrapperInitializerInfo(wrappedValueInit, projectedValueInit);

Diff for: test/Concurrency/global_actor_inference.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -415,12 +415,13 @@ actor WrapperActorBad2<Wrapped: Sendable> {
415415
struct WrapperWithMainActorDefaultInit {
416416
var wrappedValue: Int { fatalError() }
417417

418-
@MainActor init() {} // expected-note {{calls to initializer 'init()' from outside of its actor context are implicitly asynchronous}}
418+
@MainActor init() {} // expected-note 2 {{calls to initializer 'init()' from outside of its actor context are implicitly asynchronous}}
419419
}
420420

421421
actor ActorWithWrapper {
422422
@WrapperOnActor var synced: Int = 0
423423
// expected-note@-1 3{{property declared here}}
424+
@WrapperWithMainActorDefaultInit var property: Int // expected-error {{call to main actor-isolated initializer 'init()' in a synchronous actor-isolated context}}
424425
func f() {
425426
_ = synced // expected-error{{main actor-isolated property 'synced' can not be referenced on a different actor instance}}
426427
_ = $synced // expected-error{{global actor 'SomeGlobalActor'-isolated property '$synced' can not be referenced on a different actor instance}}

Diff for: test/SILGen/property_wrappers.swift

+28
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,34 @@ struct TestAutoclosureComposition {
957957
@Once @ObservedObject var model = Model()
958958
}
959959

960+
@propertyWrapper
961+
struct SR_15940Foo {
962+
var wrappedValue: Int { 0 }
963+
}
964+
965+
struct SR_15940_C {
966+
func a() {
967+
@SR_15940Foo var b: Int
968+
}
969+
}
970+
971+
// CHECK-LABEL: sil hidden [ossa] @$s17property_wrappers10SR_15940_CV1ayyF : $@convention(method) (SR_15940_C) -> () {
972+
// CHECK: bb0(%0 : $SR_15940_C):
973+
// CHECK-NEXT: debug_value %0 : $SR_15940_C, let, name "self", argno 1, implicit
974+
// CHECK-NEXT: [[BOX:%.*]] = alloc_box ${ var SR_15940Foo }, var, name "_b"
975+
// CHECK-NEXT: [[LIFETIME:%.*]] = begin_borrow [lexical] [[BOX]] : ${ var SR_15940Foo }
976+
// CHECK-NEXT: [[BOXADDR:%.*]] = project_box [[LIFETIME]] : ${ var SR_15940Foo }, 0
977+
// CHECK-NEXT: [[METATYPE:%.*]] = metatype $@thin SR_15940Foo.Type
978+
// CHECK-NEXT: // function_ref SR_15940Foo.init()
979+
// CHECK-NEXT: [[DEFAULTVALUE_FN:%.*]] = function_ref @$s17property_wrappers11SR_15940FooVACycfC : $@convention(method) (@thin SR_15940Foo.Type) -> SR_15940Foo
980+
// CHECK-NEXT: [[DEFAULTRESULT:%.*]] = apply [[DEFAULTVALUE_FN]]([[METATYPE]]) : $@convention(method) (@thin SR_15940Foo.Type) -> SR_15940Foo
981+
// CHECK-NEXT: store [[DEFAULTRESULT]] to [trivial] [[BOXADDR]] : $*SR_15940Foo
982+
// CHECK-NEXT: end_borrow [[LIFETIME]] : ${ var SR_15940Foo }
983+
// CHECK-NEXT: destroy_value [[BOX]] : ${ var SR_15940Foo }
984+
// CHECK-NEXT: [[TUPLE:%.*]] = tuple ()
985+
// CHECK-NEXT: return [[TUPLE]] : $()
986+
// CHECK-NEXT: } // end sil function '$s17property_wrappers10SR_15940_CV1ayyF
987+
960988
// CHECK-LABEL: sil_vtable ClassUsingWrapper {
961989
// CHECK-NEXT: #ClassUsingWrapper.x!getter: (ClassUsingWrapper) -> () -> Int : @$s17property_wrappers17ClassUsingWrapperC1xSivg // ClassUsingWrapper.x.getter
962990
// CHECK-NEXT: #ClassUsingWrapper.x!setter: (ClassUsingWrapper) -> (Int) -> () : @$s17property_wrappers17ClassUsingWrapperC1xSivs // ClassUsingWrapper.x.setter

Diff for: test/Sema/property_wrappers.swift

+34
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,37 @@ struct TestInitSubscript {
4747
@Wrapper(wrappedValue: Color.allCases[0])
4848
var color: Color
4949
}
50+
51+
@propertyWrapper
52+
public class SR_15940Bar<Value> {
53+
private var _value: Value
54+
55+
public var wrappedValue: Value {
56+
get { _value }
57+
set {
58+
_value = newValue
59+
}
60+
}
61+
62+
public init(wrappedValue value: @autoclosure @escaping () -> Value) {
63+
self._value = value()
64+
}
65+
}
66+
67+
// CHECK-LABEL: struct_decl{{.*}}SR_15940_A
68+
struct SR_15940_A {
69+
// CHECK: argument_list implicit labels=wrappedValue:
70+
// CHECK-NEXT: argument label=wrappedValue
71+
// CHECK-NEXT: autoclosure_expr implicit type='() -> Bool?' discriminator=0 captures=(<opaque_value> ) escaping
72+
// CHECK: autoclosure_expr implicit type='() -> Bool?' discriminator=1 escaping
73+
@SR_15940Bar var a: Bool?
74+
}
75+
76+
// CHECK-LABEL: struct_decl{{.*}}SR_15940_B
77+
struct SR_15940_B {
78+
// CHECK: argument_list implicit labels=wrappedValue:
79+
// CHECK-NEXT: argument label=wrappedValue
80+
// CHECK-NEXT: autoclosure_expr implicit type='() -> Bool' location={{.*}}.swift:[[@LINE+2]]:30 range=[{{.+}}] discriminator=0 captures=(<opaque_value> ) escaping
81+
// CHECK: autoclosure_expr implicit type='() -> Bool' location={{.*}}.swift:[[@LINE+1]]:30 range=[{{.+}}] discriminator=1 escaping
82+
@SR_15940Bar var b: Bool = false
83+
}

0 commit comments

Comments
 (0)