rust-lang / rust

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

ICE: `expected int of size 8, but got size 1` in `rustc_middle/src/ty/consts/int.rs` with feature `adt_const_params` #131052

Open cushionbadak opened 2 weeks ago

cushionbadak commented 2 weeks ago

Code

(manually reduced)

#![feature(adt_const_params)]
#![allow(incomplete_features)]

struct ConstBytes<const T: &'static [*mut u8; 3]>;

pub fn main() {
    let _: ConstBytes<b"AAA"> = ConstBytes::<b"BBB">;
}
Original Code

```Rust //@ revisions: full min #![cfg_attr(full, feature(adt_const_params))] #![cfg_attr(full, allow(incomplete_features))] struct ConstString; //[min]~^ ERROR struct ConstBytes() / mem::size_of::<*mut u8>()]>; //[min]~^ ERROR pub fn main() { let _: ConstString<"Hello"> = ConstString::<"Hello">; let _: ConstString<"Hello"> = None::>; //~ ERROR mismatched types let _: ConstString<"ā„‡ć‡ˆā†¦"> = ConstString::<"ā„‡ć‡ˆā†¦">; let _: ConstString<"ā„‡ć‡ˆā†¦"> = ConstString::<"ā„‡ć‡ˆā†„">; //~ ERROR mismatched types let _: ConstBytes = ConstBytes::<{&[0x41, 0x41, 0x41]}>; let _: ConstBytes = ConstBytes::; //~ ERROR mismatched types } // from tests/ui/const-generics/slice-const-param-mismatch.rs ```

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

Command: rustc

error[E0741]: `&'static [*mut u8; 3]` can't be used as a const parameter type
 --> r_E364D18BCB8846EE07601029D3F8CBE898970C9D0A44FC6609A112B969C95B8D_2.rs:4:28
  |
4 | struct ConstBytes<const T: &'static [*mut u8; 3]>;
  |                            ^^^^^^^^^^^^^^^^^^^^^

error: internal compiler error: compiler/rustc_middle/src/ty/consts/int.rs:263:13: expected int of size 8, but got size 1
Backtrace

``` thread 'rustc' panicked at compiler/rustc_middle/src/ty/consts/int.rs:263:13: Box stack backtrace: 0: std::panicking::begin_panic:: 1: ::emit_producing_guarantee 2: rustc_middle::util::bug::opt_span_bug_fmt::::{closure#0} 3: rustc_middle::ty::context::tls::with_opt::::{closure#0}, !>::{closure#0} 4: rustc_middle::ty::context::tls::with_context_opt::::{closure#0}, !>::{closure#0}, !> 5: rustc_middle::util::bug::bug_fmt 6: ::pretty_print_const_scalar_int 7: ::pretty_print_const_valtree 8: ::pretty_print_const 9: ::pretty_print_const_valtree 10: ::pretty_print_const_valtree 11: ::pretty_print_const 12: ::fmt 13: ::cmp 14: ::values_str 15: ::note_type_err 16: ::report_and_explain_type_error 17: ::demand_coerce_diag 18: ::check_decl 19: ::check_block_with_expected 20: ::check_expr_with_expectation_and_args 21: ::check_return_expr 22: rustc_hir_typeck::check::check_fn 23: rustc_hir_typeck::typeck [... omitted 1 frame ...] 24: ::par_body_owners::::{closure#0} 25: rustc_hir_analysis::check_crate 26: rustc_interface::passes::run_required_analyses 27: rustc_interface::passes::analysis [... omitted 1 frame ...] 28: >::enter::, rustc_driver_impl::run_compiler::{closure#0}::{closure#1}::{closure#5}> 29: rustc_interface::interface::run_compiler::, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1} note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. 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 `/Volumes/T7/workspace/blackbox icemaker ź²°ź³¼/240926_240401_blackbox_result/rustc-ice-2024-09-30T08_07_24-63216.txt` to your bug report query stack during panic: #0 [typeck] type-checking `main` #1 [analysis] running analysis passes on this crate end of query stack error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0741`. ```

Note

@rustbot label +F-adt_const_params

clubby789 commented 2 weeks ago

The error that we're trying to print out is

error[E0308]: mismatched types
 --> poc.rs:7:33
  |
7 |     let _: ConstBytes<b"AAA"> = ConstBytes::<b"BBB">;
  |            ------------------   ^^^^^^^^^^^^^^^^^^^^ expected `&[{0x41 as *mut u8}, {0x41 as *mut u8}, {0x41 as *mut u8}]`, found `&[{0x42 as *mut u8}, {0x42 as *mut u8}, {0x42 as *mut u8}]`
  |            |
  |            expected due to this
  |
  = note: expected struct `ConstBytes<&[{0x41 as *mut u8}, {0x41 as *mut u8}, {0x41 as *mut u8}]>`
             found struct `ConstBytes<&[{0x42 as *mut u8}, {0x42 as *mut u8}, {0x42 as *mut u8}]>`

The size mismatch happens because each integer is a constant of size 1 byte, but as the expected type is a pointer, the pretty printer hits https://github.com/rust-lang/rust/blob/7608018cbdac9e55d0d13529cf43adc33d53efcf/compiler/rustc_middle/src/ty/print/pretty.rs#L1745-L1747