Skip to content

Unsafe shenigans in constants can result in missing errors or ICE #49296

Closed
@oli-obk

Description

@oli-obk
#![feature(const_fn)]

const unsafe fn transmute<T: Copy, U: Copy>(t: T) -> U {
    union Transmute<T: Copy, U: Copy> {
        from: T,
        to: U,
    }

    Transmute { from: t }.to
}

const fn wat(x: u64) -> &'static u64 {
    unsafe { transmute(&x) }
}
const X: u64 = *wat(42);

fn main() {
    println!("{}", X);
}

produces

error[E0080]: constant evaluation error
  --> src/main.rs:18:20
   |
18 |     println!("{}", X);
   |                    ^ referenced constant has errors

without any additional info. If the value is not dereferenced, but instead fed to llvm, we get an ICE in trans:

#![feature(const_fn)]

const unsafe fn transmute<T: Copy, U: Copy>(t: T) -> U {
    union Transmute<T: Copy, U: Copy> {
        from: T,
        to: U,
    }

    Transmute { from: t }.to
}

const fn wat(x: u64) -> u64 {
    unsafe { transmute(&x) }
}
const X: u64 = wat(42);

fn main() {
    println!("{}", X);
}

results in

error: internal compiler error: librustc_mir/monomorphize/collector.rs:1131: alloc id without corresponding allocation: 39

Even though it should be reporting a dangling pointer const eval error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions