Skip to content

niche optimization cannot be applied to non-power-of-two-sized int #116833

Closed as not planned
@anderspapitto

Description

@anderspapitto

I'd like to be able to apply niche optimizations to irregular integer sizes, e.g. u24 (or [u8;3]), where the niche is the zero pattern for all bytes. That's led me to this ICE.

Code

#![feature(rustc_attrs)]

#[rustc_layout_scalar_valid_range_start(1)]
#[rustc_nonnull_optimization_guaranteed]
struct Foo(pub u8, pub u8, pub u8);
fn foo1() -> Option<Foo> { Some(unsafe { Foo(0, 0, 0) }) }

Meta

rustc --version --verbose:

1.75.0-nightly (2023-10-16 49691b1f70d71dd7b834)

(via rust playground https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=c68c4d3734c44fdb16f959686c5d61be)

Error output

Compiling playground v0.0.1 (/playground)
warning: the feature `rustc_attrs` is internal to the compiler or standard library
 --> src/lib.rs:1:12
  |
1 | #![feature(rustc_attrs)]
  |            ^^^^^^^^^^^
  |
  = note: using it is strongly discouraged
  = note: `#[warn(internal_features)]` on by default

thread 'rustc' panicked at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/compiler/rustc_abi/src/layout.rs:281:22:
nonscalar layout for layout_scalar_valid_range type: Layout {
    size: Size(3 bytes),
    align: AbiAndPrefAlign {
        abi: Align(1 bytes),
        pref: Align(8 bytes),
    },
    abi: Aggregate {
        sized: true,
    },
    fields: Arbitrary {
        offsets: [
            Size(0 bytes),
            Size(1 bytes),
            Size(2 bytes),
        ],
        memory_index: [
            0,
            1,
            2,
        ],
    },
    largest_niche: None,
    variants: Single {
        index: 0,
    },
    max_repr_align: None,
    unadjusted_abi_align: Align(1 bytes),
}
stack backtrace:
   0:     0x7ff382386fbc - std::backtrace_rs::backtrace::libunwind::trace::h52e21b6d54f4efe1
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7ff382386fbc - std::backtrace_rs::backtrace::trace_unsynchronized::ha6bdebc0b682a39e
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7ff382386fbc - std::sys_common::backtrace::_print_fmt::h23ab7873c0a83148
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x7ff382386fbc - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h83da7f60cc7c7cd3
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7ff3823eea50 - core::fmt::rt::Argument::fmt::h695747813e9a4377
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/core/src/fmt/rt.rs:142:9
   5:     0x7ff3823eea50 - core::fmt::write::h8167ebd23b9a1080
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/core/src/fmt/mod.rs:1117:17
   6:     0x7ff38237a2ff - std::io::Write::write_fmt::hbb584c40957aadf3
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/std/src/io/mod.rs:1762:15
   7:     0x7ff382386da4 - std::sys_common::backtrace::_print::h0771a24c55f9a845
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7ff382386da4 - std::sys_common::backtrace::print::h0a0146f60845778a
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7ff382389bd7 - std::panicking::default_hook::{{closure}}::h5c829ccf7ff10dcf
  10:     0x7ff38238991a - std::panicking::default_hook::h0b7592f4ae109148
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/std/src/panicking.rs:292:9
  11:     0x7ff384f4434a - std[c698645e906a989e]::panicking::update_hook::<alloc[bb531d63f2d8c501]::boxed::Box<rustc_driver_impl[a565de00d2c1816a]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7ff38238a378 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h1edc5bec4ce090d5
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/alloc/src/boxed.rs:2021:9
  13:     0x7ff38238a378 - std::panicking::rust_panic_with_hook::h9275c70e8cb8097f
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/std/src/panicking.rs:735:13
  14:     0x7ff38238a0d1 - std::panicking::begin_panic_handler::{{closure}}::h5946c93671f604d2
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/std/src/panicking.rs:609:13
  15:     0x7ff3823874d6 - std::sys_common::backtrace::__rust_end_short_backtrace::h782a9a422ef9d5aa
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/std/src/sys_common/backtrace.rs:170:18
  16:     0x7ff382389e02 - rust_begin_unwind
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/std/src/panicking.rs:597:5
  17:     0x7ff3823eb075 - core::panicking::panic_fmt::hdde6b669b056f39d
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/core/src/panicking.rs:72:14
  18:     0x7ff3868be253 - rustc_ty_utils[69d546cb106a7340]::layout::layout_of_uncached
  19:     0x7ff3868b304e - rustc_ty_utils[69d546cb106a7340]::layout::layout_of
  20:     0x7ff3868b2fd1 - rustc_query_impl[40da232d0ce86907]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[40da232d0ce86907]::query_impl::layout_of::dynamic_query::{closure#2}::{closure#0}, rustc_middle[cf52819fa178b5a6]::query::erase::Erased<[u8; 16usize]>>
  21:     0x7ff3868b27e4 - rustc_query_system[e3e293bd174804eb]::query::plumbing::try_execute_query::<rustc_query_impl[40da232d0ce86907]::DynamicConfig<rustc_query_system[e3e293bd174804eb]::query::caches::DefaultCache<rustc_middle[cf52819fa178b5a6]::ty::ParamEnvAnd<rustc_middle[cf52819fa178b5a6]::ty::Ty>, rustc_middle[cf52819fa178b5a6]::query::erase::Erased<[u8; 16usize]>>, false, true, false>, rustc_query_impl[40da232d0ce86907]::plumbing::QueryCtxt, false>
  22:     0x7ff3868b24de - rustc_query_impl[40da232d0ce86907]::query_impl::layout_of::get_query_non_incr::__rust_end_short_backtrace
  23:     0x7ff38621e192 - <core[8466deefe4bf88b6]::iter::adapters::GenericShunt<core[8466deefe4bf88b6]::iter::adapters::by_ref_sized::ByRefSized<core[8466deefe4bf88b6]::iter::adapters::map::Map<core[8466deefe4bf88b6]::slice::iter::Iter<rustc_middle[cf52819fa178b5a6]::ty::VariantDef>, rustc_ty_utils[69d546cb106a7340]::layout::layout_of_uncached::{closure#8}>>, core[8466deefe4bf88b6]::result::Result<core[8466deefe4bf88b6]::convert::Infallible, &rustc_middle[cf52819fa178b5a6]::ty::layout::LayoutError>> as core[8466deefe4bf88b6]::iter::traits::iterator::Iterator>::next
  24:     0x7ff3868b4c3f - rustc_ty_utils[69d546cb106a7340]::layout::layout_of_uncached
  25:     0x7ff3868b304e - rustc_ty_utils[69d546cb106a7340]::layout::layout_of
  26:     0x7ff3868b2fd1 - rustc_query_impl[40da232d0ce86907]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[40da232d0ce86907]::query_impl::layout_of::dynamic_query::{closure#2}::{closure#0}, rustc_middle[cf52819fa178b5a6]::query::erase::Erased<[u8; 16usize]>>
  27:     0x7ff3868b27e4 - rustc_query_system[e3e293bd174804eb]::query::plumbing::try_execute_query::<rustc_query_impl[40da232d0ce86907]::DynamicConfig<rustc_query_system[e3e293bd174804eb]::query::caches::DefaultCache<rustc_middle[cf52819fa178b5a6]::ty::ParamEnvAnd<rustc_middle[cf52819fa178b5a6]::ty::Ty>, rustc_middle[cf52819fa178b5a6]::query::erase::Erased<[u8; 16usize]>>, false, true, false>, rustc_query_impl[40da232d0ce86907]::plumbing::QueryCtxt, false>
  28:     0x7ff3868b24de - rustc_query_impl[40da232d0ce86907]::query_impl::layout_of::get_query_non_incr::__rust_end_short_backtrace
  29:     0x7ff386b9b3ce - <rustc_mir_transform[c1d87982e12de6b0]::const_prop::CanConstProp>::check
  30:     0x7ff386148a5d - <rustc_mir_transform[c1d87982e12de6b0]::const_prop_lint::ConstPropLint as rustc_mir_transform[c1d87982e12de6b0]::pass_manager::MirLint>::run_lint
  31:     0x7ff38615b286 - rustc_mir_transform[c1d87982e12de6b0]::run_analysis_to_runtime_passes
  32:     0x7ff383cd9836 - rustc_mir_transform[c1d87982e12de6b0]::mir_drops_elaborated_and_const_checked
  33:     0x7ff38613bb75 - rustc_query_impl[40da232d0ce86907]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[40da232d0ce86907]::query_impl::mir_drops_elaborated_and_const_checked::dynamic_query::{closure#2}::{closure#0}, rustc_middle[cf52819fa178b5a6]::query::erase::Erased<[u8; 8usize]>>
  34:     0x7ff38613bdac - rustc_query_system[e3e293bd174804eb]::query::plumbing::try_execute_query::<rustc_query_impl[40da232d0ce86907]::DynamicConfig<rustc_query_system[e3e293bd174804eb]::query::caches::VecCache<rustc_span[8b3ee959687609c3]::def_id::LocalDefId, rustc_middle[cf52819fa178b5a6]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[40da232d0ce86907]::plumbing::QueryCtxt, false>
  35:     0x7ff38613b6d0 - rustc_query_impl[40da232d0ce86907]::query_impl::mir_drops_elaborated_and_const_checked::get_query_non_incr::__rust_end_short_backtrace
  36:     0x7ff3869b476b - rustc_interface[bf8842423122044e]::passes::analysis
  37:     0x7ff3869b3e6f - rustc_query_impl[40da232d0ce86907]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[40da232d0ce86907]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[cf52819fa178b5a6]::query::erase::Erased<[u8; 1usize]>>
  38:     0x7ff386c08c26 - rustc_query_system[e3e293bd174804eb]::query::plumbing::try_execute_query::<rustc_query_impl[40da232d0ce86907]::DynamicConfig<rustc_query_system[e3e293bd174804eb]::query::caches::SingleCache<rustc_middle[cf52819fa178b5a6]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[40da232d0ce86907]::plumbing::QueryCtxt, false>
  39:     0x7ff386c08a55 - rustc_query_impl[40da232d0ce86907]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  40:     0x7ff386cf2a92 - std[c698645e906a989e]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[bf8842423122044e]::util::run_in_thread_with_globals<rustc_interface[bf8842423122044e]::interface::run_compiler<core[8466deefe4bf88b6]::result::Result<(), rustc_span[8b3ee959687609c3]::ErrorGuaranteed>, rustc_driver_impl[a565de00d2c1816a]::run_compiler::{closure#1}>::{closure#0}, core[8466deefe4bf88b6]::result::Result<(), rustc_span[8b3ee959687609c3]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[8466deefe4bf88b6]::result::Result<(), rustc_span[8b3ee959687609c3]::ErrorGuaranteed>>
  41:     0x7ff386cf1d73 - <<std[c698645e906a989e]::thread::Builder>::spawn_unchecked_<rustc_interface[bf8842423122044e]::util::run_in_thread_with_globals<rustc_interface[bf8842423122044e]::interface::run_compiler<core[8466deefe4bf88b6]::result::Result<(), rustc_span[8b3ee959687609c3]::ErrorGuaranteed>, rustc_driver_impl[a565de00d2c1816a]::run_compiler::{closure#1}>::{closure#0}, core[8466deefe4bf88b6]::result::Result<(), rustc_span[8b3ee959687609c3]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[8466deefe4bf88b6]::result::Result<(), rustc_span[8b3ee959687609c3]::ErrorGuaranteed>>::{closure#1} as core[8466deefe4bf88b6]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  42:     0x7ff3823952f5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h65ed97a99699e110
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/alloc/src/boxed.rs:2007:9
  43:     0x7ff3823952f5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h7496a8683d4f5253
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/alloc/src/boxed.rs:2007:9
  44:     0x7ff3823952f5 - std::sys::unix::thread::Thread::new::thread_start::h3cc99d3e71f64c79
                               at /rustc/49691b1f70d71dd7b8349c332b7f277ee527bf08/library/std/src/sys/unix/thread.rs:108:17
  45:     0x7ff38225e609 - start_thread
  46:     0x7ff382181133 - clone
  47:                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 attach the file at `/playground/rustc-ice-2023-10-17T04:38:35.260903133Z-116.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 `Foo`
#1 [layout_of] computing layout of `core::option::Option<Foo>`
#2 [mir_drops_elaborated_and_const_checked] elaborating drops for `foo1`
#3 [analysis] running analysis passes on this crate
end of query stack
warning: `playground` (lib) generated 1 warning
error: could not compile `playground` (lib); 1 warning emitted

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-layoutArea: Memory layout of typesC-feature-requestCategory: A feature request, i.e: not implemented / a PR.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