Skip to content

Assertion failed: "Ptr must be a pointer to Val type!" #17257

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

Closed
rozaliev opened this issue Sep 14, 2014 · 13 comments
Closed

Assertion failed: "Ptr must be a pointer to Val type!" #17257

rozaliev opened this issue Sep 14, 2014 · 13 comments
Labels
A-codegen Area: Code generation 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

@rozaliev
Copy link

rust version: rustc 0.12.0-pre-nightly (09abbbd 2014-09-11 00:05:41 +0000)

Assertion failed: (getOperand(0)->getType() == cast<PointerType>(getOperand(1)->getType())->getElementType() && "Ptr must be a pointer to Val type!"), function AssertOK, file /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/llvm/lib/IR/Instructions.cpp, line 1085.

code to reproduce:

trait Tr: Clone {
  fn clone_to_tobj(&self) -> Box<Tr> {
    return box self.clone()
  }
}

#[deriving(Clone)]
struct A;
impl Tr for A {
}


fn main() {
   let a: Box<Tr> = box A;
}

but this compiles fine

trait Tr: Clone {
  fn clone_to_tobj(&self) -> Box<Tr> {
    box self.clone()
  }
}

#[deriving(Clone)]
struct A;
impl Tr for A {
}


fn main() {
   let a: Box<Tr> = box A;
}
@retep998
Copy link
Member

Ran into this assertion while compiling retep998/ftb-rs@27ce2aa on win64.

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Assertion failed!

Program: A:\rust64\bin\rustc.exe
File: A:/msys64/home/retep998/rust/src/llvm/lib/IR/Instructions.cpp, Line 1085

Expression: getOperand(0)->getType() == cast<PointerType>(getOperand(1)->getType())->getElementType() && "Ptr must be a pointer to Val type!"

@pnathan
Copy link

pnathan commented Oct 3, 2014

This will engender the bug as well:

fn read_file(path : String) -> Box<[u8]> {
    return box []
}


fn main() {
}

@mdup
Copy link

mdup commented Oct 20, 2014

This example crashes on the same assert.

trait Vehicle { }
struct Car;
impl Vehicle for Car { }

fn build_car<'a>() -> Box<Vehicle + 'a> {
    loop {
        return box Car;
    }
}

fn main() { }

Interestingly, it does not fail to compile when return box Car; is moved out of the loop:

struct Car;
trait Vehicle { }
impl Vehicle for Car { }

fn build_car<'a>() -> Box<Vehicle + 'a> {
    return box Car;
}

fn main() { }

@huonw huonw added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Nov 9, 2014
@wackywendell
Copy link
Contributor

I also see this for this minimal version of this project. cargo build works fine, but cargo test has the same error.

@tomjakubowski
Copy link
Contributor

Another (as minimal as I could get it) reproduction on rustc 0.13.0-dev (8d8f41b75 2014-11-29 21:51:34 +0000:

extern "C" fn foo(_: Blah) { }

struct Blah {
    _ptr: extern "C" fn(Blah)
}

fn main() {
    Blah { _ptr: foo };
}
rustc: /build/rust-git/src/rust/src/llvm/lib/IR/Instructions.cpp:1086: void llvm::StoreInst::AssertOK(): Assertion `getOperand(0)->getType() == cast<PointerType>(getOperand(1)->getType())->getElementType() && "Ptr must be a pointer to Val type!"' failed.
Aborted (core dumped)

Note that there is no assertion failure without the extern "C".

@ftxqxd
Copy link
Contributor

ftxqxd commented Dec 3, 2014

Another case that causes the same error (but for all I know could have a completely different cause):

struct Foo<Sized? T> {
    x: u8,
    y: T,
}

fn main() {
    let foo: &Foo<[int]> = &Foo { x: 1, y: [1i, 2, 3] };
    match *foo {
        Foo { x, ref y } => {},
    }
}

@tomassedovic
Copy link
Contributor

Here's a shorter example for unboxed closures:

#![feature(unboxed_closures)]

fn main() {
    let i = 0i;
    let some_closure = |&: | {
        let _capture = i;
    };
}

Compiling with rustc --debuginfo 2 repro.rs:

rustc: /home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/llvm/lib/IR/Instructions.cpp:1086: void llvm::StoreInst::AssertOK(): Assertion `getOperand(0)->getType() == cast<PointerType>(getOperand(1)->getType())->getElementType() && "Ptr must be a pointer to Val type!"' failed.
Aborted

If you specify --debuginfo 1 or no debuginfo at all, it compiles fine.

It also compiles correctly if don't capture anything from within the closure.

Reproduced on:

$ rustc --version verbose
rustc 0.13.0-nightly (5484d6f6d 2014-12-02 00:22:00 +0000)
binary: rustc
commit-hash: 5484d6f6d2844e9c52d42db52a1ba94739e10996
commit-date: 2014-12-02 00:22:00 +0000
host: x86_64-unknown-linux-gnu
release: 0.13.0-nightly

tomassedovic added a commit to tomassedovic/tcod-rs that referenced this issue Dec 3, 2014
Test are broken with a regression that crashes the compiler on an
unboxed closure which captures some environment:

rust-lang/rust#17257

As this only happens with debuginfo on, this temporarily disables it for
dev & test. This should make Travis happy again.
@hugwijst
Copy link
Contributor

hugwijst commented Dec 4, 2014

A few variations on the example from @mdup that do and don't compile.
TL;DR A workaround for this issue is to cast the box to the function return type, eg. box Car as Box<Vehicle>.

Implicit return: no failure

fn build_car<'a>() -> Box<Vehicle + 'a> {
    box Car
}

Explicit return, no closing semicolon: failure

fn build_car<'a>() -> Box<Vehicle + 'a> {
    return box Car
}

Explicit return, closing semicolon: no failure

fn build_car<'a>() -> Box<Vehicle + 'a> {
    return box Car;
}

Explicit return, cast, both closing and no closing semicolon: no failure

fn build_car<'a>() -> Box<Vehicle + 'a> {
    return box Car as Box<Vehicle>;
}

In loop, explicit return, both closing and no closing semicolon: failure

fn build_car<'a>() -> Box<Vehicle + 'a> {
    loop {
        return box Car;
    }
}

In loop, explicit return, cast, both closing and no closing semicolon: no failure

fn build_car<'a>() -> Box<Vehicle + 'a> {
    loop {
        return box Car as Box<Vehicle>;
    }
}

japaric pushed a commit to japaric-archived/parallel.rs that referenced this issue Dec 5, 2014
japaric pushed a commit to japaric-archived/stats.rs that referenced this issue Dec 5, 2014
japaric pushed a commit to bheisler/criterion.rs that referenced this issue Dec 5, 2014
japaric pushed a commit to japaric-archived/euler_criterion.rs that referenced this issue Dec 8, 2014
milibopp added a commit to milibopp/acacia that referenced this issue Dec 11, 2014
The bug is [1] and it ought to be fixed at some point.

[1] rust-lang/rust#17257
@michaelwoerister michaelwoerister added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-codegen Area: Code generation and removed A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. labels Dec 17, 2014
@JustAPerson
Copy link
Contributor

Updating for current Rust (on playpen):
tl;dr: only @P1start example is still broken (See below for updated syntax).

I was unsure how to update @rozaliev's example because of object safety errors.

@pnathan's example compiles fine:

fn read_file(path : String) -> Box<[u8]> {
    return Box::new([])
}


fn main() {
}

@mdup's example compiles fine:

trait Vehicle { fn stuff(&self) {} }
struct Car;
impl Vehicle for Car { }

fn build_car<'a>() -> Box<Vehicle + 'a> {
    loop {
        return Box::new(Car);
    }
}

fn main() { }

@tomjakubowski's example compiles fine as is.

@P1start's example is still broken and causes a coredump:

struct Foo<T: ?Sized> {
    x: u8,
    y: T,
}

fn main() {
    let foo: &Foo<[isize]> = &Foo { x: 1, y: [1, 2, 3] };
    match *foo {
        Foo { x, ref y } => {},
    }
}

@tomassedovic's example compiles fine even with rustc -C debuginfo=2

#![feature(unboxed_closures)]

fn main() {
    let i: isize = 0;
    let some_closure = || {
        let _capture = i;
    };
}

All of @hugwijst's variants now compile fine.

@randomPoison
Copy link
Contributor

I ran into this issue on the latest nightly with the following example:

type EngineInit = extern fn (Box<Window>) -> Box<Engine>;
type EngineUpdateAndRender = extern fn (&mut Engine);

fn main() {
    // Open the game as a dynamic library;
    let lib = DynamicLibrary::open(Some(Path::new("gunship-24517baeade73325.dll"))).unwrap();

    let engine_init = unsafe {
        mem::transmute::<*mut EngineInit, EngineInit>(lib.symbol("engine_init").unwrap())
    };

    let engine_update_and_render = unsafe {
        mem::transmute::<*mut EngineUpdateAndRender, EngineUpdateAndRender>(lib.symbol("engine_update_and_render").unwrap())
    };

    let mut window = Window::new("Game Window");
    let mut engine = engine_init(window);
    engine_update_and_render(&mut engine);
}

If I change the first line to

type EngineInit = extern fn (Box<Window>) -> Engine;

It compiles fine.

The error I get:

@pmarcelll
Copy link
Contributor

@JustAPerson, I couldn't trigger an ICE with any of the code it this issue, but I found something interesting: as you said, this compiles fine:

fn read_file(path : String) -> Box<[u8]> {
    return Box::new([])
}

fn main() {
}

But I think, it shouldn't. I've never seen return without a semicolon mentioned as valid Rust code.

EDIT: I didn't find an open issue for this. If it's a bug, we should open a new one, but if it's a feature, it should be documented.

@pmarcelll
Copy link
Contributor

Ok, it turned out to be valid syntax (#26425) and I still can't reproduce the ICE 👍

@alexcrichton
Copy link
Member

Thanks @pmarcelll!

GauravBholaris pushed a commit to GauravBholaris/criterion.rs that referenced this issue Jul 20, 2022
GauravBholaris pushed a commit to GauravBholaris/criterion.rs that referenced this issue Jul 20, 2022
milibopp added a commit to milibopp/acacia that referenced this issue Jun 10, 2023
The bug is [1] and it ought to be fixed at some point.

[1] rust-lang/rust#17257
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation 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