Repository: rust
Issue: access outside the bounds for given AllocRange
Labels: I-ICE,T-compiler,C-bug,S-bug-has-test,F-associated_const_equality,F-generic_const_items

<!--
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

```Rust
#![feature(generic_const_exprs, generic_arg_infer, generic_const_items, associated_const_equality, adt_const_params)]
#![allow(incomplete_features)]

fn foo<const N: usize, const M: usize>(_: [(); N + 1 + M]) {}

#![a]

trait Owner {
    const C<const N: u32>: u32;
}

impl Owner for () {
    const C<const N: u32>: u32 = N;
}

fn take0<const N: usize>(_: impl Owner<C<N> = { N }>) {}

fn main() {
    take0::<128>(());
}
```


### 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.83.0-nightly (7608018cb 2024-09-29)
binary: rustc
commit-hash: 7608018cbdac9e55d0d13529cf43adc33d53efcf
commit-date: 2024-09-29
host: x86_64-apple-darwin
release: 1.83.0-nightly
LLVM version: 19.1.0
```

### Error output

```
error: an inner attribute is not permitted in this context
  --> ./9A756-1.rs:11:1
   |
11 |   #![this_just_works]
   |   ^^^^^^^^^^^^^^^^^^^
12 |
13 | / trait Owner {
14 | |     const C<const N: u32>: u32;
15 | | }
   | |_- the inner attribute doesn't annotate this trait
   |
   = note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files

error: the constant `N` is not of type `u32`
  --> ./9A756-1.rs:21:40
   |
21 | fn take0<const N: usize>(_: impl Owner<C<N> = { N }>) {}
   |                                        ^^^^^^^^^^^^ expected `u32`, found `usize`
   |
note: required by a const generic parameter in `Owner::C`
  --> ./9A756-1.rs:14:13
   |
14 |     const C<const N: u32>: u32;
   |             ^^^^^^^^^^^^ required by this const generic parameter in `Owner::C`
```

<!--
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>

```
thread 'rustc' panicked at /rustc/7608018cbdac9e55d0d13529cf43adc33d53efcf/compiler/rustc_middle/src/mir/interpret/allocation.rs:268:9:
access outside the bounds for given AllocRange
stack backtrace:
   0:        0x10d3193e7 - std::backtrace::Backtrace::create::h697070a74b47c5a1
   1:        0x10d319335 - std::backtrace::Backtrace::force_capture::hd4e20dc44637c239
   2:        0x10b13a7ce - std[a533c313b02b6cfe]::panicking::update_hook::<alloc[3194a7a73d70d243]::boxed::Box<rustc_driver_impl[e0e0c21a20d76ec4]::install_ice_hook::{closure#0}>>::{closure#0}
   3:        0x10d334af8 - std::panicking::rust_panic_with_hook::h9b093984c0b2ae3e
   4:        0x10d334735 - std::panicking::begin_panic_handler::{{closure}}::h350248c9538498ad
   5:        0x10d331f49 - std::sys::backtrace::__rust_end_short_backtrace::h51a3ed22bf72bc03
   6:        0x10d3343ac - _rust_begin_unwind
   7:        0x10ff4ff8f - core::panicking::panic_fmt::ha29ff0ee788aef57
   8:        0x10b001b9f - <rustc_middle[3d266d78c9b198e4]::mir::interpret::allocation::AllocRange>::subrange
   9:        0x10b0862dd - <rustc_const_eval[298ced3d0b3bd135]::interpret::memory::AllocRefMut<rustc_middle[3d266d78c9b198e4]::mir::interpret::pointer::CtfeProvenance, ()>>::write_scalar
  10:        0x10b097b4a - <rustc_const_eval[298ced3d0b3bd135]::interpret::eval_context::InterpCx<rustc_const_eval[298ced3d0b3bd135]::const_eval::machine::CompileTimeMachine>>::write_immediate_to_mplace_no_validate
  11:        0x10b097977 - <rustc_const_eval[298ced3d0b3bd135]::interpret::eval_context::InterpCx<rustc_const_eval[298ced3d0b3bd135]::const_eval::machine::CompileTimeMachine>>::write_immediate_no_validate::<rustc_const_eval[298ced3d0b3bd135]::interpret::place::MPlaceTy>
  12:        0x10b0c993b - <rustc_const_eval[298ced3d0b3bd135]::interpret::eval_context::InterpCx<rustc_const_eval[298ced3d0b3bd135]::const_eval::machine::CompileTimeMachine>>::copy_op_no_validate::<rustc_const_eval[298ced3d0b3bd135]::interpret::operand::OpTy, rustc_const_eval[298ced3d0b3bd135]::interpret::place::MPlaceTy>
  13:        0x10b0c278b - <rustc_const_eval[298ced3d0b3bd135]::interpret::eval_context::InterpCx<rustc_const_eval[298ced3d0b3bd135]::const_eval::machine::CompileTimeMachine>>::return_from_current_stack_frame
  14:        0x10b0a1446 - <rustc_const_eval[298ced3d0b3bd135]::interpret::eval_context::InterpCx<rustc_const_eval[298ced3d0b3bd135]::const_eval::machine::CompileTimeMachine>>::eval_terminator
  15:        0x10b0b9cd4 - rustc_const_eval[298ced3d0b3bd135]::const_eval::eval_queries::eval_to_allocation_raw_provider
  16:        0x10c980cfc - rustc_query_impl[75f1704df0e38289]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[75f1704df0e38289]::query_impl::eval_to_allocation_raw::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3d266d78c9b198e4]::query::erase::Erased<[u8; 24usize]>>
  17:        0x10c93173e - <rustc_query_impl[75f1704df0e38289]::query_impl::eval_to_allocation_raw::dynamic_query::{closure#2} as core[907a20686bf8bf7a]::ops::function::FnOnce<(rustc_middle[3d266d78c9b198e4]::ty::context::TyCtxt, rustc_middle[3d266d78c9b198e4]::ty::ParamEnvAnd<rustc_middle[3d266d78c9b198e4]::mir::interpret::GlobalId>)>>::call_once
  18:        0x10c748579 - rustc_query_system[13e780f7d1d88911]::query::plumbing::try_execute_query::<rustc_query_impl[75f1704df0e38289]::DynamicConfig<rustc_query_system[13e780f7d1d88911]::query::caches::DefaultCache<rustc_middle[3d266d78c9b198e4]::ty::ParamEnvAnd<rustc_middle[3d266d78c9b198e4]::mir::interpret::GlobalId>, rustc_middle[3d266d78c9b198e4]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[75f1704df0e38289]::plumbing::QueryCtxt, false>
  19:        0x10c9b823c - rustc_query_impl[75f1704df0e38289]::query_impl::eval_to_allocation_raw::get_query_non_incr::__rust_end_short_backtrace
  20:        0x10afc62a2 - rustc_middle[3d266d78c9b198e4]::query::plumbing::query_get_at::<rustc_query_system[13e780f7d1d88911]::query::caches::DefaultCache<rustc_middle[3d266d78c9b198e4]::ty::ParamEnvAnd<rustc_middle[3d266d78c9b198e4]::mir::interpret::GlobalId>, rustc_middle[3d266d78c9b198e4]::query::erase::Erased<[u8; 24usize]>>>
  21:        0x10b05e831 - rustc_const_eval[298ced3d0b3bd135]::const_eval::valtrees::eval_to_valtree
  22:        0x10bae8d03 - <rustc_const_eval[298ced3d0b3bd135]::provide::{closure#0} as core[907a20686bf8bf7a]::ops::function::FnOnce<(rustc_middle[3d266d78c9b198e4]::ty::context::TyCtxt, rustc_middle[3d266d78c9b198e4]::ty::ParamEnvAnd<rustc_middle[3d266d78c9b198e4]::mir::interpret::GlobalId>)>>::call_once
  23:        0x10c97ee5c - rustc_query_impl[75f1704df0e38289]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[75f1704df0e38289]::query_impl::eval_to_valtree::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3d266d78c9b198e4]::query::erase::Erased<[u8; 24usize]>>
  24:        0x10c910a7e - <rustc_query_impl[75f1704df0e38289]::query_impl::eval_to_valtree::dynamic_query::{closure#2} as core[907a20686bf8bf7a]::ops::function::FnOnce<(rustc_middle[3d266d78c9b198e4]::ty::context::TyCtxt, rustc_middle[3d266d78c9b198e4]::ty::ParamEnvAnd<rustc_middle[3d266d78c9b198e4]::mir::interpret::GlobalId>)>>::call_once
  25:        0x10c748579 - rustc_query_system[13e780f7d1d88911]::query::plumbing::try_execute_query::<rustc_query_impl[75f1704df0e38289]::DynamicConfig<rustc_query_system[13e780f7d1d88911]::query::caches::DefaultCache<rustc_middle[3d266d78c9b198e4]::ty::ParamEnvAnd<rustc_middle[3d266d78c9b198e4]::mir::interpret::GlobalId>, rustc_middle[3d266d78c9b198e4]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[75f1704df0e38289]::plumbing::QueryCtxt, false>
  26:        0x10c9b946c - rustc_query_impl[75f1704df0e38289]::query_impl::eval_to_valtree::get_query_non_incr::__rust_end_short_backtrace
  27:        0x10bf74b88 - rustc_middle[3d266d78c9b198e4]::query::plumbing::query_get_at::<rustc_query_system[13e780f7d1d88911]::query::caches::DefaultCache<rustc_middle[3d266d78c9b198e4]::ty::ParamEnvAnd<rustc_middle[3d266d78c9b198e4]::mir::interpret::GlobalId>, rustc_middle[3d266d78c9b198e4]::query::erase::Erased<[u8; 24usize]>>>
  28:        0x10bf796f3 - <rustc_middle[3d266d78c9b198e4]::ty::context::TyCtxt>::const_eval_global_id_for_typeck
  29:        0x10bf79028 - <rustc_middle[3d266d78c9b198e4]::ty::context::TyCtxt>::const_eval_resolve_for_typeck
  30:        0x10b9c469a - <rustc_infer[9b67ab4c7e1e7e36]::infer::InferCtxt>::const_eval_resolve
  31:        0x10b9b3c9a - <rustc_infer[9b67ab4c7e1e7e36]::infer::InferCtxt>::try_const_eval_resolve
  32:        0x10d0fd1cf - <rustc_trait_selection[1363494f2f4ec6d3]::traits::fulfill::FulfillProcessor as rustc_data_structures[3069d1c4d7b76162]::obligation_forest::ObligationProcessor>::process_obligation::{closure#0}
  33:        0x10d0fc596 - <rustc_trait_selection[1363494f2f4ec6d3]::traits::fulfill::FulfillProcessor as rustc_data_structures[3069d1c4d7b76162]::obligation_forest::ObligationProcessor>::process_obligation
  34:        0x10b602dfb - <rustc_data_structures[3069d1c4d7b76162]::obligation_forest::ObligationForest<rustc_trait_selection[1363494f2f4ec6d3]::traits::fulfill::PendingPredicateObligation>>::process_obligations::<rustc_trait_selection[1363494f2f4ec6d3]::traits::fulfill::FulfillProcessor>
  35:        0x10b714556 - <rustc_trait_selection[1363494f2f4ec6d3]::traits::fulfill::FulfillmentContext<rustc_trait_selection[1363494f2f4ec6d3]::traits::FulfillmentError> as rustc_infer[9b67ab4c7e1e7e36]::traits::engine::TraitEngine<rustc_trait_selection[1363494f2f4ec6d3]::traits::FulfillmentError>>::select_where_possible
  36:        0x10b7d18c2 - <rustc_hir_typeck[83d45668b9c71bf7]::fn_ctxt::FnCtxt>::check_argument_types
  37:        0x10b76d5ce - <rustc_hir_typeck[83d45668b9c71bf7]::fn_ctxt::FnCtxt>::confirm_builtin_call
  38:        0x10b8802b1 - <rustc_hir_typeck[83d45668b9c71bf7]::fn_ctxt::FnCtxt>::check_expr_kind
  39:        0x10b79c073 - <rustc_hir_typeck[83d45668b9c71bf7]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  40:        0x10b7e087e - <rustc_hir_typeck[83d45668b9c71bf7]::fn_ctxt::FnCtxt>::check_block_with_expected
  41:        0x10b79c073 - <rustc_hir_typeck[83d45668b9c71bf7]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  42:        0x10b79e237 - <rustc_hir_typeck[83d45668b9c71bf7]::fn_ctxt::FnCtxt>::check_return_expr
  43:        0x10b8661c2 - rustc_hir_typeck[83d45668b9c71bf7]::check::check_fn
  44:        0x10b85f9bc - rustc_hir_typeck[83d45668b9c71bf7]::typeck
  45:        0x10c982a7a - rustc_query_impl[75f1704df0e38289]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[75f1704df0e38289]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3d266d78c9b198e4]::query::erase::Erased<[u8; 8usize]>>
  46:        0x10c7d8f4e - rustc_query_system[13e780f7d1d88911]::query::plumbing::try_execute_query::<rustc_query_impl[75f1704df0e38289]::DynamicConfig<rustc_query_system[13e780f7d1d88911]::query::caches::VecCache<rustc_span[51e7cf5b04f4660f]::def_id::LocalDefId, rustc_middle[3d266d78c9b198e4]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[75f1704df0e38289]::plumbing::QueryCtxt, false>
  47:        0x10c9b1bca - rustc_query_impl[75f1704df0e38289]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace
  48:        0x10b3f209e - <rustc_middle[3d266d78c9b198e4]::hir::map::Map>::par_body_owners::<rustc_hir_analysis[e5ecc290d4e18640]::check_crate::{closure#4}>::{closure#0}
  49:        0x10b57345c - rustc_hir_analysis[e5ecc290d4e18640]::check_crate
  50:        0x10bb0a187 - rustc_interface[ee26b89eef7e70ed]::passes::run_required_analyses
  51:        0x10bb0d170 - rustc_interface[ee26b89eef7e70ed]::passes::analysis
  52:        0x10c982aca - rustc_query_impl[75f1704df0e38289]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[75f1704df0e38289]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3d266d78c9b198e4]::query::erase::Erased<[u8; 1usize]>>
  53:        0x10c7388ee - rustc_query_system[13e780f7d1d88911]::query::plumbing::try_execute_query::<rustc_query_impl[75f1704df0e38289]::DynamicConfig<rustc_query_system[13e780f7d1d88911]::query::caches::SingleCache<rustc_middle[3d266d78c9b198e4]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[75f1704df0e38289]::plumbing::QueryCtxt, false>
  54:        0x10c98f177 - rustc_query_impl[75f1704df0e38289]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  55:        0x10b0e3e77 - <rustc_interface[ee26b89eef7e70ed]::queries::QueryResult<&rustc_middle[3d266d78c9b198e4]::ty::context::GlobalCtxt>>::enter::<core[907a20686bf8bf7a]::result::Result<(), rustc_span[51e7cf5b04f4660f]::ErrorGuaranteed>, rustc_driver_impl[e0e0c21a20d76ec4]::run_compiler::{closure#0}::{closure#1}::{closure#5}>
  56:        0x10b14195d - rustc_interface[ee26b89eef7e70ed]::interface::run_compiler::<core[907a20686bf8bf7a]::result::Result<(), rustc_span[51e7cf5b04f4660f]::ErrorGuaranteed>, rustc_driver_impl[e0e0c21a20d76ec4]::run_compiler::{closure#0}>::{closure#1}
  57:        0x10b12d59c - std[a533c313b02b6cfe]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[ee26b89eef7e70ed]::util::run_in_thread_with_globals<rustc_interface[ee26b89eef7e70ed]::util::run_in_thread_pool_with_globals<rustc_interface[ee26b89eef7e70ed]::interface::run_compiler<core[907a20686bf8bf7a]::result::Result<(), rustc_span[51e7cf5b04f4660f]::ErrorGuaranteed>, rustc_driver_impl[e0e0c21a20d76ec4]::run_compiler::{closure#0}>::{closure#1}, core[907a20686bf8bf7a]::result::Result<(), rustc_span[51e7cf5b04f4660f]::ErrorGuaranteed>>::{closure#0}, core[907a20686bf8bf7a]::result::Result<(), rustc_span[51e7cf5b04f4660f]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[907a20686bf8bf7a]::result::Result<(), rustc_span[51e7cf5b04f4660f]::ErrorGuaranteed>>
  58:        0x10b143f1a - <<std[a533c313b02b6cfe]::thread::Builder>::spawn_unchecked_<rustc_interface[ee26b89eef7e70ed]::util::run_in_thread_with_globals<rustc_interface[ee26b89eef7e70ed]::util::run_in_thread_pool_with_globals<rustc_interface[ee26b89eef7e70ed]::interface::run_compiler<core[907a20686bf8bf7a]::result::Result<(), rustc_span[51e7cf5b04f4660f]::ErrorGuaranteed>, rustc_driver_impl[e0e0c21a20d76ec4]::run_compiler::{closure#0}>::{closure#1}, core[907a20686bf8bf7a]::result::Result<(), rustc_span[51e7cf5b04f4660f]::ErrorGuaranteed>>::{closure#0}, core[907a20686bf8bf7a]::result::Result<(), rustc_span[51e7cf5b04f4660f]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[907a20686bf8bf7a]::result::Result<(), rustc_span[51e7cf5b04f4660f]::ErrorGuaranteed>>::{closure#1} as core[907a20686bf8bf7a]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  59:        0x10d33f95b - std::sys::pal::unix::thread::Thread::new::thread_start::hc5e64771570de417
  60:     0x7ff801f5318b - __pthread_start


rustc version: 1.83.0-nightly (7608018cb 2024-09-29)
platform: x86_64-apple-darwin

query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `<impl at ./9A756-1.rs:17:1: 17:18>::C`
#1 [eval_to_valtree] evaluating type-level constant
#2 [typeck] type-checking `main`
#3 [analysis] running analysis passes on this crate
end of query stack
```

</p>
</details>

### Note
<strong>Ice location:</strong>
https://github.com/rust-lang/rust/blob/7608018cbdac9e55d0d13529cf43adc33d53efcf/compiler/rustc_middle/src/mir/interpret/allocation.rs#L257-L271