Skip to content

SILGen: Fix setting variables with property wrappers marked objc dynamic #27516

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/SILGen/SILGenLValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,14 @@ namespace {

// Create the allocating setter function. It captures the base address.
auto setterInfo = SGF.getConstantInfo(setter);
SILValue setterFRef = SGF.emitGlobalFunctionRef(loc, setter, setterInfo);
SILValue setterFRef;
if (setter.hasDecl() && setter.getDecl()->isObjCDynamic()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the setter be a class method or a witness method also

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, this code is for property wrapped vars.

This:

class SomeWrapperTests {
@SomeWrapper var someWrapper: Int = 0

will just emit a function ref.

I am guessing we treat this like stored properties.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Property-wrapped vars should definitely be overridable in subclasses.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, then the code before this patch is broken:

 @propertyWrapper
 public struct SomeWrapper {
   private var value: Int

 
    public init(wrappedValue: Int) {
     value = wrappedValue
   }

 
    public var wrappedValue: Int {
     get { value }
     set { value = newValue }
   }
 }

SILGen will emit a direct function_ref and not a class_method call for the setter in the example below:

  class SomeWrapperTests {
   @SomeWrapper var someWrapper: Int = 0
   func testAssignment() {
     someWrapper = 1000
   }
 }
sil hidden [ossa] @$s7Wrapper04SomeA5TestsC14testAssignmentyyF : $@convention(method) (@guaranteed SomeWrapperTests) -> () {
// %0                                             // users: %12, %6, %1
bb0(%0 : @guaranteed $SomeWrapperTests):
  ...
  %9 = function_ref @$s7Wrapper04SomeA0V12wrappedValueACSi_tcfCTc : $@convention(thin) (@thin SomeWrapper.Type) -> @owned @callee_guaranteed (Int) -> SomeWrapper
  %10 = apply %9(%8) : $@convention(thin) (@thin SomeWrapper.Type) -> @owned @callee_guaranteed (Int) -> SomeWrapper
  // function_ref SomeWrapperTests.someWrapper.setter
  %11 = function_ref @$s7Wrapper04SomeA5TestsC04someA0Sivs : $@convention(method) (Int, @guaranteed SomeWrapperTests) -> ()
  %12 = copy_value %0 : $SomeWrapperTests
  %13 = partial_apply [callee_guaranteed] %11(%12) : $@convention(method) (Int, @guaranteed SomeWrapperTests) -> ()
  assign_by_wrapper %5 : $Int to %7 : $*SomeWrapper, init %10 : $@callee_guaranteed (Int) -> SomeWrapper, set %13 : $@callee_guaranteed (Int) -> ()
  end_access %7 : $*SomeWrapper 
 

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes! We should fix that too, then. :-) I guess I'll file a separate Radar.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed SR-11571, soon to be cloned.

auto methodTy = SILType::getPrimitiveObjectType(
SGF.SGM.Types.getConstantFunctionType(setter));
setterFRef = SGF.B.createObjCMethod(
loc, base.getValue(), setter, methodTy);
} else
setterFRef = SGF.emitGlobalFunctionRef(loc, setter, setterInfo);
CanSILFunctionType setterTy = setterFRef->getType().castTo<SILFunctionType>();
SILFunctionConventions setterConv(setterTy, SGF.SGM.M);

Expand Down
24 changes: 24 additions & 0 deletions test/IRGen/objc_properties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ class Class17127126 {
static var sharedInstance: AnyObject { get set }
}

@propertyWrapper
public struct SomeWrapper {
private var value: Int


public init(wrappedValue: Int) {
value = wrappedValue
}


public var wrappedValue: Int {
get { value }
set { value = newValue }
}
}

class SomeWrapperTests {
@objc @SomeWrapper dynamic var someWrapper: Int = 0

func testAssignment() {
// This used to crash irgen.
someWrapper = 1000
}
}
// CHECK-NEW: [[SHARED_NAME:@.*]] = private unnamed_addr constant [11 x i8] c"sharedProp\00"
// CHECK-NEW: [[SHARED_ATTRS:@.*]] = private unnamed_addr constant [5 x i8] c"Tq,N\00"

Expand Down
27 changes: 27 additions & 0 deletions test/SILGen/objc_properties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,30 @@ func testPropSetWithPreposition(object: ObjectWithSplitProperty?) {
// CHECK: #ObjectWithSplitProperty.flagForSomething!setter.1.foreign : (ObjectWithSplitProperty) -> (Bool) -> (), $@convention(objc_method) ({{Bool|ObjCBool}}, ObjectWithSplitProperty) -> ()
object?.flagForSomething = false
}

@propertyWrapper
public struct SomeWrapper {
private var value: Int


public init(wrappedValue: Int) {
value = wrappedValue
}


public var wrappedValue: Int {
get { value }
set { value = newValue }
}
}

class SomeWrapperTests {
@objc @SomeWrapper dynamic var someWrapper: Int = 0
// CHECK-LABEL: sil hidden [ossa] @$s15objc_properties16SomeWrapperTestsC14testAssignmentyyF
// CHECK: [[M:%.*]] = objc_method %0 : $SomeWrapperTests, #SomeWrapperTests.someWrapper!setter.1.foreign
// CHECK: [[C:%.*]] = partial_apply [callee_guaranteed] [[M]]({{.*}}) : $@convention(objc_method)
// CHECK: assign_by_wrapper {{%.*}}: $Int to {{%.*}} : $*SomeWrapper, init {{.*}} : $@callee_guaranteed (Int) -> SomeWrapper, set [[C]] : $@callee_guaranteed (Int) -> ()
func testAssignment() {
someWrapper = 1000
}
}