rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.8k stars 12.66k forks source link

access outside the bounds for given AllocRange #131046

Open Naserume opened 2 weeks ago

Naserume commented 2 weeks ago

Code

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

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`
Backtrace

``` 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::>::{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 - ::subrange 9: 0x10b0862dd - >::write_scalar 10: 0x10b097b4a - >::write_immediate_to_mplace_no_validate 11: 0x10b097977 - >::write_immediate_no_validate:: 12: 0x10b0c993b - >::copy_op_no_validate:: 13: 0x10b0c278b - >::return_from_current_stack_frame 14: 0x10b0a1446 - >::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::> 17: 0x10c93173e - )>>::call_once 18: 0x10c748579 - rustc_query_system[13e780f7d1d88911]::query::plumbing::try_execute_query::, 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_middle[3d266d78c9b198e4]::query::erase::Erased<[u8; 24usize]>>> 21: 0x10b05e831 - rustc_const_eval[298ced3d0b3bd135]::const_eval::valtrees::eval_to_valtree 22: 0x10bae8d03 - )>>::call_once 23: 0x10c97ee5c - rustc_query_impl[75f1704df0e38289]::plumbing::__rust_begin_short_backtrace::> 24: 0x10c910a7e - )>>::call_once 25: 0x10c748579 - rustc_query_system[13e780f7d1d88911]::query::plumbing::try_execute_query::, 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_middle[3d266d78c9b198e4]::query::erase::Erased<[u8; 24usize]>>> 28: 0x10bf796f3 - ::const_eval_global_id_for_typeck 29: 0x10bf79028 - ::const_eval_resolve_for_typeck 30: 0x10b9c469a - ::const_eval_resolve 31: 0x10b9b3c9a - ::try_const_eval_resolve 32: 0x10d0fd1cf - ::process_obligation::{closure#0} 33: 0x10d0fc596 - ::process_obligation 34: 0x10b602dfb - >::process_obligations:: 35: 0x10b714556 - as rustc_infer[9b67ab4c7e1e7e36]::traits::engine::TraitEngine>::select_where_possible 36: 0x10b7d18c2 - ::check_argument_types 37: 0x10b76d5ce - ::confirm_builtin_call 38: 0x10b8802b1 - ::check_expr_kind 39: 0x10b79c073 - ::check_expr_with_expectation_and_args 40: 0x10b7e087e - ::check_block_with_expected 41: 0x10b79c073 - ::check_expr_with_expectation_and_args 42: 0x10b79e237 - ::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::> 46: 0x10c7d8f4e - rustc_query_system[13e780f7d1d88911]::query::plumbing::try_execute_query::>, 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 - ::par_body_owners::::{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::> 53: 0x10c7388ee - rustc_query_system[13e780f7d1d88911]::query::plumbing::try_execute_query::>, 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 - >::enter::, rustc_driver_impl[e0e0c21a20d76ec4]::run_compiler::{closure#0}::{closure#1}::{closure#5}> 56: 0x10b14195d - rustc_interface[ee26b89eef7e70ed]::interface::run_compiler::, rustc_driver_impl[e0e0c21a20d76ec4]::run_compiler::{closure#0}>::{closure#1} 57: 0x10b12d59c - std[a533c313b02b6cfe]::sys::backtrace::__rust_begin_short_backtrace::, 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 - <::spawn_unchecked_, 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 `::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 ```

Note

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

cushionbadak commented 2 weeks ago

Little bit reduced:

#![feature(associated_const_equality, generic_const_items)]
#![allow(incomplete_features)]

fn foo() {}

#![abcd]

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>(());
}
cyrgani commented 2 weeks ago

Slightly less:

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

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

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

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