Skip to content

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

@archshift

Description

@archshift
Contributor
#![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

Activity

changed the title [-]ICE when passing certain arrays to generic function[/-] [+]ICE when using `copy_from_slice` on an array literal[/+] on May 22, 2017
changed the title [-]ICE when using `copy_from_slice` on an array literal[/-] [+]ICE when passing `()` to a function generic over `Copy`[/+] on May 22, 2017
kennytm

kennytm commented on May 22, 2017

@kennytm
Member

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); }
}
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.
on May 22, 2017
TimNN

TimNN commented on May 24, 2017

@TimNN
Contributor

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

Aaron1011 commented on May 27, 2017

@Aaron1011
Member

I'd like to work on this.

arielb1

arielb1 commented on May 27, 2017

@arielb1
Contributor

@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.

eminence

eminence commented on Dec 6, 2017

@eminence
Contributor

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))

added
E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
and removed
I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
on Dec 6, 2017
added a commit that references this issue on Jan 11, 2018
9649c4a
added a commit that references this issue on Jan 11, 2018
4551f3a

4 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.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

        Participants

        @kennytm@eminence@nagisa@TimNN@Aaron1011

        Issue actions

          ICE when passing `()` to a function generic over `Copy` · Issue #42148 · rust-lang/rust