Skip to content

ICE when combining const (fn) and unions #47788

Closed
@jonas-schievink

Description

@jonas-schievink

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions