Skip to content

ICE: opaque_types_defined_by not defined for synthetic mir body #141466

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

Closed
matthiaskrgr opened this issue May 23, 2025 · 4 comments · Fixed by #141584
Closed

ICE: opaque_types_defined_by not defined for synthetic mir body #141466

matthiaskrgr opened this issue May 23, 2025 · 4 comments · Fixed by #141584
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

Code

//@ compile-flags: --edition=2021 --crate-type lib
fn closure() -> impl AsyncFn() {
    async || {
        use std::ptr::null;
        use std::task::{RawWaker, RawWakerVTable};
        RawWaker::new(
            null(),
            &RawWakerVTable::new(|_| todo!(), |_| (), |_| (), |_| ()),
        );
    }
}

Meta

rustc --version --verbose:

rustc 1.89.0-nightly (e88e85463 2025-05-23)
binary: rustc
commit-hash: e88e85463468ce5d5ce468414eb69e3b15fa8d42
commit-date: 2025-05-23
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Error output

warning: function `closure` is never used
 --> code.rs:1:4
  |
1 | fn closure() -> impl AsyncFn() {
  |    ^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: unused return value of `RawWaker::new` that must be used
 --> code.rs:5:9
  |
5 | /         RawWaker::new(
6 | |             null(),
7 | |             &RawWakerVTable::new(|_| todo!(), |_| (), |_| (), |_| ()),
8 | |         );
  | |_________^
  |
  = note: `#[warn(unused_must_use)]` on by default
help: use `let _ = ...` to ignore the resulting value
  |
5 |         let _ = RawWaker::new(
  |         +++++++
Backtrace

error: internal compiler error: compiler/rustc_ty_utils/src/opaque_types.rs:348:13: `opaque_types_defined_by` not defined for synthetic mir body `DefId(0:15 ~ code[6e65]::closure::{closure#0}::{synthetic#0})`
 --> code.rs:2:14
  |
2 |       async || {
  |  ______________^
3 | |         use std::ptr::null;
4 | |         use std::task::{RawWaker, RawWakerVTable};
5 | |         RawWaker::new(
... |
8 | |         );
9 | |     }
  | |_____^


thread 'rustc' panicked at compiler/rustc_ty_utils/src/opaque_types.rs:348:13:
Box<dyn Any>
stack backtrace:
   0:     0x7e50f756e073 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h03779a6f0d0528fb
   1:     0x7e50f7c05ab7 - core::fmt::write::h37188b143c8299db
   2:     0x7e50f9066fd1 - std::io::Write::write_fmt::hf33b730412790e29
   3:     0x7e50f756ded2 - std::sys::backtrace::BacktraceLock::print::hbc62909c49e926d5
   4:     0x7e50f7571aca - std::panicking::default_hook::{{closure}}::h30d9b30f64bce3ec
   5:     0x7e50f757164f - std::panicking::default_hook::hc1d74f335a317740
   6:     0x7e50f659a363 - std[72f0f555f0ececc7]::panicking::update_hook::<alloc[3d9b53f579cadf77]::boxed::Box<rustc_driver_impl[52f955853b1bdd09]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x7e50f7572343 - std::panicking::rust_panic_with_hook::hd85f4b59c2a8b76a
   8:     0x7e50f65d61b1 - std[72f0f555f0ececc7]::panicking::begin_panic::<rustc_errors[58cd91a829d19a54]::ExplicitBug>::{closure#0}
   9:     0x7e50f65ca1e6 - std[72f0f555f0ececc7]::sys::backtrace::__rust_end_short_backtrace::<std[72f0f555f0ececc7]::panicking::begin_panic<rustc_errors[58cd91a829d19a54]::ExplicitBug>::{closure#0}, !>
  10:     0x7e50f65ca077 - std[72f0f555f0ececc7]::panicking::begin_panic::<rustc_errors[58cd91a829d19a54]::ExplicitBug>
  11:     0x7e50f65e0921 - <rustc_errors[58cd91a829d19a54]::diagnostic::BugAbort as rustc_errors[58cd91a829d19a54]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x7e50f6baff1c - <rustc_errors[58cd91a829d19a54]::DiagCtxtHandle>::span_bug::<rustc_span[5d0591bc0b087f3b]::span_encoding::Span, alloc[3d9b53f579cadf77]::string::String>
  13:     0x7e50f6c4b3a7 - rustc_middle[c34ff90c6f2112aa]::util::bug::opt_span_bug_fmt::<rustc_span[5d0591bc0b087f3b]::span_encoding::Span>::{closure#0}
  14:     0x7e50f6c25dda - rustc_middle[c34ff90c6f2112aa]::ty::context::tls::with_opt::<rustc_middle[c34ff90c6f2112aa]::util::bug::opt_span_bug_fmt<rustc_span[5d0591bc0b087f3b]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  15:     0x7e50f6c25c4b - rustc_middle[c34ff90c6f2112aa]::ty::context::tls::with_context_opt::<rustc_middle[c34ff90c6f2112aa]::ty::context::tls::with_opt<rustc_middle[c34ff90c6f2112aa]::util::bug::opt_span_bug_fmt<rustc_span[5d0591bc0b087f3b]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  16:     0x7e50f56e1b37 - rustc_middle[c34ff90c6f2112aa]::util::bug::span_bug_fmt::<rustc_span[5d0591bc0b087f3b]::span_encoding::Span>
  17:     0x7e50f80f830f - rustc_ty_utils[906cc961ec565acf]::opaque_types::opaque_types_defined_by
  18:     0x7e50f80f7b9b - rustc_query_impl[a210f1cfd61f5ef1]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a210f1cfd61f5ef1]::query_impl::opaque_types_defined_by::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c34ff90c6f2112aa]::query::erase::Erased<[u8; 8usize]>>
  19:     0x7e50f7dd7b66 - rustc_query_system[26848a8fb0a689fb]::query::plumbing::try_execute_query::<rustc_query_impl[a210f1cfd61f5ef1]::DynamicConfig<rustc_data_structures[b478411c3a8316d9]::vec_cache::VecCache<rustc_span[5d0591bc0b087f3b]::def_id::LocalDefId, rustc_middle[c34ff90c6f2112aa]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[26848a8fb0a689fb]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[a210f1cfd61f5ef1]::plumbing::QueryCtxt, false>
  20:     0x7e50f7dd7752 - rustc_query_impl[a210f1cfd61f5ef1]::query_impl::opaque_types_defined_by::get_query_non_incr::__rust_end_short_backtrace
  21:     0x7e50f99d2f1b - <rustc_middle[c34ff90c6f2112aa]::ty::context::TyCtxt as rustc_type_ir[eb6680b263ba7d43]::interner::Interner>::opaque_types_defined_by.cold
  22:     0x7e50f7c28753 - <rustc_const_eval[c28c0ff2b603dd2d]::check_consts::qualifs::HasMutInterior as rustc_const_eval[c28c0ff2b603dd2d]::check_consts::qualifs::Qualif>::in_any_value_of_ty
  23:     0x7e50f8508120 - <alloc[3d9b53f579cadf77]::vec::Vec<rustc_mir_transform[a6343ba8374f461]::promote_consts::Candidate>>::retain::<rustc_mir_transform[a6343ba8374f461]::promote_consts::validate_candidates::{closure#0}>::{closure#0}
  24:     0x7e50f514886a - <rustc_mir_transform[a6343ba8374f461]::promote_consts::PromoteTemps as rustc_mir_transform[a6343ba8374f461]::pass_manager::MirPass>::run_pass
  25:     0x7e50f51bb7ee - rustc_mir_transform[a6343ba8374f461]::mir_promoted
  26:     0x7e50f7fdcb12 - rustc_query_impl[a210f1cfd61f5ef1]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a210f1cfd61f5ef1]::query_impl::mir_promoted::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c34ff90c6f2112aa]::query::erase::Erased<[u8; 16usize]>>
  27:     0x7e50f7fdcdb6 - rustc_query_system[26848a8fb0a689fb]::query::plumbing::try_execute_query::<rustc_query_impl[a210f1cfd61f5ef1]::DynamicConfig<rustc_data_structures[b478411c3a8316d9]::vec_cache::VecCache<rustc_span[5d0591bc0b087f3b]::def_id::LocalDefId, rustc_middle[c34ff90c6f2112aa]::query::erase::Erased<[u8; 16usize]>, rustc_query_system[26848a8fb0a689fb]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[a210f1cfd61f5ef1]::plumbing::QueryCtxt, false>
  28:     0x7e50f7fdc952 - rustc_query_impl[a210f1cfd61f5ef1]::query_impl::mir_promoted::get_query_non_incr::__rust_end_short_backtrace
  29:     0x7e50f6e42eed - rustc_mir_transform[a6343ba8374f461]::coroutine::mir_coroutine_witnesses
  30:     0x7e50f704ba7f - rustc_query_impl[a210f1cfd61f5ef1]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a210f1cfd61f5ef1]::query_impl::mir_coroutine_witnesses::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c34ff90c6f2112aa]::query::erase::Erased<[u8; 8usize]>>
  31:     0x7e50f809603f - rustc_query_system[26848a8fb0a689fb]::query::plumbing::try_execute_query::<rustc_query_impl[a210f1cfd61f5ef1]::DynamicConfig<rustc_query_system[26848a8fb0a689fb]::query::caches::DefIdCache<rustc_middle[c34ff90c6f2112aa]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[a210f1cfd61f5ef1]::plumbing::QueryCtxt, false>
  32:     0x7e50f7054bb8 - rustc_query_impl[a210f1cfd61f5ef1]::query_impl::mir_coroutine_witnesses::get_query_non_incr::__rust_end_short_backtrace
  33:     0x7e50f7dc5e39 - rustc_mir_transform[a6343ba8374f461]::mir_drops_elaborated_and_const_checked
  34:     0x7e50f7dc5119 - rustc_query_impl[a210f1cfd61f5ef1]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a210f1cfd61f5ef1]::query_impl::mir_drops_elaborated_and_const_checked::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c34ff90c6f2112aa]::query::erase::Erased<[u8; 8usize]>>
  35:     0x7e50f7dd7b66 - rustc_query_system[26848a8fb0a689fb]::query::plumbing::try_execute_query::<rustc_query_impl[a210f1cfd61f5ef1]::DynamicConfig<rustc_data_structures[b478411c3a8316d9]::vec_cache::VecCache<rustc_span[5d0591bc0b087f3b]::def_id::LocalDefId, rustc_middle[c34ff90c6f2112aa]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[26848a8fb0a689fb]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[a210f1cfd61f5ef1]::plumbing::QueryCtxt, false>
  36:     0x7e50f7dd69d2 - rustc_query_impl[a210f1cfd61f5ef1]::query_impl::mir_drops_elaborated_and_const_checked::get_query_non_incr::__rust_end_short_backtrace
  37:     0x7e50f86d8e51 - rustc_mir_transform[a6343ba8374f461]::optimized_mir
  38:     0x7e50f86d87bb - rustc_query_impl[a210f1cfd61f5ef1]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a210f1cfd61f5ef1]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c34ff90c6f2112aa]::query::erase::Erased<[u8; 8usize]>>
  39:     0x7e50f809603f - rustc_query_system[26848a8fb0a689fb]::query::plumbing::try_execute_query::<rustc_query_impl[a210f1cfd61f5ef1]::DynamicConfig<rustc_query_system[26848a8fb0a689fb]::query::caches::DefIdCache<rustc_middle[c34ff90c6f2112aa]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[a210f1cfd61f5ef1]::plumbing::QueryCtxt, false>
  40:     0x7e50f8095462 - rustc_query_impl[a210f1cfd61f5ef1]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
  41:     0x7e50f817e6d7 - <rustc_metadata[b94d7ace3b1283ef]::rmeta::encoder::EncodeContext>::encode_crate_root
  42:     0x7e50f8bb9994 - rustc_metadata[b94d7ace3b1283ef]::rmeta::encoder::encode_metadata
  43:     0x7e50f89fba73 - rustc_metadata[b94d7ace3b1283ef]::fs::encode_and_write_metadata
  44:     0x7e50f8d51444 - <rustc_interface[683f45d0a04ac5b6]::queries::Linker>::codegen_and_build_linker
  45:     0x7e50f8d4f0b8 - rustc_interface[683f45d0a04ac5b6]::passes::create_and_enter_global_ctxt::<core[5de45699eb5b5ad3]::option::Option<rustc_interface[683f45d0a04ac5b6]::queries::Linker>, rustc_driver_impl[52f955853b1bdd09]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  46:     0x7e50f8caa28f - rustc_interface[683f45d0a04ac5b6]::interface::run_compiler::<(), rustc_driver_impl[52f955853b1bdd09]::run_compiler::{closure#0}>::{closure#1}
  47:     0x7e50f8c64986 - std[72f0f555f0ececc7]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[683f45d0a04ac5b6]::util::run_in_thread_with_globals<rustc_interface[683f45d0a04ac5b6]::util::run_in_thread_pool_with_globals<rustc_interface[683f45d0a04ac5b6]::interface::run_compiler<(), rustc_driver_impl[52f955853b1bdd09]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  48:     0x7e50f8c6462b - <<std[72f0f555f0ececc7]::thread::Builder>::spawn_unchecked_<rustc_interface[683f45d0a04ac5b6]::util::run_in_thread_with_globals<rustc_interface[683f45d0a04ac5b6]::util::run_in_thread_pool_with_globals<rustc_interface[683f45d0a04ac5b6]::interface::run_compiler<(), rustc_driver_impl[52f955853b1bdd09]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[5de45699eb5b5ad3]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  49:     0x7e50f8c6597d - std::sys::pal::unix::thread::Thread::new::thread_start::hb186a50e9c63d629
  50:     0x7e50f2aa57eb - <unknown>
  51:     0x7e50f2b2918c - <unknown>
  52:                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: please make sure that you have updated to the latest nightly

note: please attach the file at `/home/matthias/vcs/github/CRED/rustc-ice-2025-05-23T19_15_30-3104862.txt` to your bug report

note: compiler flags: --crate-type lib

query stack during panic:
#0 [opaque_types_defined_by] computing the opaque types defined by `closure::{closure#0}::{synthetic#0}`
#1 [mir_promoted] promoting constants in MIR for `closure::{closure#0}::{synthetic#0}`
#2 [mir_coroutine_witnesses] coroutine witness types for `closure::{closure#0}::{synthetic#0}`
#3 [mir_drops_elaborated_and_const_checked] elaborating drops for `closure::{closure#0}::{synthetic#0}`
#4 [optimized_mir] optimizing MIR for `closure::{closure#0}::{synthetic#0}`
end of query stack
error: aborting due to 1 previous error; 2 warnings emitted

@matthiaskrgr matthiaskrgr added 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. C-bug Category: This is a bug. labels May 23, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 23, 2025
@matthiaskrgr matthiaskrgr added regression-untriaged Untriaged performance or correctness regression. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 23, 2025
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label May 23, 2025
@matthiaskrgr
Copy link
Member Author

this compiled previously

seems to bisect to #128440 ?

@cyrgani
Copy link
Contributor

cyrgani commented May 23, 2025

reproduces with other functions too, as long as they are #[rustc_promotable]:

//@ compile-flags: --edition=2021 --crate-type lib
pub fn foo() {
    async || {
        use std::ops::RangeInclusive;
        &RangeInclusive::new((), ());
    };
}

@apiraino
Copy link
Contributor

Assigning priority (discussion on Zulip).

cc @oli-obk for commit 6650252

@rustbot label -I-prioritize +P-high

@rustbot rustbot added P-high High priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels May 26, 2025
@compiler-errors
Copy link
Member

@rustbot label: -P-high +P-medium

This is very hard to hit, so I'm going to reprioritize it as medium. It doesn't really matter anyways, b/c I'll open a fix in a second anyways.

@rustbot rustbot added P-medium Medium priority and removed P-high High priority labels May 26, 2025
jieyouxu added a commit to jieyouxu/rust that referenced this issue May 26, 2025
…ic-body, r=lcnr

Support `opaque_types_defined_by` for `SyntheticCoroutineBody`

We create a synthetic MIR body for the `AsyncFnOnce` impl for async closures. That body goes through all passes that a regular body does, including promotion.

Promotion sometimes requires computing that the type of an rvalue is `Freeze`, which requires computing the typing env of a body. This requires calling `opaque_types_defined_by` on the body's def id, which leads to an ICE today since we don't expect that query to be called for synthetic bodies.

While we could fix this by, for example, computing the typeck root of the body before calling a `TypingEnv` constructor, I think it's appropriate to do a more general fix here since I think it's reasonable that other passes might do analysis too.

Fixes rust-lang#141466

r? `@lcnr` or `@oli-obk`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 27, 2025
…ic-body, r=lcnr

Support `opaque_types_defined_by` for `SyntheticCoroutineBody`

We create a synthetic MIR body for the `AsyncFnOnce` impl for async closures. That body goes through all passes that a regular body does, including promotion.

Promotion sometimes requires computing that the type of an rvalue is `Freeze`, which requires computing the typing env of a body. This requires calling `opaque_types_defined_by` on the body's def id, which leads to an ICE today since we don't expect that query to be called for synthetic bodies.

While we could fix this by, for example, computing the typeck root of the body before calling a `TypingEnv` constructor, I think it's appropriate to do a more general fix here since I think it's reasonable that other passes might do analysis too.

Fixes rust-lang#141466

r? ``@lcnr`` or ``@oli-obk``
@bors bors closed this as completed in 9d46af1 May 27, 2025
rust-timer added a commit that referenced this issue May 27, 2025
Rollup merge of #141584 - compiler-errors:typing-env-synthetic-body, r=lcnr

Support `opaque_types_defined_by` for `SyntheticCoroutineBody`

We create a synthetic MIR body for the `AsyncFnOnce` impl for async closures. That body goes through all passes that a regular body does, including promotion.

Promotion sometimes requires computing that the type of an rvalue is `Freeze`, which requires computing the typing env of a body. This requires calling `opaque_types_defined_by` on the body's def id, which leads to an ICE today since we don't expect that query to be called for synthetic bodies.

While we could fix this by, for example, computing the typeck root of the body before calling a `TypingEnv` constructor, I think it's appropriate to do a more general fix here since I think it's reasonable that other passes might do analysis too.

Fixes #141466

r? ```@lcnr``` or ```@oli-obk```
@apiraino apiraino removed the regression-untriaged Untriaged performance or correctness regression. label May 28, 2025
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) ❄️ P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants