Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casting an enum with const generic causes compiler panic #97634

Closed
mcscholtz opened this issue Jun 1, 2022 · 2 comments · Fixed by #98429
Closed

Casting an enum with const generic causes compiler panic #97634

mcscholtz opened this issue Jun 1, 2022 · 2 comments · Fixed by #98429
Assignees
Labels
C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mcscholtz
Copy link

mcscholtz commented Jun 1, 2022

Hi all,

I ran into the following error as shown in the example below. The error also happened with version 1.61 stable.

If the cast is on the same line it seems to panic.

Code

#[repr(u16)]
#[derive(Debug, PartialEq)]
pub enum Register<const N: u16> {
	Field0 = 40,
	Field1,
	Field2,
}

#[cfg(test)]
mod test {
	use crate::Register;

	#[test]
	fn test_ok1() {
		let a = Register::<0>::Field1;
		assert_eq!(a as u16, 41);
	}

	#[test]
	fn test_ok2() {
		let a = Register::<0>::Field1;
		let b = a as u16;
		assert_eq!(b as u16, 41);
	}

	#[test]
	fn test_crash() {
		let b = Register::<0>::Field1 as u16;
		assert_eq!(b, 41);
	}
}

Meta

rustc --version --verbose:

rustc 1.63.0-nightly (e09449220 2022-05-31)
binary: rustc
commit-hash: e0944922007e1bb4fe59809293acf4364410cccc
commit-date: 2022-05-31
host: x86_64-unknown-linux-gnu
release: 1.63.0-nightly
LLVM version: 14.0.4

Error output

➜  cargo test                                          
   Compiling bug_test v0.1.0 (/home/mariuss/Documents/projects/bug_test)
error: internal compiler error: compiler/rustc_middle/src/ty/subst.rs:646:17: const parameter `N/#0` (Const { ty: u16, val: Param(N/#0) }/0) out of range when substituting substs=[]

thread 'rustc' panicked at 'Box<dyn Any>', /rustc/e0944922007e1bb4fe59809293acf4364410cccc/compiler/rustc_errors/src/lib.rs:1275:9
stack backtrace:
...

note: the compiler unexpectedly panicked. this is a bug.

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: rustc 1.63.0-nightly (e09449220 2022-05-31) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [mir_drops_elaborated_and_const_checked] elaborating drops for `test::test_register_overlap`
#1 [optimized_mir] optimizing MIR for `test::test_register_overlap`
#2 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
error: could not compile `bug_test`
Backtrace

    0:     0x7f2da697afcd - std::backtrace_rs::backtrace::libunwind::trace::h5e7b7b93d1f3acd5
                               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7f2da697afcd - std::backtrace_rs::backtrace::trace_unsynchronized::h0b55e59d6533cb7a
                               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f2da697afcd - std::sys_common::backtrace::_print_fmt::h8d9df3b204a71ddc
                               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/std/src/sys_common/backtrace.rs:66:5
   3:     0x7f2da697afcd - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h166638903f004cd7
                               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/std/src/sys_common/backtrace.rs:45:22
   4:     0x7f2da69d6cac - core::fmt::write::h4a54f58ff9354b6b
                               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/core/src/fmt/mod.rs:1196:17
   5:     0x7f2da696c6c1 - std::io::Write::write_fmt::h285cd63945fc257a
                               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/std/src/io/mod.rs:1654:15
   6:     0x7f2da697dcb5 - std::sys_common::backtrace::_print::h2c317b930e1ae70b
                               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/std/src/sys_common/backtrace.rs:48:5
   7:     0x7f2da697dcb5 - std::sys_common::backtrace::print::h27906e0460eccc74
                               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/std/src/sys_common/backtrace.rs:35:9
   8:     0x7f2da697dcb5 - std::panicking::default_hook::{{closure}}::h0440e717959fdde2
                               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/std/src/panicking.rs:295:22
   9:     0x7f2da697d9d6 - std::panicking::default_hook::h9b8b53679edadd5f
                               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/std/src/panicking.rs:314:9
  10:     0x7f2da7153411 - rustc_driver[353b86defc14ca1]::DEFAULT_HOOK::{closure#0}::{closure#0}
  11:     0x7f2da697e38a - std::panicking::rust_panic_with_hook::h2fd904b55fab5e80
                               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/std/src/panicking.rs:702:17
  12:     0x7f2da822fbc1 - std[7074de9448f53455]::panicking::begin_panic::<rustc_errors[bd68527f243ec694]::ExplicitBug>::{closure#0}
  13:     0x7f2da822f566 - std[7074de9448f53455]::sys_common::backtrace::__rust_end_short_backtrace::<std[7074de9448f53455]::panicking::begin_panic<rustc_errors[bd68527f243ec694]::ExplicitBug>::{closure#0}, !>
  14:     0x7f2da81a69d6 - std[7074de9448f53455]::panicking::begin_panic::<rustc_errors[bd68527f243ec694]::ExplicitBug>
  15:     0x7f2da81c3566 - std[7074de9448f53455]::panic::panic_any::<rustc_errors[bd68527f243ec694]::ExplicitBug>
  16:     0x7f2da81c30f1 - <rustc_errors[bd68527f243ec694]::HandlerInner>::span_bug::<rustc_span[d87438c6b13c482c]::span_encoding::Span, &alloc[2e12de4c3a4f3fb]::string::String>
  17:     0x7f2da81c15a0 - <rustc_errors[bd68527f243ec694]::Handler>::span_bug::<rustc_span[d87438c6b13c482c]::span_encoding::Span, &alloc[2e12de4c3a4f3fb]::string::String>
  18:     0x7f2da8206708 - rustc_middle[e2a68416ee813954]::ty::context::tls::with_opt::<rustc_middle[e2a68416ee813954]::util::bug::opt_span_bug_fmt<rustc_span[d87438c6b13c482c]::span_encoding::Span>::{closure#0}, ()>
  19:     0x7f2da8206846 - rustc_middle[e2a68416ee813954]::util::bug::opt_span_bug_fmt::<rustc_span[d87438c6b13c482c]::span_encoding::Span>
  20:     0x7f2da8206804 - rustc_middle[e2a68416ee813954]::util::bug::span_bug_fmt::<rustc_span[d87438c6b13c482c]::span_encoding::Span>
  21:     0x7f2da91dea28 - <rustc_middle[e2a68416ee813954]::ty::consts::Const as rustc_middle[e2a68416ee813954]::ty::fold::TypeFoldable>::try_fold_with::<rustc_middle[e2a68416ee813954]::ty::subst::SubstFolder>
  22:     0x7f2da922aff1 - <rustc_middle[e2a68416ee813954]::ty::subst::SubstFolder as rustc_middle[e2a68416ee813954]::ty::fold::TypeFolder>::fold_const
  23:     0x7f2da85de70e - <rustc_middle[e2a68416ee813954]::ty::context::TyCtxt>::try_subst_and_normalize_erasing_regions::<rustc_middle[e2a68416ee813954]::mir::ConstantKind>
  24:     0x7f2da94a48a4 - <rustc_const_eval[62add1e0e435ad77]::interpret::eval_context::InterpCx<rustc_mir_transform[a4cd593d8ef39143]::const_prop::ConstPropMachine>>::subst_from_current_frame_and_normalize_erasing_regions::<rustc_middle[e2a68416ee813954]::mir::ConstantKind>
  25:     0x7f2da948e4cf - <rustc_mir_transform[a4cd593d8ef39143]::const_prop_lint::ConstPropagator>::check_binary_op
  26:     0x7f2da85b02f3 - <rustc_mir_transform[a4cd593d8ef39143]::const_prop_lint::ConstPropagator as rustc_middle[e2a68416ee813954]::mir::visit::Visitor>::visit_body
  27:     0x7f2da85aec44 - <rustc_mir_transform[a4cd593d8ef39143]::const_prop_lint::ConstProp as rustc_mir_transform[a4cd593d8ef39143]::pass_manager::MirLint>::run_lint
  28:     0x7f2da85665a0 - rustc_mir_transform[a4cd593d8ef39143]::pass_manager::run_passes
  29:     0x7f2da85bf4bc - rustc_mir_transform[a4cd593d8ef39143]::mir_drops_elaborated_and_const_checked
  30:     0x7f2da9770ffc - <rustc_query_system[b4d382c62d3fb0f4]::dep_graph::graph::DepGraph<rustc_middle[e2a68416ee813954]::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle[e2a68416ee813954]::ty::context::TyCtxt, rustc_middle[e2a68416ee813954]::ty::WithOptConstParam<rustc_span[d87438c6b13c482c]::def_id::LocalDefId>, &rustc_data_structures[bb156c26c22ac352]::steal::Steal<rustc_middle[e2a68416ee813954]::mir::Body>>
  31:     0x7f2da8cbdd49 - rustc_query_system[b4d382c62d3fb0f4]::query::plumbing::try_execute_query::<rustc_query_impl[af9326b4a8ec607e]::plumbing::QueryCtxt, rustc_query_system[b4d382c62d3fb0f4]::query::caches::DefaultCache<rustc_middle[e2a68416ee813954]::ty::WithOptConstParam<rustc_span[d87438c6b13c482c]::def_id::LocalDefId>, &rustc_data_structures[bb156c26c22ac352]::steal::Steal<rustc_middle[e2a68416ee813954]::mir::Body>>>
  32:     0x7f2da8caa20a - <rustc_query_impl[af9326b4a8ec607e]::Queries as rustc_middle[e2a68416ee813954]::ty::query::QueryEngine>::mir_drops_elaborated_and_const_checked
  33:     0x7f2da85c071c - rustc_mir_transform[a4cd593d8ef39143]::optimized_mir
  34:     0x7f2da9787247 - <rustc_query_system[b4d382c62d3fb0f4]::dep_graph::graph::DepGraph<rustc_middle[e2a68416ee813954]::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle[e2a68416ee813954]::ty::context::TyCtxt, rustc_span[d87438c6b13c482c]::def_id::DefId, &rustc_middle[e2a68416ee813954]::mir::Body>
  35:     0x7f2da8ce39ed - rustc_query_system[b4d382c62d3fb0f4]::query::plumbing::try_execute_query::<rustc_query_impl[af9326b4a8ec607e]::plumbing::QueryCtxt, rustc_query_system[b4d382c62d3fb0f4]::query::caches::DefaultCache<rustc_span[d87438c6b13c482c]::def_id::DefId, &rustc_middle[e2a68416ee813954]::mir::Body>>
  36:     0x7f2da8caa61e - <rustc_query_impl[af9326b4a8ec607e]::Queries as rustc_middle[e2a68416ee813954]::ty::query::QueryEngine>::optimized_mir
  37:     0x7f2da91ac4e7 - <rustc_middle[e2a68416ee813954]::ty::context::TyCtxt>::instance_mir
  38:     0x7f2da85424cc - rustc_monomorphize[a5b93f047227dc5b]::collector::collect_neighbours
  39:     0x7f2da853f1fa - rustc_monomorphize[a5b93f047227dc5b]::collector::collect_items_rec
  40:     0x7f2da853f6d8 - rustc_monomorphize[a5b93f047227dc5b]::collector::collect_items_rec
  41:     0x7f2da853f6d8 - rustc_monomorphize[a5b93f047227dc5b]::collector::collect_items_rec
  42:     0x7f2da9468dcb - <rustc_session[5cf55b4a9bcfa8ac]::session::Session>::time::<(), rustc_monomorphize[a5b93f047227dc5b]::collector::collect_crate_mono_items::{closure#1}>
  43:     0x7f2da94604db - rustc_monomorphize[a5b93f047227dc5b]::collector::collect_crate_mono_items
  44:     0x7f2da9464506 - rustc_monomorphize[a5b93f047227dc5b]::partitioning::collect_and_partition_mono_items
  45:     0x7f2da979ae74 - <rustc_query_system[b4d382c62d3fb0f4]::dep_graph::graph::DepGraph<rustc_middle[e2a68416ee813954]::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle[e2a68416ee813954]::ty::context::TyCtxt, (), (&std[7074de9448f53455]::collections::hash::set::HashSet<rustc_span[d87438c6b13c482c]::def_id::DefId, core[f48e8397d735e928]::hash::BuildHasherDefault<rustc_hash[1192cda37891db5b]::FxHasher>>, &[rustc_middle[e2a68416ee813954]::mir::mono::CodegenUnit])>
  46:     0x7f2da984f0d7 - rustc_query_system[b4d382c62d3fb0f4]::query::plumbing::try_execute_query::<rustc_query_impl[af9326b4a8ec607e]::plumbing::QueryCtxt, rustc_query_system[b4d382c62d3fb0f4]::query::caches::DefaultCache<(), (&std[7074de9448f53455]::collections::hash::set::HashSet<rustc_span[d87438c6b13c482c]::def_id::DefId, core[f48e8397d735e928]::hash::BuildHasherDefault<rustc_hash[1192cda37891db5b]::FxHasher>>, &[rustc_middle[e2a68416ee813954]::mir::mono::CodegenUnit])>>
  47:     0x7f2da9889df0 - rustc_query_system[b4d382c62d3fb0f4]::query::plumbing::get_query::<rustc_query_impl[af9326b4a8ec607e]::queries::collect_and_partition_mono_items, rustc_query_impl[af9326b4a8ec607e]::plumbing::QueryCtxt>
  48:     0x7f2da97e8052 - <rustc_query_impl[af9326b4a8ec607e]::Queries as rustc_middle[e2a68416ee813954]::ty::query::QueryEngine>::collect_and_partition_mono_items
  49:     0x7f2da9342aac - <rustc_codegen_llvm[ced656a9410415d7]::LlvmCodegenBackend as rustc_codegen_ssa[d7b3178ad7beee69]::traits::backend::CodegenBackend>::codegen_crate
  50:     0x7f2da93066e7 - <rustc_session[5cf55b4a9bcfa8ac]::session::Session>::time::<alloc[2e12de4c3a4f3fb]::boxed::Box<dyn core[f48e8397d735e928]::any::Any>, rustc_interface[aca4066e2cb604ea]::passes::start_codegen::{closure#0}>
  51:     0x7f2da9301bbf - <rustc_interface[aca4066e2cb604ea]::passes::QueryContext>::enter::<<rustc_interface[aca4066e2cb604ea]::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}, core[f48e8397d735e928]::result::Result<alloc[2e12de4c3a4f3fb]::boxed::Box<dyn core[f48e8397d735e928]::any::Any>, rustc_errors[bd68527f243ec694]::ErrorGuaranteed>>
  52:     0x7f2da932681f - <rustc_interface[aca4066e2cb604ea]::queries::Queries>::ongoing_codegen
  53:     0x7f2da92d104b - <rustc_interface[aca4066e2cb604ea]::interface::Compiler>::enter::<rustc_driver[353b86defc14ca1]::run_compiler::{closure#1}::{closure#2}, core[f48e8397d735e928]::result::Result<core[f48e8397d735e928]::option::Option<rustc_interface[aca4066e2cb604ea]::queries::Linker>, rustc_errors[bd68527f243ec694]::ErrorGuaranteed>>
  54:     0x7f2da92f3b7f - rustc_span[d87438c6b13c482c]::with_source_map::<core[f48e8397d735e928]::result::Result<(), rustc_errors[bd68527f243ec694]::ErrorGuaranteed>, rustc_interface[aca4066e2cb604ea]::interface::create_compiler_and_run<core[f48e8397d735e928]::result::Result<(), rustc_errors[bd68527f243ec694]::ErrorGuaranteed>, rustc_driver[353b86defc14ca1]::run_compiler::{closure#1}>::{closure#1}>
  55:     0x7f2da92d1c37 - <scoped_tls[f8c24a3c7c5578e]::ScopedKey<rustc_span[d87438c6b13c482c]::SessionGlobals>>::set::<rustc_interface[aca4066e2cb604ea]::interface::run_compiler<core[f48e8397d735e928]::result::Result<(), rustc_errors[bd68527f243ec694]::ErrorGuaranteed>, rustc_driver[353b86defc14ca1]::run_compiler::{closure#1}>::{closure#0}, core[f48e8397d735e928]::result::Result<(), rustc_errors[bd68527f243ec694]::ErrorGuaranteed>>
  56:     0x7f2da92e6bef - std[7074de9448f53455]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[aca4066e2cb604ea]::util::run_in_thread_pool_with_globals<rustc_interface[aca4066e2cb604ea]::interface::run_compiler<core[f48e8397d735e928]::result::Result<(), rustc_errors[bd68527f243ec694]::ErrorGuaranteed>, rustc_driver[353b86defc14ca1]::run_compiler::{closure#1}>::{closure#0}, core[f48e8397d735e928]::result::Result<(), rustc_errors[bd68527f243ec694]::ErrorGuaranteed>>::{closure#0}, core[f48e8397d735e928]::result::Result<(), rustc_errors[bd68527f243ec694]::ErrorGuaranteed>>
  57:     0x7f2da92e6d29 - <<std[7074de9448f53455]::thread::Builder>::spawn_unchecked_<rustc_interface[aca4066e2cb604ea]::util::run_in_thread_pool_with_globals<rustc_interface[aca4066e2cb604ea]::interface::run_compiler<core[f48e8397d735e928]::result::Result<(), rustc_errors[bd68527f243ec694]::ErrorGuaranteed>, rustc_driver[353b86defc14ca1]::run_compiler::{closure#1}>::{closure#0}, core[f48e8397d735e928]::result::Result<(), rustc_errors[bd68527f243ec694]::ErrorGuaranteed>>::{closure#0}, core[f48e8397d735e928]::result::Result<(), rustc_errors[bd68527f243ec694]::ErrorGuaranteed>>::{closure#1} as core[f48e8397d735e928]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  58:     0x7f2da69882b3 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h9ead32ab7d5c798d
                               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/alloc/src/boxed.rs:1886:9
  59:     0x7f2da69882b3 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h2e6b5f116485518d
                               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/alloc/src/boxed.rs:1886:9
  60:     0x7f2da69882b3 - std::sys::unix::thread::Thread::new::thread_start::h69816c82f6439aed
                               at /rustc/e0944922007e1bb4fe59809293acf4364410cccc/library/std/src/sys/unix/thread.rs:108:17
  61:     0x7f2da689c609 - start_thread
                               at /build/glibc-sMfBJT/glibc-2.31/nptl/pthread_create.c:477:8
  62:     0x7f2da67bf163 - clone
  63:                0x0 - <unknown>

@mcscholtz mcscholtz added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 1, 2022
@b-naber
Copy link
Contributor

b-naber commented Jun 2, 2022

@rustbot claim

@matthiaskrgr
Copy link
Member

reduced:

pub enum Register<const N: u16> {
    Field0 = 40,
    Field1,
}

fn main() {
    let _b = Register::<0>::Field1 as u16;
}

matthiaskrgr added a commit to matthiaskrgr/glacier that referenced this issue Jun 2, 2022
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Jun 2, 2022
compiler-errors added a commit to compiler-errors/rust that referenced this issue Jun 25, 2022
…nant-cast, r=lcnr

Use correct substs in enum discriminant cast

Fixes rust-lang#97634

r? `@lcnr`
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jun 25, 2022
…nant-cast, r=lcnr

Use correct substs in enum discriminant cast

Fixes rust-lang#97634

r? ``@lcnr``
@bors bors closed this as completed in ea07b96 Jun 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants