rust-lang / rust-lang/rust

Unsafe Loop when using mem::transmute & Unpreventable Due to a Macro

Open
#134,018 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lints S-needs-info S-needs-repro T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
#[macro_export]
macro_rules! vpr_as_m128i {
    ($n64:expr, $idx:expr) => {
        unsafe {
            std::mem::transmute::<u128, std::arch::x86_64::__m128i>($[$idx as usize])
        }
    }
}
Current output
// With 'Unsafe' Keyword
unnecessary `unsafe` block
`#[warn(unused_unsafe)]` on by default
Error originated from macro call here
because it's nested under this `unsafe` block
unnecessary `unsafe` block
unnecessary `unsafe` block
Error originated from macro call here
because it's nested under this `unsafe` block

// When Removing the 'Unsafe' Keyword
call to unsafe function `std::intrinsics::transmute` is unsafe and requires unsafe function or block
consult the function's documentation for information on how to avoid undefined behavior.
Desired output
No warning when adding unsafe since transmuting memory goes out of the bounds of the borrow checker.
Rationale and extra context

The problem occurs when using a macro, normally the issue could be avoided by using the recommended #[warn(unused_unsafe)] procedural macro. However, when adding the attribute, it specifies issue https://github.com/rust-lang/rust/issues/15701:

attributes on expressions are experimental
see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more informationrustc[Click](rust-analyzer-diagnostics-view:/diagnostic message [1]?1#file:///home/will/Documents/Projects/p64/p64-core/src/rsp.rs)

The proposed fix is either to recognise that mem::transmute requires unsafe due to the borrow checker's absence or allow the #[warn(unused_unsafe)] procedural macro to be allowed to be specified with macros. I would prefer the former over the latter.

Other cases

Rust Version
$ rustc --version --verbose
rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the vpr_as_m128i macro on the reported Rust 1.82.0 version, comparing diagnostics with and without the unsafe block. Read the unused_unsafe behavior around macro expansion and the expression-attribute limitation in issue #15701; done means the mem::transmute case no longer produces contradictory diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.