Skip to content

Sema: Property wrapper storage wrappers ($foo) inherit 'final' bit from original property [5.2] #30750

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
10 changes: 9 additions & 1 deletion lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,15 @@ IsFinalRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
VD->getOriginalWrappedProperty(PropertyWrapperSynthesizedPropertyKind::Backing))
return true;

if (auto *nominalDecl = VD->getDeclContext()->getSelfClassDecl()) {
// Property wrapper storage wrappers are final if the original property
// is final.
if (auto *original = VD->getOriginalWrappedProperty(
PropertyWrapperSynthesizedPropertyKind::StorageWrapper)) {
if (original->isFinal())
return true;
}

if (VD->getDeclContext()->getSelfClassDecl()) {
// If this variable is a class member, mark it final if the
// class is final, or if it was declared with 'let'.
auto *PBD = VD->getParentPatternBinding();
Expand Down
56 changes: 56 additions & 0 deletions test/SILGen/property_wrappers_final.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-emit-silgen %s | %FileCheck %s

// Test that the storage wrapper for a final property is itself final, and that
// its accessors do not appear in the vtable.

public class MyClass {
public init() { }

@PropertyWrapper()
public static var staticProperty: Bool

@PropertyWrapper()
public final var instanceProperty: Bool

}

@propertyWrapper
public struct PropertyWrapper {
public init() {}

public var projectedValue: PropertyWrapper {
get {
return self
}
set {
self = newValue
}
}

public var wrappedValue: Bool {
return false
}
}

// CHECK-LABEL: sil [ossa] @$s23property_wrappers_final17useStorageWrapperyyAA7MyClassCF : $@convention(thin) (@guaranteed MyClass) -> () {
public func useStorageWrapper(_ c: MyClass) {
// CHECK: function_ref @$s23property_wrappers_final7MyClassC15$staticPropertyAA0G7WrapperVvgZ
_ = MyClass.$staticProperty

// CHECK: function_ref @$s23property_wrappers_final7MyClassC15$staticPropertyAA0G7WrapperVvsZ
MyClass.$staticProperty = PropertyWrapper()

// CHECK: $s23property_wrappers_final7MyClassC17$instancePropertyAA0G7WrapperVvg
_ = c.$instanceProperty

// CHECK: $s23property_wrappers_final7MyClassC17$instancePropertyAA0G7WrapperVvs
c.$instanceProperty = PropertyWrapper()

// CHECK: return
}

// CHECK-LABEL: sil_vtable [serialized] MyClass {
// CHECK-NEXT: #MyClass.init!allocator.1: (MyClass.Type) -> () -> MyClass : @$s23property_wrappers_final7MyClassCACycfC
// CHECK-NEXT: #MyClass.deinit!deallocator.1: @$s23property_wrappers_final7MyClassCfD
// CHECK-NEXT: }
43 changes: 43 additions & 0 deletions test/multifile/Inputs/sr12429.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
public class MyClass {
public init() { }

@PropertyWrapper(key: "key", defaultValue: false)
public static var wrappedProperty: Bool

public var otherProperty: String? {
didSet {
fatalError("Set this other property with value: \(String(describing: otherProperty)), even though we called `myClass.property = `")
}
}

public var property: String? {
didSet {
print("Set expected property: \(String(describing: property))")
}
}
}

@propertyWrapper
public struct PropertyWrapper<Value> {
public let key: String
public let defaultValue: Value

public var projectedValue: PropertyWrapper<Value> {
get {
return self
}
// Having this setter is what causes the mis-compilation
set {
self = newValue
}
}

public var wrappedValue: Value {
return defaultValue
}

public init(key: String, defaultValue: Value) {
self.key = key
self.defaultValue = defaultValue
}
}
10 changes: 10 additions & 0 deletions test/multifile/property-wrappers-sr12429.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %empty-directory(%t)
// RUN: cp %s %t/main.swift
// RUN: %target-build-swift -o %t/main %t/main.swift %S/Inputs/sr12429.swift
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main

// REQUIRES: executable_test

let object = MyClass()
object.property = "value"