Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICE In rustc_mir::monomorphize::collector #86469

Closed
HalfVoxel opened this issue Jun 19, 2021 · 7 comments
Closed

ICE In rustc_mir::monomorphize::collector #86469

HalfVoxel opened this issue Jun 19, 2021 · 7 comments
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@HalfVoxel
Copy link

HalfVoxel commented Jun 19, 2021

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>

@HalfVoxel HalfVoxel added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 19, 2021
@jonas-schievink jonas-schievink added E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jun 19, 2021
@steffahn
Copy link
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);
}

@steffahn
Copy link
Member

steffahn commented Jun 21, 2021

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

@steffahn
Copy link
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

@steffahn
Copy link
Member

steffahn commented Jun 21, 2021

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.

@rustbot

This comment has been minimized.

@rustbot rustbot added regression-from-stable-to-stable Performance or correctness regression from one stable version to another. and removed E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example labels Jun 21, 2021
@steffahn
Copy link
Member

Looks like this is a duplicate of #85447

@apiraino
Copy link
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

@apiraino apiraino removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants