rust-lang / rust

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

Compiler panics on #[repr(C)] + #[derive(debug)] + #[rustc_layout_scalar_valid_range_end(_)] on tuple structs #83394

Closed mejrs closed 6 months ago

mejrs commented 3 years ago

The compiler will panic when all these attributes are enabled at once. Note the commented-out code that triggers a related panic.

Code

Playground

#![feature(rustc_attrs)]

// Uncommenting any of these attributes will successfully compile 
#[repr(C)]
#[derive(Debug)]
#[rustc_layout_scalar_valid_range_end(0x7F)]
struct Integer(u8);

// triggers different compiler panic
/*
#[repr(C)]
#[derive(Debug)]
#[rustc_layout_scalar_valid_range_end(0xFFFF)]
struct Integers(u8, u8);
*/

Meta

rustc --version --verbose:

rustc 1.52.0-nightly (8e54a2113 2021-02-13)
binary: rustc
commit-hash: 8e54a21139ae96a2aca3129100b057662e2799b9
commit-date: 2021-02-13
host: x86_64-pc-windows-msvc
release: 1.52.0-nightly
LLVM version: 11.0.1

Error output

thread 'rustc' panicked at 'nonscalar layout for layout_scalar_valid_range type Integer: Layout {
    fields: Arbitrary {
        offsets: [
            Size {
                raw: 0,
            },
        ],
        memory_index: [
            0,
        ],
    },
    variants: Single {
        index: 0,
    },
    abi: Aggregate {
        sized: true,
    },
    largest_niche: None,
    align: AbiAndPrefAlign {
        abi: Align {
            pow2: 0,
        },
        pref: Align {
            pow2: 3,
        },
    },
    size: Size {
        raw: 1,
    },
}', compiler\rustc_middle\src\ty\layout.rs:955:30
stack backtrace:
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: internal compiler error: unexpected panic

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.52.0-nightly (8e54a2113 2021-02-13) running on x86_64-pc-windows-msvc

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

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

query stack during panic:
#0 [layout_raw] computing layout of `Integer`
end of query stack
Backtrace

``` stack backtrace: 0: 0x7ff9efe0857e - ::fmt::h160322fcc0f12a19 1: 0x7ff9efe3461b - core::fmt::write::ha3ea633b18d2da75 2: 0x7ff9efdfb6c8 - ::fmt::h0112a934cd010a1c 3: 0x7ff9efe0c74d - std::panicking::take_hook::h7c389d310fc84231 4: 0x7ff9efe0c219 - std::panicking::take_hook::h7c389d310fc84231 5: 0x7ff9db5e7fb7 - rustc_driver::report_ice::hd4f773c8d782ddcc 6: 0x7ff9efe0d1b2 - std::panicking::rust_panic_with_hook::h13af0da83034d6f3 7: 0x7ff9efe0cca1 - rust_begin_unwind 8: 0x7ff9efe08f0f - ::fmt::h160322fcc0f12a19 9: 0x7ff9efe0cbf9 - rust_begin_unwind 10: 0x7ff9efe0cbac - std::panicking::begin_panic_fmt::he3c286d5fa8e7e0e 11: 0x7ff9df76efdc - rustc_middle::ty::layout::provide::h744eb072f27cc1fb 12: 0x7ff9df762eee - ::fmt::heca06e76b524fab9 13: 0x7ff9df6c733e - ::visit_lifetime::h8f7e64a9af649c18 14: 0x7ff9df92a7af - rustc_middle::dep_graph::::debug_node::h9ab25c38d7e58d1f 15: 0x7ff9df99538f - rustc_middle::ty::structural_impls::::lift_to_tcx::hb296e5db2f221b13 16: 0x7ff9df71ee49 - ::fmt::h92978b980e216966 17: 0x7ff9df68bc2c - ::visit_lifetime::h8f7e64a9af649c18 18: 0x7ff9df5ab1f9 - ::visit_lifetime::h8f7e64a9af649c18 19: 0x7ff9df772055 - as rustc_target::abi::LayoutOf>::layout_of::h163bea4617d945e6 20: 0x7ff9db9e8672 - ::layout_of::h8a1f9503e576acc2 21: 0x7ff9db8f7bbe - as rustc_codegen_llvm::abi::FnAbiLlvmExt>::apply_attrs_callsite::h7396d38e02214aaa 22: 0x7ff9db8b25ec - ::drop::ha2c97848c144591f 23: 0x7ff9db8b4747 - ::drop::ha2c97848c144591f 24: 0x7ff9db9db57a - ::fmt::he99486cc7e05be9d 25: 0x7ff9db8afa1f - ::drop::ha2c97848c144591f 26: 0x7ff9db8f0a8a - as rustc_codegen_llvm::abi::FnAbiLlvmExt>::apply_attrs_callsite::h7396d38e02214aaa 27: 0x7ff9db8b086e - ::drop::ha2c97848c144591f 28: 0x7ff9db9eb32a - rustc_codegen_llvm::mono_item::::predefine_fn::hedf0f64e9e762a10 29: 0x7ff9db8ac83b - ::next::h587d021e0c727ed0 30: 0x7ff9db8fc68e - as rustc_codegen_llvm::abi::FnAbiLlvmExt>::apply_attrs_callsite::h7396d38e02214aaa 31: 0x7ff9dba1ba25 - ::fmt::hdb8b973c68ec0bd5 32: 0x7ff9db8ac453 - ::next::h587d021e0c727ed0 33: 0x7ff9db9d38a1 - ::codegen_crate::hd916541c477771f1 34: 0x7ff9db801431 - rustc_interface::queries::Queries::ongoing_codegen::hf5bf7ace4f995c32 35: 0x7ff9db612ac4 - rustc_ast::util::parser::prec_let_scrutinee_needs_par::hfc7ab7b91c80b01e 36: 0x7ff9db5ec17c - ::fmt::h565c544a3fcd0386 37: 0x7ff9db609009 - ::fmt::h565c544a3fcd0386 38: 0x7ff9db615cee - rustc_ast::util::parser::prec_let_scrutinee_needs_par::hfc7ab7b91c80b01e 39: 0x7ff9db58962d - ::fmt::ha43055df5546b109 40: 0x7ff9efe1c9fa - std::sys::windows::thread::Thread::new::hd38a560d738a063d 41: 0x7ffa58957034 - BaseThreadInitThunk 42: 0x7ffa58d42651 - RtlUserThreadStart ```

chengniansun commented 3 years ago

A duplicate

#[rustc_layout_scalar_valid_range_start(1)]
struct S<T> {
    i: u8,
    t: T,
}
struct A((u32, u32));
static Ca: S<A>;
JohnTitor commented 11 months ago

Fixed on the latest nightly, marking as E-needs-test @rustbot labels: +E-needs-test

the8472 commented 9 months ago

This still panics on current nightly

#![feature(rustc_attrs)]

#[rustc_layout_scalar_valid_range_end(38)]
#[repr(packed)]
struct Len(u16);

impl Clone for Len {
    fn clone(&self) -> Self { 
        unsafe { Len(self.0) }
     }
}
Backtrace ``` thread 'rustc' panicked at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/compiler/rustc_abi/src/layout.rs:281:22: nonscalar layout for layout_scalar_valid_range type: Layout { size: Size(2 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(1 bytes), }, abi: Aggregate { sized: true, }, fields: Arbitrary { offsets: [ Size(0 bytes), ], memory_index: [ 0, ], }, largest_niche: None, variants: Single { index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), } stack backtrace: 0: 0x7f65e82d06f6 - std::backtrace_rs::backtrace::libunwind::trace::h4c39d39dfbe9783d at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5 1: 0x7f65e82d06f6 - std::backtrace_rs::backtrace::trace_unsynchronized::h4dc8ace54ae311e5 at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f65e82d06f6 - std::sys_common::backtrace::_print_fmt::hb93bb30078f88f5e at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/sys_common/backtrace.rs:68:5 3: 0x7f65e82d06f6 - ::fmt::hc0a088b7e0825f6a at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/sys_common/backtrace.rs:44:22 4: 0x7f65e8322f40 - core::fmt::rt::Argument::fmt::h3bf37701eb7e7bdb at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/core/src/fmt/rt.rs:142:9 5: 0x7f65e8322f40 - core::fmt::write::h7e6ab5f6daf5c422 at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/core/src/fmt/mod.rs:1120:17 6: 0x7f65e82c44af - std::io::Write::write_fmt::h08c3306cb5bf723d at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/io/mod.rs:1810:15 7: 0x7f65e82d04d4 - std::sys_common::backtrace::_print::hafed43d2fa285a46 at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/sys_common/backtrace.rs:47:5 8: 0x7f65e82d04d4 - std::sys_common::backtrace::print::h39c23a58558ca2e2 at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/sys_common/backtrace.rs:34:9 9: 0x7f65e82d3267 - std::panicking::default_hook::{{closure}}::h33423deafbc8e615 10: 0x7f65e82d2fc9 - std::panicking::default_hook::h50ae27bf0009ee3b at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/panicking.rs:292:9 11: 0x7f65eb0c7ccc - std[3a009c5aa3cd6e18]::panicking::update_hook::>::{closure#0} 12: 0x7f65e82d39b6 - as core::ops::function::Fn>::call::h5ddf83902817cab1 at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/alloc/src/boxed.rs:2029:9 13: 0x7f65e82d39b6 - std::panicking::rust_panic_with_hook::hbc586ac9df6b73a8 at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/panicking.rs:783:13 14: 0x7f65e82d3702 - std::panicking::begin_panic_handler::{{closure}}::hf311b40154e75f12 at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/panicking.rs:657:13 15: 0x7f65e82d0bf6 - std::sys_common::backtrace::__rust_end_short_backtrace::he9e1eb231bad02f7 at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/sys_common/backtrace.rs:171:18 16: 0x7f65e82d3460 - rust_begin_unwind at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/panicking.rs:645:5 17: 0x7f65e831f645 - core::panicking::panic_fmt::hefa1490703b8b929 at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/core/src/panicking.rs:72:14 18: 0x7f65ec8ca304 - rustc_ty_utils[9a2fab9997b923d4]::layout::layout_of_uncached 19: 0x7f65ec8bfa12 - rustc_ty_utils[9a2fab9997b923d4]::layout::layout_of 20: 0x7f65ec8bf991 - rustc_query_impl[a5b594d180e67741]::plumbing::__rust_begin_short_backtrace::> 21: 0x7f65ec8becba - rustc_query_system[f0f2c4fcd6daa9a]::query::plumbing::try_execute_query::, rustc_middle[8402766bc0d86440]::query::erase::Erased<[u8; 16usize]>>, false, true, false>, rustc_query_impl[a5b594d180e67741]::plumbing::QueryCtxt, false> 22: 0x7f65ec8be938 - rustc_query_impl[a5b594d180e67741]::query_impl::layout_of::get_query_non_incr::__rust_end_short_backtrace 23: 0x7f65ecf83409 - ::check 24: 0x7f65ec56d491 - ::run_lint 25: 0x7f65ec3a7caf - rustc_mir_transform[19f7e7f8136d1e5c]::run_analysis_to_runtime_passes 26: 0x7f65e9cc7baf - rustc_mir_transform[19f7e7f8136d1e5c]::mir_drops_elaborated_and_const_checked 27: 0x7f65ec55b3b5 - rustc_query_impl[a5b594d180e67741]::plumbing::__rust_begin_short_backtrace::> 28: 0x7f65ec55b643 - rustc_query_system[f0f2c4fcd6daa9a]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[a5b594d180e67741]::plumbing::QueryCtxt, false> 29: 0x7f65ec55af12 - rustc_query_impl[a5b594d180e67741]::query_impl::mir_drops_elaborated_and_const_checked::get_query_non_incr::__rust_end_short_backtrace 30: 0x7f65eca47a5b - rustc_interface[dfee6d40a3df20bb]::passes::analysis 31: 0x7f65eca4725d - rustc_query_impl[a5b594d180e67741]::plumbing::__rust_begin_short_backtrace::> 32: 0x7f65ed24dac0 - rustc_query_system[f0f2c4fcd6daa9a]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[a5b594d180e67741]::plumbing::QueryCtxt, false> 33: 0x7f65ed24d8c7 - rustc_query_impl[a5b594d180e67741]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace 34: 0x7f65ed2e4252 - rustc_interface[dfee6d40a3df20bb]::interface::run_compiler::, rustc_driver_impl[99e8362fbaa298ef]::run_compiler::{closure#0}>::{closure#0} 35: 0x7f65ed26a346 - std[3a009c5aa3cd6e18]::sys_common::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[99e8362fbaa298ef]::run_compiler::{closure#0}>::{closure#0}, core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>>::{closure#0}, core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>> 36: 0x7f65ed26a173 - <::spawn_unchecked_, rustc_driver_impl[99e8362fbaa298ef]::run_compiler::{closure#0}>::{closure#0}, core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>>::{closure#0}, core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>>::{closure#1} as core[2b984803ee3f5e21]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 37: 0x7f65e82dd8e5 - as core::ops::function::FnOnce>::call_once::h446fa4c55b1bf8c4 at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/alloc/src/boxed.rs:2015:9 38: 0x7f65e82dd8e5 - as core::ops::function::FnOnce>::call_once::hb661ed53115c5033 at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/alloc/src/boxed.rs:2015:9 39: 0x7f65e82dd8e5 - std::sys::unix::thread::Thread::new::thread_start::hbd74c47d36330101 at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/sys/unix/thread.rs:108:17 40: 0x7f65e81f0609 - start_thread 41: 0x7f65e8113353 - clone 42: 0x0 - error: the compiler unexpectedly panicked. this is a bug. note: using internal features is not supported and expected to cause internal compiler errors when used incorrectly note: please attach the file at `/playground/rustc-ice-2023-12-23T15_52_19-21.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 `Len` #1 [mir_drops_elaborated_and_const_checked] elaborating drops for `::clone` #2 [analysis] running analysis passes on this crate end of query stack ```
oli-obk commented 6 months ago

Yea, that internal attribute may only be used in very specific circumstances that the layout code describes. Please do not use it outside the standard library or the compiler