Skip to content

Panic in rustc when casting invalid enum discriminant #131902

Closed
@irh

Description

@irh

If a #[repr(u8)] enum contains more than 256 entries, then attempting to cast an overflowed discriminant triggers a panic.

Code

#[repr(u8)]
enum EnumU8 {
    // Uncomment the line below to trigger a panic
    // XXXX,
    X000, X001, X002, X003, X004, X005, X006, X007, X008, X009,
    X010, X011, X012, X013, X014, X015, X016, X017, X018, X019,
    X020, X021, X022, X023, X024, X025, X026, X027, X028, X029,
    X030, X031, X032, X033, X034, X035, X036, X037, X038, X039,
    X040, X041, X042, X043, X044, X045, X046, X047, X048, X049,
    X050, X051, X052, X053, X054, X055, X056, X057, X058, X059,
    X060, X061, X062, X063, X064, X065, X066, X067, X068, X069,
    X070, X071, X072, X073, X074, X075, X076, X077, X078, X079,
    X080, X081, X082, X083, X084, X085, X086, X087, X088, X089,
    X090, X091, X092, X093, X094, X095, X096, X097, X098, X099,
    X100, X101, X102, X103, X104, X105, X106, X107, X108, X109,
    X110, X111, X112, X113, X114, X115, X116, X117, X118, X119,
    X120, X121, X122, X123, X124, X125, X126, X127, X128, X129,
    X130, X131, X132, X133, X134, X135, X136, X137, X138, X139,
    X140, X141, X142, X143, X144, X145, X146, X147, X148, X149,
    X150, X151, X152, X153, X154, X155, X156, X157, X158, X159,
    X160, X161, X162, X163, X164, X165, X166, X167, X168, X169,
    X170, X171, X172, X173, X174, X175, X176, X177, X178, X179,
    X180, X181, X182, X183, X184, X185, X186, X187, X188, X189,
    X190, X191, X192, X193, X194, X195, X196, X197, X198, X199,
    X200, X201, X202, X203, X204, X205, X206, X207, X208, X209,
    X210, X211, X212, X213, X214, X215, X216, X217, X218, X219,
    X220, X221, X222, X223, X224, X225, X226, X227, X228, X229,
    X230, X231, X232, X233, X234, X235, X236, X237, X238, X239,
    X240, X241, X242, X243, X244, X245, X246, X247, X248, X249,
    X250, X251, X252, X253, X254, X255,
}

fn main() {
    let x = EnumU8::X255 as u8;
}

Meta

rustc --version --verbose:

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: aarch64-apple-darwin
release: 1.82.0
LLVM version: 19.1.1

This also happens with the latest nightly build.

Error output

thread 'rustc' panicked at compiler/rustc_mir_build/src/thir/cx/expr.rs:209:76:
called `Option::unwrap()` on a `None` value
Backtrace

error[E0370]: enum discriminant overflowed
  --> src/main.rs:33:35
   |
33 |     X250, X251, X252, X253, X254, X255,
   |                                   ^^^^ overflowed on value after 255
   |
   = note: explicitly set `X255 = 0` if that is desired outcome

error[E0081]: discriminant value `0` assigned more than once
  --> src/main.rs:5:1
   |
5  | enum EnumU8 {
   | ^^^^^^^^^^^
6  |     // Uncomment the line below to cause rustc to panic
7  |     XXXX,
   |     ----
   |     |
   |     `0` assigned here
   |     discriminant for `X255` incremented from this startpoint (`XXXX` + 256 variants later => `X255` = 0)
...
33 |     X250, X251, X252, X253, X254, X255,
   |                                   ---- `0` assigned here

thread 'rustc' panicked at compiler/rustc_mir_build/src/thir/cx/expr.rs:209:76:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0:        0x1116b8bdc - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::habbf9c4f641febb1
   1:        0x10ec85770 - core::fmt::write::ha36a8060c13608ea
   2:        0x1116acb0c - std::io::Write::write_fmt::h431832c8ebcc85c9
   3:        0x1116bb2a4 - std::panicking::default_hook::{{closure}}::h4aa1f60327dfff6a
   4:        0x1116baef8 - std::panicking::default_hook::h4ebc6eb4ae179807
   5:        0x10f7f6afc - <alloc[764fc8c78a1bb3e1]::boxed::Box<rustc_driver_impl[d9f1096c2de14668]::install_ice_hook::{closure#0}> as core[fafc87a594706398]::ops::function::Fn<(&dyn for<'a, 'b> core[fafc87a594706398]::ops::function::Fn<(&'a std[d8d90c69e022292b]::panic::PanicHookInfo<'b>,), Output = ()> + core[fafc87a594706398]::marker::Sync + core[fafc87a594706398]::marker::Send, &std[d8d90c69e022292b]::panic::PanicHookInfo)>>::call
   6:        0x1116bc428 - std::panicking::rust_panic_with_hook::h6a84efe4dcab239c
   7:        0x1116bb818 - std::panicking::begin_panic_handler::{{closure}}::h5eef292190467fef
   8:        0x1116b9084 - std::sys::backtrace::__rust_end_short_backtrace::hd7e7925203f20af9
   9:        0x1116bb514 - _rust_begin_unwind
  10:        0x113d37b60 - core::panicking::panic_fmt::h410d3f147658259b
  11:        0x113d37bcc - core::panicking::panic::hee236ca94fc05047
  12:        0x113d37ae8 - core::option::unwrap_failed::h187ebe480b20e6be
  13:        0x1105ae47c - <rustc_mir_build[7ce48aba1ac7abe4]::thir::cx::Cx>::make_mirror_unadjusted::{closure#0}
  14:        0x1105aa59c - <rustc_mir_build[7ce48aba1ac7abe4]::thir::cx::Cx>::mirror_expr_inner
  15:        0x1106186bc - <&mut <rustc_mir_build[7ce48aba1ac7abe4]::thir::cx::Cx>::mirror_stmts::{closure#0} as core[fafc87a594706398]::ops::function::FnMut<((usize, &rustc_hir[a16f20121d05aec5]::hir::Stmt),)>>::call_mut
  16:        0x11057db7c - <alloc[764fc8c78a1bb3e1]::vec::Vec<rustc_middle[1486d011505b3441]::thir::StmtId> as alloc[764fc8c78a1bb3e1]::vec::spec_from_iter::SpecFromIter<rustc_middle[1486d011505b3441]::thir::StmtId, core[fafc87a594706398]::iter::adapters::filter_map::FilterMap<core[fafc87a594706398]::iter::adapters::enumerate::Enumerate<core[fafc87a594706398]::slice::iter::Iter<rustc_hir[a16f20121d05aec5]::hir::Stmt>>, <rustc_mir_build[7ce48aba1ac7abe4]::thir::cx::Cx>::mirror_stmts::{closure#0}>>>::from_iter
  17:        0x1105a8e54 - <rustc_mir_build[7ce48aba1ac7abe4]::thir::cx::Cx>::mirror_block
  18:        0x1105ab2c0 - <rustc_mir_build[7ce48aba1ac7abe4]::thir::cx::Cx>::make_mirror_unadjusted::{closure#0}
  19:        0x1105aa59c - <rustc_mir_build[7ce48aba1ac7abe4]::thir::cx::Cx>::mirror_expr_inner
  20:        0x1105a8730 - rustc_mir_build[7ce48aba1ac7abe4]::thir::cx::thir_body
  21:        0x110c9e090 - rustc_query_impl[d98edaeb063d7c4c]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[d98edaeb063d7c4c]::query_impl::thir_body::dynamic_query::{closure#2}::{closure#0}, rustc_middle[1486d011505b3441]::query::erase::Erased<[u8; 16usize]>>
  22:        0x110cef7a0 - <rustc_query_impl[d98edaeb063d7c4c]::query_impl::thir_body::dynamic_query::{closure#2} as core[fafc87a594706398]::ops::function::FnOnce<(rustc_middle[1486d011505b3441]::ty::context::TyCtxt, rustc_span[12a1c67e1f6abb]::def_id::LocalDefId)>>::call_once
  23:        0x110c43dc4 - rustc_query_system[1bcdf744069b5f02]::query::plumbing::try_execute_query::<rustc_query_impl[d98edaeb063d7c4c]::DynamicConfig<rustc_query_system[1bcdf744069b5f02]::query::caches::VecCache<rustc_span[12a1c67e1f6abb]::def_id::LocalDefId, rustc_middle[1486d011505b3441]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[d98edaeb063d7c4c]::plumbing::QueryCtxt, true>
  24:        0x110beca28 - rustc_query_system[1bcdf744069b5f02]::query::plumbing::force_query::<rustc_query_impl[d98edaeb063d7c4c]::DynamicConfig<rustc_query_system[1bcdf744069b5f02]::query::caches::VecCache<rustc_span[12a1c67e1f6abb]::def_id::LocalDefId, rustc_middle[1486d011505b3441]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[d98edaeb063d7c4c]::plumbing::QueryCtxt>
  25:        0x110c6c78c - <rustc_query_impl[d98edaeb063d7c4c]::plumbing::query_callback<rustc_query_impl[d98edaeb063d7c4c]::query_impl::thir_body::QueryType>::{closure#0} as core[fafc87a594706398]::ops::function::FnOnce<(rustc_middle[1486d011505b3441]::ty::context::TyCtxt, rustc_query_system[1bcdf744069b5f02]::dep_graph::dep_node::DepNode)>>::call_once
  26:        0x110d6df40 - <rustc_query_system[1bcdf744069b5f02]::dep_graph::graph::DepGraphData<rustc_middle[1486d011505b3441]::dep_graph::DepsType>>::try_mark_previous_green::<rustc_query_impl[d98edaeb063d7c4c]::plumbing::QueryCtxt>
  27:        0x110d6dcd4 - <rustc_query_system[1bcdf744069b5f02]::dep_graph::graph::DepGraphData<rustc_middle[1486d011505b3441]::dep_graph::DepsType>>::try_mark_green::<rustc_query_impl[d98edaeb063d7c4c]::plumbing::QueryCtxt>
  28:        0x110befe90 - rustc_query_system[1bcdf744069b5f02]::query::plumbing::ensure_must_run::<rustc_query_impl[d98edaeb063d7c4c]::DynamicConfig<rustc_query_system[1bcdf744069b5f02]::query::caches::VecCache<rustc_span[12a1c67e1f6abb]::def_id::LocalDefId, rustc_middle[1486d011505b3441]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[d98edaeb063d7c4c]::plumbing::QueryCtxt>
  29:        0x110d21c60 - rustc_query_impl[d98edaeb063d7c4c]::query_impl::check_unsafety::get_query_incr::__rust_end_short_backtrace
  30:        0x1100afe28 - <rustc_data_structures[4379925a6ea25aa8]::sync::parallel::ParallelGuard>::run::<(), rustc_data_structures[4379925a6ea25aa8]::sync::parallel::disabled::par_for_each_in<&[rustc_span[12a1c67e1f6abb]::def_id::LocalDefId], <rustc_middle[1486d011505b3441]::hir::map::Map>::par_body_owners<rustc_interface[8c972d485a8e2aa0]::passes::run_required_analyses::{closure#2}::{closure#0}>::{closure#0}>::{closure#0}::{closure#0}::{closure#0}>
  31:        0x1100316c8 - rustc_interface[8c972d485a8e2aa0]::passes::analysis
  32:        0x110c9d9e0 - rustc_query_impl[d98edaeb063d7c4c]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[d98edaeb063d7c4c]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[1486d011505b3441]::query::erase::Erased<[u8; 1usize]>>
  33:        0x110cee2a8 - <rustc_query_impl[d98edaeb063d7c4c]::query_impl::analysis::dynamic_query::{closure#2} as core[fafc87a594706398]::ops::function::FnOnce<(rustc_middle[1486d011505b3441]::ty::context::TyCtxt, ())>>::call_once
  34:        0x110c034b8 - rustc_query_system[1bcdf744069b5f02]::query::plumbing::try_execute_query::<rustc_query_impl[d98edaeb063d7c4c]::DynamicConfig<rustc_query_system[1bcdf744069b5f02]::query::caches::SingleCache<rustc_middle[1486d011505b3441]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[d98edaeb063d7c4c]::plumbing::QueryCtxt, true>
  35:        0x110d1669c - rustc_query_impl[d98edaeb063d7c4c]::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
  36:        0x10f837608 - <rustc_middle[1486d011505b3441]::ty::context::GlobalCtxt>::enter::<rustc_driver_impl[d9f1096c2de14668]::run_compiler::{closure#0}::{closure#1}::{closure#5}, core[fafc87a594706398]::result::Result<(), rustc_span[12a1c67e1f6abb]::ErrorGuaranteed>>
  37:        0x10f7d7cc4 - <rustc_interface[8c972d485a8e2aa0]::interface::Compiler>::enter::<rustc_driver_impl[d9f1096c2de14668]::run_compiler::{closure#0}::{closure#1}, core[fafc87a594706398]::result::Result<core[fafc87a594706398]::option::Option<rustc_interface[8c972d485a8e2aa0]::queries::Linker>, rustc_span[12a1c67e1f6abb]::ErrorGuaranteed>>
  38:        0x10f7ec200 - <scoped_tls[db9af8800088675c]::ScopedKey<rustc_span[12a1c67e1f6abb]::SessionGlobals>>::set::<rustc_interface[8c972d485a8e2aa0]::util::run_in_thread_with_globals<rustc_interface[8c972d485a8e2aa0]::interface::run_compiler<core[fafc87a594706398]::result::Result<(), rustc_span[12a1c67e1f6abb]::ErrorGuaranteed>, rustc_driver_impl[d9f1096c2de14668]::run_compiler::{closure#0}>::{closure#1}, core[fafc87a594706398]::result::Result<(), rustc_span[12a1c67e1f6abb]::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}, core[fafc87a594706398]::result::Result<(), rustc_span[12a1c67e1f6abb]::ErrorGuaranteed>>
  39:        0x10f7e89fc - std[d8d90c69e022292b]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[8c972d485a8e2aa0]::util::run_in_thread_with_globals<rustc_interface[8c972d485a8e2aa0]::interface::run_compiler<core[fafc87a594706398]::result::Result<(), rustc_span[12a1c67e1f6abb]::ErrorGuaranteed>, rustc_driver_impl[d9f1096c2de14668]::run_compiler::{closure#0}>::{closure#1}, core[fafc87a594706398]::result::Result<(), rustc_span[12a1c67e1f6abb]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[fafc87a594706398]::result::Result<(), rustc_span[12a1c67e1f6abb]::ErrorGuaranteed>>
  40:        0x10f7f50c0 - <<std[d8d90c69e022292b]::thread::Builder>::spawn_unchecked_<rustc_interface[8c972d485a8e2aa0]::util::run_in_thread_with_globals<rustc_interface[8c972d485a8e2aa0]::interface::run_compiler<core[fafc87a594706398]::result::Result<(), rustc_span[12a1c67e1f6abb]::ErrorGuaranteed>, rustc_driver_impl[d9f1096c2de14668]::run_compiler::{closure#0}>::{closure#1}, core[fafc87a594706398]::result::Result<(), rustc_span[12a1c67e1f6abb]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[fafc87a594706398]::result::Result<(), rustc_span[12a1c67e1f6abb]::ErrorGuaranteed>>::{closure#1} as core[fafc87a594706398]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  41:        0x1116c6d44 - std::sys::pal::unix::thread::Thread::new::thread_start::hd88bc8e95f2ca709
  42:        0x185f75f94 - __pthread_joiner_wake

error: 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.82.0 (f6e511eec 2024-10-15) running on aarch64-apple-darwin

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked -C incremental=[REDACTED]

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

query stack during panic:
#0 [thir_body] building THIR for `main`
#1 [analysis] running analysis passes on this crate
end of query stack
there was a panic while trying to force a dep node
try_mark_green dep node stack:
#0 check_unsafety(rustc_crash_test[ca1f]::main)
end of try_mark_green dep node stack

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlC-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions