Skip to content
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

Add attribute for Swift ownership support #314

Merged
merged 2 commits into from
Feb 2, 2025

Conversation

chinedufn
Copy link
Owner

@chinedufn chinedufn commented Feb 2, 2025

Add attribute for Swift ownership support

This commit introduces a new attribute that can be used to enable
experimental support for Swift ownership.

The new __experimental_swift_ownership can be used on an opaque Rust
type.

#[swift_bridge::bridge]
mod foo {
    extern "Rust" {
        #[swift_bridge(__experimental_swift_ownership)]
        type SomeType;
    }
}

This new attribute currently does nothing.
In future commits, when swift-bridge sees that a type has the
experimental ownership attribute it will generate code that takes
advantage of Swift 6's ownership features.

For instance, we will use Swift's ~Copyable protocol to ensure that
when Swift has ownership over an opaque Rust type it cannot copy that
handle, which should make attempts to use-after-free a compile-time
error instead of a runtime error.

This commit introduces a new attribute that can be used to enable
experimental support for Swift ownership.

The new `__experimental_swift_ownership` can be used on an opaque Rust
type.

```rust
#[swift_bridge::bridge]
mod foo {
    extern "Rust" {
        #[swift_bridge(__experimental_swift_ownership)]
        type SomeType;
    }
}
```

This new attribute currently does nothing.
In future commits, when `swift-bridge` sees that a type has the
experimental ownership attribute it will generate code that takes
advantage of Swift 6's ownership features.

For instance, we will use Swift's `~Copyable` protocol to ensure that
when Swift has ownership over an opaque Rust type it cannot copy that
handle, which should make attempts to use-after-free a compile-time
error instead of a runtime error.
@chinedufn chinedufn changed the title experimental swift ownership attribute Add attribute for Swift ownership support Feb 2, 2025
@@ -805,158 +805,6 @@ mod tests {
assert_eq!(parse_errors(tokens).len(), 0,);
}

/// Verify that we can parse the `already_declared` attribute.
Copy link
Owner Author

Choose a reason for hiding this comment

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

tests moved to another file

Comment on lines +262 to +276
/// Verify that we parse a Rust opaque type's experimental Swift ownership attribute.
#[test]
fn parse_experimental_swift_ownership_attribute() {
let tokens = quote! {
mod foo {
extern "Rust" {
#[swift_bridge(__experimental_swift_ownership)]
type SomeType;
}
}
};
let attribs = unwrap_opaque_type_attributes(tokens, "SomeType");

assert_eq!(attribs.experimental_swift_ownership, true);
}
Copy link
Owner Author

@chinedufn chinedufn Feb 2, 2025

Choose a reason for hiding this comment

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

This is the only new test case.
The rest were moved from another file.

@chinedufn chinedufn merged commit a661800 into master Feb 2, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant