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

ICE when passing () to a function generic over Copy #42148

Closed
archshift opened this issue May 22, 2017 · 5 comments
Closed

ICE when passing () to a function generic over Copy #42148

archshift opened this issue May 22, 2017 · 5 comments
Labels
C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@archshift
Copy link
Contributor

archshift commented May 22, 2017

#![feature(core_intrinsics)]
extern crate core;
use core::intrinsics;

fn write_reg<T: Copy>(reg: u32, val: T) {
    unsafe { intrinsics::volatile_store(reg as *mut T, val); }
}

fn main() {
    write_reg(0x10000000, ());
}

I was met with an internal compiler error:
error: internal compiler error: unexpected panic

I expected some kind of user-comprehensible error, instead.

Meta

Rust version: rustc 1.19.0-nightly (01951a61a 2017-05-20)

Associated Rust Playground: https://is.gd/ZHy4Av

Backtrace:

thread 'rustc' panicked at 'index out of bounds: the len is 1 but the index is 1', /checkout/src/librustc_trans/intrinsic.rs:248
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace:
   0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
             at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at /checkout/src/libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at /checkout/src/libstd/sys_common/backtrace.rs:60
             at /checkout/src/libstd/panicking.rs:355
   3: std::panicking::default_hook
             at /checkout/src/libstd/panicking.rs:365
   4: std::panicking::rust_panic_with_hook
             at /checkout/src/libstd/panicking.rs:549
   5: std::panicking::begin_panic
             at /checkout/src/libstd/panicking.rs:511
   6: std::panicking::begin_panic_fmt
             at /checkout/src/libstd/panicking.rs:495
   7: rust_begin_unwind
             at /checkout/src/libstd/panicking.rs:471
   8: core::panicking::panic_fmt
             at /checkout/src/libcore/panicking.rs:69
   9: core::panicking::panic_bounds_check
             at /checkout/src/libcore/panicking.rs:56
  10: rustc_trans::intrinsic::trans_intrinsic_call
  11: rustc_trans::mir::block::<impl rustc_trans::mir::MirContext<'a, 'tcx>>::trans_block
  12: rustc_trans::mir::trans_mir
  13: rustc_trans::trans_item::TransItem::define
  14: rustc_trans::base::trans_crate
  15: rustc_driver::driver::phase_4_translate_to_llvm
  16: rustc_driver::driver::compile_input::{{closure}}
  17: rustc_driver::driver::phase_3_run_analysis_passes::{{closure}}
  18: rustc_driver::driver::phase_3_run_analysis_passes
  19: rustc_driver::driver::compile_input
  20: rustc_driver::run_compiler
@archshift archshift changed the title ICE when passing certain arrays to generic function ICE when using copy_from_slice on an array literal May 22, 2017
@archshift archshift changed the title ICE when using copy_from_slice on an array literal ICE when passing () to a function generic over Copy May 22, 2017
@kennytm
Copy link
Member

kennytm commented May 22, 2017

The problem is use of ZST, not generics.

#![feature(core_intrinsics)]
extern crate core;
use core::intrinsics;

struct Zst;

fn main() {
    unsafe { intrinsics::volatile_store(1 as *mut Zst, Zst); }
}

@nagisa nagisa added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 22, 2017
@TimNN
Copy link
Contributor

TimNN commented May 24, 2017

This can be reproduced without directly using the intrinsic as well:

struct Zst;

fn main() {
    unsafe { ::std::ptr::write_volatile(1 as *mut Zst, Zst) }
    
}

@Aaron1011
Copy link
Member

I'd like to work on this.

@arielb1
Copy link
Contributor

arielb1 commented May 27, 2017

@Aaron1011

I can try to mentor this (ping me on IRC)

The problem occurs when function calls are translated to LLVM - the match block here:
https://github.com/rust-lang/rust/blob/master/src/librustc_trans/mir/block.rs#L393

This is a part of trans code, which turns Rust MIR (mid-level IR, defined at https://github.com/rust-lang/rust/blob/master/src/librustc/mir/mod.rs) into LLVM IR (documented at http://llvm.org/docs/LangRef.html). It translates the typed, "unified-value-representation", ABI-unaware MIR into mostly-typeless and ABI-aware LLVM IR.

The problem is that in normal ABIs, zero-typed arguments are marked as ignore here: https://github.com/rust-lang/rust/blob/master/src/librustc_trans/abi.rs#L701, which leads to trans_argument not pushing them into the argument array here: https://github.com/rust-lang/rust/blob/master/src/librustc_trans/mir/block.rs#L626.

As a fix, you could either refactor intrinsics not to use the trans_argument path, or to change the ABI code to not mark ZSTs as ignore with the rust-intrinsic ABI. The latter should be just changing the if to also require the ABI to not be rust-intrinsic.

@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@eminence
Copy link
Contributor

eminence commented Dec 6, 2017

Bug triage: The minimal ZST reproducer from TimNN now compiles without error on the current stable (1.22.1), beta, and nightly. The original reproducer (using core_intrinsics) now compiles without error on the current nightly (rustc 1.23.0-nightly (e97ba8328 2017-11-25))

@kennytm kennytm added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ labels Dec 6, 2017
topecongiro added a commit to topecongiro/rust that referenced this issue Jan 11, 2018
kennytm added a commit to kennytm/rust that referenced this issue Jan 11, 2018
kennytm added a commit to kennytm/rust that referenced this issue Jan 11, 2018
kennytm added a commit to kennytm/rust that referenced this issue Jan 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants