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

Confusing message when using unsafe to initialize statics #94077

Closed
mejrs opened this issue Feb 17, 2022 · 0 comments · Fixed by #103964
Closed

Confusing message when using unsafe to initialize statics #94077

mejrs opened this issue Feb 17, 2022 · 0 comments · Fixed by #103964
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mejrs
Copy link
Contributor

mejrs commented Feb 17, 2022

Given the following code:

fn foo() {
    unsafe {
        static BAR: u32 = std::mem::transmute(0_i32);
    }
}

The current output is:

Compiling playground v0.0.1 (/playground)
warning: unnecessary `unsafe` block
 [--> src/lib.rs:2:5
](https://play.rust-lang.org/#)  |
2 |     unsafe {
  |     ^^^^^^ unnecessary `unsafe` block
  |
  = note: `#[warn(unused_unsafe)]` on by default

error[[E0133]](https://doc.rust-lang.org/stable/error-index.html#E0133): call to unsafe function is unsafe and requires unsafe function or block
 [--> src/lib.rs:3:27
](https://play.rust-lang.org/#)  |
3 |         static BAR: u32 = std::mem::transmute(0_i32);
  |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
  |
  = note: consult the function's documentation for information on how to avoid undefined behavior

For more information about this error, try `rustc --explain E0133`.
warning: `playground` (lib) generated 1 warning
error: could not compile `playground` due to previous error; 1 warning emitted

Ideally the output should look like:

It could suggest moving the unsafe block:

static BAR: u32 = unsafe { std::mem::transmute(0_i32) };
                  ^^^^^^

Or perhaps this should be valid? It is inside an unsafe block after all.

@mejrs mejrs added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 17, 2022
@estebank estebank added D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Feb 18, 2022
@bors bors closed this as completed in 347c478 Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants