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]: Both stable and nightly crash at compiler/rustc_const_eval/src/interpret/operand.rs:84:42 on the invalid code under opt-level=3 #135128

Open
2 of 4 tasks
wangbo15 opened this issue Jan 5, 2025 · 13 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@wangbo15
Copy link

wangbo15 commented Jan 5, 2025

Code

#![allow(unused)]
#![allow(type_alias_bounds)]
pub trait Foo {
    fn test(&self);
}
async fn generic_function<'g, X: Foo>(x: &'g X) {}
enum E
where
    i32: Foo,
{
    V,
}
struct S
where
    i32: Foo;
trait T where
    i32: Foo,
{
}
union U
where
    i32: Foo,
{
    f: i32,
}
type Y
where
    i32: Foo,
= ();
impl Foo for ()
where
    i32: Foo,
{
    fn test(&self) {
        3i32.test();
        Foo::test(&4i32);
        generic_function(5i32);
    }
}
async fn f()
where
    i32: Foo,
{
    let s = S;
    3i32.test();
    Foo::test(&4i32);
    generic_function(5i32);
}
async fn use_op<'g>(s: &'g String) -> String
where
    String: ::std::ops::Neg<Output = String>,
{
    -s
}
async fn use_for()
where
    i32: Iterator,
{
    for _ in 2i32 {}
}
trait A {}
impl A for i32 {}
struct Dst {
    x: X,
}
struct TwoStrs(str, str)
where
    str: Sized;
async fn unsized_local()
where
    Dst<dyn A>: Sized,
{
    let x: Dst<dyn A> = *(Box::new(Dst { x: 1 }) as Box<Dst<dyn A>>);
}
async fn return_str() -> str
where
    str: Sized,
{
    *"Sized".to_string().into_boxed_str()
}
async fn global_hr<'g>(x: &'g fn(&()))
where
    fn(&()): Foo,
{
    x.test();
}
fn main() {}

Affected release channels

  • Previous Stable
  • Current Stable
  • Current Beta
  • Current Nightly

Rust Version

$ rustc --version --verbose
rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1

Current error output

$ rustc -C opt-level=3 --edition=2021 1.rs 
error[E0412]: cannot find type `X` in this scope
  --> 1.rs:64:8
   |
61 | trait A {}
   | ------- similarly named trait `A` defined here
...
64 |     x: X,
   |        ^
   |
help: a trait with a similar name exists
   |
64 |     x: A,
   |        ~
help: you might be missing a type parameter
   |
63 | struct Dst<X> {
   |           +++

error[E0277]: the trait bound `i32: Foo` is not satisfied
 --> 1.rs:9:5
  |
9 |     i32: Foo,
  |     ^^^^^^^^ the trait `Foo` is not implemented for `i32`
  |
  = help: the trait `Foo` is implemented for `()`
  = help: see issue #48214

error[E0277]: the trait bound `i32: Foo` is not satisfied
  --> 1.rs:15:5
   |
15 |     i32: Foo;
   |     ^^^^^^^^ the trait `Foo` is not implemented for `i32`
   |
   = help: the trait `Foo` is implemented for `()`
   = help: see issue #48214

error[E0277]: the trait bound `i32: Foo` is not satisfied
  --> 1.rs:17:5
   |
17 |     i32: Foo,
   |     ^^^^^^^^ the trait `Foo` is not implemented for `i32`
   |
   = help: the trait `Foo` is implemented for `()`
   = help: see issue #48214

error[E0277]: the trait bound `i32: Foo` is not satisfied
  --> 1.rs:22:5
   |
22 |     i32: Foo,
   |     ^^^^^^^^ the trait `Foo` is not implemented for `i32`
   |
   = help: the trait `Foo` is implemented for `()`
   = help: see issue #48214

error[E0277]: the trait bound `i32: Foo` is not satisfied
  --> 1.rs:32:5
   |
32 |     i32: Foo,
   |     ^^^^^^^^ the trait `Foo` is not implemented for `i32`
   |
   = help: the trait `Foo` is implemented for `()`
   = help: see issue #48214

error[E0277]: the trait bound `i32: Foo` is not satisfied
  --> 1.rs:42:5
   |
42 |     i32: Foo,
   |     ^^^^^^^^ the trait `Foo` is not implemented for `i32`
   |
   = help: the trait `Foo` is implemented for `()`
   = help: see issue #48214

error[E0277]: the trait bound `String: Neg` is not satisfied
  --> 1.rs:51:5
   |
51 |     String: ::std::ops::Neg<Output = String>,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Neg` is not implemented for `String`
   |
   = help: see issue #48214

error[E0277]: `i32` is not an iterator
  --> 1.rs:57:5
   |
57 |     i32: Iterator,
   |     ^^^^^^^^^^^^^ `i32` is not an iterator
   |
   = help: the trait `Iterator` is not implemented for `i32`
   = help: see issue #48214

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> 1.rs:68:5
   |
68 |     str: Sized;
   |     ^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `str`
   = help: see issue #48214

error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied
  --> 1.rs:71:5
   |
71 |     Dst<dyn A>: Sized,
   |     ^^^------- help: remove the unnecessary generics
   |     |
   |     expected 0 generic arguments
   |
note: struct defined here, with 0 generic parameters
  --> 1.rs:63:8
   |
63 | struct Dst {
   |        ^^^

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> 1.rs:77:5
   |
77 |     str: Sized,
   |     ^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `str`
   = help: see issue #48214

error[E0308]: mismatched types
  --> 1.rs:47:22
   |
47 |     generic_function(5i32);
   |     ---------------- ^^^^ expected `&_`, found `i32`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected reference `&_`
                   found type `i32`
note: function defined here
  --> 1.rs:6:10
   |
6  | async fn generic_function<'g, X: Foo>(x: &'g X) {}
   |          ^^^^^^^^^^^^^^^^             --------
help: consider borrowing here
   |
47 |     generic_function(&5i32);
   |                      +

error[E0600]: cannot apply unary operator `-` to type `&String`
  --> 1.rs:53:5
   |
53 |     -s
   |     ^^ cannot apply unary operator `-`

error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied
  --> 1.rs:73:12
   |
73 |     let x: Dst<dyn A> = *(Box::new(Dst { x: 1 }) as Box<Dst<dyn A>>);
   |            ^^^------- help: remove the unnecessary generics
   |            |
   |            expected 0 generic arguments
   |
note: struct defined here, with 0 generic parameters
  --> 1.rs:63:8
   |
63 | struct Dst {
   |        ^^^

error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied
  --> 1.rs:73:57
   |
73 |     let x: Dst<dyn A> = *(Box::new(Dst { x: 1 }) as Box<Dst<dyn A>>);
   |                                                         ^^^------- help: remove the unnecessary generics
   |                                                         |
   |                                                         expected 0 generic arguments
   |
note: struct defined here, with 0 generic parameters
  --> 1.rs:63:8
   |
63 | struct Dst {
   |        ^^^

error[E0308]: mismatched types
  --> 1.rs:37:26
   |
37 |         generic_function(5i32);
   |         ---------------- ^^^^ expected `&_`, found `i32`
   |         |
   |         arguments to this function are incorrect
   |
   = note: expected reference `&_`
                   found type `i32`
note: function defined here
  --> 1.rs:6:10
   |
6  | async fn generic_function<'g, X: Foo>(x: &'g X) {}
   |          ^^^^^^^^^^^^^^^^             --------
help: consider borrowing here
   |
37 |         generic_function(&5i32);
   |                          +

Backtrace

error: internal compiler error: /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/compiler/rustc_const_eval/src/interpret/operand.rs:84:42: Got a scalar pair where a scalar was expected

thread 'rustc' panicked at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/compiler/rustc_const_eval/src/interpret/operand.rs:84:42:
Box<dyn Any>
stack backtrace:
   0:     0x7f57f8acb12a - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h5b6bd5631a6d1f6b
   1:     0x7f57f92ac8f8 - core::fmt::write::h7550c97b06c86515
   2:     0x7f57fa4e3b91 - std::io::Write::write_fmt::h7b09c64fe0be9c84
   3:     0x7f57f8acaf82 - std::sys::backtrace::BacktraceLock::print::h2395ccd2c84ba3aa
   4:     0x7f57f8acd456 - std::panicking::default_hook::{{closure}}::he19d4c7230e07961
   5:     0x7f57f8acd2a0 - std::panicking::default_hook::hf614597d3c67bbdb
   6:     0x7f57f7b8f556 - std[c6eb78587944e35c]::panicking::update_hook::<alloc[148a978a4a62f5d]::boxed::Box<rustc_driver_impl[4c2d2ad79fb810ac]::install_ice_hook::{closure#0}>>::{closure#0}
   7:     0x7f57f8acdb68 - std::panicking::rust_panic_with_hook::h8942133a8b252070
   8:     0x7f57f7bc6371 - std[c6eb78587944e35c]::panicking::begin_panic::<rustc_errors[7f4c80274b6ccf5]::ExplicitBug>::{closure#0}
   9:     0x7f57f7bb9976 - std[c6eb78587944e35c]::sys::backtrace::__rust_end_short_backtrace::<std[c6eb78587944e35c]::panicking::begin_panic<rustc_errors[7f4c80274b6ccf5]::ExplicitBug>::{closure#0}, !>
  10:     0x7f57f7bb9933 - std[c6eb78587944e35c]::panicking::begin_panic::<rustc_errors[7f4c80274b6ccf5]::ExplicitBug>
  11:     0x7f57f7bcfa31 - <rustc_errors[7f4c80274b6ccf5]::diagnostic::BugAbort as rustc_errors[7f4c80274b6ccf5]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x7f57f81f9e74 - rustc_middle[a886f61dbc61428a]::util::bug::opt_span_bug_fmt::<rustc_span[3e5cf3424d44936d]::span_encoding::Span>::{closure#0}
  13:     0x7f57f81dff6a - rustc_middle[a886f61dbc61428a]::ty::context::tls::with_opt::<rustc_middle[a886f61dbc61428a]::util::bug::opt_span_bug_fmt<rustc_span[3e5cf3424d44936d]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  14:     0x7f57f81dfdfb - rustc_middle[a886f61dbc61428a]::ty::context::tls::with_context_opt::<rustc_middle[a886f61dbc61428a]::ty::context::tls::with_opt<rustc_middle[a886f61dbc61428a]::util::bug::opt_span_bug_fmt<rustc_span[3e5cf3424d44936d]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  15:     0x7f57f6b0e9a0 - rustc_middle[a886f61dbc61428a]::util::bug::bug_fmt
  16:     0x7f57fab8f291 - <rustc_mir_transform[b36c87ceb4bb9a8e]::gvn::VnState>::simplify_operand.cold
  17:     0x7f57f6bcb95b - <rustc_mir_transform[b36c87ceb4bb9a8e]::gvn::GVN as rustc_mir_transform[b36c87ceb4bb9a8e]::pass_manager::MirPass>::run_pass
  18:     0x7f57f92963dd - rustc_mir_transform[b36c87ceb4bb9a8e]::pass_manager::run_passes_inner
  19:     0x7f57f97959fa - rustc_mir_transform[b36c87ceb4bb9a8e]::optimized_mir
  20:     0x7f57f9793369 - rustc_query_impl[db795c774d495014]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[db795c774d495014]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 8usize]>>
  21:     0x7f57f92afb2d - rustc_query_system[b2bb6e43dd6b7fda]::query::plumbing::try_execute_query::<rustc_query_impl[db795c774d495014]::DynamicConfig<rustc_query_system[b2bb6e43dd6b7fda]::query::caches::DefIdCache<rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[db795c774d495014]::plumbing::QueryCtxt, false>
  22:     0x7f57f92af1b3 - rustc_query_impl[db795c774d495014]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
  23:     0x7f57f9d5939f - rustc_middle[a886f61dbc61428a]::query::plumbing::query_get_at::<rustc_query_system[b2bb6e43dd6b7fda]::query::caches::DefIdCache<rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 8usize]>>>
  24:     0x7f57f81f98b5 - <rustc_middle[a886f61dbc61428a]::ty::context::TyCtxt>::coroutine_layout
  25:     0x7f57f98fec28 - rustc_ty_utils[45bf0f8bee683616]::layout::layout_of_uncached
  26:     0x7f57f98efa46 - rustc_ty_utils[45bf0f8bee683616]::layout::layout_of
  27:     0x7f57f98ef9d1 - rustc_query_impl[db795c774d495014]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[db795c774d495014]::query_impl::layout_of::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 16usize]>>
  28:     0x7f57f98ef0b2 - rustc_query_system[b2bb6e43dd6b7fda]::query::plumbing::try_execute_query::<rustc_query_impl[db795c774d495014]::DynamicConfig<rustc_query_system[b2bb6e43dd6b7fda]::query::caches::DefaultCache<rustc_middle[a886f61dbc61428a]::ty::ParamEnvAnd<rustc_middle[a886f61dbc61428a]::ty::Ty>, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 16usize]>>, false, true, false>, rustc_query_impl[db795c774d495014]::plumbing::QueryCtxt, false>
  29:     0x7f57f98eeda9 - rustc_query_impl[db795c774d495014]::query_impl::layout_of::get_query_non_incr::__rust_end_short_backtrace
  30:     0x7f57f689e0da - <rustc_mir_transform[b36c87ceb4bb9a8e]::known_panics_lint::KnownPanicsLint as rustc_mir_transform[b36c87ceb4bb9a8e]::pass_manager::MirLint>::run_lint
  31:     0x7f57f92916b9 - rustc_mir_transform[b36c87ceb4bb9a8e]::run_analysis_to_runtime_passes
  32:     0x7f57f94812de - rustc_mir_transform[b36c87ceb4bb9a8e]::mir_drops_elaborated_and_const_checked
  33:     0x7f57f9480cfd - rustc_query_impl[db795c774d495014]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[db795c774d495014]::query_impl::mir_drops_elaborated_and_const_checked::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 8usize]>>
  34:     0x7f57f957ee68 - rustc_query_system[b2bb6e43dd6b7fda]::query::plumbing::try_execute_query::<rustc_query_impl[db795c774d495014]::DynamicConfig<rustc_query_system[b2bb6e43dd6b7fda]::query::caches::VecCache<rustc_span[3e5cf3424d44936d]::def_id::LocalDefId, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[db795c774d495014]::plumbing::QueryCtxt, false>
  35:     0x7f57f957e6cd - rustc_query_impl[db795c774d495014]::query_impl::mir_drops_elaborated_and_const_checked::get_query_non_incr::__rust_end_short_backtrace
  36:     0x7f57f9b7b705 - rustc_interface[88a02114bbdb2383]::passes::run_required_analyses
  37:     0x7f57f9b722e5 - rustc_interface[88a02114bbdb2383]::passes::analysis
  38:     0x7f57f9b722c9 - rustc_query_impl[db795c774d495014]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[db795c774d495014]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 1usize]>>
  39:     0x7f57fa07d662 - rustc_query_system[b2bb6e43dd6b7fda]::query::plumbing::try_execute_query::<rustc_query_impl[db795c774d495014]::DynamicConfig<rustc_query_system[b2bb6e43dd6b7fda]::query::caches::SingleCache<rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[db795c774d495014]::plumbing::QueryCtxt, false>
  40:     0x7f57fa07d38f - rustc_query_impl[db795c774d495014]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  41:     0x7f57f9fed0bb - rustc_interface[88a02114bbdb2383]::interface::run_compiler::<core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>, rustc_driver_impl[4c2d2ad79fb810ac]::run_compiler::{closure#0}>::{closure#1}
  42:     0x7f57f9fde3d9 - std[c6eb78587944e35c]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[88a02114bbdb2383]::util::run_in_thread_with_globals<rustc_interface[88a02114bbdb2383]::interface::run_compiler<core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>, rustc_driver_impl[4c2d2ad79fb810ac]::run_compiler::{closure#0}>::{closure#1}, core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>>
  43:     0x7f57fa0adfac - <<std[c6eb78587944e35c]::thread::Builder>::spawn_unchecked_<rustc_interface[88a02114bbdb2383]::util::run_in_thread_with_globals<rustc_interface[88a02114bbdb2383]::interface::run_compiler<core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>, rustc_driver_impl[4c2d2ad79fb810ac]::run_compiler::{closure#0}>::{closure#1}, core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>>::{closure#1} as core[c06ff78fa456ca03]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  44:     0x7f57fa0aea6b - std::sys::pal::unix::thread::Thread::new::thread_start::hcc78f3943333fa94
  45:     0x7f57f4649609 - start_thread
                               at /build/glibc-LcI20x/glibc-2.31/nptl/pthread_create.c:477:8
  46:     0x7f57f456e353 - clone
                               at /build/glibc-LcI20x/glibc-2.31/misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:95
  47:                0x0 - <unknown>

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.83.0 (90b35a623 2024-11-26) running on x86_64-unknown-linux-gnu

note: compiler flags: -C opt-level=3

query stack during panic:
#0 [optimized_mir] optimizing MIR for `return_str::{closure#0}`
#1 [layout_of] computing layout of `{async fn body of return_str()}`
end of query stack
error: aborting due to 18 previous errors

Some errors have detailed explanations: E0107, E0277, E0308, E0412, E0600.
For more information about an error, try `rustc --explain E0107`.

Anything else?

Note that the bug can only be reproduced by rustc -C opt-level=3 --edition=2021 1.rs .
If we remove -C opt-level=3, the ICE disappears.

@wangbo15 wangbo15 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 Jan 5, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 5, 2025
@wangbo15 wangbo15 changed the title [ICE]: Both stable and nightly crashes on the invalid code under opt-level=3 [ICE]: Both stable and nightly crashes at compiler/rustc_const_eval/src/interpret/operand.rs:84:42 on the invalid code under opt-level=3 Jan 5, 2025
@wangbo15 wangbo15 changed the title [ICE]: Both stable and nightly crashes at compiler/rustc_const_eval/src/interpret/operand.rs:84:42 on the invalid code under opt-level=3 [ICE]: Both stable and nightly crashes at compiler/rustc_const_eval/src/interpret/operand.rs:84:42 on the invalid code under opt-level=3 Jan 5, 2025
@wangbo15 wangbo15 changed the title [ICE]: Both stable and nightly crashes at compiler/rustc_const_eval/src/interpret/operand.rs:84:42 on the invalid code under opt-level=3 [ICE]: Both stable and nightly crash at compiler/rustc_const_eval/src/interpret/operand.rs:84:42 on the invalid code under opt-level=3 Jan 5, 2025
@jieyouxu jieyouxu added E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) labels Jan 5, 2025
@jieyouxu
Copy link
Member

jieyouxu commented Jan 5, 2025

@wangbo15 are these ICEs fuzzer-generated?

@lqd
Copy link
Member

lqd commented Jan 5, 2025

👨 🦋 Is this minimized?

@wangbo15
Copy link
Author

wangbo15 commented Jan 5, 2025

@jieyouxu Yes, I apologize for trying to minimize it, but it is still oversized.

@jieyouxu
Copy link
Member

jieyouxu commented Jan 5, 2025

Please refer to fuzzing guidelines, in particular please disclose that the ICE is generated through fuzzing.

@jieyouxu
Copy link
Member

jieyouxu commented Jan 5, 2025

Also, -Copt-level=3 is not needed, -Copt-level=1 is sufficient to ICE stable rustc (1.83.0). Please try the minimum opt-level as well since this is fuzzed.

@wangbo15
Copy link
Author

wangbo15 commented Jan 5, 2025

@jieyouxu Thanks. I would minimize it and commit it later.

@matthiaskrgr
Copy link
Member

Curious to see if this will turn out to be a duplicate of #121363

@jieyouxu
Copy link
Member

jieyouxu commented Jan 5, 2025

Bisection script:

#!/usr/bin/env bash
RUSTC_ICE=0 rustc -Copt=level=1 issue-135128.rs
test ${PIPESTATUS[0]} -eq 101

bisected with

cargo-bisect-rustc --script=./script.sh --regress=success --start=1.76.0 --end=1.79.0
********************************************************************************
Regression in nightly-2024-02-15
********************************************************************************

fetching https://static.rust-lang.org/dist/2024-02-14/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2024-02-14: 40 B / 40 B [==========================================================================] 100.00 % 1012.93 KB/s converted 2024-02-14 to a84bb95a1f65bfe25038f188763a18e096a86ab2
fetching https://static.rust-lang.org/dist/2024-02-15/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2024-02-15: 40 B / 40 B [=============================================================================] 100.00 % 1.27 MB/s converted 2024-02-15 to ee9c7c940c07d8b67c9a6b2ec930db70dcd23a46
looking for regression commit between 2024-02-14 and 2024-02-15
fetching (via remote github) commits from max(a84bb95a1f65bfe25038f188763a18e096a86ab2, 2024-02-12) to ee9c7c940c07d8b67c9a6b2ec930db70dcd23a46
ending github query because we found starting sha: a84bb95a1f65bfe25038f188763a18e096a86ab2
get_commits_between returning commits, len: 9
  commit[0] 2024-02-13: Auto merge of #121036 - matthiaskrgr:rollup-ul05q8e, r=matthiaskrgr
  commit[1] 2024-02-14: Auto merge of #120942 - compiler-errors:deep-assoc-hang, r=lcnr
  commit[2] 2024-02-14: Auto merge of #121055 - matthiaskrgr:rollup-bzn5sda, r=matthiaskrgr
  commit[3] 2024-02-14: Auto merge of #120454 - clubby789:cargo-update, r=Nilstrieb
  commit[4] 2024-02-14: Auto merge of #121018 - oli-obk:impl_unsafety, r=TaKO8Ki
  commit[5] 2024-02-14: Auto merge of #100603 - tmandry:zst-guards, r=dtolnay
  commit[6] 2024-02-14: Auto merge of #121078 - oli-obk:rollup-p11zsav, r=oli-obk
  commit[7] 2024-02-14: Auto merge of #121086 - GuillaumeGomez:rollup-y82fs9y, r=GuillaumeGomez
  commit[8] 2024-02-14: Auto merge of #120847 - oli-obk:track_errors9, r=compiler-errors
ERROR: no CI builds available between a84bb95a1f65bfe25038f188763a18e096a86ab2 and ee9c7c940c07d8b67c9a6b2ec930db70dcd23a46 within last 167 days

@jieyouxu
Copy link
Member

jieyouxu commented Jan 5, 2025

This example has a bunch of unrelated errors that makes it very hard to tell, but maybe #120847 exposes stuff that const-eval doesn't handle? Will need a minimization to have any clear ideas anyway.

@wangbo15
Copy link
Author

wangbo15 commented Jan 5, 2025

@jieyouxu Here is a shorten version:

pub trait Foo {
fn test(&self);
}
async fn return_str() -> str
where
str: Sized,
{
*"Sized".to_string().into_boxed_str()
}
async fn global_hr<'g>(x: &'g fn(&()))
where
fn(&()): Foo,
{
x.test();
}
fn main() {}

@jieyouxu
Copy link
Member

jieyouxu commented Jan 5, 2025

MCVE (I confirm this preserves the bisection range locally)

async fn return_str() -> str
where
    str: Sized,
{
    *"Sized".to_string().into_boxed_str()
}
fn main() {}

@jieyouxu jieyouxu added S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 5, 2025
@jieyouxu
Copy link
Member

jieyouxu commented Jan 5, 2025

Curious to see if this will turn out to be a duplicate of #121363

The shape looks similar but bisection range does not overlap.

@lukas-code
Copy link
Member

lukas-code commented Jan 5, 2025

We have this code to replace all MIR of bodies with unsatisfiable trait clauses (like str: Sized) with a single unreachable terminator before MIR optimizations are run:

// Check if it's even possible to satisfy the 'where' clauses
// for this item.
//
// This branch will never be taken for any normal function.
// However, it's possible to `#!feature(trivial_bounds)]` to write
// a function with impossible to satisfy clauses, e.g.:
// `fn foo() where String: Copy {}`
//
// We don't usually need to worry about this kind of case,
// since we would get a compilation error if the user tried
// to call it. However, since we optimize even without any
// calls to the function, we need to make sure that it even
// makes sense to try to evaluate the body.
//
// If there are unsatisfiable where clauses, then all bets are
// off, and we just give up.
//
// We manually filter the predicates, skipping anything that's not
// "global". We are in a potentially generic context
// (e.g. we are evaluating a function without instantiating generic
// parameters, so this filtering serves two purposes:
//
// 1. We skip evaluating any predicates that we would
// never be able prove are unsatisfiable (e.g. `<T as Foo>`
// 2. We avoid trying to normalize predicates involving generic
// parameters (e.g. `<T as Foo>::MyItem`). This can confuse
// the normalization code (leading to cycle errors), since
// it's usually never invoked in this way.
let predicates = tcx
.predicates_of(body.source.def_id())
.predicates
.iter()
.filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None });
if traits::impossible_predicates(tcx, traits::elaborate(tcx, predicates).collect()) {
trace!("found unsatisfiable predicates for {:?}", body.source);
// Clear the body to only contain a single `unreachable` statement.
let bbs = body.basic_blocks.as_mut();
bbs.raw.truncate(1);
bbs[START_BLOCK].statements.clear();
bbs[START_BLOCK].terminator_mut().kind = TerminatorKind::Unreachable;
body.var_debug_info.clear();
body.local_decls.raw.truncate(body.arg_count + 1);
}

This is done to avoid ICEs from trying to evaluate the MIR in the of bodies unsatisfiable predicates, but the code currently only checks the own predicates of the MIR body owner itself and not at the predicates of it's parent.

So for an async fn, the MIR of the outer function is replaced with unreachable, but not the MIR of the inner coroutine (async block), and then attempting to optimize the MIR of the coroutine will ICE:

fn return_str() -> impl core::future::Future<Output = str>
where
    str: Sized,
{ // the body of this function is replaced with `unreachable`
    async { // but not the body of this coroutine!
        *"Sized".to_string().into_boxed_str()
    }
}

#121363 does indeed look very similar, there we also have MIR body (the fn fmt method) whose parent (the impl block) has an unsatisfiable predicate:

struct TwoStrs(str, str) where str: Sized;
impl ::core::fmt::Debug for TwoStrs where str: Sized {
                                 // ^^^^^^^^^^^^^^^^ unsatisfiable parent predicate
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { // < optimizing this body's MIR ICEs
        ::core::fmt::Formatter::debug_tuple_field2_finish(f, "TwoStrs",
            &self.0, &&self.1)
    }
}

@jieyouxu jieyouxu removed the E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc label Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue 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

6 participants