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: None in compiler/rustc_middle/src/ty/sty.rs #126378

Closed
matthiaskrgr opened this issue Jun 13, 2024 · 9 comments · Fixed by #127220
Closed

ICE: None in compiler/rustc_middle/src/ty/sty.rs #126378

matthiaskrgr opened this issue Jun 13, 2024 · 9 comments · Fixed by #127220
Assignees
Labels
C-bug Category: This is a bug. F-const_trait_impl `#![feature(const_trait_impl)]` F-effects `#![feature(effects)]` 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.

Comments

@matthiaskrgr
Copy link
Member

auto-reduced (treereduce-rust):

struct A();

impl const Drop for A {}

#![feature(effects)]

original:

#![feature(const_trait_impl)]
#![feature(const_trait_impl)]

struct A();

impl const Drop for A {
    fn main() {
    let _: &'static A = &A(); //~ ERROR temporary value dropped while borrowed
    let _: &'static [A] = &[C]; //~ ERROR temporary value dropped while borrowed
}
}

const const_trait_impl: A = A();

fn main() {
    let _: &'static A = &A(); //~ ERROR temporary value dropped while borrowed
    let _: &'static [A] = &[C]; //~ ERROR temporary value dropped while borrowed
}

Version information

rustc 1.81.0-nightly (8cf5101d7 2024-06-12)
binary: rustc
commit-hash: 8cf5101d77cd9eeb12751c563d8098aba2c604d0
commit-date: 2024-06-12
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7

Command:
/home/gh-matthiaskrgr/.rustup/toolchains/master/bin/rustc -Zcrate-attr=feature(effects)

Program output

error[E0658]: const trait impls are experimental
 --> /tmp/icemaker_global_tempdir.qucPmmmLlnXC/rustc_testrunner_tmpdir_reporting.BE4likY1WzZ7/mvce.rs:3:6
  |
3 | impl const Drop for A {}
  |      ^^^^^
  |
  = note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
  = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
  = note: this compiler was built on 2024-06-12; consider upgrading it if it is out of date

error[E0601]: `main` function not found in crate `mvce`
 --> /tmp/icemaker_global_tempdir.qucPmmmLlnXC/rustc_testrunner_tmpdir_reporting.BE4likY1WzZ7/mvce.rs:3:25
  |
3 | impl const Drop for A {}
  |                         ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.qucPmmmLlnXC/rustc_testrunner_tmpdir_reporting.BE4likY1WzZ7/mvce.rs`

error: const `impl` for trait `Drop` which is not marked with `#[const_trait]`
 --> /tmp/icemaker_global_tempdir.qucPmmmLlnXC/rustc_testrunner_tmpdir_reporting.BE4likY1WzZ7/mvce.rs:3:12
  |
3 | impl const Drop for A {}
  |            ^^^^
  |
  = note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
  = note: adding a non-const method body in the future would be a breaking change

thread 'rustc' panicked at compiler/rustc_middle/src/ty/sty.rs:360:36:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0:     0x72429878aae5 - std::backtrace_rs::backtrace::libunwind::trace::h1eae5369495be94c
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:     0x72429878aae5 - std::backtrace_rs::backtrace::trace_unsynchronized::hb9ff15614ae80516
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x72429878aae5 - std::sys_common::backtrace::_print_fmt::h715013f8db9fa836
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x72429878aae5 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hb2525cfb3de001ec
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7242987db77b - core::fmt::rt::Argument::fmt::h313d6a20d718e675
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/core/src/fmt/rt.rs:165:63
   5:     0x7242987db77b - core::fmt::write::h34b41f02f735d6be
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/core/src/fmt/mod.rs:1168:21
   6:     0x72429877f76f - std::io::Write::write_fmt::h3163bf1024b30f9d
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/std/src/io/mod.rs:1835:15
   7:     0x72429878a8be - std::sys_common::backtrace::_print::h3cf8718ea70ad379
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x72429878a8be - std::sys_common::backtrace::print::hb2023c867b337d63
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x72429878d2f9 - std::panicking::default_hook::{{closure}}::h077ef55df06e466d
  10:     0x72429878d09c - std::panicking::default_hook::hfd347bda164b89a9
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/std/src/panicking.rs:292:9
  11:     0x72429bab5ed0 - std[1b0e87a7881e3f2e]::panicking::update_hook::<alloc[256686a2906c29a0]::boxed::Box<rustc_driver_impl[935dcf8ae4c858c0]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x72429878dbff - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::he8ea12bfb8de16b1
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/alloc/src/boxed.rs:2076:9
  13:     0x72429878dbff - std::panicking::rust_panic_with_hook::h77a553aef83cdbaf
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/std/src/panicking.rs:801:13
  14:     0x72429878d7f3 - std::panicking::begin_panic_handler::{{closure}}::h5b05d7e6508b1742
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/std/src/panicking.rs:660:13
  15:     0x72429878afa9 - std::sys_common::backtrace::__rust_end_short_backtrace::h5c68199ef3e35ced
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x72429878d4d4 - rust_begin_unwind
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/std/src/panicking.rs:658:5
  17:     0x7242987d7d33 - core::panicking::panic_fmt::h2a691adfa40775d5
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/core/src/panicking.rs:74:14
  18:     0x7242987d7dbc - core::panicking::panic::h5482279ace8e11cf
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/core/src/panicking.rs:148:5
  19:     0x7242987d7b29 - core::option::unwrap_failed::hf7e2b68d8b2b7d2b
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/core/src/option.rs:1985:5
  20:     0x72429e76dcbc - <rustc_middle[ca7f0dc9254b8975]::ty::sty::ParamConst>::find_ty_from_env.cold
  21:     0x724299bb1e32 - <rustc_trait_selection[61b62075c2ac6296]::traits::fulfill::FulfillProcessor as rustc_data_structures[75e680cad15e3340]::obligation_forest::ObligationProcessor>::process_obligation
  22:     0x72429cef8907 - <rustc_data_structures[75e680cad15e3340]::obligation_forest::ObligationForest<rustc_trait_selection[61b62075c2ac6296]::traits::fulfill::PendingPredicateObligation>>::process_obligations::<rustc_trait_selection[61b62075c2ac6296]::traits::fulfill::FulfillProcessor>
  23:     0x72429cefa943 - <rustc_trait_selection[61b62075c2ac6296]::traits::fulfill::FulfillmentContext<rustc_trait_selection[61b62075c2ac6296]::traits::FulfillmentError> as rustc_infer[b02e37160fc9aec]::traits::engine::TraitEngine<rustc_trait_selection[61b62075c2ac6296]::traits::FulfillmentError>>::select_all_or_error
  24:     0x72429daaec8f - rustc_hir_analysis[5d45d15212514940]::check::dropck::check_drop_impl
  25:     0x72429daae746 - <rustc_middle[ca7f0dc9254b8975]::ty::context::TyCtxt>::calculate_dtor::<rustc_hir_analysis[5d45d15212514940]::check::dropck::check_drop_impl>::{closure#0}
  26:     0x72429d90e862 - rustc_hir_analysis[5d45d15212514940]::check::adt_destructor
  27:     0x72429d90ea72 - rustc_query_impl[74ded2e9ca83132e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[74ded2e9ca83132e]::query_impl::adt_destructor::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ca7f0dc9254b8975]::query::erase::Erased<[u8; 12usize]>>
  28:     0x72429d4089ad - rustc_query_system[32db906e1bbe7f36]::query::plumbing::try_execute_query::<rustc_query_impl[74ded2e9ca83132e]::DynamicConfig<rustc_query_system[32db906e1bbe7f36]::query::caches::DefIdCache<rustc_middle[ca7f0dc9254b8975]::query::erase::Erased<[u8; 12usize]>>, false, false, false>, rustc_query_impl[74ded2e9ca83132e]::plumbing::QueryCtxt, false>
  29:     0x72429d40828f - rustc_query_impl[74ded2e9ca83132e]::query_impl::adt_destructor::get_query_non_incr::__rust_end_short_backtrace
  30:     0x72429d70b372 - <rustc_middle[ca7f0dc9254b8975]::ty::adt::AdtDef>::destructor
  31:     0x72429978a44a - rustc_hir_analysis[5d45d15212514940]::check::check::check_item_type
  32:     0x72429a78ad4c - rustc_hir_analysis[5d45d15212514940]::check::wfcheck::check_well_formed
  33:     0x72429d0f19a9 - rustc_query_impl[74ded2e9ca83132e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[74ded2e9ca83132e]::query_impl::check_well_formed::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ca7f0dc9254b8975]::query::erase::Erased<[u8; 1usize]>>
  34:     0x72429d0f1c2a - rustc_query_system[32db906e1bbe7f36]::query::plumbing::try_execute_query::<rustc_query_impl[74ded2e9ca83132e]::DynamicConfig<rustc_query_system[32db906e1bbe7f36]::query::caches::VecCache<rustc_hir[c2711ee5d901482]::hir_id::OwnerId, rustc_middle[ca7f0dc9254b8975]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[74ded2e9ca83132e]::plumbing::QueryCtxt, false>
  35:     0x72429d0f1986 - rustc_query_impl[74ded2e9ca83132e]::query_impl::check_well_formed::get_query_non_incr::__rust_end_short_backtrace
  36:     0x72429d0f270f - rustc_hir_analysis[5d45d15212514940]::check::wfcheck::check_mod_type_wf
  37:     0x72429d0f2555 - rustc_query_impl[74ded2e9ca83132e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[74ded2e9ca83132e]::query_impl::check_mod_type_wf::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ca7f0dc9254b8975]::query::erase::Erased<[u8; 1usize]>>
  38:     0x72429d8aaab0 - rustc_query_system[32db906e1bbe7f36]::query::plumbing::try_execute_query::<rustc_query_impl[74ded2e9ca83132e]::DynamicConfig<rustc_query_system[32db906e1bbe7f36]::query::caches::DefaultCache<rustc_span[504087e652f64f62]::def_id::LocalModDefId, rustc_middle[ca7f0dc9254b8975]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[74ded2e9ca83132e]::plumbing::QueryCtxt, false>
  39:     0x72429d8aa859 - rustc_query_impl[74ded2e9ca83132e]::query_impl::check_mod_type_wf::get_query_non_incr::__rust_end_short_backtrace
  40:     0x72429d10428c - rustc_hir_analysis[5d45d15212514940]::check_crate
  41:     0x72429d8452fe - rustc_interface[30a5feafe5a08242]::passes::analysis
  42:     0x72429d844e47 - rustc_query_impl[74ded2e9ca83132e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[74ded2e9ca83132e]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ca7f0dc9254b8975]::query::erase::Erased<[u8; 1usize]>>
  43:     0x72429dc31f65 - rustc_query_system[32db906e1bbe7f36]::query::plumbing::try_execute_query::<rustc_query_impl[74ded2e9ca83132e]::DynamicConfig<rustc_query_system[32db906e1bbe7f36]::query::caches::SingleCache<rustc_middle[ca7f0dc9254b8975]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[74ded2e9ca83132e]::plumbing::QueryCtxt, false>
  44:     0x72429dc31ccf - rustc_query_impl[74ded2e9ca83132e]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  45:     0x72429dac8f12 - rustc_interface[30a5feafe5a08242]::interface::run_compiler::<core[1ccefa92ed8426b9]::result::Result<(), rustc_span[504087e652f64f62]::ErrorGuaranteed>, rustc_driver_impl[935dcf8ae4c858c0]::run_compiler::{closure#0}>::{closure#1}
  46:     0x72429dae7949 - std[1b0e87a7881e3f2e]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[30a5feafe5a08242]::util::run_in_thread_with_globals<rustc_interface[30a5feafe5a08242]::util::run_in_thread_pool_with_globals<rustc_interface[30a5feafe5a08242]::interface::run_compiler<core[1ccefa92ed8426b9]::result::Result<(), rustc_span[504087e652f64f62]::ErrorGuaranteed>, rustc_driver_impl[935dcf8ae4c858c0]::run_compiler::{closure#0}>::{closure#1}, core[1ccefa92ed8426b9]::result::Result<(), rustc_span[504087e652f64f62]::ErrorGuaranteed>>::{closure#0}, core[1ccefa92ed8426b9]::result::Result<(), rustc_span[504087e652f64f62]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[1ccefa92ed8426b9]::result::Result<(), rustc_span[504087e652f64f62]::ErrorGuaranteed>>
  47:     0x72429dae76fa - <<std[1b0e87a7881e3f2e]::thread::Builder>::spawn_unchecked_<rustc_interface[30a5feafe5a08242]::util::run_in_thread_with_globals<rustc_interface[30a5feafe5a08242]::util::run_in_thread_pool_with_globals<rustc_interface[30a5feafe5a08242]::interface::run_compiler<core[1ccefa92ed8426b9]::result::Result<(), rustc_span[504087e652f64f62]::ErrorGuaranteed>, rustc_driver_impl[935dcf8ae4c858c0]::run_compiler::{closure#0}>::{closure#1}, core[1ccefa92ed8426b9]::result::Result<(), rustc_span[504087e652f64f62]::ErrorGuaranteed>>::{closure#0}, core[1ccefa92ed8426b9]::result::Result<(), rustc_span[504087e652f64f62]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[1ccefa92ed8426b9]::result::Result<(), rustc_span[504087e652f64f62]::ErrorGuaranteed>>::{closure#2} as core[1ccefa92ed8426b9]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  48:     0x724298797a7b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hbaa641b300aece5a
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/alloc/src/boxed.rs:2062:9
  49:     0x724298797a7b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h67b521bf367d14d7
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/alloc/src/boxed.rs:2062:9
  50:     0x724298797a7b - std::sys::pal::unix::thread::Thread::new::thread_start::hc86a294633ed1c1b
                               at /rustc/8cf5101d77cd9eeb12751c563d8098aba2c604d0/library/std/src/sys/pal/unix/thread.rs:108:17
  51:     0x724298494ac3 - start_thread
                               at ./nptl/pthread_create.c:442:8
  52:     0x724298526850 - __GI___clone3
                               at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
  53:                0x0 - <unknown>

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: please make sure that you have updated to the latest nightly

note: rustc 1.81.0-nightly (8cf5101d7 2024-06-12) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z crate-attr=feature(effects) -Z dump-mir-dir=dir

query stack during panic:
#0 [adt_destructor] computing `Drop` impl for `A`
#1 [check_well_formed] checking that `A` is well-formed
end of query stack
error[E0207]: the const parameter `host` is not constrained by the impl trait, self type, or predicates
 --> /tmp/icemaker_global_tempdir.qucPmmmLlnXC/rustc_testrunner_tmpdir_reporting.BE4likY1WzZ7/mvce.rs:3:6
  |
3 | impl const Drop for A {}
  |      ^^^^^ unconstrained const parameter
  |
  = note: expressions using a const parameter must map each value to a distinct output value
  = note: proving the result of expressions other than the parameter are unique is not supported

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0207, E0601, E0658.
For more information about an error, try `rustc --explain E0207`.

@rustbot label +F-const_trait_impl +F-const_trait_impl +F-effects

@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 Jun 13, 2024
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. F-const_trait_impl `#![feature(const_trait_impl)]` F-effects `#![feature(effects)]` labels Jun 13, 2024
@matthiaskrgr matthiaskrgr changed the title ICE: None in `compiler/rustc_middle/src/ty/sty.rs: ICE: None in compiler/rustc_middle/src/ty/sty.rs Jun 13, 2024
@BoxyUwU
Copy link
Member

BoxyUwU commented Jun 13, 2024

This is a dupe of #126147, it doesnt ICE on nightly

@BoxyUwU BoxyUwU closed this as completed Jun 13, 2024
@matthiaskrgr

This comment was marked as resolved.

@matthiaskrgr matthiaskrgr reopened this Jun 13, 2024
@oli-obk
Copy link
Contributor

oli-obk commented Jun 13, 2024

The playground does not reproduce this issue

@matthiaskrgr
Copy link
Member Author

with rustc master 56e112a I can still reproduce this.
this is also what the report was generated with

@matthiaskrgr
Copy link
Member Author

ah nevermind, I think I was inside a repo with a rustc-toolchain which downgraded the rustc to nightly again without me noticing xD

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 13, 2024
@matthiaskrgr
Copy link
Member Author

After all, I stand correct that the

struct A();
impl const Drop for A {}

snippet with effects was never fixed:

#126147 (comment) fixed
#126147 (comment) fixed
#126147 (comment) fixed

never fixed: #126147 (comment)

playground repro ICE long https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=4f5758a90a2ae608d4a7ebcb768ce68b

playground repro ICE short
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=489f57bd378a68a1be8c7a5b1623eb2c

@matthiaskrgr matthiaskrgr reopened this Jun 13, 2024
@matthiaskrgr
Copy link
Member Author

rustc does not like having the feature standalone, so

struct A();

impl const Drop for A {}

#![feature(effects)]

does not ICE; but this is only generated because people wanted to have the feature gets inside the repro (I forgot to manually fix it and copy it to the top):

so

#![feature(effects)]
struct A();
impl const Drop for A {}

as well as

struct A();
impl const Drop for A {}

with -Zcrate-attr=feature(effects) should still ICE on nightly / master

@BoxyUwU
Copy link
Member

BoxyUwU commented Jun 13, 2024

Likely caused by the ConstArgHasType predicates we generate in predicates_of being based off the hir generics instead of generics_of.

@BoxyUwU BoxyUwU self-assigned this Jun 13, 2024
@BoxyUwU
Copy link
Member

BoxyUwU commented Jun 13, 2024

Actually no, while that's something we should fix, the ICE here is actually caused by the fact that we prove the impl predicates in the env of the adt which.... ConstArgHasType(HostParam, bool) does not hold in

@bors bors closed this as completed in 29314e4 Jul 26, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jul 26, 2024
Rollup merge of rust-lang#127220 - BoxyUwU:dropck_handle_extra_impl_params, r=compiler-errors

Graciously handle `Drop` impls introducing more generic parameters than the ADT

Follow up to rust-lang#110577
Fixes rust-lang#126378
Fixes rust-lang#126889

## Motivation

A current issue with the way we check drop impls do not specialize any of their generic parameters is that when the `Drop` impl introduces *more* generic parameters than are present on the ADT, we fail to prove any bounds involving those parameters. This can be demonstrated with the following [code on stable](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=139b65e4294634d7286a3282bc61e628) which fails due to the fact that `<T as Trait>::Assoc == U` is not present in `Foo`s `ParamEnv` even though arguably there is no reason it cannot compiler:
```rust
struct Foo<T: Trait>(T);

trait Trait {
    type Assoc;
}

impl<T: Trait<Assoc = U>, U: ?Sized> Drop for Foo<T> {
    //~^ ERROR: `Drop` impl requires `<T as Trait>::Assoc == U` but the struct ...
    fn drop(&mut self) {}
}

fn main() {}
```

I think the motivation for supporting this code is somewhat lacking, it might be useful in practice for deeply nested associated types where you might want to be able to write:
`where T: Trait<Assoc: Other<AnotherAssoc: MoreTrait<YetAnotherAssoc: InnerTrait<Final = U>>>>`
in order to be able to just use `U` in the function body instead of writing out the whole nested associated type. Regardless I don't think there is really any reason to *not* support this code and it is relatively easy to support it.

What I find slightly more compelling is the fact that when defining a const parameter `const N: u8` we desugar that to having a where clause requiring the constant `N` is typed as `u8` (`ClauseKind::ConstArgHasType`). As we *always* desugar const parameters to have these bounds, if we attempt to prove that some const parameter `N` is of type `u8` and there is no bound on `N` in the enviroment that generally indicates usage of an incorrect `ParamEnv` (this has caught a bug already).

Given that, if we write the following code:
```rust
#![feature(associated_const_equality)]
struct Foo<T: Trait>(T);

trait Trait {
    const ASSOC: usize;
}

impl<T: Trait<ASSOC = N>, const N: usize> Drop for Foo<T> {
    fn drop(&mut self) {}
}

fn main() {}
```

The `Drop` impl would have this desugared where clause about `N` being of type `usize`, and if we were to try to prove that where clause in `Foo`'s `ParamEnv` we would ICE as there would not be any `ConstArgHasType` in the environment (which generally indicates improper `ParamEnv` usage. As this is otherwise well formed code (the `T: Trait<ASSOC = N>` causes `N` to be constrained) we have to handle this *somehow* and I believe the only principled way to support this is the changes I have made to `dropck.rs` that would cause these code examples to compiler (Perhaps we could just throw out all `ConstArgHasType` where clauses from the predicates we prove but that makes me nervous even if it might actually be okay).

## The changes

Currently the way `dropck.rs` works is that take the `ParamEnv` of the ADT and instantiate it with the generic arguments used on the self ty of the `impl`. We then instantiate the predicates of the drop impl with the identity params to the impl,  e.g. in the original example `<T as Trait>::Assoc == U` stays as `<T as Trait>::Assoc == U`. We then attempt to prove all the where clauses in the instantiated env of the self type ADT.

This PR changes us to first instantiate the impl with infer vars, then we equate the self type (with infer vars as its generic arguments) with the self type as written by the user. This causes all generic parameters on the impl that are constrained via associated type/const equality bounds to be left as inference variables while all other parameters are still `Ty`/`Const`/`Region`

Finally when instantiating the predicates on the impl, instead of using the identity arguments, we use the list of inference variables of which some have been inferred to the impl parameters. In practice this means that we wind up proving `<T as Trait>::Assoc == ?x` which can succeed just fine. In the const generics example we would wind up trying to prove `ConstArgHasType(?x: usize)` instead of `ConstArgHasType(N: usize)` which avoids the ICE as it is expected to encounter goals of the form `?x: usize`.

At a higher level the way I justify/think about this is that as we are proving goals in the environment of the ADT (`Foo` in the above examples), we do not expect to encounter generic parameters from a different environment so we must "deal with them" somehow. In this PR we handle them by replacing them with inference variables as they should either *actually* be unconstrained (and we will error later) or they are constrained to be equal to some associated type/const.

To go along with this it would be nice if we were not instantiating the adt's env with the generic arguments to the ADT in the `Drop` impl as it would make it clearer we are proving bounds in the adt's env instead of the `Drop` impl's. Instead we would map the predicates on the drop impl to be valid in the environment of the adt. In practice this causes diagnostic regressions as all of the generic parameters in errors refer to the ones defined on the adt; attempting to map these back to the ones on the impl, while possible, is involved as writing a `TypeFolder` over `FulfillmentError` is non trivial.

## Edge cases

There are some subtle interactions here:

One is that we should not allow `<T as Trait>::Assoc == U` to be present on the `Drop` if `U` is constrained by the self type of the impl and the bound is not present in the ADT's environment. demonstrated with the [following code](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=af839e2c3e43e03a624825c58af84dff):
```rust
trait Trait {
    type Assoc;
}

struct Foo<T: Trait, U: ?Sized>(T, U);

impl<T: Trait<Assoc = U>, U: ?Sized> Drop for Foo<T, U> {
    //~^ ERROR: `Drop` impl requires `<T as Trait>::Assoc == U`
    fn drop(&mut self) {}
}

fn main() {}
```
This is tested at `tests/ui/dropck/constrained_by_assoc_type_equality_and_self_ty.rs`.

Another weirdness is that we permit the following code to compile now:
```rust
struct Foo<T>(T);

impl<'a, T: 'a> Drop for Foo<T> {
    fn drop(&mut self) {}
}
```
This is caused by the fact that we permit unconstrained lifetime parameters in trait implementations as long as they are not used in associated types (so we do not wind up erroring on this code like we perhaps ought to), combined with the fact that as we are now proving `T: '?x` instead of `T: 'a` which allows proving the bound via `'?x= 'empty` wheras previously it would have failed.

This is tested as part of `tests/ui/dropck/reject-specialized-drops-8142.rs`.

---

r? `@compiler-errors`
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. F-const_trait_impl `#![feature(const_trait_impl)]` F-effects `#![feature(effects)]` 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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants