rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.99k stars 12.68k forks source link

ICE: `slices are not scalars` II #116721

Closed matthiaskrgr closed 3 months ago

matthiaskrgr commented 1 year ago

File: /tmp/icemaker/033b7887410cef9d40e915f3b66f44d747d3e0d2.rs

https://github.com/rust-lang/rust/issues/116513 season two

auto-reduced (treereduce-rust):

fn hey<T>(it: &[T])
where
    [T]: Clone,
{
}

fn main() {}

original:

// compile-flags: -Z mir-opt-level=0
// EMIT_MIR issue_91633.hey.mir_map.0.mir
fn hey<T> (it: &[T])
 where
     [T] : Clone,
 {
     let _ = {0;0};
 }

// EMIT_MIR issue_91633.bar.mir_map.0.mir
fn bar<T> (it: Box<[T]>)
 where
     [T] : std::ops::Index<usize>,
 {
     let _ = it[0];
 }

// EMIT_MIR issue_91633.fun.mir_map.0.mir
fn fun<T> (it: &[T]) -> &T
 {
     let f = &it[0];
     f
 }

// EMIT_MIR issue_91633.foo.mir_map.0.mir
fn foo<T: Clone> (it: Box<[T]>) -> T
 {
     let f = it[0].clone();
     f
 }
 fn main(){}

Version information

rustc 1.75.0-nightly (57ef88985 2023-10-13)
binary: rustc
commit-hash: 57ef8898520f7ff2c60341636bb86d94cb550e79
commit-date: 2023-10-13
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.2

Command: /home/matthias/.rustup/toolchains/master/bin/rustc -Zmir-opt-level=3 --emit=mir

Program output

``` warning: unused variable: `it` --> /tmp/icemaker/033b7887410cef9d40e915f3b66f44d747d3e0d2.rs:3:12 | 3 | fn hey (it: &[T]) | ^^ help: if this is intentional, prefix it with an underscore: `_it` | = note: `#[warn(unused_variables)]` on by default warning: function `hey` is never used --> /tmp/icemaker/033b7887410cef9d40e915f3b66f44d747d3e0d2.rs:3:4 | 3 | fn hey (it: &[T]) | ^^^ | = note: `#[warn(dead_code)]` on by default warning: function `bar` is never used --> /tmp/icemaker/033b7887410cef9d40e915f3b66f44d747d3e0d2.rs:11:4 | 11 | fn bar (it: Box<[T]>) | ^^^ warning: function `fun` is never used --> /tmp/icemaker/033b7887410cef9d40e915f3b66f44d747d3e0d2.rs:19:4 | 19 | fn fun (it: &[T]) -> &T | ^^^ warning: function `foo` is never used --> /tmp/icemaker/033b7887410cef9d40e915f3b66f44d747d3e0d2.rs:26:4 | 26 | fn foo (it: Box<[T]>) -> T | ^^^ thread 'rustc' panicked at compiler/rustc_mir_dataflow/src/value_analysis.rs:764:13: slices are not scalars stack backtrace: 0: 0x7f2ba6b628fc - std::backtrace_rs::backtrace::libunwind::trace::h03c0bf785ff8ac95 at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5 1: 0x7f2ba6b628fc - std::backtrace_rs::backtrace::trace_unsynchronized::heb554a194a8b8d76 at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f2ba6b628fc - std::sys_common::backtrace::_print_fmt::h5104548cf2f1d04d at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/std/src/sys_common/backtrace.rs:67:5 3: 0x7f2ba6b628fc - ::fmt::h60a89bd281db8532 at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/std/src/sys_common/backtrace.rs:44:22 4: 0x7f2ba6bca350 - core::fmt::rt::Argument::fmt::h3f967ff7ed1eb864 at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/core/src/fmt/rt.rs:142:9 5: 0x7f2ba6bca350 - core::fmt::write::h6c7eacf90d438ccc at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/core/src/fmt/mod.rs:1117:17 6: 0x7f2ba6b55c4f - std::io::Write::write_fmt::heed557ad0e7e227d at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/std/src/io/mod.rs:1762:15 7: 0x7f2ba6b626e4 - std::sys_common::backtrace::_print::hc14194a28bba6478 at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/std/src/sys_common/backtrace.rs:47:5 8: 0x7f2ba6b626e4 - std::sys_common::backtrace::print::h49644fe6309a893a at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/std/src/sys_common/backtrace.rs:34:9 9: 0x7f2ba6b654f7 - std::panicking::default_hook::{{closure}}::h2aa5ccff7297f9c8 10: 0x7f2ba6b6523a - std::panicking::default_hook::h9e4bff1aa2bdf161 at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/std/src/panicking.rs:292:9 11: 0x7f2ba9718daa - std[5f80c380de201c9c]::panicking::update_hook::>::{closure#0} 12: 0x7f2ba6b65c98 - as core::ops::function::Fn>::call::h372bad5385f8c0c5 at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/alloc/src/boxed.rs:2021:9 13: 0x7f2ba6b65c98 - std::panicking::rust_panic_with_hook::h6c6741452089fc1b at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/std/src/panicking.rs:735:13 14: 0x7f2ba6b659a6 - std::panicking::begin_panic_handler::{{closure}}::h281b553b64b3c920 at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/std/src/panicking.rs:601:13 15: 0x7f2ba6b62e16 - std::sys_common::backtrace::__rust_end_short_backtrace::h3fa2532f3895ceba at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/std/src/sys_common/backtrace.rs:170:18 16: 0x7f2ba6b65722 - rust_begin_unwind at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/std/src/panicking.rs:597:5 17: 0x7f2ba6bc6975 - core::panicking::panic_fmt::h7601551d8f1de7d6 at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/core/src/panicking.rs:72:14 18: 0x7f2ba9c2a8b7 - ::register_children 19: 0x7f2ba9c299b8 - ::new 20: 0x7f2ba9d0fd45 - ::run_pass 21: 0x7f2ba8560a5f - rustc_mir_transform[892ade550f628091]::pass_manager::run_passes_inner 22: 0x7f2ba8d69a6c - rustc_mir_transform[892ade550f628091]::optimized_mir 23: 0x7f2ba7a196dd - rustc_query_impl[d93ff4b57be0a3f9]::plumbing::__rust_begin_short_backtrace::> 24: 0x7f2ba7dcf2d1 - rustc_query_system[bf593801cb8c9f4a]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[d93ff4b57be0a3f9]::plumbing::QueryCtxt, false> 25: 0x7f2ba91d8a22 - rustc_query_impl[d93ff4b57be0a3f9]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace 26: 0x7f2ba7c8bba0 - ::instance_mir 27: 0x7f2ba9af9e5e - rustc_middle[48fc6e82f368dfd0]::mir::pretty::write_mir_pretty 28: 0x7f2ba9cde8e3 - rustc_mir_transform[892ade550f628091]::dump_mir::emit_mir 29: 0x7f2ba8ef940f - rustc_interface[d1c057f52199548c]::passes::start_codegen 30: 0x7f2ba8ef8916 - ::ongoing_codegen 31: 0x7f2ba8ee7552 - std[5f80c380de201c9c]::sys_common::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[3ff10296a9c85239]::run_compiler::{closure#1}>::{closure#0}, core[5ecddd5f6ab0df47]::result::Result<(), rustc_span[3385b0a88a85fcb6]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[5ecddd5f6ab0df47]::result::Result<(), rustc_span[3385b0a88a85fcb6]::ErrorGuaranteed>> 32: 0x7f2ba8ee67d3 - <::spawn_unchecked_, rustc_driver_impl[3ff10296a9c85239]::run_compiler::{closure#1}>::{closure#0}, core[5ecddd5f6ab0df47]::result::Result<(), rustc_span[3385b0a88a85fcb6]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[5ecddd5f6ab0df47]::result::Result<(), rustc_span[3385b0a88a85fcb6]::ErrorGuaranteed>>::{closure#1} as core[5ecddd5f6ab0df47]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 33: 0x7f2ba6b70c05 - as core::ops::function::FnOnce>::call_once::h0018b354bb46a254 at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/alloc/src/boxed.rs:2007:9 34: 0x7f2ba6b70c05 - as core::ops::function::FnOnce>::call_once::hb166705f6d4a8b53 at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/alloc/src/boxed.rs:2007:9 35: 0x7f2ba6b70c05 - std::sys::unix::thread::Thread::new::thread_start::hdac26740529f6e03 at /rustc/57ef8898520f7ff2c60341636bb86d94cb550e79/library/std/src/sys/unix/thread.rs:108:17 36: 0x7f2ba69139eb - 37: 0x7f2ba69977cc - 38: 0x0 - error: 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.75.0-nightly (57ef88985 2023-10-13) running on x86_64-unknown-linux-gnu note: compiler flags: -Z mir-opt-level=3 -Z dump-mir-dir=dir query stack during panic: #0 [optimized_mir] optimizing MIR for `hey` end of query stack warning: 5 warnings emitted ```

ouz-a commented 1 year ago

cc @cjgillot

Noratrieb commented 8 months ago

I cannot reproduce the original anymore, but #121357 does reproduce. The body is impossible because of the where clauses, so no MIR opts should ever run on it... I'll take a look.

matthiaskrgr commented 8 months ago

I can still reproduce this with the original mvce when I use the flags that I listed.

Sky9x commented 8 months ago

bisected #121357 with -Zmir-opt-level=4

searched nightlies: from nightly-2023-09-01 to nightly-2023-10-01 regressed nightly: nightly-2023-09-09 searched commit range: https://github.com/rust-lang/rust/compare/1e746d7741d44551e9378daf13b8797322aa0b74...62ebe3a2b177d50ec664798d731b8a8d1a9120d1 regressed commit: https://github.com/rust-lang/rust/commit/3cd97ed3c3efe11bf6b63d23dce2515238b78a57

bisected with cargo-bisect-rustc v0.6.8

cc #115612 oli was right: https://github.com/rust-lang/rust/issues/116513#issuecomment-1752003649

Noratrieb commented 8 months ago

The bisection here isn't useful sadly, but thank you anyways. I took a look and yeah, the issue is what I suspected. impossible_predicates (https://github.com/rust-lang/rust/blob/bb594538fc6e84213a6b8d5e165442570aa48923/compiler/rustc_mir_transform/src/lib.rs#L451) checks whether there are any impossible predicates (like Self: Sized here). It sadly has to filter out "non-global" predicates, which Self: Sized belongs to. It has to do this because of overflows as mentioned above. @compiler-errors told me that the new trait solvers non-fatal handling of overflow will allow this to be fixed. But until then, there's nothing we can do.

As for getting the code working, the solution here so to avoid these impossible bounds wherever possible. In the example here, that's easy to do, just delete it. In #121357 that's a bit more annoying, I don't really see a solution.

We could try playing whack-a-mole by trying to make the MIR opts sorta-handle these cases that slip through, but I don't think that's very sustainable. Might work at least for the trivial cases like #121357 though, but I don't open PRs for that.

matthiaskrgr commented 7 months ago

another one

fn cast_same_meta<T: ?Sized, U: ?Sized>(ptr: *const T) -> *const U {}

fn sized_slice<T>(ptr: *const [T]) -> *const str
where
    [T]: Sized,
{
    cast_same_meta(ptr)
}
LukasKalbertodt commented 5 months ago

I'm afraid that this has reached stable with 1.78. I couldn't reproduce it with the minimal example in the previous comment, but a crate of mine now fails on stable, but compiled fine on 1.77.2:

/tmp/ ➜  cargo new ice
    Creating binary (application) `ice` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
/tmp/ ➜  cd ice 
/tmp/ice/ (master ✔) ➜  cargo add lina
    Updating crates.io index
      Adding lina v0.2.1 to dependencies
    Updating crates.io index
/tmp/ice/ (master ✔) ➜  cargo +1.77.2 build -r
   Compiling autocfg v1.3.0
   Compiling bytemuck v1.15.0
   Compiling num-traits v0.2.19
   Compiling lina v0.2.1
   Compiling ice v0.1.0 (/tmp/ice)
    Finished release [optimized] target(s) in 0.69s
/tmp/ice/ (master ✔) ➜  cargo +1.78.0 build -r
   Compiling autocfg v1.3.0
   Compiling bytemuck v1.15.0
   Compiling num-traits v0.2.19
   Compiling lina v0.2.1
thread 'rustc' panicked at compiler/rustc_mir_dataflow/src/value_analysis.rs:849:13:
slices are not scalars
full output ``` stack backtrace: 0: 0x7f1d4b78d892 - std::backtrace_rs::backtrace::libunwind::trace::he4ee80166a02c846 at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5 1: 0x7f1d4b78d892 - std::backtrace_rs::backtrace::trace_unsynchronized::h476faccf57e88641 at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f1d4b78d892 - std::sys_common::backtrace::_print_fmt::h430c922a77e7a59c at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:68:5 3: 0x7f1d4b78d892 - ::fmt::hffecb437d922f988 at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:44:22 4: 0x7f1d4b7dea6c - core::fmt::rt::Argument::fmt::hf3df69369399bfa9 at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/fmt/rt.rs:142:9 5: 0x7f1d4b7dea6c - core::fmt::write::hd9a8d7d029f9ea1a at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/fmt/mod.rs:1153:17 6: 0x7f1d4b78278f - std::io::Write::write_fmt::h0e1226b2b8d973fe at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/io/mod.rs:1843:15 7: 0x7f1d4b78d664 - std::sys_common::backtrace::_print::hd2df4a083f6e69b8 at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:47:5 8: 0x7f1d4b78d664 - std::sys_common::backtrace::print::he907f6ad7eee41cb at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:34:9 9: 0x7f1d4b79035b - std::panicking::default_hook::{{closure}}::h3926193b61c9ca9b 10: 0x7f1d4b7900b3 - std::panicking::default_hook::h25ba2457dea68e65 at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:292:9 11: 0x7f1d4862845d - std[e4dfbc2c3f4b09f1]::panicking::update_hook::>::{closure#0} 12: 0x7f1d4b790ac0 - as core::ops::function::Fn>::call::h022ca2c0d8c21c9e at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/boxed.rs:2034:9 13: 0x7f1d4b790ac0 - std::panicking::rust_panic_with_hook::h0ad14d90dcf5224f at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:783:13 14: 0x7f1d4b7907c9 - std::panicking::begin_panic_handler::{{closure}}::h4a1838a06f542647 at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:649:13 15: 0x7f1d4b78dd66 - std::sys_common::backtrace::__rust_end_short_backtrace::h77cc4dc3567ca904 at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:171:18 16: 0x7f1d4b790534 - rust_begin_unwind at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:645:5 17: 0x7f1d4b7daf85 - core::panicking::panic_fmt::h940d4fd01a4b4fd1 at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:72:14 18: 0x7f1d4a04cf9e - ::new 19: 0x7f1d4a07c894 - ::run_pass 20: 0x7f1d4980160d - rustc_mir_transform[d35781a685dd05de]::pass_manager::run_passes_inner 21: 0x7f1d49f064fd - rustc_mir_transform[d35781a685dd05de]::optimized_mir 22: 0x7f1d49f03f93 - rustc_query_impl[e4152ad88c3d6c78]::plumbing::__rust_begin_short_backtrace::> 23: 0x7f1d49908a23 - rustc_query_system[475239fef39bf53f]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[e4152ad88c3d6c78]::plumbing::QueryCtxt, false> 24: 0x7f1d499080de - rustc_query_impl[e4152ad88c3d6c78]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace 25: 0x7f1d4a7c448d - ::encode_crate_root 26: 0x7f1d4a7bba25 - rustc_metadata[70192afe97828208]::rmeta::encoder::encode_metadata 27: 0x7f1d4a5b4c61 - rustc_metadata[70192afe97828208]::fs::encode_and_write_metadata 28: 0x7f1d4a5b40ca - rustc_interface[ba2b6dc4c96cb491]::passes::start_codegen 29: 0x7f1d4a5b39c8 - ::codegen_and_build_linker 30: 0x7f1d4a3fc86f - rustc_interface[ba2b6dc4c96cb491]::interface::run_compiler::, rustc_driver_impl[24a943716c49befe]::run_compiler::{closure#0}>::{closure#0} 31: 0x7f1d4a73eb6e - std[e4dfbc2c3f4b09f1]::sys_common::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[24a943716c49befe]::run_compiler::{closure#0}>::{closure#0}, core[836963c7c1decc11]::result::Result<(), rustc_span[a4517f2b2e65298c]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[836963c7c1decc11]::result::Result<(), rustc_span[a4517f2b2e65298c]::ErrorGuaranteed>> 32: 0x7f1d4a73e9ca - <::spawn_unchecked_, rustc_driver_impl[24a943716c49befe]::run_compiler::{closure#0}>::{closure#0}, core[836963c7c1decc11]::result::Result<(), rustc_span[a4517f2b2e65298c]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[836963c7c1decc11]::result::Result<(), rustc_span[a4517f2b2e65298c]::ErrorGuaranteed>>::{closure#1} as core[836963c7c1decc11]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 33: 0x7f1d4b79a145 - as core::ops::function::FnOnce>::call_once::h19b9e642d37e7272 at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/boxed.rs:2020:9 34: 0x7f1d4b79a145 - as core::ops::function::FnOnce>::call_once::h97265befc434d3ae at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/boxed.rs:2020:9 35: 0x7f1d4b79a145 - std::sys::pal::unix::thread::Thread::new::thread_start::h420dad5cf01a9f35 at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys/pal/unix/thread.rs:108:17 36: 0x7f1d4567455a - 37: 0x7f1d456f1a3c - 38: 0x0 - error: 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.78.0 (9b00956e5 2024-04-29) running on x86_64-unknown-linux-gnu note: compiler flags: --crate-type lib -C opt-level=3 -C embed-bitcode=no -C strip=debuginfo note: some of the compiler flags provided by cargo are hidden query stack during panic: #0 [optimized_mir] optimizing MIR for `mat::hc::::as_ref` end of query stack error: could not compile `lina` (lib) ```

The code responsible for this is fairly simple:

https://github.com/LukasKalbertodt/lina/blob/ce5fb13cd6fec882a1fdd4ec85ae8dadcad037f6/src/mat/hc.rs

Removing that impl makes the crate compile again.


Will we still just wait for the new solver, now that it's a stable-stable regression?

LukasKalbertodt commented 5 months ago

I managed to minimize the example from lina:

use bytemuck::Pod; // 1.15.0

struct NPlusOneArray<T, const N: usize>([T; N], T);

impl<T: Pod, const N: usize> AsRef<[T]> for NPlusOneArray<T, N>
where
    [T]: Pod,
{
    fn as_ref(&self) -> &[T] {
        todo!()
    }
}

(On Playground, ICE in release mode)