Skip to content

Sema: Fix crash with stored property that has on-demand 'modify' accessor [5.3] #31434

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
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
9 changes: 8 additions & 1 deletion lib/Sema/TypeCheckStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1760,9 +1760,16 @@ synthesizeCoroutineAccessorBody(AccessorDecl *accessor, ASTContext &ctx) {

bool isModify = accessor->getAccessorKind() == AccessorKind::Modify;

// Special-case for a modify coroutine of a simple stored property with
// observers. We can yield a borrowed copy of the underlying storage
// in this case. However, if the accessor was synthesized on-demand,
// we do the more general thing, because on-demand accessors might be
// serialized, which prevents them from being able to directly reference
// didSet/willSet accessors, which are private.
if (isModify &&
(storageReadWriteImpl == ReadWriteImplKind::StoredWithSimpleDidSet ||
storageReadWriteImpl == ReadWriteImplKind::InheritedWithSimpleDidSet)) {
storageReadWriteImpl == ReadWriteImplKind::InheritedWithSimpleDidSet) &&
!accessor->hasForcedStaticDispatch()) {
return synthesizeModifyCoroutineBodyWithSimpleDidSet(accessor, ctx);
}

Expand Down
17 changes: 17 additions & 0 deletions test/SILGen/observers_with_on_demand_modify_accessor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-swift-emit-silgen %s -disable-objc-attr-requires-foundation-module | %FileCheck %s
// REQUIRES: objc_interop

public protocol Proto {
var property: Int { get set }
}

public class ClassWithDynamicObservedProperty : Proto {
@objc public dynamic var property: Int = 0 {
didSet {}
}
}

// CHECK-LABEL: sil shared [serialized] [ossa] @$s40observers_with_on_demand_modify_accessor32ClassWithDynamicObservedPropertyC8propertySivM : $@yield_once @convention(method) (@guaranteed ClassWithDynamicObservedProperty) -> @yields @inout Int {
// CHECK: objc_method %0 : $ClassWithDynamicObservedProperty, #ClassWithDynamicObservedProperty.property!getter.foreign
// CHECK: objc_method %0 : $ClassWithDynamicObservedProperty, #ClassWithDynamicObservedProperty.property!setter.foreign
// CHECK: return