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

Assertion failed while type-casting #21073

Closed
jethrogb opened this issue Jan 13, 2015 · 7 comments
Closed

Assertion failed while type-casting #21073

jethrogb opened this issue Jan 13, 2015 · 7 comments
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@jethrogb
Copy link
Contributor

jethrogb commented Jan 13, 2015

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

@kmcallister kmcallister added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ labels Jan 13, 2015
@tamird
Copy link
Contributor

tamird commented Apr 21, 2015

Triage: still ICEs. Reproduces without using macros:

struct Foo;

const OFFSET_OF_C: *const () = unsafe {
    &*(0 as *const Foo)as *const _ as *const _
};

fn main() {
    println!("offset of c {}" , OFFSET_OF_C as usize)
}
$ rustc main.rs
main.rs:8:33: 8:44 error: internal compiler error: constant expression should not reach expr::trans_def
main.rs:8     println!("offset of c {}" , OFFSET_OF_C as usize)

@IvanUkhov
Copy link
Contributor

Here is another example:

const TAG: &'static [u8] = b"ABCD";

macro_rules! tag(
    ($value:expr) => (unsafe {
        let value: [u8; 4] = ::std::mem::transmute($value);
        &vec![value[3], value[2], value[1], value[0]][..]
    })
);

fn main() {
    match tag!(42) {
        TAG => println!("Yes."),
        _ => println!("No."),
    }
}
Assertion failed: (isa<X>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file /Users/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-mac/build/src/llvm/include/llvm/Support/Casting.h, line 237.

Regards,
Ivan

@arielb1
Copy link
Contributor

arielb1 commented Jul 8, 2015

Simpler:

const TAG: &'static [u8] = b"ABCD";

fn main() {
    match &[][..] {
        TAG => println!("Yes."),
        _ => println!("No."),
    }
}

Seems like a missing slice-patterns check.

@jethrogb
Copy link
Contributor Author

jethrogb commented Oct 1, 2015

The first two examples no longer ICE. The last two still do (SIGILL on stable, LLVM assertion failed on nightly).

bltavares pushed a commit to bltavares/glacier that referenced this issue Oct 25, 2015
@jonas-schievink
Copy link
Contributor

Still ICEs on rustc 1.11.0-nightly (34505e222 2016-06-08), but not with -Zorbit, so it looks like this will be fixed with MIR trans

@DemiMarie
Copy link
Contributor

Is this undefined behavior?

@steveklabnik
Copy link
Member

This ICE seems to be fixed on

rustc 1.12.0-nightly (b30eff7ba 2016-08-05)

I have a hunch that this is due to MIR being turned on, though I'm not sure. Please let me know if this still ICEs for you, but closing for now 🎊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

No branches or pull requests

8 participants