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 for broken MIR in 1.57.0, beta and nightly #91633

Closed
marmeladema opened this issue Dec 7, 2021 · 9 comments · Fixed by #100598
Closed

ICE for broken MIR in 1.57.0, beta and nightly #91633

marmeladema opened this issue Dec 7, 2021 · 9 comments · Fixed by #100598
Labels
C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority 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

@marmeladema
Copy link
Contributor

Code

#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Handle(u64);

impl Default for Handle {
    fn default() -> Self {
        Handle(u64::MAX)
    }
}

impl Handle {
    fn as_usize(self) -> usize {
        // transmute is used to get a proper compile
        // time error when usize is not 64 bits long.
        unsafe { std::mem::transmute(self) }
    }
}

use std::ops::Index;

impl<T> Index<Handle> for [T] where Self: Index<usize> {
    type Output = <Self as Index<usize>>::Output;

    fn index(&self, index: Handle) -> &Self::Output {
        &self[index.as_usize()]
    }
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8289d2eaf690a945df598352845ea173

Meta

$ rustc --version --verbose
rustc 1.57.0 (f1edd0429 2021-11-29)
binary: rustc
commit-hash: f1edd0429582dd29cccacaf50fd134b05593bd9c
commit-date: 2021-11-29
host: x86_64-unknown-linux-gnu
release: 1.57.0
LLVM version: 13.0.0

Error output

error: internal compiler error: broken MIR in DefId(0:13 ~ playground[20eb]::{impl#2}::index) (_3 = &(*_1)[_4]): bad assignment (&<[T] as std::ops::Index<usize>>::Output = &T): NoSolution
  --> src/lib.rs:25:9
   |
25 |         &self[index.as_usize()]
   |         ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: delayed at compiler/rustc_borrowck/src/type_check/mod.rs:330:27

error: internal compiler error: broken MIR in Item(WithOptConstParam { did: DefId(0:13 ~ playground[20eb]::{impl#2}::index), const_param_did: None }) (end of phase Optimization) at bb2[0]:
encountered `Assign((_0, &(*_1)[_3]))` with incompatible types:
left-hand side has type: &<[T] as Index<usize>>::Output
right-hand side has type: &T
  --> src/lib.rs:25:9
   |
25 |         &self[index.as_usize()]
   |         ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: delayed at compiler/rustc_const_eval/src/transform/validate.rs:120:36

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', compiler/rustc_errors/src/lib.rs:1165:13
note: run with `RUST_BACKTRACE=1` environment variable to display a 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.57.0 (f1edd0429 2021-11-29) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type lib

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

query stack during panic:
end of query stack
error: could not compile `playground`
Backtrace

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', compiler/rustc_errors/src/lib.rs:1165:13                                                                                                                              
stack backtrace:                                                                                                                                                                                                                                              
   0:     0x7f707abfda9c - std::backtrace_rs::backtrace::libunwind::trace::hf6a6dfd7da937cb0                                                                                                                                                                  
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/../../backtrace/src/backtrace/libunwind.rs:90:5                                                                                                             
   1:     0x7f707abfda9c - std::backtrace_rs::backtrace::trace_unsynchronized::hc596a19e4891f7f3                                                                                                                                                              
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5                                                                                                                   
   2:     0x7f707abfda9c - std::sys_common::backtrace::_print_fmt::hb16700db31584325                                                                                                                                                                          
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x7f707abfda9c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h231c4190cfa75162 
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/sys_common/backtrace.rs:46:22 
   4:     0x7f707ac5afdc - core::fmt::write::h2a1462b5f8eea807
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/fmt/mod.rs:1163:17
   5:     0x7f707abedc05 - std::io::Write::write_fmt::h71ddfebc68685972
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/io/mod.rs:1696:15
   6:     0x7f707ac00f60 - std::sys_common::backtrace::_print::hcc197d4bebf2b369
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/sys_common/backtrace.rs:49:5
   7:     0x7f707ac00f60 - std::sys_common::backtrace::print::h335a66af06738c7c
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/sys_common/backtrace.rs:36:9
   8:     0x7f707ac00f60 - std::panicking::default_hook::{{closure}}::h6fac9ac9c8b79e52
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:210:50
   9:     0x7f707ac00b15 - std::panicking::default_hook::h341c1030c6a1161b
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:227:9
  10:     0x7f707b3ed2b1 - rustc_driver::DEFAULT_HOOK::{{closure}}::{{closure}}::h932547f60770f26a
  11:     0x7f7069205993 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::ha04241e5889574ec
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/alloc/src/boxed.rs:1705:9
  12:     0x7f70691d7ebd - proc_macro::bridge::client::<impl proc_macro::bridge::Bridge>::enter::{{closure}}::{{closure}}::h79da094b1be6293d
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/proc_macro/src/bridge/client.rs:320:21
  13:     0x7f707ac01779 - std::panicking::rust_panic_with_hook::h50680ff4b44510c6
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:628:17
  14:     0x7f707ac01230 - std::panicking::begin_panic_handler::{{closure}}::h9371c0fbb1e8465a
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:521:13
  15:     0x7f707abfdf44 - std::sys_common::backtrace::__rust_end_short_backtrace::h9b3efa22a5768c0f
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/sys_common/backtrace.rs:139:18
  16:     0x7f707ac01199 - rust_begin_unwind
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:517:5
  17:     0x7f707abc5441 - core::panicking::panic_fmt::h23b9203e89cc61cf
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/panicking.rs:100:14
  18:     0x7f707c4d1c3c - core::panicking::panic_display::h0c6ac292783ee908
  19:     0x7f707d9fc866 - rustc_errors::HandlerInner::flush_delayed::hc35129b420cfd4e2
  20:     0x7f707d9fb0ad - <rustc_errors::HandlerInner as core::ops::drop::Drop>::drop::h3c24eb5201838cbf
  21:     0x7f707cfed2a8 - core::ptr::drop_in_place<rustc_session::parse::ParseSess>::hd814f279f0e36752
  22:     0x7f707cfef6ca - <alloc::rc::Rc<T> as core::ops::drop::Drop>::drop::hf486b211acc1f0aa
  23:     0x7f707cfc07ad - core::ptr::drop_in_place<rustc_interface::interface::Compiler>::hefd4f3f97698e310
  24:     0x7f707cfc02ba - rustc_span::with_source_map::h6ab8a240e103b5b9
  25:     0x7f707cfd22ac - scoped_tls::ScopedKey<T>::set::hd1fbd64c6f645895
  26:     0x7f707cfc0ef5 - std::sys_common::backtrace::__rust_begin_short_backtrace::h0a1328c9fa7f7448
  27:     0x7f707cfec962 - core::ops::function::FnOnce::call_once{{vtable.shim}}::h4ea1ced06d6b3e97
  28:     0x7f707ac0c933 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h7bd677a5dc988be6
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/alloc/src/boxed.rs:1691:9
  29:     0x7f707ac0c933 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h7b1c1ba11c4db785
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/alloc/src/boxed.rs:1691:9
  30:     0x7f707ac0c933 - std::sys::unix::thread::Thread::new::thread_start::h9c58c0d12d84e854
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/sys/unix/thread.rs:106:17
  31:     0x7f707ab3beae - start_thread
                               at /builddir/glibc-2.32/nptl/pthread_create.c:463:8
  32:     0x7f707aa582ff - __GI___clone
                               at /builddir/glibc-2.32/misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:95
  33:                0x0 - <unknown>

@marmeladema marmeladema 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 Dec 7, 2021
@marmeladema
Copy link
Contributor Author

Minimized a bit more:

use std::ops::Index;

pub struct Handle(u64);

impl<T> Index<Handle> for [T] where Self: Index<usize> {
    type Output = <Self as Index<usize>>::Output;

    fn index(&self, index: Handle) -> &Self::Output {
        &self[index.0 as usize]
    }
}

@marmeladema
Copy link
Contributor Author

It started ICE-ing in 1.41.0:

$ rustc +1.40.0 issue-91633.rs 
error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
 --> issue-91633.rs:5:6
  |
5 | impl<T> Index<Handle> for [T] where Self: Index<usize> {
  |      ^ type parameter `T` must be used as the type parameter for some local type
  |
  = note: only traits defined in the current crate can be implemented for a type parameter

error: aborting due to previous error

For more information about this error, try `rustc --explain E0210`.
$ rustc +1.41.0 issue-91633.rs 
error: internal compiler error: broken MIR in DefId(0:10 ~ issue_91633[317d]::{{impl}}[0]::index[0]) (_3 = &(*_1)[_4]): bad assignment (&<[T] as std::ops::Index<usize>>::Output = &T): NoSolution
 --> issue-91633.rs:9:9
  |
9 |         &self[index.0 as usize]
  |         ^^^^^^^^^^^^^^^^^^^^^^^

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:359:17
note: run with `RUST_BACKTRACE=1` environment variable to display a 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/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.41.0 (5e1a79984 2020-01-27) running on x86_64-unknown-linux-gnu

@SNCPlay42
Copy link
Contributor

This compiles successfully without the where Self: Index<usize> bound.

It also compiles if you change the bound to constrain the Output associated type: where Self: Index<usize, Output = T>

@orium
Copy link
Member

orium commented Dec 7, 2021

I bisected it to be between 9e34664 and ac162c6:

found 6 bors merge commits in the specified range
  commit[0] 2019-11-08UTC: Auto merge of #66225 - Centril:rollup-it0t5tk, r=Centril
  commit[1] 2019-11-08UTC: Auto merge of #66194 - alexcrichton:update-clang, r=Mark-Simulacrum
  commit[2] 2019-11-09UTC: Auto merge of #65879 - ohadravid:stabilize-re-rebalance-coherence, r=nikomatsakis
  commit[3] 2019-11-09UTC: Auto merge of #66242 - Centril:rollup-h73ztr1, r=Centril
  commit[4] 2019-11-09UTC: Auto merge of #66243 - RalfJung:miri, r=RalfJung
  commit[5] 2019-11-09UTC: Auto merge of #63871 - BatmanAoD:FloatFnMustUse, r=withoutboats
ERROR: no CI builds available between 9e346646e93cc243567e27bb0f4e8716d56ad1f1 and ac162c6abe34cdf965afc0389f6cefa79653c63b within last 167 days

I've tried to bisect between these commits, but I'm getting a compilation error in llvm.

@SNCPlay42
Copy link
Contributor

SNCPlay42 commented Dec 7, 2021

It'll be #65879, which changed the coherence rules to allow impl<T> ForeignTrait<LocalType> for ForeignType<T>.

The impl in this code is intentionally allowed under the new rules and should be fine, so it didn't really regress in that PR so much as it was newly permitted but likely surfaced a pre-existing bug.

I tried to replicate this issue with a local trait but this compiles successfully, so this does seem to involve the built-in Index trait and operator (the error message complains about the MIR statement _3 = &(*_1)[_4] which is a built-in index).

@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Dec 8, 2021
@danielhenrymantilla
Copy link
Contributor

Further reduced to:

fn f<T> (it: &[T])
where
    [T] : std::ops::Index<usize>,
{
    let _ = &it[0];
}

I agree with @SNCPlay42's assessment:

involve the built-in Index trait and operator

@marmeladema
Copy link
Contributor Author

So it could be considered a stable-to-stable regression? I don't how or who could add this label.

@danielhenrymantilla
Copy link
Contributor

Yeah, I guess we could

@rustbot modify labels +regression-from-stable-to-stable

although it's a regression from a very old stable 😄

@rustbot rustbot added regression-from-stable-to-stable Performance or correctness regression from one stable version to another. I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Dec 26, 2021
@apiraino
Copy link
Contributor

Assigning priority as discussed in the Zulip thread of the Prioritization Working Group.

@rustbot label -I-prioritize +P-medium

@rustbot rustbot added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Dec 28, 2021
@bors bors closed this as completed in 490d04b Aug 19, 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. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority 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
7 participants