Skip to content

ICE: Misuse of #[rustc_layout_scalar_valid_range_start(..)] causes ICE even though#![feature(rustc_attrs)] is not enabled #147761

@Zalathar

Description

@Zalathar

Modified from #147756:

// Note that `#![feature(rustc_attrs)]` is _not_ enabled!

fn array() {
    #[rustc_layout_scalar_valid_range_start(1)]
    struct NonZero<T>([T; 4]);
    let nums = [1, 2, 3, 4];
    let _ = unsafe { NonZero(nums) };
}

(Playground link)

Full compiler output (expand):
   Compiling playground v0.0.1 (/playground)
error[E0658]: use of an internal attribute
 --> src/lib.rs:2:5
  |
2 |     #[rustc_layout_scalar_valid_range_start(1)]
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: add `#![feature(rustc_attrs)]` to the crate attributes to enable
  = note: the `#[rustc_layout_scalar_valid_range_start]` attribute is an internal implementation detail that will never be stable
  = note: the `#[rustc_layout_scalar_valid_range_start]` attribute is just used to enable niche optimizations in the standard library


thread 'rustc' (27) panicked at /rustc-dev/4b94758d2ba7d0ef71ccf5fde29ce4bc5d6fe2a4/compiler/rustc_abi/src/layout.rs:574:18:
nonscalar layout for layout_scalar_valid_range type: Layout {
    size: Size(16 bytes),
    align: AbiAlign {
        abi: Align(4 bytes),
    },
    backend_repr: Memory {
        sized: true,
    },
    fields: Arbitrary {
        offsets: [
            Size(0 bytes),
        ],
        memory_index: [
            0,
        ],
    },
    largest_niche: None,
    uninhabited: false,
    variants: Single {
        index: 0,
    },
    max_repr_align: None,
    unadjusted_abi_align: Align(4 bytes),
    randomization_seed: 16964116088959717513,
}
stack backtrace:
   0:     0x72929d1de7d3 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::hd1cc67f258704e81
   1:     0x72929d8020bc - core::fmt::write::h930ef66242f5d4a9
   2:     0x72929d192bd3 - std::io::Write::write_fmt::hd2f4e02ad47565e7
   3:     0x72929d1a3ef2 - std::sys::backtrace::BacktraceLock::print::hd759b34d7cbcba13
   4:     0x72929d1a9fb9 - std::panicking::default_hook::{{closure}}::h024c4ec418a5988c
   5:     0x72929d1a9ae3 - std::panicking::default_hook::hb11c83d23f7a9193
   6:     0x72929c1b9917 - std[a11a9d624ecb0b3d]::panicking::update_hook::<alloc[2bb24bfe3f76b733]::boxed::Box<rustc_driver_impl[272b56d37d1c53c]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x72929d1aa3df - std::panicking::panic_with_hook::hd36c87f6be7ec018
   8:     0x72929d1aa19a - std::panicking::panic_handler::{{closure}}::hfff1cd1134e726a9
   9:     0x72929d1a4029 - std::sys::backtrace::__rust_end_short_backtrace::h413657df94e0ee5d
  10:     0x72929d184ded - __rustc[5beaa93ec76eda40]::rust_begin_unwind
  11:     0x72929a202820 - core::panicking::panic_fmt::h4ff17f6cf8cfddaf
  12:     0x72929e135717 - rustc_ty_utils[cd60bdd38eccd53e]::layout::layout_of_uncached
  13:     0x72929e126749 - rustc_ty_utils[cd60bdd38eccd53e]::layout::layout_of
  14:     0x72929e1266ae - rustc_query_impl[cdcd6814043a13df]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[cdcd6814043a13df]::query_impl::layout_of::dynamic_query::{closure#2}::{closure#0}, rustc_middle[4a546dff9836264c]::query::erase::Erased<[u8; 16usize]>>
  15:     0x72929e1255bd - rustc_query_system[145825613dd0d940]::query::plumbing::try_execute_query::<rustc_query_impl[cdcd6814043a13df]::DynamicConfig<rustc_query_system[145825613dd0d940]::query::caches::DefaultCache<rustc_middle[4a546dff9836264c]::ty::PseudoCanonicalInput<rustc_middle[4a546dff9836264c]::ty::Ty>, rustc_middle[4a546dff9836264c]::query::erase::Erased<[u8; 16usize]>>, false, true, false>, rustc_query_impl[cdcd6814043a13df]::plumbing::QueryCtxt, false>
  16:     0x72929e1251e8 - rustc_query_impl[cdcd6814043a13df]::query_impl::layout_of::get_query_non_incr::__rust_end_short_backtrace
  17:     0x72929e793132 - <rustc_mir_transform[18c35b3c15a2611d]::known_panics_lint::KnownPanicsLint as rustc_mir_transform[18c35b3c15a2611d]::pass_manager::MirLint>::run_lint
  18:     0x72929d80a3e9 - rustc_mir_transform[18c35b3c15a2611d]::run_analysis_to_runtime_passes
  19:     0x72929db614d5 - rustc_mir_transform[18c35b3c15a2611d]::mir_drops_elaborated_and_const_checked
  20:     0x72929db60e5d - rustc_query_impl[cdcd6814043a13df]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[cdcd6814043a13df]::query_impl::mir_drops_elaborated_and_const_checked::dynamic_query::{closure#2}::{closure#0}, rustc_middle[4a546dff9836264c]::query::erase::Erased<[u8; 8usize]>>
  21:     0x72929db5d9f7 - rustc_query_system[145825613dd0d940]::query::plumbing::try_execute_query::<rustc_query_impl[cdcd6814043a13df]::DynamicConfig<rustc_data_structures[4a479dd3ed9c100c]::vec_cache::VecCache<rustc_span[ee1456bbb6d10cec]::def_id::LocalDefId, rustc_middle[4a546dff9836264c]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[145825613dd0d940]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[cdcd6814043a13df]::plumbing::QueryCtxt, false>
  22:     0x72929db5d089 - rustc_query_impl[cdcd6814043a13df]::query_impl::mir_drops_elaborated_and_const_checked::get_query_non_incr::__rust_end_short_backtrace
  23:     0x72929db5c065 - <rustc_middle[4a546dff9836264c]::ty::context::TyCtxt>::par_hir_body_owners::<rustc_interface[9373108c49bfdcc5]::passes::run_required_analyses::{closure#1}::{closure#0}>::{closure#0}
  24:     0x72929db578ea - rustc_interface[9373108c49bfdcc5]::passes::analysis
  25:     0x72929db56d73 - rustc_query_impl[cdcd6814043a13df]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[cdcd6814043a13df]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[4a546dff9836264c]::query::erase::Erased<[u8; 0usize]>>
  26:     0x72929e7fe0ce - rustc_query_system[145825613dd0d940]::query::plumbing::try_execute_query::<rustc_query_impl[cdcd6814043a13df]::DynamicConfig<rustc_query_system[145825613dd0d940]::query::caches::SingleCache<rustc_middle[4a546dff9836264c]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[cdcd6814043a13df]::plumbing::QueryCtxt, false>
  27:     0x72929e7fdd06 - rustc_query_impl[cdcd6814043a13df]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  28:     0x72929eb05a26 - <rustc_interface[9373108c49bfdcc5]::passes::create_and_enter_global_ctxt<core[cdc9ee0e3466da03]::option::Option<rustc_interface[9373108c49bfdcc5]::queries::Linker>, rustc_driver_impl[272b56d37d1c53c]::run_compiler::{closure#0}::{closure#2}>::{closure#2} as core[cdc9ee0e3466da03]::ops::function::FnOnce<(&rustc_session[be5d28f85a14d8af]::session::Session, rustc_middle[4a546dff9836264c]::ty::context::CurrentGcx, alloc[2bb24bfe3f76b733]::sync::Arc<rustc_data_structures[4a479dd3ed9c100c]::jobserver::Proxy>, &std[a11a9d624ecb0b3d]::sync::once_lock::OnceLock<rustc_middle[4a546dff9836264c]::ty::context::GlobalCtxt>, &rustc_data_structures[4a479dd3ed9c100c]::sync::worker_local::WorkerLocal<rustc_middle[4a546dff9836264c]::arena::Arena>, &rustc_data_structures[4a479dd3ed9c100c]::sync::worker_local::WorkerLocal<rustc_hir[b7aab00ba03ad347]::Arena>, rustc_driver_impl[272b56d37d1c53c]::run_compiler::{closure#0}::{closure#2})>>::call_once::{shim:vtable#0}
  29:     0x72929e99b640 - rustc_interface[9373108c49bfdcc5]::interface::run_compiler::<(), rustc_driver_impl[272b56d37d1c53c]::run_compiler::{closure#0}>::{closure#1}
  30:     0x72929ea86f8b - std[a11a9d624ecb0b3d]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[9373108c49bfdcc5]::util::run_in_thread_with_globals<rustc_interface[9373108c49bfdcc5]::util::run_in_thread_pool_with_globals<rustc_interface[9373108c49bfdcc5]::interface::run_compiler<(), rustc_driver_impl[272b56d37d1c53c]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  31:     0x72929ea86c6c - <<std[a11a9d624ecb0b3d]::thread::Builder>::spawn_unchecked_<rustc_interface[9373108c49bfdcc5]::util::run_in_thread_with_globals<rustc_interface[9373108c49bfdcc5]::util::run_in_thread_pool_with_globals<rustc_interface[9373108c49bfdcc5]::interface::run_compiler<(), rustc_driver_impl[272b56d37d1c53c]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[cdc9ee0e3466da03]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  32:     0x72929e53d9af - std::sys::thread::unix::Thread::new::thread_start::h331a76fdbfad0791
  33:     0x72929848aaa4 - <unknown>
  34:     0x729298517a64 - clone
  35:                0x0 - <unknown>

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: please make sure that you have updated to the latest nightly

note: please attach the file at `/playground/rustc-ice-2025-10-16T03_34_05-25.txt` to your bug report

note: compiler flags: --crate-type lib -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2

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

query stack during panic:
#0 [layout_of] computing layout of `array::NonZero<i32>`
#1 [mir_drops_elaborated_and_const_checked] elaborating drops for `array`
... and 1 other queries... use `env RUST_BACKTRACE=1` to see the full query stack
For more information about this error, try `rustc --explain E0658`.
error: could not compile `playground` (lib) due to 1 previous error

The compiler produces an error message about using an internal attribute, as expected. But it also then goes on to report an ICE, even though #![feature(rustc_attrs)] isn't enabled.

(If the feature were enabled, then an ICE for applying rustc_layout_scalar_valid_range_start to a type with non-scalar layout would be unremarkable.)

This can be reproduced on nightly or stable, starting with 1.78.

The ICE seems to only occur in a codegen build, not in a check build.

If the attribute is applied to a type with scalar layout, or if the type with non-scalar layout is not actually used, then the ICE does not occur.

Expected behaviour

If #![feature(rustc_attrs)] is not enabled, the build should fail cleanly with an error about an illegal attribute, without an ICE.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-internal-featuresThis issue requires the use of internal features.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions