Skip to content

Compiler panic when taking the pointer of a static mut variable #28751

Closed
@Snaipe

Description

@Snaipe

I'm trying to reproduce this bit of C code into Rust, but I'm getting hit by a compiler panic.

The context is that I want the rust code to put a pointer to some static data in a custom section, that will be read by a C codebase in the final executable.

IRC user scott reduced the sample code needed to trigger the panic:

struct DataType {
    data: *mut (),
}

unsafe impl Sync for DataType {}

static mut EXTRA_DATA: () = ();

static DATA : DataType = DataType {
    data: unsafe { &mut EXTRA_DATA }
};

fn main() {
}

Compiling this with RUST_BACKTRACE=1 rustc panic.rs produces the following output:

panic2.rs:1:1: 3:2 warning: struct is never used: `DataType`, #[warn(dead_code)] on by default
panic2.rs:1 struct DataType {
panic2.rs:2     data: *mut (),
panic2.rs:3 }
panic2.rs:2:5: 2:18 warning: struct field is never used: `data`, #[warn(dead_code)] on by default
panic2.rs:2     data: *mut (),
                ^~~~~~~~~~~~~
panic2.rs:7:1: 7:32 warning: static item is never used: `EXTRA_DATA`, #[warn(dead_code)] on by default
panic2.rs:7 static mut EXTRA_DATA: () = ();
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
panic2.rs:9:1: 11:3 warning: static item is never used: `DATA`, #[warn(dead_code)] on by default
panic2.rs: 9 static DATA : DataType = DataType {
panic2.rs:10     data: unsafe { &mut EXTRA_DATA }
panic2.rs:11 };
panic2.rs:10:25: 10:35 error: internal compiler error: expected a const, fn, struct, or variant def
panic2.rs:10     data: unsafe { &mut EXTRA_DATA }
                                     ^~~~~~~~~~
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
thread 'rustc' panicked at 'Box<Any>', src/libsyntax/diagnostic.rs:176

stack backtrace:
   1:     0x7fa9c220466e - sys::backtrace::write::ha67a6a2049d22fadIvs
   2:     0x7fa9c220cd55 - panicking::on_panic::hfa4bf0215983f759lzx
   3:     0x7fa9c21cd94e - rt::unwind::begin_unwind_inner::ha1b5da93bdf7e8ffC0w
   4:     0x7fa9bf5f3cdc - rt::unwind::begin_unwind::h10002216390169315342
   5:     0x7fa9bf5f3c7b - diagnostic::SpanHandler::span_bug::h6d3d1eb598e37d9aX6A
   6:     0x7fa9c00c2cf8 - session::Session::span_bug::h103bc02ef373c2e4nRt
   7:     0x7fa9c106a7ad - trans::consts::const_expr_unadjusted::h1d2d83c04c480e728Et
   8:     0x7fa9c1067e7f - trans::consts::const_expr::h08aedd7988733ed6Yot
   9:     0x7fa9c106aa1c - trans::consts::const_expr_unadjusted::h1d2d83c04c480e728Et
  10:     0x7fa9c1067e7f - trans::consts::const_expr::h08aedd7988733ed6Yot
  11:     0x7fa9c1069fce - trans::consts::const_expr_unadjusted::h1d2d83c04c480e728Et
  12:     0x7fa9c1067e7f - trans::consts::const_expr::h08aedd7988733ed6Yot
  13:     0x7fa9c1073727 - trans::consts::const_expr_unadjusted::closure.49914
  14:     0x7fa9c107349e - vec::Vec<T>.FromIterator<T>::from_iter::h17887475302167253138
  15:     0x7fa9c1073089 - trans::consts::const_expr_unadjusted::closure.49903
  16:     0x7fa9c106b212 - trans::consts::const_expr_unadjusted::h1d2d83c04c480e728Et
  17:     0x7fa9c1067e7f - trans::consts::const_expr::h08aedd7988733ed6Yot
  18:     0x7fa9c1011493 - trans::base::trans_item::hd3ac3af06c93993fPdj
  19:     0x7fa9c101e40c - trans::base::trans_crate::h14e6ea6d455593f8i0j
  20:     0x7fa9c27732f4 - driver::phase_4_translate_to_llvm::h8ce7c7bfb0fd9b185Oa
  21:     0x7fa9c276d885 - driver::phase_3_run_analysis_passes::closure.16542
  22:     0x7fa9c2767ab1 - middle::ty::ctxt<'tcx>::create_and_enter::h9578579149294230291
  23:     0x7fa9c2762991 - driver::phase_3_run_analysis_passes::h8415522514568150077
  24:     0x7fa9c2746a90 - driver::compile_input::h52c7dafd49963360Tba
  25:     0x7fa9c282ca63 - run_compiler::h473c62e00f865fa9A7b
  26:     0x7fa9c282a4de - boxed::F.FnBox<A>::call_box::h10713159552398332324
  27:     0x7fa9c2829e09 - rt::unwind::try::try_fn::h16887904248528792425
  28:     0x7fa9c220c84d - __rust_try
  29:     0x7fa9c21f78f7 - rt::unwind::try::inner_try::hafffff77ddacfa5fvWw
  30:     0x7fa9c282a028 - boxed::F.FnBox<A>::call_box::h5417278073590676257
  31:     0x7fa9c220b991 - sys::thread::Thread::new::thread_start::h29dca2a2cf2294b535v
  32:     0x7fa9bcaad353 - start_thread
  33:     0x7fa9c1e69bfc - __clone
  34:                0x0 - <unknown>

version info

$ rustc --version --verbose
rustc 1.3.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    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