rust-lang / rust

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

const adt params and const mut refs ICE #100313

Closed DutchGhost closed 2 years ago

DutchGhost commented 2 years ago

I know the code below is wrong, but I expect a normal compiler error rather than an ICE.

Code

#![feature(const_mut_refs)]
#![feature(adt_const_params)]

struct T<const B: &'static bool>;

impl <const B: &'static bool> T<B> {
    const fn set_false(&self) {
        unsafe {
            *(B as *const bool as *mut bool) = false;
        }
    }
}

const _: () = {
    let x = T::<{&true}>;
    x.set_false();
};

fn main() {

}

Meta

rustc --version --verbose:

rustc 1.65.0-nightly (f03ce3096 2022-08-08)

Error output

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

``` thread 'rustc' panicked at 'Box', /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/compiler/rustc_errors/src/lib.rs:1392:9 stack backtrace: 0: 0x7f3fe7e5ec60 - std::backtrace_rs::backtrace::libunwind::trace::he73bf82d737d304d at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 1: 0x7f3fe7e5ec60 - std::backtrace_rs::backtrace::trace_unsynchronized::h07fc55aea59a0f98 at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f3fe7e5ec60 - std::sys_common::backtrace::_print_fmt::h8a2fa659aab33962 at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/std/src/sys_common/backtrace.rs:66:5 3: 0x7f3fe7e5ec60 - ::fmt::h8d53bccf60c06da7 at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/std/src/sys_common/backtrace.rs:45:22 4: 0x7f3fe7eba62c - core::fmt::write::hc4adad996c06bcaa at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/core/src/fmt/mod.rs:1198:17 5: 0x7f3fe7e4fca5 - std::io::Write::write_fmt::h46dfa8a98fb93088 at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/std/src/io/mod.rs:1672:15 6: 0x7f3fe7e61941 - std::sys_common::backtrace::_print::h42e4c3a53bdb0e01 at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/std/src/sys_common/backtrace.rs:48:5 7: 0x7f3fe7e61941 - std::sys_common::backtrace::print::h49df31fe119a7172 at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/std/src/sys_common/backtrace.rs:35:9 8: 0x7f3fe7e61941 - std::panicking::default_hook::{{closure}}::h915c48a155020ec1 at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/std/src/panicking.rs:295:22 9: 0x7f3fe7e6160e - std::panicking::default_hook::hc62fd81d406d34d4 at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/std/src/panicking.rs:314:9 10: 0x7f3fea6d9024 - >::call_once::{shim:vtable#0} 11: 0x7f3fe7e62176 - std::panicking::rust_panic_with_hook::hc8104bd1930d34de at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/std/src/panicking.rs:702:17 12: 0x7f3feb6768b1 - std[d287d5bb05f95ef6]::panicking::begin_panic::::{closure#0} 13: 0x7f3feb676236 - std[d287d5bb05f95ef6]::sys_common::backtrace::__rust_end_short_backtrace::::{closure#0}, !> 14: 0x7f3feb658906 - std[d287d5bb05f95ef6]::panicking::begin_panic:: 15: 0x7f3feb69c376 - std[d287d5bb05f95ef6]::panic::panic_any:: 16: 0x7f3feb69ba35 - ::bug::<&alloc[b42d50d8077e1268]::string::String> 17: 0x7f3feb69b790 - ::bug::<&alloc[b42d50d8077e1268]::string::String> 18: 0x7f3feb6c09fd - rustc_middle[b803c3f789f93a69]::ty::context::tls::with_context_opt::::{closure#0}, ()>::{closure#0}, ()> 19: 0x7f3feb6c3ab6 - rustc_middle[b803c3f789f93a69]::util::bug::opt_span_bug_fmt:: 20: 0x7f3fe9132783 - rustc_middle[b803c3f789f93a69]::util::bug::bug_fmt 21: 0x7f3feb664d72 - ::pretty_print_const_scalar_int 22: 0x7f3feb665c33 - ::pretty_print_const_valtree 23: 0x7f3feb66336a - ::pretty_print_const 24: 0x7f3fea451a74 - ::print_def_path 25: 0x7f3fea44e07d - ::pretty_print_type 26: 0x7f3fea453691 - ::path_qualified 27: 0x7f3fea45118f - ::print_def_path 28: 0x7f3fea450934 - ::print_def_path 29: 0x7f3fe97a286b - ::fmt 30: 0x7f3fe7eba62c - core::fmt::write::hc4adad996c06bcaa at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/core/src/fmt/mod.rs:1198:17 31: 0x7f3fe7ebb534 - core::fmt::Formatter::write_fmt::hd4f816e5f30408b7 at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/core/src/fmt/mod.rs:1652:9 32: 0x7f3feae62390 - ::fmt 33: 0x7f3feae863b7 - ::struct_generic::<::report_as_error::{closure#0}>::{closure#0} 34: 0x7f3fe97a2c4b - ::report_as_error 35: 0x7f3fe970d5dd - rustc_const_eval[201bffe6500e0095]::const_eval::eval_queries::eval_to_allocation_raw_provider 36: 0x7f3fe9fcc4bf - rustc_query_system[bac2197a4722c86]::query::plumbing::get_query:: 37: 0x7f3fe9fcc085 - ::eval_to_allocation_raw 38: 0x7f3fea0fe88b - rustc_const_eval[201bffe6500e0095]::const_eval::eval_queries::eval_to_const_value_raw_provider 39: 0x7f3fe9c746c0 - rustc_query_system[bac2197a4722c86]::query::plumbing::get_query:: 40: 0x7f3fe9c74285 - ::eval_to_const_value_raw 41: 0x7f3fea0fe736 - rustc_const_eval[201bffe6500e0095]::const_eval::eval_queries::eval_to_const_value_raw_provider 42: 0x7f3fe9c746c0 - rustc_query_system[bac2197a4722c86]::query::plumbing::get_query:: 43: 0x7f3fe9c74285 - ::eval_to_const_value_raw 44: 0x7f3fe9b116a1 - ::const_eval_poly 45: 0x7f3fe9b0dc9b - ::check_item 46: 0x7f3fe9b09d24 - rustc_hir[7de338cdf5457711]::intravisit::walk_mod::> 47: 0x7f3fe9b0903f - rustc_lint[3fc1eb8939f490e7]::late::late_lint_mod:: 48: 0x7f3fe9b08e7a - rustc_lint[3fc1eb8939f490e7]::lint_mod 49: 0x7f3fe993c08b - rustc_query_system[bac2197a4722c86]::query::plumbing::try_execute_query::> 50: 0x7f3fea130b47 - rustc_query_system[bac2197a4722c86]::query::plumbing::get_query:: 51: 0x7f3fea335c20 - ::for_each_module::::{closure#1}::{closure#0}::{closure#0}> 52: 0x7f3fea335893 - ::time::<(), rustc_lint[3fc1eb8939f490e7]::late::check_crate::{closure#1}::{closure#0}> 53: 0x7f3fea0b8134 - ::time::<(), rustc_interface[e7882ae3c034202e]::passes::analysis::{closure#5}::{closure#1}::{closure#2}::{closure#0}> 54: 0x7f3fea0b7f31 - as core[bdb6c2c9b92b14cf]::ops::function::FnOnce<()>>::call_once 55: 0x7f3fea0b7a5a - ::time::<(), rustc_interface[e7882ae3c034202e]::passes::analysis::{closure#5}> 56: 0x7f3fea0b53ec - rustc_interface[e7882ae3c034202e]::passes::analysis 57: 0x7f3fea473555 - rustc_query_system[bac2197a4722c86]::query::plumbing::try_execute_query::>> 58: 0x7f3fea4732de - rustc_query_system[bac2197a4722c86]::query::plumbing::get_query:: 59: 0x7f3fe9eba4da - ::enter::, rustc_errors[cc7e50c8484f644f]::ErrorGuaranteed>> 60: 0x7f3fe9eadeea - rustc_span[b2129a79ff31bdef]::with_source_map::, rustc_interface[e7882ae3c034202e]::interface::create_compiler_and_run, rustc_driver[f63e02b65247c824]::run_compiler::{closure#1}>::{closure#1}> 61: 0x7f3fe9ead8c0 - rustc_interface[e7882ae3c034202e]::interface::create_compiler_and_run::, rustc_driver[f63e02b65247c824]::run_compiler::{closure#1}> 62: 0x7f3fe9eabc31 - >::set::, rustc_driver[f63e02b65247c824]::run_compiler::{closure#1}>::{closure#0}, core[bdb6c2c9b92b14cf]::result::Result<(), rustc_errors[cc7e50c8484f644f]::ErrorGuaranteed>> 63: 0x7f3fe9eab91f - std[d287d5bb05f95ef6]::sys_common::backtrace::__rust_begin_short_backtrace::, rustc_driver[f63e02b65247c824]::run_compiler::{closure#1}>::{closure#0}, core[bdb6c2c9b92b14cf]::result::Result<(), rustc_errors[cc7e50c8484f644f]::ErrorGuaranteed>>::{closure#0}, core[bdb6c2c9b92b14cf]::result::Result<(), rustc_errors[cc7e50c8484f644f]::ErrorGuaranteed>> 64: 0x7f3fea502389 - <::spawn_unchecked_, rustc_driver[f63e02b65247c824]::run_compiler::{closure#1}>::{closure#0}, core[bdb6c2c9b92b14cf]::result::Result<(), rustc_errors[cc7e50c8484f644f]::ErrorGuaranteed>>::{closure#0}, core[bdb6c2c9b92b14cf]::result::Result<(), rustc_errors[cc7e50c8484f644f]::ErrorGuaranteed>>::{closure#1} as core[bdb6c2c9b92b14cf]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 65: 0x7f3fe7e6c143 - as core::ops::function::FnOnce>::call_once::hc6c6702d9d7f8ac5 at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/alloc/src/boxed.rs:1935:9 66: 0x7f3fe7e6c143 - as core::ops::function::FnOnce>::call_once::h6ca0aa1af63cbeae at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/alloc/src/boxed.rs:1935:9 67: 0x7f3fe7e6c143 - std::sys::unix::thread::Thread::new::thread_start::h79459e84af59d3fc at /rustc/f03ce30962cf1b2a5158667eabae8bf6e8d1cb03/library/std/src/sys/unix/thread.rs:108:17 68: 0x7f3fe7d3e609 - start_thread 69: 0x7f3fe7c61133 - clone 70: 0x0 - ```

DutchGhost commented 2 years ago

It's funny, if I replace the bool with an integral type of size 8, I get a normal error:

#![feature(const_mut_refs)]
#![feature(adt_const_params)]

struct T<const B: &'static u64>;

impl <const B: &'static u64> T<B> {
    const fn set_false(&self) {
        unsafe {
            *(B as *const u64 as *mut u64) -= 1;
        }
    }
}

const _: () = {
    let x = T::<{&1}>;
    x.set_false();
};

fn main() {

}
error[[E0080]](https://doc.rust-lang.org/nightly/error-index.html#E0080): evaluation of constant value failed
  --> src/main.rs:9:13
   |
9  |             *(B as *const u64 as *mut u64) -= 1;
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |             |
   |             writing to alloc7 which is read-only
   |             inside `T::<{0x1 as &u64}>::set_false` at src/main.rs:9:13
...
16 |     x.set_false();
   |     ------------- inside `_` at src/main.rs:16:5
b-naber commented 2 years ago

@rustbot claim