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: Expected type for ... but found Lifetime(ReErased) when substituting #119064

Closed
saethlin opened this issue Dec 18, 2023 · 2 comments · Fixed by #122168
Closed

ICE: Expected type for ... but found Lifetime(ReErased) when substituting #119064

saethlin opened this issue Dec 18, 2023 · 2 comments · Fixed by #122168
Assignees
Labels
A-mir-opt-inlining Area: MIR inlining 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

@saethlin
Copy link
Member

https://github.com/oxidecomputer/cancel-safe-futures on commit 857ddd3a30c5f82e6edb72ac7c8a471860501d9f

RUSTFLAGS="-Zinline-mir -Zcross-crate-inline-threshold=always -Zinline-mir-hint-threshold=10000 -Zinline-mir-threshold=10000" cargo +nightly test --target=x86_64-unknown-linux-gnu

If you don't set a --target you will OOM while optimizing syn with these very silly flags.

error: internal compiler error: compiler/rustc_middle/src/ty/generic_args.rs:885:9: expected type for `T/#0` (T/#0/0) but found Lifetime(ReErased) when substituting
error: internal compiler error: compiler/rustc_middle/src/ty/generic_args.rs:885:9: expected type for `T/#0` (T/#0/0) but found Lifetime(ReErased) when substituting

I also have another example of this that produces a "but found Const` ICE. They look similar, I'll post the other one later if someone wants.

rustc-ice-2023-12-18T05_32_24-344139.txt

@saethlin saethlin 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 Dec 18, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 18, 2023
@saethlin saethlin added A-mir-opt-inlining Area: MIR inlining and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 18, 2023
@compiler-errors compiler-errors added the E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example label Dec 18, 2023
@saethlin
Copy link
Member Author

I've been able to minimize to this:

[package]
name = "cancel-safe-futures"
edition = "2021"

[dependencies]
futures-core = { version = "0.3.28" }
futures-util = { version = "0.3.28" }
tokio = { version = "1.28.2", features = ["macros", "sync"] }
futures-lite = "2.1.0"
use futures_core::future::BoxFuture;
use futures_util::FutureExt;
use tokio::sync::MutexGuard;
use futures_lite::future::block_on;

async fn run() {
    let m = tokio::sync::Mutex::new(());
    let _ = async move {
        let permit = ActionPermit { guard: m.lock().await };
        cancel_perform_async_boxed(permit).await;
    }
    .await;
}

fn main() {
    block_on(run())
}

async fn cancel_perform_async_boxed<T>(permit: ActionPermit<'_, T>) {
    let fut = permit.perform_async_boxed(|_| async move {}.boxed());
    tokio::select! {
        _ = fut => {}
    }
}

struct ActionPermit<'a, T> {
    guard: MutexGuard<'a, T>,
}

impl<'a, T> ActionPermit<'a, T> {
    async fn perform_async_boxed<R, F>(mut self, action: F) -> R
    where
        F: for<'lock> FnOnce(&'lock mut T) -> BoxFuture<'lock, R>,
    {
        action(&mut *self.guard).await
    }
}

@kpreid
Copy link
Contributor

kpreid commented Dec 19, 2023

Here is a further minimized core-only repro:

pub async fn run(permit: ActionPermit<'_, ()>, ctx: &mut core::task::Context<'_>) {
    run2(permit, ctx);
}

fn run2<T>(permit: ActionPermit<'_, T>, ctx: &mut core::task::Context) {
    _ = || {
        let mut fut = ActionPermit::perform(permit);
        let fut = unsafe { core::pin::Pin::new_unchecked(&mut fut) };
        _ = core::future::Future::poll(fut, ctx);
    };
}

pub struct ActionPermit<'a, T> {
    _guard: core::cell::Ref<'a, T>,
}

impl<'a, T> ActionPermit<'a, T> {
    async fn perform(self) {
        core::future::ready(()).await
    }
}

The poll came out of the innards of tokio::select!. The use of core::cell::Ref is not critical, but the use of a type with both a lifetime and a type generic is; for example, std::borrow::Cow will do too. Tested with the above RUSTFLAGS, under rustc 1.76.0-nightly (d86d65bbc 2023-12-10) x86_64-apple-darwin.

error: internal compiler error: compiler/rustc_middle/src/ty/generic_args.rs:885:9: expected type for `T/#0` (T/#0/0) but found Lifetime(ReErased) when substituting, args=[ReErased, T/#0, std::future::ResumeTy, (), (), CoroutineWitness(DefId(0:16 ~ scratchpad[7815]::{impl#0}::perform::{closure#0}), [ReErased, T/#0]), (ActionPermit<ReErased, T/#0>,)]

Backtrace
thread 'rustc' panicked at /rustc/d86d65bbc19b928387f68427fcc3a0da498d8a19/compiler/rustc_errors/src/lib.rs:1113:75:
Box<dyn Any>
stack backtrace:
   0:        0x1039210eb - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h5e659123d19f3605
   1:        0x10396d733 - core::fmt::write::h3553f749a30e8cce
   2:        0x10391757e - std::io::Write::write_fmt::ha0a0cbd22c8ebc52
   3:        0x103920ec9 - std::sys_common::backtrace::print::ha58ecd228ca176e9
   4:        0x103924095 - std::panicking::default_hook::{{closure}}::h17c3184394eb8856
   5:        0x103923e14 - std::panicking::default_hook::h2fbacdca4604565a
   6:        0x11137a41e - <alloc[67ea17a77aaf9eaf]::boxed::Box<rustc_driver_impl[36417fddfe0df6e]::install_ice_hook::{closure#0}> as core[2238ef0cf162900b]::ops::function::Fn<(&dyn for<'a, 'b> core[2238ef0cf162900b]::ops::function::Fn<(&'a core[2238ef0cf162900b]::panic::panic_info::PanicInfo<'b>,), Output = ()> + core[2238ef0cf162900b]::marker::Sync + core[2238ef0cf162900b]::marker::Send, &core[2238ef0cf162900b]::panic::panic_info::PanicInfo)>>::call
   7:        0x103924735 - std::panicking::rust_panic_with_hook::h563bb071cac0df33
   8:        0x11141140a - std[d8ec8f7e4f9a1335]::panicking::begin_panic::<rustc_errors[42dabb35cd23131d]::ExplicitBug>::{closure#0}
   9:        0x11140f3d9 - std[d8ec8f7e4f9a1335]::sys_common::backtrace::__rust_end_short_backtrace::<std[d8ec8f7e4f9a1335]::panicking::begin_panic<rustc_errors[42dabb35cd23131d]::ExplicitBug>::{closure#0}, !>
  10:        0x115b41e19 - std[d8ec8f7e4f9a1335]::panicking::begin_panic::<rustc_errors[42dabb35cd23131d]::ExplicitBug>
  11:        0x1113e265d - <rustc_errors[42dabb35cd23131d]::diagnostic_builder::Bug as rustc_errors[42dabb35cd23131d]::diagnostic_builder::EmissionGuarantee>::diagnostic_builder_emit_producing_guarantee
  12:        0x111fd02fb - <rustc_errors[42dabb35cd23131d]::Handler>::bug::<alloc[67ea17a77aaf9eaf]::string::String>
  13:        0x111ff115b - rustc_middle[99776e18600d2c31]::util::bug::opt_span_bug_fmt::<rustc_span[b6253c3433877e36]::span_encoding::Span>::{closure#0}
  14:        0x111fed147 - rustc_middle[99776e18600d2c31]::ty::context::tls::with_opt::<rustc_middle[99776e18600d2c31]::util::bug::opt_span_bug_fmt<rustc_span[b6253c3433877e36]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  15:        0x111fed102 - rustc_middle[99776e18600d2c31]::ty::context::tls::with_context_opt::<rustc_middle[99776e18600d2c31]::ty::context::tls::with_opt<rustc_middle[99776e18600d2c31]::util::bug::opt_span_bug_fmt<rustc_span[b6253c3433877e36]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  16:        0x115bf058b - rustc_middle[99776e18600d2c31]::util::bug::bug_fmt
  17:        0x115be6df6 - <rustc_middle[99776e18600d2c31]::ty::generic_args::ArgFolder>::type_param_expected
  18:        0x111ef297b - <rustc_middle[99776e18600d2c31]::ty::generic_args::ArgFolder as rustc_type_ir[2a68048d6213fca9]::fold::TypeFolder<rustc_middle[99776e18600d2c31]::ty::context::TyCtxt>>::fold_ty
  19:        0x111ed3075 - <&rustc_middle[99776e18600d2c31]::ty::list::List<rustc_middle[99776e18600d2c31]::ty::generic_args::GenericArg> as rustc_type_ir[2a68048d6213fca9]::fold::TypeFoldable<rustc_middle[99776e18600d2c31]::ty::context::TyCtxt>>::try_fold_with::<rustc_middle[99776e18600d2c31]::ty::generic_args::ArgFolder>
  20:        0x111f4373a - <rustc_middle[99776e18600d2c31]::ty::Ty as rustc_type_ir[2a68048d6213fca9]::fold::TypeSuperFoldable<rustc_middle[99776e18600d2c31]::ty::context::TyCtxt>>::try_super_fold_with::<rustc_middle[99776e18600d2c31]::ty::generic_args::ArgFolder>
  21:        0x11122a936 - <rustc_const_eval[732a1bba9fb60b50]::transform::validate::TypeChecker as rustc_middle[99776e18600d2c31]::mir::visit::Visitor>::visit_projection_elem
  22:        0x11122b82b - <rustc_const_eval[732a1bba9fb60b50]::transform::validate::TypeChecker as rustc_middle[99776e18600d2c31]::mir::visit::Visitor>::visit_place
  23:        0x11122ea58 - <rustc_const_eval[732a1bba9fb60b50]::transform::validate::TypeChecker as rustc_middle[99776e18600d2c31]::mir::visit::Visitor>::visit_statement
  24:        0x111229871 - rustc_const_eval[732a1bba9fb60b50]::transform::validate::validate_types
  25:        0x112323c21 - <rustc_mir_transform[23b75684e486e9bc]::inline::Inliner>::try_inlining
  26:        0x11232294f - <rustc_mir_transform[23b75684e486e9bc]::inline::Inliner>::process_blocks
  27:        0x112322418 - <rustc_mir_transform[23b75684e486e9bc]::inline::Inline as rustc_middle[99776e18600d2c31]::mir::MirPass>::run_pass
  28:        0x1123c3f98 - rustc_mir_transform[23b75684e486e9bc]::pass_manager::run_passes_inner
  29:        0x112331d73 - rustc_mir_transform[23b75684e486e9bc]::optimized_mir
  30:        0x112880081 - rustc_query_impl[52be1802f6aa83c9]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[52be1802f6aa83c9]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[99776e18600d2c31]::query::erase::Erased<[u8; 8usize]>>
  31:        0x1129b2eff - <rustc_query_impl[52be1802f6aa83c9]::query_impl::optimized_mir::dynamic_query::{closure#2} as core[2238ef0cf162900b]::ops::function::FnOnce<(rustc_middle[99776e18600d2c31]::ty::context::TyCtxt, rustc_span[b6253c3433877e36]::def_id::DefId)>>::call_once
  32:        0x1127dc137 - rustc_query_system[48ab03958a5795b4]::query::plumbing::try_execute_query::<rustc_query_impl[52be1802f6aa83c9]::DynamicConfig<rustc_query_system[48ab03958a5795b4]::query::caches::DefaultCache<rustc_span[b6253c3433877e36]::def_id::DefId, rustc_middle[99776e18600d2c31]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[52be1802f6aa83c9]::plumbing::QueryCtxt, true>
  33:        0x112a62db5 - rustc_query_impl[52be1802f6aa83c9]::query_impl::optimized_mir::get_query_incr::__rust_end_short_backtrace
  34:        0x111d96497 - rustc_middle[99776e18600d2c31]::query::plumbing::query_get_at::<rustc_query_system[48ab03958a5795b4]::query::caches::DefaultCache<rustc_span[b6253c3433877e36]::def_id::DefId, rustc_middle[99776e18600d2c31]::query::erase::Erased<[u8; 8usize]>>>
  35:        0x111d9b8b0 - <rustc_metadata[ad3b2a361d891836]::rmeta::encoder::EncodeContext>::encode_crate_root
  36:        0x111da8dc3 - rustc_metadata[ad3b2a361d891836]::rmeta::encoder::encode_metadata
  37:        0x111e412ef - rustc_metadata[ad3b2a361d891836]::fs::encode_and_write_metadata
  38:        0x111c1fa9b - rustc_interface[35319ced0081c4aa]::passes::start_codegen
  39:        0x111bd6bc3 - <rustc_middle[99776e18600d2c31]::ty::context::GlobalCtxt>::enter::<<rustc_interface[35319ced0081c4aa]::queries::Queries>::codegen_and_build_linker::{closure#0}, core[2238ef0cf162900b]::result::Result<rustc_interface[35319ced0081c4aa]::queries::Linker, rustc_span[b6253c3433877e36]::ErrorGuaranteed>>
  40:        0x111bd29a4 - <rustc_interface[35319ced0081c4aa]::queries::Queries>::codegen_and_build_linker
  41:        0x111303f7d - <rustc_interface[35319ced0081c4aa]::interface::Compiler>::enter::<rustc_driver_impl[36417fddfe0df6e]::run_compiler::{closure#0}::{closure#0}, core[2238ef0cf162900b]::result::Result<core[2238ef0cf162900b]::option::Option<rustc_interface[35319ced0081c4aa]::queries::Linker>, rustc_span[b6253c3433877e36]::ErrorGuaranteed>>
  42:        0x11136cfa6 - rustc_span[b6253c3433877e36]::set_source_map::<core[2238ef0cf162900b]::result::Result<(), rustc_span[b6253c3433877e36]::ErrorGuaranteed>, rustc_interface[35319ced0081c4aa]::interface::run_compiler<core[2238ef0cf162900b]::result::Result<(), rustc_span[b6253c3433877e36]::ErrorGuaranteed>, rustc_driver_impl[36417fddfe0df6e]::run_compiler::{closure#0}>::{closure#0}::{closure#0}>
  43:        0x111380e41 - <scoped_tls[2656f72bfa0842ac]::ScopedKey<rustc_span[b6253c3433877e36]::SessionGlobals>>::set::<rustc_interface[35319ced0081c4aa]::util::run_in_thread_pool_with_globals<rustc_interface[35319ced0081c4aa]::interface::run_compiler<core[2238ef0cf162900b]::result::Result<(), rustc_span[b6253c3433877e36]::ErrorGuaranteed>, rustc_driver_impl[36417fddfe0df6e]::run_compiler::{closure#0}>::{closure#0}, core[2238ef0cf162900b]::result::Result<(), rustc_span[b6253c3433877e36]::ErrorGuaranteed>>::{closure#0}, core[2238ef0cf162900b]::result::Result<(), rustc_span[b6253c3433877e36]::ErrorGuaranteed>>
  44:        0x111378d13 - std[d8ec8f7e4f9a1335]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[35319ced0081c4aa]::util::run_in_thread_with_globals<rustc_interface[35319ced0081c4aa]::util::run_in_thread_pool_with_globals<rustc_interface[35319ced0081c4aa]::interface::run_compiler<core[2238ef0cf162900b]::result::Result<(), rustc_span[b6253c3433877e36]::ErrorGuaranteed>, rustc_driver_impl[36417fddfe0df6e]::run_compiler::{closure#0}>::{closure#0}, core[2238ef0cf162900b]::result::Result<(), rustc_span[b6253c3433877e36]::ErrorGuaranteed>>::{closure#0}, core[2238ef0cf162900b]::result::Result<(), rustc_span[b6253c3433877e36]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[2238ef0cf162900b]::result::Result<(), rustc_span[b6253c3433877e36]::ErrorGuaranteed>>
  45:        0x111387639 - <<std[d8ec8f7e4f9a1335]::thread::Builder>::spawn_unchecked_<rustc_interface[35319ced0081c4aa]::util::run_in_thread_with_globals<rustc_interface[35319ced0081c4aa]::util::run_in_thread_pool_with_globals<rustc_interface[35319ced0081c4aa]::interface::run_compiler<core[2238ef0cf162900b]::result::Result<(), rustc_span[b6253c3433877e36]::ErrorGuaranteed>, rustc_driver_impl[36417fddfe0df6e]::run_compiler::{closure#0}>::{closure#0}, core[2238ef0cf162900b]::result::Result<(), rustc_span[b6253c3433877e36]::ErrorGuaranteed>>::{closure#0}, core[2238ef0cf162900b]::result::Result<(), rustc_span[b6253c3433877e36]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[2238ef0cf162900b]::result::Result<(), rustc_span[b6253c3433877e36]::ErrorGuaranteed>>::{closure#1} as core[2238ef0cf162900b]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  46:        0x10392da19 - std::sys::unix::thread::Thread::new::thread_start::h04013d08f8688bf1
  47:     0x7ff801e4f4e1 - __pthread_start

@saethlin saethlin 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 labels Dec 19, 2023
@compiler-errors compiler-errors self-assigned this Mar 8, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 23, 2024
…ody-validation, r=cjgillot

Fix validation on substituted callee bodies in MIR inliner

When inlining a coroutine, we will substitute the MIR body with the args of the call. There is code in the MIR validator that attempts to prevent query cycles, and will use the coroutine body directly when it detects that's the body that's being validated. That means that when inlining a coroutine body that has been substituted, it may no longer be parameterized over the original args of the coroutine, which will lead to substitution ICEs.

Fixes rust-lang#119064
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 24, 2024
…ody-validation, r=cjgillot

Fix validation on substituted callee bodies in MIR inliner

When inlining a coroutine, we will substitute the MIR body with the args of the call. There is code in the MIR validator that attempts to prevent query cycles, and will use the coroutine body directly when it detects that's the body that's being validated. That means that when inlining a coroutine body that has been substituted, it may no longer be parameterized over the original args of the coroutine, which will lead to substitution ICEs.

Fixes rust-lang#119064
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 24, 2024
Rollup merge of rust-lang#122168 - compiler-errors:inline-coroutine-body-validation, r=cjgillot

Fix validation on substituted callee bodies in MIR inliner

When inlining a coroutine, we will substitute the MIR body with the args of the call. There is code in the MIR validator that attempts to prevent query cycles, and will use the coroutine body directly when it detects that's the body that's being validated. That means that when inlining a coroutine body that has been substituted, it may no longer be parameterized over the original args of the coroutine, which will lead to substitution ICEs.

Fixes rust-lang#119064
RenjiSann pushed a commit to RenjiSann/rust that referenced this issue Mar 25, 2024
…ody-validation, r=cjgillot

Fix validation on substituted callee bodies in MIR inliner

When inlining a coroutine, we will substitute the MIR body with the args of the call. There is code in the MIR validator that attempts to prevent query cycles, and will use the coroutine body directly when it detects that's the body that's being validated. That means that when inlining a coroutine body that has been substituted, it may no longer be parameterized over the original args of the coroutine, which will lead to substitution ICEs.

Fixes rust-lang#119064
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir-opt-inlining Area: MIR inlining 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

Successfully merging a pull request may close this issue.

4 participants