Closed
Description
This code posted by @sfleischman105 in #24111 currently compiles:
#![feature(const_fn)]
use std::time::Instant;
pub struct GlobalTimer {
time: Instant,
}
union DummyUnion {
time: Instant,
junk: u32
}
impl GlobalTimer {
pub const fn init() -> GlobalTimer {
const DUMMY: DummyUnion = DummyUnion {
junk: 0
};
const UNINIT: Instant = unsafe {
DUMMY.time
};
// Ta-da!
GlobalTimer {
time: UNINIT,
}
}
}
fn main() {
//GlobalTimer::init();
}
With this suspicious (and wrong?) warning:
warning: constant evaluation error: nonexistent struct field
--> src/main.rs:20:33
|
20 | const UNINIT: Instant = unsafe {
| _________________________________^
21 | | DUMMY.time
22 | | };
| |_________^
|
= note: #[warn(const_err)] on by default
However, trying to use the const fn
by uncommenting the line in main
causes an ICE:
error: internal compiler error: librustc_trans/type_of.rs:386: TyLayout::llvm_field_index(TyLayout { ty: DummyUnion, details: LayoutDetails { variants: Single { index: 0 }, fields: Union(2), abi: Aggregate { sized: true }, align: Align { abi: 3, pref: 3 }, size: Size { raw: 16 } } }): not applicable