Skip to content

Assertion failed while type-casting #21073

Closed
@jethrogb

Description

@jethrogb

While trying to write an offsetof that works in a constant expr, the compiler gets a low-level assertion failure.

struct Foo {
    a: u64,
    b: u32,
    c: u8
}

macro_rules! offset_of {
  ($t:ty, $field:ident) => {
    unsafe {
      (&(*(0 as *const $t)).$field) as *const _ as *const _
    }
  }
}

const OFFSET_OF_C: *const () = offset_of!(Foo, c);

fn main() {
  println!("offset of c: {}" , OFFSET_OF_C as usize);
}

With rust-nightly 2015011204074bed1e8trusty:

$rustc test.rs 
test.rs:2:5: 2:11 warning: struct field is never used: `a`, #[warn(dead_code)] on by default
test.rs:2     a: u64,
              ^~~~~~
test.rs:3:5: 3:11 warning: struct field is never used: `b`, #[warn(dead_code)] on by default
test.rs:3     b: u32,
              ^~~~~~
rustc: /build/buildd/rust-nightly-201501120407~4bed1e8~trusty/src/llvm/include/llvm/Support/Casting.h:237: typename llvm::cast_retty<X, Y*>::ret_type llvm::cast(Y*) [with X = llvm::GlobalVariable; Y = llvm::Value; typename llvm::cast_retty<X, Y*>::ret_type = llvm::GlobalVariable*]: Assertion `isa<X>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
Aborted

Via @edef1c

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions