Skip to content

Misleading error message about "call to unsafe function" #44674

Closed
@ruuda

Description

@ruuda

The following code generates a misleading error message on Rust 1.20.0:

fn main() {
    use std::mem;
    unsafe {
        const MAGIC: i32 = mem::transmute([1u8, 2, 3, 4]);
        println!("{}", MAGIC);
    }
}

Observed output:

error[E0133]: call to unsafe function requires unsafe function or block
 --> src/main.rs:4:28
  |
4 |         const MAGIC: i32 = mem::transmute([1u8, 2, 3, 4]);
  |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function

It is misleading, because the call is in fact inside an unsafe block. Expected output:

error[E0015]: calls in constants are limited to struct and enum constructors

On the current nightly the problem is even worse because the compiler also warns about an unused unsafe block, while at the same time telling that an unsafe block is required.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions