Skip to content

ICE In rustc_mir::monomorphize::collector #86469

Closed

Description

@HalfVoxel

I'm getting an ICE when compiling some of my code.
Unfortunately, it has resisted all my attempts at producing a smaller code sample. It's very finicky.
I have uploaded a reproducible repo here: https://github.com/HalfVoxel/graphica/tree/rustc-replicate, unfortunately it's quite a lot of code. The code that seems to trigger it is this:

fn hash_material(material: &Material, changes: &[BindGroupEntryArc]) -> u64 {
    let mut hasher = std::collections::hash_map::DefaultHasher::default();
    std::ptr::hash(Arc::as_ptr(&material.bind_group_layout.0), &mut hasher);
    hasher.finish()
}

However, I have been unable to replicate it in a smaller project.

Meta

rustc --version --verbose:

rustc 1.55.0-nightly (ce1d5611a 2021-06-18)
binary: rustc
commit-hash: ce1d5611a28468663e275078649e7ca6eef735a8
commit-date: 2021-06-18
host: x86_64-unknown-linux-gnu
release: 1.55.0-nightly
LLVM version: 12.0.1

The bug exists both in the current nightly compiler and the current stable compiler.

Error output

thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', compiler/rustc_mir/src/monomorphize/collector.rs:884:93

This is the location in rustc where it crashes:

image

Backtrace

<backtrace>

thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', compiler/rustc_mir/src/monomorphize/collector.rs:884:93
stack backtrace:
   0: rust_begin_unwind
             at /rustc/ce1d5611a28468663e275078649e7ca6eef735a8/library/std/src/panicking.rs:515:5
   1: core::panicking::panic_fmt
             at /rustc/ce1d5611a28468663e275078649e7ca6eef735a8/library/core/src/panicking.rs:92:14
   2: core::panicking::panic
             at /rustc/ce1d5611a28468663e275078649e7ca6eef735a8/library/core/src/panicking.rs:50:5
   3: <rustc_mir::monomorphize::collector::MirNeighborCollector as rustc_middle::mir::visit::Visitor>::visit_terminator
   4: rustc_mir::monomorphize::collector::collect_neighbours
   5: rustc_mir::monomorphize::collector::collect_items_rec
   6: rustc_mir::monomorphize::collector::collect_items_rec
   7: rustc_mir::monomorphize::collector::collect_items_rec
   8: rustc_mir::monomorphize::collector::collect_items_rec
   9: rustc_mir::monomorphize::collector::collect_items_rec
  10: rustc_mir::monomorphize::collector::collect_items_rec
  11: rustc_mir::monomorphize::collector::collect_items_rec
  12: rustc_mir::monomorphize::collector::collect_items_rec
  13: rustc_mir::monomorphize::collector::collect_items_rec
  14: rustc_mir::monomorphize::collector::collect_items_rec
  15: rustc_mir::monomorphize::collector::collect_items_rec
  16: rustc_mir::monomorphize::collector::collect_items_rec
  17: rustc_session::utils::<impl rustc_session::session::Session>::time
  18: rustc_mir::monomorphize::collector::collect_crate_mono_items
  19: rustc_mir::monomorphize::partitioning::collect_and_partition_mono_items
  20: rustc_query_system::query::plumbing::get_query_impl
  21: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::collect_and_partition_mono_items
  22: rustc_codegen_ssa::back::symbol_export::exported_symbols_provider_local
  23: rustc_query_system::query::plumbing::get_query_impl
  24: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::exported_symbols
  25: rustc_metadata::rmeta::encoder::EncodeContext::encode_crate_root
  26: rustc_metadata::rmeta::encoder::encode_metadata_impl
  27: rustc_data_structures::sync::join
  28: rustc_metadata::rmeta::decoder::cstore_impl::<impl rustc_middle::middle::cstore::CrateStore for rustc_metadata::creader::CStore>::encode_metadata
  29: rustc_middle::ty::context::TyCtxt::encode_metadata
  30: rustc_interface::passes::QueryContext::enter
  31: rustc_interface::queries::Queries::ongoing_codegen
  32: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  33: rustc_span::with_source_map
  34: rustc_interface::interface::create_compiler_and_run
  35: scoped_tls::ScopedKey<T>::set
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.55.0-nightly (ce1d5611a 2021-06-18) running on x86_64-unknown-linux-gnu

note: compiler flags: -C opt-level=z -C embed-bitcode=no --crate-type lib

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [collect_and_partition_mono_items] collect_and_partition_mono_items
#1 [exported_symbols] exported_symbols
end of query stack
</p>
</details>

Activity

  1. added
    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.
    on Jun 19, 2021
  2. added
    E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jun 19, 2021
  3. steffahn commented on Jun 21, 2021

    @steffahn
    Member

    significantly reduced:

    use std::ptr;
    
    trait Context {
        type BindGroupLayoutId;
    }
    pub struct C;
    pub struct BindGroupLayout {
        id: <C as Context>::BindGroupLayoutId,
    }
    impl Context for C {
        type BindGroupLayoutId = ();
    }
    
    pub fn main() {
        let y: *const BindGroupLayout = ptr::null();
    
        let mut hasher = std::collections::hash_map::DefaultHasher::default();
        std::ptr::hash(y, &mut hasher);
    }
  4. steffahn commented on Jun 21, 2021

    @steffahn
    Member

    Without using DefaultHasher or std

    trait Trait {
        type Associated;
    }
    pub struct T;
    impl Trait for T {
        type Associated = ();
    }
    pub struct Struct {
        _field: <T as Trait>::Associated,
    }
    
    struct H;
    impl core::hash::Hasher for H {
        fn finish(&self) -> u64 {
            todo!()
        }
        fn write(&mut self, _: &[u8]) {
            todo!()
        }
    }
    
    pub fn main() {
        let y: *const Struct = core::ptr::null();
        core::hash::Hash::hash(&y, &mut H);
    }
  5. steffahn commented on Jun 21, 2021

    @steffahn
    Member

    Unwrapping the core internals you can get this repro:

    #![feature(ptr_metadata)]
    
    use core::ptr::Pointee;
    trait Trait {
        type Associated;
    }
    pub struct T;
    impl Trait for T {
        type Associated = ();
    }
    pub struct Struct {
        _field: <T as Trait>::Associated,
    }
    
    struct H;
    impl core::hash::Hasher for H {
        fn finish(&self) -> u64 {
            todo!()
        }
        fn write(&mut self, _: &[u8]) {
            todo!()
        }
    }
    
    fn panic<T>() -> T {
        panic!()
    }
    
    pub fn main() {
        use core::hash::Hash;
        let metadata: <Struct as Pointee>::Metadata = panic();
        metadata.hash(&mut H);
    }

    Or using something other than Hash methods

    #![feature(ptr_metadata)]
    
    use core::ptr::Pointee;
    trait Trait {
        type Associated;
    }
    pub struct T;
    impl Trait for T {
        type Associated = ();
    }
    pub struct Struct {
        _field: <T as Trait>::Associated,
    }
    
    
    fn panic<T>() -> T {
        panic!()
    }
    
    pub fn main() {
        let metadata: <Struct as Pointee>::Metadata = panic();
        metadata == metadata;
    }

    With no usage at all, the ICE changes:

    #![feature(ptr_metadata)]
    
    use core::ptr::Pointee;
    trait Trait {
        type Associated;
    }
    pub struct T;
    impl Trait for T {
        type Associated = ();
    }
    pub struct Struct {
        _field: <T as Trait>::Associated,
    }
    
    pub fn main() {
        let _x: <Struct as Pointee>::Metadata;
    }
    error: internal compiler error: compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:704:14: debuginfo: unexpected type in type_metadata: <Struct as std::ptr::Pointee>::Metadata
    
    thread 'rustc' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1007:9
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
    
    note: the compiler unexpectedly panicked. this is a bug.
    
    note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md
    
    note: rustc 1.55.0-nightly (a85f584ae 2021-06-16) running on x86_64-unknown-linux-gnu
    
    note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type bin
    
    note: some of the compiler flags provided by cargo are hidden
    
    query stack during panic:
    end of query stack
    error: aborting due to previous error
    
  6. steffahn commented on Jun 21, 2021

    @steffahn
    Member

    Bisecting #86469 (comment), regression at: d1462d8, which is #81172, cc @SimonSapin.

    This issue is a recent regression (stable 1.52), turning previously compiling, valid code into an ICE.

    @rustbot label -E-needs-mcve, regression-from-stable-to-stable.

  7. rustbot commented on Jun 21, 2021

    @rustbot
  8. added
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    and removed
    E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example
    on Jun 21, 2021
  9. steffahn commented on Jun 21, 2021

    @steffahn
    Member

    Looks like this is a duplicate of #85447

  10. apiraino commented on Jun 23, 2021

    @apiraino
    Contributor

    great job with the analysis and bisection, thanks @steffahn !

    I'm gonna close as duplicate but (as always) feel free to reopen if it's not the case

    @rustbot label -I-prioritize

  11. removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Mar 10, 2022
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.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.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions