Skip to content

"the compiler unexpectedly panicked" in try_normalize_generic_arg_after_erasing_regions #130967

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
findepi opened this issue Sep 28, 2024 · 1 comment · Fixed by #135057
Closed
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@findepi
Copy link

findepi commented Sep 28, 2024

Code

trait Producer {
    type Produced;
    fn make_one(&self) -> Self::Produced;
}

struct ResultProducer<Delegate> {
    delegate: Delegate,
}
impl<T, E, Delegate> Producer for ResultProducer<Delegate>
where
    E: ?Sized,
    Delegate: Producer<Produced=T>,
{
    type Produced = Result<Delegate::Produced, E>;

    fn make_one(&self) -> Self::Produced {
        Ok(self.delegate.make_one())
    }
}

Meta

rustc --version --verbose:

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: aarch64-apple-darwin
release: 1.81.0
LLVM version: 18.1.7

nightly panics as well

Error output

...
error[E0282]: type annotations needed
  --> src/lib.rs:17:9
   |
17 |         Ok(self.delegate.make_one())
   |         ^^ cannot infer type of the type parameter `E` declared on the enum `Result`
   |
help: consider specifying the generic arguments
   |
17 |         Ok::<T, E>(self.delegate.make_one())
   |           ++++++++

thread 'rustc' panicked at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/compiler/rustc_type_ir/src/ty_kind.rs:892:17:
type variables should not be hashed: ?0t
stack backtrace:
   0:        0x1016c773c - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h243268f17d714c7f
   1:        0x10170a688 - core::fmt::write::hb3cfb8a30e72d7ff
   2:        0x1016bd720 - std::io::Write::write_fmt::hfb2314975de9ecf1
   3:        0x1016c9c4c - std::panicking::default_hook::{{closure}}::h14c7718ccf39d316
   4:        0x1016c9870 - std::panicking::default_hook::hc62e60da3be2f352
   5:        0x10b2c05b8 - <alloc[47bc6d386d7ae45f]::boxed::Box<rustc_driver_impl[54c40c94c6cfc0b2]::install_ice_hook::{closure#0}> as core[f827f14b5e761a5d]::ops::function::Fn<(&dyn for<'a, 'b> core[f827f14b5e761a5d]::ops::function::Fn<(&'a std[4f7d7c3ef984657a]::panic::PanicHookInfo<'b>,), Output = ()> + core[f827f14b5e761a5d]::marker::Sync + core[f827f14b5e761a5d]::marker::Send, &std[4f7d7c3ef984657a]::panic::PanicHookInfo)>>::call
   6:        0x1016ca868 - std::panicking::rust_panic_with_hook::h09e8a656f11e82b2
   7:        0x1016ca178 - std::panicking::begin_panic_handler::{{closure}}::h1230eb3cc91b241c
   8:        0x1016c7bc8 - std::sys::backtrace::__rust_end_short_backtrace::hc3491307aceda2c2
   9:        0x1016c9e40 - _rust_begin_unwind
  10:        0x101724ad8 - core::panicking::panic_fmt::ha4b80a05b9fff47a
  11:        0x10bfb24bc - <rustc_type_ir[4427c4614cfd2abb]::ty_kind::InferTy as rustc_data_structures[5fa37262921da6b8]::stable_hasher::HashStable<rustc_query_system[5f1672c0485b57da]::ich::hcx::StableHashingContext>>::hash_stable
  12:        0x10bfd25d8 - <rustc_type_ir[4427c4614cfd2abb]::ty_info::WithCachedTypeInfo<rustc_type_ir[4427c4614cfd2abb]::ty_kind::TyKind<rustc_middle[5a798f9924bfd2e0]::ty::context::TyCtxt>> as rustc_data_structures[5fa37262921da6b8]::stable_hasher::HashStable<rustc_query_system[5f1672c0485b57da]::ich::hcx::StableHashingContext>>::hash_stable
  13:        0x10bfe064c - <&rustc_middle[5a798f9924bfd2e0]::ty::list::RawList<(), rustc_middle[5a798f9924bfd2e0]::ty::generic_args::GenericArg> as rustc_data_structures[5fa37262921da6b8]::stable_hasher::HashStable<rustc_query_system[5f1672c0485b57da]::ich::hcx::StableHashingContext>>::hash_stable
  14:        0x10bfd2540 - <rustc_type_ir[4427c4614cfd2abb]::ty_info::WithCachedTypeInfo<rustc_type_ir[4427c4614cfd2abb]::ty_kind::TyKind<rustc_middle[5a798f9924bfd2e0]::ty::context::TyCtxt>> as rustc_data_structures[5fa37262921da6b8]::stable_hasher::HashStable<rustc_query_system[5f1672c0485b57da]::ich::hcx::StableHashingContext>>::hash_stable
  15:        0x10c77ca0c - <rustc_query_impl[5e7782f17777a7c9]::query_impl::try_normalize_generic_arg_after_erasing_regions::dynamic_query::{closure#7} as core[f827f14b5e761a5d]::ops::function::FnOnce<(&mut rustc_query_system[5f1672c0485b57da]::ich::hcx::StableHashingContext, &rustc_middle[5a798f9924bfd2e0]::query::erase::Erased<[u8; 8usize]>)>>::call_once
  16:        0x10c6ac240 - rustc_query_system[5f1672c0485b57da]::query::plumbing::try_execute_query::<rustc_query_impl[5e7782f17777a7c9]::DynamicConfig<rustc_query_system[5f1672c0485b57da]::query::caches::DefaultCache<rustc_middle[5a798f9924bfd2e0]::ty::ParamEnvAnd<rustc_middle[5a798f9924bfd2e0]::ty::generic_args::GenericArg>, rustc_middle[5a798f9924bfd2e0]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[5e7782f17777a7c9]::plumbing::QueryCtxt, true>
  17:        0x10c7ccea8 - rustc_query_impl[5e7782f17777a7c9]::query_impl::try_normalize_generic_arg_after_erasing_regions::get_query_incr::__rust_end_short_backtrace
  18:        0x10bf1aa08 - rustc_middle[5a798f9924bfd2e0]::query::plumbing::query_get_at::<rustc_query_system[5f1672c0485b57da]::query::caches::DefaultCache<rustc_middle[5a798f9924bfd2e0]::ty::ParamEnvAnd<rustc_middle[5a798f9924bfd2e0]::ty::generic_args::GenericArg>, rustc_middle[5a798f9924bfd2e0]::query::erase::Erased<[u8; 8usize]>>>
  19:        0x10c26e864 - <rustc_middle[5a798f9924bfd2e0]::ty::context::TyCtxt>::try_normalize_erasing_regions::<rustc_middle[5a798f9924bfd2e0]::ty::Ty>
  20:        0x10c29c390 - <rustc_mir_transform[ed8a8c9edc8f1ca0]::reveal_all::RevealAll as rustc_middle[5a798f9924bfd2e0]::mir::MirPass>::run_pass
  21:        0x10c17f8f4 - rustc_mir_transform[ed8a8c9edc8f1ca0]::pass_manager::run_passes_inner
  22:        0x10c287da0 - rustc_mir_transform[ed8a8c9edc8f1ca0]::run_analysis_to_runtime_passes
  23:        0x10c287a5c - rustc_mir_transform[ed8a8c9edc8f1ca0]::mir_drops_elaborated_and_const_checked
  24:        0x10c72e668 - rustc_query_impl[5e7782f17777a7c9]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[5e7782f17777a7c9]::query_impl::mir_drops_elaborated_and_const_checked::dynamic_query::{closure#2}::{closure#0}, rustc_middle[5a798f9924bfd2e0]::query::erase::Erased<[u8; 8usize]>>
  25:        0x10c77bac0 - <rustc_query_impl[5e7782f17777a7c9]::query_impl::mir_drops_elaborated_and_const_checked::dynamic_query::{closure#2} as core[f827f14b5e761a5d]::ops::function::FnOnce<(rustc_middle[5a798f9924bfd2e0]::ty::context::TyCtxt, rustc_span[ab16d476329f5d04]::def_id::LocalDefId)>>::call_once
  26:        0x10c6ddd2c - rustc_query_system[5f1672c0485b57da]::query::plumbing::try_execute_query::<rustc_query_impl[5e7782f17777a7c9]::DynamicConfig<rustc_query_system[5f1672c0485b57da]::query::caches::VecCache<rustc_span[ab16d476329f5d04]::def_id::LocalDefId, rustc_middle[5a798f9924bfd2e0]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[5e7782f17777a7c9]::plumbing::QueryCtxt, true>
  27:        0x10c7aa2a0 - rustc_query_impl[5e7782f17777a7c9]::query_impl::mir_drops_elaborated_and_const_checked::get_query_incr::__rust_end_short_backtrace
  28:        0x10bb57264 - rustc_interface[1340bb505392beac]::passes::analysis
  29:        0x10c72edf8 - rustc_query_impl[5e7782f17777a7c9]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[5e7782f17777a7c9]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[5a798f9924bfd2e0]::query::erase::Erased<[u8; 1usize]>>
  30:        0x10c77d8ac - <rustc_query_impl[5e7782f17777a7c9]::query_impl::analysis::dynamic_query::{closure#2} as core[f827f14b5e761a5d]::ops::function::FnOnce<(rustc_middle[5a798f9924bfd2e0]::ty::context::TyCtxt, ())>>::call_once
  31:        0x10c694b4c - rustc_query_system[5f1672c0485b57da]::query::plumbing::try_execute_query::<rustc_query_impl[5e7782f17777a7c9]::DynamicConfig<rustc_query_system[5f1672c0485b57da]::query::caches::SingleCache<rustc_middle[5a798f9924bfd2e0]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[5e7782f17777a7c9]::plumbing::QueryCtxt, true>
  32:        0x10c7a68a4 - rustc_query_impl[5e7782f17777a7c9]::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
  33:        0x10b2ab0b4 - <rustc_interface[1340bb505392beac]::queries::QueryResult<&rustc_middle[5a798f9924bfd2e0]::ty::context::GlobalCtxt>>::enter::<core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>, rustc_driver_impl[54c40c94c6cfc0b2]::run_compiler::{closure#0}::{closure#1}::{closure#5}>
  34:        0x10b2c21a0 - <rustc_interface[1340bb505392beac]::interface::Compiler>::enter::<rustc_driver_impl[54c40c94c6cfc0b2]::run_compiler::{closure#0}::{closure#1}, core[f827f14b5e761a5d]::result::Result<core[f827f14b5e761a5d]::option::Option<rustc_interface[1340bb505392beac]::queries::Linker>, rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>
  35:        0x10b2b698c - <scoped_tls[df49f867320abf2e]::ScopedKey<rustc_span[ab16d476329f5d04]::SessionGlobals>>::set::<rustc_interface[1340bb505392beac]::util::run_in_thread_with_globals<rustc_interface[1340bb505392beac]::interface::run_compiler<core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>, rustc_driver_impl[54c40c94c6cfc0b2]::run_compiler::{closure#0}>::{closure#1}, core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}, core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>
  36:        0x10b2c1b34 - rustc_span[ab16d476329f5d04]::create_session_globals_then::<core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>, rustc_interface[1340bb505392beac]::util::run_in_thread_with_globals<rustc_interface[1340bb505392beac]::interface::run_compiler<core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>, rustc_driver_impl[54c40c94c6cfc0b2]::run_compiler::{closure#0}>::{closure#1}, core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}>
  37:        0x10b2df8fc - std[4f7d7c3ef984657a]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[1340bb505392beac]::util::run_in_thread_with_globals<rustc_interface[1340bb505392beac]::interface::run_compiler<core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>, rustc_driver_impl[54c40c94c6cfc0b2]::run_compiler::{closure#0}>::{closure#1}, core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>
  38:        0x10b2bf7dc - <<std[4f7d7c3ef984657a]::thread::Builder>::spawn_unchecked_<rustc_interface[1340bb505392beac]::util::run_in_thread_with_globals<rustc_interface[1340bb505392beac]::interface::run_compiler<core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>, rustc_driver_impl[54c40c94c6cfc0b2]::run_compiler::{closure#0}>::{closure#1}, core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>::{closure#1} as core[f827f14b5e761a5d]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  39:        0x1016d2fa4 - std::sys::pal::unix::thread::Thread::new::thread_start::h1bd1b9c95010bf71
  40:        0x1a025b2e4 - __pthread_deallocate

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.81.0 (eeb90cda1 2024-09-04) running on aarch64-apple-darwin

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked -C incremental=[REDACTED]

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

query stack during panic:
#0 [try_normalize_generic_arg_after_erasing_regions] normalizing `<ResultProducer<Delegate> as Producer>::Produced`
#1 [mir_drops_elaborated_and_const_checked] elaborating drops for `<impl at src/lib.rs:9:1: 12:36>::make_one`
end of query stack
...
Backtrace

   Compiling compiler-panick v0.1.0 (/a-path/a-path/a-path/compiler-panick)
error[E0207]: the type parameter `E` is not constrained by the impl trait, self type, or predicates
 --> src/lib.rs:9:9
  |
9 | impl<T, E, Delegate> Producer for ResultProducer<Delegate>
  |         ^ unconstrained type parameter

error[E0277]: the size for values of type `E` cannot be known at compilation time
   --> src/lib.rs:14:21
    |
9   | impl<T, E, Delegate> Producer for ResultProducer<Delegate>
    |         - this type parameter needs to be `Sized`
...
14  |     type Produced = Result<Delegate::Produced, E>;
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
    |
note: required by an implicit `Sized` bound in `Result`
   --> /a-path/a-path/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:527:20
    |
527 | pub enum Result<T, E> {
    |                    ^ required by the implicit `Sized` requirement on this type parameter in `Result`
help: consider removing the `?Sized` bound to make the type parameter `Sized`
    |
11  -     E: ?Sized,
    |

error[E0282]: type annotations needed
  --> src/lib.rs:17:9
   |
17 |         Ok(self.delegate.make_one())
   |         ^^ cannot infer type of the type parameter `E` declared on the enum `Result`
   |
help: consider specifying the generic arguments
   |
17 |         Ok::<T, E>(self.delegate.make_one())
   |           ++++++++

thread 'rustc' panicked at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/compiler/rustc_type_ir/src/ty_kind.rs:892:17:
type variables should not be hashed: ?0t
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: <rustc_type_ir::ty_kind::InferTy as rustc_data_structures::stable_hasher::HashStable<rustc_query_system::ich::hcx::StableHashingContext>>::hash_stable
   3: <rustc_type_ir::ty_info::WithCachedTypeInfo<rustc_type_ir::ty_kind::TyKind<rustc_middle::ty::context::TyCtxt>> as rustc_data_structures::stable_hasher::HashStable<rustc_query_system::ich::hcx::StableHashingContext>>::hash_stable
   4: <&rustc_middle::ty::list::RawList<(), rustc_middle::ty::generic_args::GenericArg> as rustc_data_structures::stable_hasher::HashStable<rustc_query_system::ich::hcx::StableHashingContext>>::hash_stable
   5: <rustc_type_ir::ty_info::WithCachedTypeInfo<rustc_type_ir::ty_kind::TyKind<rustc_middle::ty::context::TyCtxt>> as rustc_data_structures::stable_hasher::HashStable<rustc_query_system::ich::hcx::StableHashingContext>>::hash_stable
   6: <rustc_query_impl::query_impl::try_normalize_generic_arg_after_erasing_regions::dynamic_query::{closure#7} as core::ops::function::FnOnce<(&mut rustc_query_system::ich::hcx::StableHashingContext, &rustc_middle::query::erase::Erased<[u8; 8]>)>>::call_once
   7: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::DefaultCache<rustc_middle::ty::ParamEnvAnd<rustc_middle::ty::generic_args::GenericArg>, rustc_middle::query::erase::Erased<[u8; 8]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, true>
   8: rustc_middle::query::plumbing::query_get_at::<rustc_query_system::query::caches::DefaultCache<rustc_middle::ty::ParamEnvAnd<rustc_middle::ty::generic_args::GenericArg>, rustc_middle::query::erase::Erased<[u8; 8]>>>
   9: <rustc_middle::ty::context::TyCtxt>::try_normalize_erasing_regions::<rustc_middle::ty::Ty>
  10: <rustc_mir_transform::reveal_all::RevealAll as rustc_middle::mir::MirPass>::run_pass
  11: rustc_mir_transform::pass_manager::run_passes_inner
  12: rustc_mir_transform::run_analysis_to_runtime_passes
  13: rustc_mir_transform::mir_drops_elaborated_and_const_checked
      [... omitted 2 frames ...]
  14: rustc_interface::passes::analysis
      [... omitted 2 frames ...]
  15: <rustc_interface::queries::QueryResult<&rustc_middle::ty::context::GlobalCtxt>>::enter::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}::{closure#1}::{closure#5}>
  16: <rustc_interface::interface::Compiler>::enter::<rustc_driver_impl::run_compiler::{closure#0}::{closure#1}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_span::ErrorGuaranteed>>
  17: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::util::run_in_thread_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>
  18: rustc_span::create_session_globals_then::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_interface::util::run_in_thread_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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.81.0 (eeb90cda1 2024-09-04) running on aarch64-apple-darwin

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked -C incremental=[REDACTED]

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

query stack during panic:
#0 [try_normalize_generic_arg_after_erasing_regions] normalizing `<ResultProducer<Delegate> as Producer>::Produced`
#1 [mir_drops_elaborated_and_const_checked] elaborating drops for `<impl at src/lib.rs:9:1: 12:36>::make_one`
#2 [analysis] running analysis passes on this crate
end of query stack
error[E0282]: type annotations needed
  --> src/lib.rs:16:27
   |
16 |     fn make_one(&self) -> Self::Produced {
   |                           ^^^^^^^^^^^^^^ cannot infer type for type parameter `E`

Some errors have detailed explanations: E0207, E0277, E0282.
For more information about an error, try `rustc --explain E0207`.
error: could not compile `compiler-panick` (lib) due to 4 previous errors

@findepi findepi 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 Sep 28, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 28, 2024
@cyrgani
Copy link
Contributor

cyrgani commented Sep 28, 2024

Reduced:

trait Producer {
    type Produced;
    fn make_one() -> Self::Produced;
}

impl<E: ?Sized> Producer for () {
    type Produced = Option<E>;
    fn make_one() -> Self::Produced {
        loop {}
    }
}

@matthiaskrgr matthiaskrgr added the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label Oct 12, 2024
@Noratrieb Noratrieb removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 9, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 3, 2025
…d, r=<try>

Project to `TyKind::Error` when there are unconstrained non-lifetime (ty/const) impl params

I think this is a bit less invasive of an approach compared to rust-lang#127973.

It splits the `enforce_impl_params_are_constrained` function into lifetime/non-lifetime, and queryfies the latter. We can then use the result of the latter query (`Result<(), ErrorGuaranteed>`) to intercept projection and constrain the projected type to `TyKind::Error`, which ensures that we leak no ty or const vars to places that don't expect them, like `normalize_erasing_regions`.

The reason we split `enforce_impl_params_are_constrained` into two parts is because we only error for *lifetimes* if the lifetime ends up showing up in any of the associated types of the impl (e.g. we allow `impl<'a> Foo { type Assoc = (); }`). However, in order to compute the `type_of` query for the anonymous associated type of an RPITIT, we need to do trait solving (in `query collect_return_position_impl_trait_in_trait_tys`). That would induce cycles. Luckily, it turns out for lifetimes we don't even care about if they're unconstrained, since they're erased in all contexts that we are trying to fix ICEs. So it's sufficient to keep this check separated out of the query.

Fixes rust-lang#123141
Fixes rust-lang#125874
Fixes rust-lang#126942
Fixes rust-lang#127804
Fixes rust-lang#130967

r? oli-obk
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 3, 2025
…d, r=<try>

Project to `TyKind::Error` when there are unconstrained non-lifetime (ty/const) impl params

It splits the `enforce_impl_params_are_constrained` function into lifetime/non-lifetime, and queryfies the latter. We can then use the result of the latter query (`Result<(), ErrorGuaranteed>`) to intercept projection and constrain the projected type to `TyKind::Error`, which ensures that we leak no ty or const vars to places that don't expect them, like `normalize_erasing_regions`.

The reason we split `enforce_impl_params_are_constrained` into two parts is because we only error for *lifetimes* if the lifetime ends up showing up in any of the associated types of the impl (e.g. we allow `impl<'a> Foo { type Assoc = (); }`). However, in order to compute the `type_of` query for the anonymous associated type of an RPITIT, we need to do trait solving (in `query collect_return_position_impl_trait_in_trait_tys`). That would induce cycles. Luckily, it turns out for lifetimes we don't even care about if they're unconstrained, since they're erased in all contexts that we are trying to fix ICEs. So it's sufficient to keep this check separated out of the query.

I think this is a bit less invasive of an approach compared to rust-lang#127973. The major difference between this PR and that PR is that we queryify the check instead of merging it into the `explicit_predicates_of` query, and we use the result to taint just projection goals, rather than trait goals too. This doesn't require a lot of new tracking in `ItemCtxt` and `GenericPredicates`, and it also seems to not require any other changes to typeck like that PR did.

Fixes rust-lang#123141
Fixes rust-lang#125874
Fixes rust-lang#126942
Fixes rust-lang#127804
Fixes rust-lang#130967

r? oli-obk
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 3, 2025
…d, r=oli-obk

Project to `TyKind::Error` when there are unconstrained non-lifetime (ty/const) impl params

It splits the `enforce_impl_params_are_constrained` function into lifetime/non-lifetime, and queryfies the latter. We can then use the result of the latter query (`Result<(), ErrorGuaranteed>`) to intercept projection and constrain the projected type to `TyKind::Error`, which ensures that we leak no ty or const vars to places that don't expect them, like `normalize_erasing_regions`.

The reason we split `enforce_impl_params_are_constrained` into two parts is because we only error for *lifetimes* if the lifetime ends up showing up in any of the associated types of the impl (e.g. we allow `impl<'a> Foo { type Assoc = (); }`). However, in order to compute the `type_of` query for the anonymous associated type of an RPITIT, we need to do trait solving (in `query collect_return_position_impl_trait_in_trait_tys`). That would induce cycles. Luckily, it turns out for lifetimes we don't even care about if they're unconstrained, since they're erased in all contexts that we are trying to fix ICEs. So it's sufficient to keep this check separated out of the query.

I think this is a bit less invasive of an approach compared to rust-lang#127973. The major difference between this PR and that PR is that we queryify the check instead of merging it into the `explicit_predicates_of` query, and we use the result to taint just projection goals, rather than trait goals too. This doesn't require a lot of new tracking in `ItemCtxt` and `GenericPredicates`, and it also seems to not require any other changes to typeck like that PR did.

Fixes rust-lang#123141
Fixes rust-lang#125874
Fixes rust-lang#126942
Fixes rust-lang#127804
Fixes rust-lang#130967

r? oli-obk
@bors bors closed this as completed in 7349f6b Jan 4, 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) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
5 participants