Repository: rust
Issue: `impl DefId ... was matchable` in `traits/select/mod.rs`
Labels: I-ICE,T-compiler,C-bug,requires-nightly,F-const_trait_impl

<!--
Thank you for finding an Internal Compiler Error! 🧊  If possible, try to provide
a minimal verifiable example. You can read "Rust Bug Minimization Patterns" for
how to create smaller examples.
http://blog.pnkfx.org/blog/2019/11/18/rust-bug-minimization-patterns/
-->

### Code

(reduced)
```Rust
use std::marker::Destruct;

struct S<'a>(&'a mut u8);

impl<'misaligned> const Drop for S<'a> {
}

const fn a<T: ~const Destruct>(_: T) {}

const fn b() -> u8 {
    a(S(&mut c));
}

fn main() {
    a(HasDropGlue(Box::new(0)));
}
```

<details><summary>(original code)</summary>

```Rust
// FIXME run-pass
//@ known-bug: #110395
//@ revisions: stock precise
#![feature(const_trait_impl)]
#![feature(const_mut_refs)]
#![feature(never_type)]
#![cfg_attr(precise, feature(const_precise_live_drops))]

use std::marker::Destruct;

struct S<'a>(&'a mut u8);

impl<'misaligned> const Drop for S<'a> {
    fn drop(&mut self) {
        *self.0 += 1;
    }
}

const fn a<T: ~const Destruct>(_: T) {}
//FIXME ~^ ERROR destructor of

const fn b() -> u8 {
    let mut c = 0;
    let _ = S(&mut c);
    //FIXME ~^ ERROR destructor of
    a(S(&mut c));
    c
}

const C: u8 = b();

macro_rules! implements_const_drop {
    ($($exp:expr),*$(,)?) => {
        $(
            const _: () = a($exp);
        )*
    }
}

#[allow(dead_code)]
mod t {
    pub struct Foo;
    pub enum Bar { A }
    pub fn foo() {}
    pub struct ConstDrop;

    impl const Drop for ConstDrop {
        fn drop(&mut self) {}
    }

    pub struct HasConstDrop(pub ConstDrop);
    pub struct TrivialFields(pub u8, pub i8, pub usize, pub isize);

    #[const_trait]
    pub trait SomeTrait {
        fn foo();
    }
    impl const SomeTrait for () {
        fn foo() { // Error won't happen if "foo" isn't used in "iterate" or has generics
}
    }
    // non-const impl
    impl SomeTrait for i32 {
        fn foo() {}
    }

    pub struct ConstDropWithBound<T: const SomeTrait>(pub core::marker::PhantomData<T>);

    impl<T: ~const SomeTrait> const Drop for ConstDropWithBound<T> {
        fn drop(&mut self) {
            T::foo();
        }
    }

    pub struct ConstDropWithNonconstBound<T: SomeTrait>(pub core::marker::PhantomData<T>);

    impl<T: SomeTrait> const Drop for ConstDropWithNonconstBound<T> {
        fn drop(&mut self) {
            // Note: we DON'T use the `T: SomeTrait` bound
        }
    }
}

use t::*;

implements_const_drop! {
    1u8
    2,
    3.0
    Foo,
    Bar::A,
    foo,
    ConstDrop,
    HasConstDrop(ConstDrop),
    TrivialFields(1, 2, 3, 4),
    &1,
    &1 as *const i32,
    ConstDropWithBound::<()>,
    ConstDropWithNonconstBound::<i32>,
    Result::<i32, !>::Ok(1),
}

fn main() {
    struct HasDropGlue(#[allow(dead_code)] Box<u8>);
    struct HasDropImpl;
    impl Drop for HasDropImpl {
        fn drop(&mut self) {
            println!("not trivial drop");
        }
    }

    // These types should pass because ~const in a non-const context should have no effect.
    a(HasDropGlue(Box::new(0)));
    c1::<isize>(3);

    assert_eq!(C, 2);
}

```

</details>

Command:
```
rustc ./2443DFCC80161BDDD9708036580640240C7CD84AF5C7BDAF6B7E2469578363AF.rs -Zcrate-attr=feature(effects)
```

### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:
```
rustc 1.79.0-nightly (4fd4797c2 2024-04-03)
binary: rustc
commit-hash: 4fd4797c2654977f545c9a91e2aa4e6cdbb38919
commit-date: 2024-04-03
host: x86_64-apple-darwin
release: 1.79.0-nightly
LLVM version: 18.1.2
```

### Error output

```
error[E0261]: use of undeclared lifetime name `'a`
 --> ./2443DFCC80161BDDD9708036580640240C7CD84AF5C7BDAF6B7E2469578363AF.rs:5:36
  |
5 | impl<'misaligned> const Drop for S<'a> {
  |      -                             ^^ undeclared lifetime
  |      |
  |      help: consider introducing lifetime `'a` here: `'a,`

error[E0425]: cannot find value `c` in this scope
  --> ./2443DFCC80161BDDD9708036580640240C7CD84AF5C7BDAF6B7E2469578363AF.rs:11:14
   |
3  | struct S<'a>(&'a mut u8);
   | ------------------------- similarly named tuple struct `S` defined here
...
11 |     a(S(&mut c));
   |              ^ help: a tuple struct with a similar name exists: `S`

error[E0658]: const trait impls are experimental
 --> ./2443DFCC80161BDDD9708036580640240C7CD84AF5C7BDAF6B7E2469578363AF.rs:5:19
  |
5 | impl<'misaligned> const Drop for S<'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-04-03; consider upgrading it if it is out of date

error[E0658]: const trait impls are experimental
 --> ./2443DFCC80161BDDD9708036580640240C7CD84AF5C7BDAF6B7E2469578363AF.rs:8:15
  |
8 | const fn a<T: ~const Destruct>(_: T) {}
  |               ^^^^^^
  |
  = 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-04-03; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature 'const_trait_impl'
 --> ./2443DFCC80161BDDD9708036580640240C7CD84AF5C7BDAF6B7E2469578363AF.rs:1:5
  |
1 | use std::marker::Destruct;
  |     ^^^^^^^^^^^^^^^^^^^^^
  |
  = 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-04-03; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature 'const_trait_impl'
 --> ./2443DFCC80161BDDD9708036580640240C7CD84AF5C7BDAF6B7E2469578363AF.rs:8:22
  |
8 | const fn a<T: ~const Destruct>(_: T) {}
  |                      ^^^^^^^^
  |
  = 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-04-03; consider upgrading it if it is out of date

error: const `impl` for trait `Drop` which is not marked with `#[const_trait]`
 --> ./2443DFCC80161BDDD9708036580640240C7CD84AF5C7BDAF6B7E2469578363AF.rs:5:25
  |
5 | impl<'misaligned> const Drop for S<'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
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary><strong>Backtrace</strong></summary>
<p>

```
error: internal compiler error: compiler/rustc_trait_selection/src/traits/select/mod.rs:2416:17: impl DefId(0:8 ~ 2443DFCC80161BDDD9708036580640240C7CD84AF5C7BDAF6B7E2469578363AF[c78c]::{impl#0}) was matchable against Binder { value: TraitPredicate(<S<'_> as std::ops::Drop>, polarity:Positive), bound_vars: [] } but now is not

thread 'rustc' panicked at compiler/rustc_trait_selection/src/traits/select/mod.rs:2416:17:
Box<dyn Any>
stack backtrace:
   0:        0x104692797 - std::backtrace::Backtrace::create::hde801799bfcf1a18
   1:        0x1046926e5 - std::backtrace::Backtrace::force_capture::h19ae693dac8964df
   2:        0x10d23dcfb - std[38e870d6de0dab70]::panicking::update_hook::<alloc[43ccfcebb6346d8b]::boxed::Box<rustc_driver_impl[b21b79e6d003739c]::install_ice_hook::{closure#0}>>::{closure#0}
   3:        0x1046ac564 - std::panicking::rust_panic_with_hook::h980ef0202c45a742
   4:        0x10d2a94ea - std[38e870d6de0dab70]::panicking::begin_panic::<rustc_errors[a7eddf5c294720cd]::ExplicitBug>::{closure#0}
   5:        0x10d2a2739 - std[38e870d6de0dab70]::sys_common::backtrace::__rust_end_short_backtrace::<std[38e870d6de0dab70]::panicking::begin_panic<rustc_errors[a7eddf5c294720cd]::ExplicitBug>::{closure#0}, !>
   6:        0x11196fc99 - std[38e870d6de0dab70]::panicking::begin_panic::<rustc_errors[a7eddf5c294720cd]::ExplicitBug>
   7:        0x10d2bcc36 - <rustc_errors[a7eddf5c294720cd]::diagnostic::BugAbort as rustc_errors[a7eddf5c294720cd]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   8:        0x10dd79a3e - rustc_middle[292fdd7a6f274851]::util::bug::opt_span_bug_fmt::<rustc_span[c77fbea70134c538]::span_encoding::Span>::{closure#0}
   9:        0x10dd2bd77 - rustc_middle[292fdd7a6f274851]::ty::context::tls::with_opt::<rustc_middle[292fdd7a6f274851]::util::bug::opt_span_bug_fmt<rustc_span[c77fbea70134c538]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  10:        0x10dd2b885 - rustc_middle[292fdd7a6f274851]::ty::context::tls::with_context_opt::<rustc_middle[292fdd7a6f274851]::ty::context::tls::with_opt<rustc_middle[292fdd7a6f274851]::util::bug::opt_span_bug_fmt<rustc_span[c77fbea70134c538]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  11:        0x111a2cdbb - rustc_middle[292fdd7a6f274851]::util::bug::bug_fmt
  12:        0x10ebb67f8 - <rustc_trait_selection[728e631cab799dc1]::traits::select::SelectionContext>::rematch_impl
  13:        0x10ec409c0 - <rustc_trait_selection[728e631cab799dc1]::traits::select::SelectionContext>::confirm_candidate
  14:        0x10ec4bef0 - <rustc_trait_selection[728e631cab799dc1]::traits::select::SelectionContext>::poly_select
  15:        0x10ec2b3c8 - <rustc_trait_selection[728e631cab799dc1]::traits::fulfill::FulfillProcessor>::process_trait_obligation
  16:        0x10ec2a069 - <rustc_trait_selection[728e631cab799dc1]::traits::fulfill::FulfillProcessor as rustc_data_structures[4cc946c9725e2121]::obligation_forest::ObligationProcessor>::process_obligation
  17:        0x10ea72a6d - <rustc_data_structures[4cc946c9725e2121]::obligation_forest::ObligationForest<rustc_trait_selection[728e631cab799dc1]::traits::fulfill::PendingPredicateObligation>>::process_obligations::<rustc_trait_selection[728e631cab799dc1]::traits::fulfill::FulfillProcessor>
  18:        0x10eb90d35 - <rustc_trait_selection[728e631cab799dc1]::traits::fulfill::FulfillmentContext as rustc_infer[ae398644a80ea5ed]::traits::engine::TraitEngine>::select_where_possible
  19:        0x10d6e217a - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_argument_types
  20:        0x10d69bb75 - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::confirm_builtin_call
  21:        0x10d699f35 - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_call
  22:        0x10d796e29 - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_expr_kind
  23:        0x10d6b95db - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  24:        0x10d6f05c6 - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_stmt
  25:        0x10d6f0dac - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_block_with_expected
  26:        0x10d6b95db - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  27:        0x10d6bb595 - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_return_expr
  28:        0x10d78103e - rustc_hir_typeck[b79567a4313f5def]::check::check_fn
  29:        0x10d7777e9 - rustc_hir_typeck[b79567a4313f5def]::typeck
  30:        0x10e66c11a - rustc_query_impl[3605ca4bc1475aca]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[3605ca4bc1475aca]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[292fdd7a6f274851]::query::erase::Erased<[u8; 8usize]>>
  31:        0x10e4ef00c - rustc_query_system[854611ef8e5fc0a7]::query::plumbing::try_execute_query::<rustc_query_impl[3605ca4bc1475aca]::DynamicConfig<rustc_query_system[854611ef8e5fc0a7]::query::caches::VecCache<rustc_hir[9efa7d06bb4f22]::hir_id::OwnerId, rustc_middle[292fdd7a6f274851]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[3605ca4bc1475aca]::plumbing::QueryCtxt, false>
  32:        0x10e68ef1b - rustc_query_impl[3605ca4bc1475aca]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace
  33:        0x10d47a3e7 - <rustc_middle[292fdd7a6f274851]::hir::map::Map>::par_body_owners::<rustc_hir_analysis[76877c2d5917afe]::check_crate::{closure#4}>::{closure#0}
  34:        0x10d58200c - rustc_hir_analysis[76877c2d5917afe]::check_crate
  35:        0x10d9e8470 - rustc_interface[9070b66c4f6dc5ef]::passes::analysis
  36:        0x10e66c16a - rustc_query_impl[3605ca4bc1475aca]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[3605ca4bc1475aca]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[292fdd7a6f274851]::query::erase::Erased<[u8; 1usize]>>
  37:        0x10e456b6e - rustc_query_system[854611ef8e5fc0a7]::query::plumbing::try_execute_query::<rustc_query_impl[3605ca4bc1475aca]::DynamicConfig<rustc_query_system[854611ef8e5fc0a7]::query::caches::SingleCache<rustc_middle[292fdd7a6f274851]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[3605ca4bc1475aca]::plumbing::QueryCtxt, false>
  38:        0x10e676537 - rustc_query_impl[3605ca4bc1475aca]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  39:        0x10d1ee547 - <rustc_interface[9070b66c4f6dc5ef]::queries::QueryResult<&rustc_middle[292fdd7a6f274851]::ty::context::GlobalCtxt>>::enter::<core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>, rustc_driver_impl[b21b79e6d003739c]::run_compiler::{closure#0}::{closure#1}::{closure#3}>
  40:        0x10d244d1f - rustc_interface[9070b66c4f6dc5ef]::interface::run_compiler::<core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>, rustc_driver_impl[b21b79e6d003739c]::run_compiler::{closure#0}>::{closure#0}
  41:        0x10d237cad - std[38e870d6de0dab70]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[9070b66c4f6dc5ef]::util::run_in_thread_with_globals<rustc_interface[9070b66c4f6dc5ef]::util::run_in_thread_pool_with_globals<rustc_interface[9070b66c4f6dc5ef]::interface::run_compiler<core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>, rustc_driver_impl[b21b79e6d003739c]::run_compiler::{closure#0}>::{closure#0}, core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>>::{closure#0}, core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>>
  42:        0x10d24ab7f - <<std[38e870d6de0dab70]::thread::Builder>::spawn_unchecked_<rustc_interface[9070b66c4f6dc5ef]::util::run_in_thread_with_globals<rustc_interface[9070b66c4f6dc5ef]::util::run_in_thread_pool_with_globals<rustc_interface[9070b66c4f6dc5ef]::interface::run_compiler<core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>, rustc_driver_impl[b21b79e6d003739c]::run_compiler::{closure#0}>::{closure#0}, core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>>::{closure#0}, core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>>::{closure#1} as core[14fbfc68cf988ae3]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  43:        0x1046b574b - std::sys::pal::unix::thread::Thread::new::thread_start::hb7c71ee179e441c0
  44:     0x7ff818270202 - __pthread_start


rustc version: 1.79.0-nightly (4fd4797c2 2024-04-03)
platform: x86_64-apple-darwin

query stack during panic:
#0 [typeck] type-checking `b`
#1 [analysis] running analysis passes on this crate
end of query stack
```

</p>
</details>