Skip to content

[Sema] TypeWrappers: Don't try to manage compiler synthesized properties #60864

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
merged 3 commits into from
Aug 31, 2022
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
4 changes: 2 additions & 2 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ static ConstructorDecl *createImplicitConstructor(NominalTypeDecl *decl,
continue;

if (!var->isAccessedViaTypeWrapper()) {
// $_storage itself.
if (var->getName() == ctx.Id_TypeWrapperProperty)
// Compiler synthesized properties are not included.
if (var->isImplicit())
continue;

// Computed properties are not included.
Expand Down
9 changes: 5 additions & 4 deletions lib/Sema/TypeCheckTypeWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,6 @@ bool IsPropertyAccessedViaTypeWrapper::evaluate(Evaluator &evaluator,
if (!(parent && parent->hasTypeWrapper()))
return false;

// Don't attempt to wrap the `$_storage` property.
if (property->getName() == property->getASTContext().Id_TypeWrapperProperty)
return false;

if (property->isStatic() || property->isLet())
return false;

Expand Down Expand Up @@ -346,6 +342,11 @@ bool IsPropertyAccessedViaTypeWrapper::evaluate(Evaluator &evaluator,
return true;
}

// Don't wrap any compiler synthesized properties except to
// property wrapper backing storage (checked above).
if (property->isImplicit())
return false;

// Check whether this is a computed property.
{
auto declaresAccessor = [&](ArrayRef<AccessorKind> kinds) -> bool {
Expand Down
48 changes: 48 additions & 0 deletions test/Interpreter/type_wrapper_with_actors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -target %target-cpu-apple-macosx10.15 -enable-experimental-feature TypeWrappers -parse-as-library -emit-library -emit-module-path %t/type_wrapper_defs.swiftmodule -module-name type_wrapper_defs %S/Inputs/type_wrapper_defs.swift -o %t/%target-library-name(type_wrapper_defs)
// RUN: %target-build-swift -target %target-cpu-apple-macosx10.15 -ltype_wrapper_defs -module-name main -I %t -L %t %s -o %t/main %target-rpath(%t)
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main %t/%target-library-name(type_wrapper_defs) | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: asserts
// REQUIRES: concurrency

// rdar://76038845
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime

// REQUIRES: OS=macosx

// This requires executable tests to be run on the same machine as the compiler,
// as it links with a dylib that it doesn't arrange to get uploaded to remote executors.
// (rdar://99051588)
// UNSUPPORTED: remote_run || device_run

import type_wrapper_defs

@Wrapper
public actor Actor {
public var name: String
@PropWrapper public var age: Int? = nil

public func setAge(_ newAge: Int) async {
age = newAge
}
}

let a = Actor(name: "Arhtur Dent")
await print(a.name)
// CHECK: in getter
// CHECK-NEXT: Arhtur Dent
await print(a.age)
// CHECK: in getter
// CHECK-NEXT: nil

await a.setAge(30)
// CHECK: in getter
// CHECK-NEXT: in setter => PropWrapper<Optional<Int>>(value: Optional(30))

await print(a.age)
// CHECK: in getter
// CHECK-NEXT: 30
5 changes: 5 additions & 0 deletions test/Interpreter/type_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
// REQUIRES: executable_test
// REQUIRES: asserts

// This requires executable tests to be run on the same machine as the compiler,
// as it links with a dylib that it doesn't arrange to get uploaded to remote executors.
// (rdar://99051588)
// UNSUPPORTED: remote_run || device_run

import type_wrapper_defs

var p: Person<String> = .init(name: "P", projects: ["A", "B"])
Expand Down
20 changes: 19 additions & 1 deletion test/type/type_wrapper.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift -enable-experimental-feature TypeWrappers
// RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-feature TypeWrappers

// REQUIRES: asserts

Expand Down Expand Up @@ -380,3 +380,21 @@ func testDeclarationsWithUnmanagedProperties() {

_ = OnlyLazyLetAndComputed(name: "Arthur Dent") // Ok
}

func testActors() async {
@NoopWrapper
actor Person {
var name: String
// expected-note@-1 {{mutation of this property is only permitted within the actor}}
var age: Int
// expected-note@-1 {{mutation of this property is only permitted within the actor}}
}

let person = Person(name: "Arthur Dent", age: 30)

_ = await person.name
_ = await person.age

person.name = "NoName" // expected-error {{actor-isolated property 'name' can not be mutated from a non-isolated context}}
person.age = 0 // expected-error {{actor-isolated property 'age' can not be mutated from a non-isolated context}}
}