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 without feature(effects) #126889

Closed
Naserume opened this issue Jun 24, 2024 · 4 comments · Fixed by #127220
Closed

ICE: None in compiler/rustc_middle/src/ty/sty.rs without feature(effects) #126889

Naserume opened this issue Jun 24, 2024 · 4 comments · Fixed by #127220
Assignees
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-low Low priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Naserume
Copy link

Code

Related issue is #126378. This ices without #![feature(effects)].

(reduced)

struct Foo {}

impl<const Ca: usize> Drop for Foo {}

fn main() {}

(original)

struct Foo {
    bar: Bar,
}

struct Bar {
    qux: i32,
}

// Issue 8142: Test that Drop impls cannot be specialized beyond the
// predicates attached to the type definition itself.
trait Bound { fn foo(&self) { } }
struct K<'l1,'l2> { x: &'l1 i8, y: &'l2 u8 }
struct L<'l1,'l2> { x: &'l1 i8, y: &'l2 u8 }
struct M<'m> { x: &'m i8 }
struct N<'n> { x: &'n i8 }
struct O<To> { x: *const To }
struct P<Tp> { x: *const Tp }
struct Q<Tq> { x: *const Tq }
struct R<Tr> { x: *const Tr }
struct S<Ts:Bound> { x: *const Ts }
struct T<'t,Ts:'t> { x: &'t Ts }
struct U;
struct V<Tva, Tvb> { x: *const Tva, y: *const Tvb }
struct W<'l1, 'l2> { x: &'l1 i8, y: &'l2 u8 }
struct X<const Ca: usize>;
struct Y<const Ca: usize, const Cb: usize>;

enum Enum<T> { Variant(T) }
struct TupleStruct<T>(T);
union Union<T: Copy> { f: T }

impl<'al,'adds_bnd:'al> Drop for K<'al,'adds_bnd> {                        // REJECT
    //~^ ERROR `Drop` impl requires `'adds_bnd: 'al`
    fn drop(&mut self) { } }

impl<'al,'adds_bnd>     Drop for L<'al,'adds_bnd> where 'adds_bnd:'al {    // REJECT
    //~^ ERROR `Drop` impl requires `'adds_bnd: 'al`
    fn drop(&mut self) { } }

impl<'ml>               Drop for M<'ml>         { fn drop(&mut self) { } } // ACCEPT

impl                    Drop for N<'static>     { fn drop(&mut self) { } } // REJECT
//~^ ERROR `Drop` impls cannot be specialized

impl<COkNoBound> Drop for O<COkNoBound> { fn drop(&mut self) { } } // ACCEPT

impl              Drop for P<i8>          { fn drop(&mut self) { } } // REJECT
//~^ ERROR `Drop` impls cannot be specialized

impl<AddsBnd:Bound> Drop for Q<AddsBnd> { fn drop(&mut self) { } } // REJECT
//~^ ERROR `Drop` impl requires `AddsBnd: Bound`

impl<'rbnd,AddsRBnd:'rbnd> Drop for R<AddsRBnd> { fn drop(&mut self) { } } // REJECT
//~^ ERROR `Drop` impl requires `AddsRBnd: 'rbnd`

impl<Bs:Bound>    Drop for S<Bs>          { fn drop(&mut self) { } } // ACCEPT

impl<'t,Bt:'t>    Drop for T<'t,Bt>       { fn drop(&mut self) { } } // ACCEPT

impl              Drop for U              { fn drop(&mut self) { } } // ACCEPT

impl<One>         Drop for V<One,One>     { fn drop(&mut self) { } } // REJECT
//~^ ERROR `Drop` impls cannot be specialized

impl<'lw>         Drop for W<'lw,'lw>     { fn drop(&mut self) { } } // REJECT
//~^ ERROR `Drop` impls cannot be specialized

impl              Drop for X<3>           { fn drop(&mut self) { } } // REJECT
//~^ ERROR `Drop` impls cannot be specialized

impl<const Ca: usize> Drop for Foo     { fn drop(&mut self) { } } // REJECT
//~^ ERROR `Drop` impls cannot be specialized

impl<AddsBnd:Bound> Drop for Enum<AddsBnd> { fn drop(&mut self) { } } // REJECT
//~^ ERROR `Drop` impl requires `AddsBnd: Bound`

impl<AddsBnd:Bound> Drop for TupleStruct<AddsBnd> { fn drop(&mut self) { } } // REJECT
//~^ ERROR `Drop` impl requires `AddsBnd: Bound`

impl<AddsBnd:Copy + Bound> Drop for Union<AddsBnd> { fn drop(&mut self) { } } // REJECT
//~^ ERROR `Drop` impl requires `AddsBnd: Bound`

pub fn main() { }

Meta

rustc --version --verbose:

rustc 1.81.0-nightly (bcf94dec5 2024-06-23)
binary: rustc
commit-hash: bcf94dec5ba6838e435902120c0384c360126a26
commit-date: 2024-06-23
host: x86_64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7

Error output

error[E0207]: the const parameter `Ca` is not constrained by the impl trait, self type, or predicates
 --> ./D2859.rs:4:6
  |
4 | impl<const Ca: usize> Drop for Foo {}
  |      ^^^^^^^^^^^^^^^ 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 1 previous error

For more information about this error, try `rustc --explain E0207`.
Backtrace

thread 'rustc' panicked at compiler/rustc_middle/src/ty/sty.rs:360:36:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0:        0x106fe6b43 - <std::sys::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::he71f7af0ddafc64d
   1:        0x10703225b - core::fmt::write::h30d0e266faff29b6
   2:        0x106fdcace - std::io::Write::write_fmt::he666ca78eb4b8406
   3:        0x106fe6931 - std::sys::backtrace::print::h22721f9ab9a2749a
   4:        0x106fe9709 - std::panicking::default_hook::{{closure}}::h145e29ed875d8021
   5:        0x106fe948a - std::panicking::default_hook::h5d437c7e6bf0e3b5
   6:        0x11028147c - std[8efa578c02603ef9]::panicking::update_hook::<alloc[135510a28be92df]::boxed::Box<rustc_driver_impl[63f2171535a6fe49]::install_ice_hook::{closure#0}>>::{closure#0}
   7:        0x106fea326 - std::panicking::rust_panic_with_hook::h6889093a56e48d04
   8:        0x106fe9b92 - std::panicking::begin_panic_handler::{{closure}}::hedafaff529964e0a
   9:        0x106fe7019 - std::sys::backtrace::__rust_end_short_backtrace::h15452907bf21f649
  10:        0x106fe989c - _rust_begin_unwind
  11:        0x10704d4aa - core::panicking::panic_fmt::hd76eb95c9d9ba8b3
  12:        0x10704d554 - core::panicking::panic::hb329c30fe2728c0f
  13:        0x10704d3f8 - core::option::unwrap_failed::hb04517061396b912
  14:        0x110f927e8 - <rustc_middle[4384e439927f71dd]::ty::sty::ParamConst>::find_ty_from_env
  15:        0x111f60d6c - <rustc_trait_selection[2bb3b73d2222b57e]::traits::fulfill::FulfillProcessor as rustc_data_structures[ff15d8e8c9f969c2]::obligation_forest::ObligationProcessor>::process_obligation
  16:        0x111d86a2b - <rustc_data_structures[ff15d8e8c9f969c2]::obligation_forest::ObligationForest<rustc_trait_selection[2bb3b73d2222b57e]::traits::fulfill::PendingPredicateObligation>>::process_obligations::<rustc_trait_selection[2bb3b73d2222b57e]::traits::fulfill::FulfillProcessor>
  17:        0x111eca542 - <rustc_trait_selection[2bb3b73d2222b57e]::traits::fulfill::FulfillmentContext<rustc_trait_selection[2bb3b73d2222b57e]::traits::FulfillmentError> as rustc_infer[48e5f050b52d2ea4]::traits::engine::TraitEngine<rustc_trait_selection[2bb3b73d2222b57e]::traits::FulfillmentError>>::select_where_possible
  18:        0x111ea2b4f - <rustc_trait_selection[2bb3b73d2222b57e]::traits::fulfill::FulfillmentContext<rustc_trait_selection[2bb3b73d2222b57e]::traits::FulfillmentError> as rustc_infer[48e5f050b52d2ea4]::traits::engine::TraitEngine<rustc_trait_selection[2bb3b73d2222b57e]::traits::FulfillmentError>>::select_all_or_error
  19:        0x1105a599d - rustc_hir_analysis[8e62bc6471a04889]::check::dropck::check_drop_impl
  20:        0x1104f418b - <rustc_middle[4384e439927f71dd]::ty::context::TyCtxt>::calculate_dtor::<rustc_hir_analysis[8e62bc6471a04889]::check::dropck::check_drop_impl>::{closure#0}
  21:        0x1105ca1b0 - rustc_hir_analysis[8e62bc6471a04889]::check::adt_destructor
  22:        0x11191cac2 - rustc_query_impl[a24e9023943e74ab]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a24e9023943e74ab]::query_impl::adt_destructor::dynamic_query::{closure#2}::{closure#0}, rustc_middle[4384e439927f71dd]::query::erase::Erased<[u8; 12usize]>>
  23:        0x1118b245e - <rustc_query_impl[a24e9023943e74ab]::query_impl::adt_destructor::dynamic_query::{closure#2} as core[9412a59d758dca42]::ops::function::FnOnce<(rustc_middle[4384e439927f71dd]::ty::context::TyCtxt, rustc_span[aed5394308446158]::def_id::DefId)>>::call_once
  24:        0x1116f9c4e - rustc_query_system[30bd0146b5104221]::query::plumbing::try_execute_query::<rustc_query_impl[a24e9023943e74ab]::DynamicConfig<rustc_query_system[30bd0146b5104221]::query::caches::DefIdCache<rustc_middle[4384e439927f71dd]::query::erase::Erased<[u8; 12usize]>>, false, false, false>, rustc_query_impl[a24e9023943e74ab]::plumbing::QueryCtxt, false>
  25:        0x11193b2dd - rustc_query_impl[a24e9023943e74ab]::query_impl::adt_destructor::get_query_non_incr::__rust_end_short_backtrace
  26:        0x110fb35c0 - rustc_middle[4384e439927f71dd]::query::plumbing::query_get_at::<rustc_query_system[30bd0146b5104221]::query::caches::DefIdCache<rustc_middle[4384e439927f71dd]::query::erase::Erased<[u8; 12usize]>>>
  27:        0x11057ca2a - rustc_hir_analysis[8e62bc6471a04889]::check::check::check_item_type
  28:        0x1105b64ae - rustc_hir_analysis[8e62bc6471a04889]::check::wfcheck::check_well_formed
  29:        0x11191e86c - rustc_query_impl[a24e9023943e74ab]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a24e9023943e74ab]::query_impl::check_well_formed::dynamic_query::{closure#2}::{closure#0}, rustc_middle[4384e439927f71dd]::query::erase::Erased<[u8; 1usize]>>
  30:        0x11179701f - rustc_query_system[30bd0146b5104221]::query::plumbing::try_execute_query::<rustc_query_impl[a24e9023943e74ab]::DynamicConfig<rustc_query_system[30bd0146b5104221]::query::caches::VecCache<rustc_hir[ae2456c39d7f6170]::hir_id::OwnerId, rustc_middle[4384e439927f71dd]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[a24e9023943e74ab]::plumbing::QueryCtxt, false>
  31:        0x111968280 - rustc_query_impl[a24e9023943e74ab]::query_impl::check_well_formed::get_query_non_incr::__rust_end_short_backtrace
  32:        0x1104acba2 - rustc_middle[4384e439927f71dd]::query::plumbing::query_ensure_error_guaranteed::<rustc_query_system[30bd0146b5104221]::query::caches::VecCache<rustc_hir[ae2456c39d7f6170]::hir_id::OwnerId, rustc_middle[4384e439927f71dd]::query::erase::Erased<[u8; 1usize]>>, ()>
  33:        0x1105c4de1 - rustc_hir_analysis[8e62bc6471a04889]::check::wfcheck::check_mod_type_wf
  34:        0x11191e83c - rustc_query_impl[a24e9023943e74ab]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a24e9023943e74ab]::query_impl::check_mod_type_wf::dynamic_query::{closure#2}::{closure#0}, rustc_middle[4384e439927f71dd]::query::erase::Erased<[u8; 1usize]>>
  35:        0x11174db79 - rustc_query_system[30bd0146b5104221]::query::plumbing::try_execute_query::<rustc_query_impl[a24e9023943e74ab]::DynamicConfig<rustc_query_system[30bd0146b5104221]::query::caches::DefaultCache<rustc_span[aed5394308446158]::def_id::LocalModDefId, rustc_middle[4384e439927f71dd]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[a24e9023943e74ab]::plumbing::QueryCtxt, false>
  36:        0x111944ed0 - rustc_query_impl[a24e9023943e74ab]::query_impl::check_mod_type_wf::get_query_non_incr::__rust_end_short_backtrace
  37:        0x11064024c - rustc_hir_analysis[8e62bc6471a04889]::check_crate
  38:        0x110bda6cb - rustc_interface[73216d77aad0c3a5]::passes::run_required_analyses
  39:        0x110bdccb3 - rustc_interface[73216d77aad0c3a5]::passes::analysis
  40:        0x111922dec - rustc_query_impl[a24e9023943e74ab]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a24e9023943e74ab]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[4384e439927f71dd]::query::erase::Erased<[u8; 1usize]>>
  41:        0x11170249e - rustc_query_system[30bd0146b5104221]::query::plumbing::try_execute_query::<rustc_query_impl[a24e9023943e74ab]::DynamicConfig<rustc_query_system[30bd0146b5104221]::query::caches::SingleCache<rustc_middle[4384e439927f71dd]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[a24e9023943e74ab]::plumbing::QueryCtxt, false>
  42:        0x11192d307 - rustc_query_impl[a24e9023943e74ab]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  43:        0x1102258d7 - <rustc_interface[73216d77aad0c3a5]::queries::QueryResult<&rustc_middle[4384e439927f71dd]::ty::context::GlobalCtxt>>::enter::<core[9412a59d758dca42]::result::Result<(), rustc_span[aed5394308446158]::ErrorGuaranteed>, rustc_driver_impl[63f2171535a6fe49]::run_compiler::{closure#0}::{closure#1}::{closure#3}>
  44:        0x11027fc94 - rustc_interface[73216d77aad0c3a5]::interface::run_compiler::<core[9412a59d758dca42]::result::Result<(), rustc_span[aed5394308446158]::ErrorGuaranteed>, rustc_driver_impl[63f2171535a6fe49]::run_compiler::{closure#0}>::{closure#1}
  45:        0x11026ec91 - std[8efa578c02603ef9]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[73216d77aad0c3a5]::util::run_in_thread_with_globals<rustc_interface[73216d77aad0c3a5]::util::run_in_thread_pool_with_globals<rustc_interface[73216d77aad0c3a5]::interface::run_compiler<core[9412a59d758dca42]::result::Result<(), rustc_span[aed5394308446158]::ErrorGuaranteed>, rustc_driver_impl[63f2171535a6fe49]::run_compiler::{closure#0}>::{closure#1}, core[9412a59d758dca42]::result::Result<(), rustc_span[aed5394308446158]::ErrorGuaranteed>>::{closure#0}, core[9412a59d758dca42]::result::Result<(), rustc_span[aed5394308446158]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[9412a59d758dca42]::result::Result<(), rustc_span[aed5394308446158]::ErrorGuaranteed>>
  46:        0x11028a3a6 - <<std[8efa578c02603ef9]::thread::Builder>::spawn_unchecked_<rustc_interface[73216d77aad0c3a5]::util::run_in_thread_with_globals<rustc_interface[73216d77aad0c3a5]::util::run_in_thread_pool_with_globals<rustc_interface[73216d77aad0c3a5]::interface::run_compiler<core[9412a59d758dca42]::result::Result<(), rustc_span[aed5394308446158]::ErrorGuaranteed>, rustc_driver_impl[63f2171535a6fe49]::run_compiler::{closure#0}>::{closure#1}, core[9412a59d758dca42]::result::Result<(), rustc_span[aed5394308446158]::ErrorGuaranteed>>::{closure#0}, core[9412a59d758dca42]::result::Result<(), rustc_span[aed5394308446158]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[9412a59d758dca42]::result::Result<(), rustc_span[aed5394308446158]::ErrorGuaranteed>>::{closure#2} as core[9412a59d758dca42]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  47:        0x106ff352b - std::sys::pal::unix::thread::Thread::new::thread_start::h68d49fa26efc1659
  48:     0x7ff801f5318b - __pthread_start

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: please attach the file at `/Users/Documents/2024-06-24-17224.txt` to your bug report

query stack during panic:
#0 [adt_destructor] computing `Drop` impl for `Foo`
#1 [check_well_formed] checking that `Foo` is well-formed
end of query stack

@Naserume Naserume 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 Jun 24, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 24, 2024
@matthiaskrgr
Copy link
Member

Isn't this just what #126378 would desugar too? 🤔
cc @BoxyUwU

@GrigorenkoPV
Copy link
Contributor

searched nightlies: from nightly-2022-02-19 to nightly-2024-06-24
regressed nightly: nightly-2024-06-07
searched commit range: 72fdf91...98489f2
regressed commit: 003a902

#125958

@BoxyUwU
Copy link
Member

BoxyUwU commented Jun 24, 2024

Yeah this should be the same underlying cause as #126378 so I'll assign this to myself too

@BoxyUwU BoxyUwU self-assigned this Jun 24, 2024
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 5, 2024
@BoxyUwU BoxyUwU added P-low Low priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. labels Jul 15, 2024
@BoxyUwU
Copy link
Member

BoxyUwU commented Jul 15, 2024

Adding P-low since this was error->ICE and we already have a fix PR open

@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. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-low Low priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. 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.

6 participants