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

Suppress unused_unsafe warnings within the generated code. #1415

Merged

Conversation

anforowicz
Copy link
Contributor

@anforowicz anforowicz commented Dec 16, 2024

Sometimes Rust functions declared inside the #[cxx::bridge] have to be marked as unsafe (e.g. if they need to use an explicit lifetime). When the actual, wrapped function is not unsafe, then the generated code will result in an unused_unsafe warning. For example:

   Compiling cxx-test-suite v0.0.0 (/usr/local/google/home/lukasza/src/github/cxx/tests/ffi)
error: unnecessary `unsafe` block
   --> tests/ffi/lib.rs:276:19
    |
276 |         unsafe fn r_return_str_via_out_param<'a>(shared: &'a Shared, out_param: &mut &'a str);
    |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^ unnecessary `unsafe` block
    |
note: the lint level is defined here
   --> tests/ffi/lib.rs:20:9
    |
20  | #![deny(warnings)]  // Check that expansion of `cxx::bridge` doesn't trigger warnings.
    |         ^^^^^^^^
    = note: `#[deny(unused_unsafe)]` implied by `#[deny(warnings)]`

The warning above comes from the following expansion:

unsafe fn __r_return_str_via_out_param<'a>(...) {
    // The `unsafe` block below is unnecessary if the actual, wrapped
    // function is *not* `unsafe`.
    unsafe { super::r_return_str_via_out_param(...) }
}

This commit avoids the warning by including an explicit #[allow(unused_unsafe)] in the generated code.

@anforowicz
Copy link
Contributor Author

This PR is motivated by the desire to address the TODO at https://skia-review.googlesource.com/c/skia/+/928799/4/experimental/rust_png/ffi/FFI.rs

@anforowicz
Copy link
Contributor Author

Hmmm... when re-reading the commit / PR description, the 2nd part seems out-of-place (since there is no foo in the commit - this expansion comes from a separate, ad-hoc, library crate that I used to understand better what is going on).

Let me do a quick force-push to update the commit descriptions... sorry...

Sometimes Rust functions declared inside the `#[cxx::bridge]` have to
be marked as unsafe (e.g. if they need to use an explicit lifetime).
When the actual, wrapped function is _not_ unsafe, then the generated
code will result in an `unused_unsafe` warning.  For example:

```
   Compiling cxx-test-suite v0.0.0 (/usr/local/google/home/lukasza/src/github/cxx/tests/ffi)
error: unnecessary `unsafe` block
   --> tests/ffi/lib.rs:276:19
    |
276 |         unsafe fn r_return_str_via_out_param<'a>(shared: &'a Shared, out_param: &mut &'a str);
    |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^ unnecessary `unsafe` block
    |
note: the lint level is defined here
   --> tests/ffi/lib.rs:20:9
    |
20  | #![deny(warnings)]  // Check that expansion of `cxx::bridge` doesn't trigger warnings.
    |         ^^^^^^^^
    = note: `#[deny(unused_unsafe)]` implied by `#[deny(warnings)]`
```

The warning above comes from the following expansion:

```
unsafe fn __r_return_str_via_out_param<'a>(...) {
    // The `unsafe` block below is unnecessary if the actual, wrapped
    // function is *not* `unsafe`.
    unsafe { super::r_return_str_via_out_param(...) }
}
```

This commit avoids the warning by including an explicit
`#[allow(unused_unsafe)]` in the generated code.
@anforowicz anforowicz force-pushed the unsafe-warning-from-generated-code branch from f1497ad to 86e8c0c Compare December 16, 2024 23:56
Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thanks!

@dtolnay dtolnay merged commit 043817c into dtolnay:master Dec 17, 2024
16 checks passed
@dtolnay
Copy link
Owner

dtolnay commented Dec 17, 2024

Published in 1.0.135.

@anforowicz anforowicz deleted the unsafe-warning-from-generated-code branch December 18, 2024 02:01
hubot pushed a commit to google/skia that referenced this pull request Dec 18, 2024
This CL updates the `WORKSPACE.bazel` dependency on `cxx` and
`cxxbridge-cmd` crates to version `1.0.135`.

This CL is motivated by the desire to stop suppressing warnings in the
proc-macro-generated code, because in the new vesion that code should
be clean of warnings (after dtolnay/cxx#1415).

The following commands have been used to mirror the crates:

$ go run ./bazel/gcs_mirror/gcs_mirror.go \
    --url https://crates.io/api/v1/crates/cxxbridge-cmd/1.0.135/download \
    --sha256 4717c9c806a9e07fdcb34c84965a414ea40fafe57667187052cf1eb7f5e8a8a9 \
    --add_suffix=.tar.gz
$ go run ./bazel/gcs_mirror/gcs_mirror.go \
    --url https://crates.io/api/v1/crates/cxx/1.0.135/download \
    --sha256 4d44ff199ff93242c3afe480ab588d544dd08d72e92885e152ffebc670f076ad \
    --add_suffix=.tar.gz

Bug: chromium:383521545
Change-Id: I713067e42c87384a76f42773adaf36a834a25735
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/930418
Reviewed-by: Kaylee Lubick <kjlubick@google.com>
Commit-Queue: Łukasz Anforowicz <lukasza@google.com>
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.

2 participants