rust-lang / rust

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

ICE: `builtin derive created an unaligned reference` #127678

Open kjughx opened 1 month ago

kjughx commented 1 month ago

I'm developing a toy kernel and created a proc-macro for annotation packed structs. Using the macro along with #[derive(Debug)] causes an ice.

Code

src/main.rs

extern crate packed_macro;
use packed_macro::{packed, Packed};

pub trait _Packed_: Sized {}

pub trait FromBytes: _Packed_ {}

#[derive(Debug)]
#[packed]
pub struct Struct {
    x: u32,
}

fn main() {}

proc-macro crate:

#[proc_macro_attribute]
pub fn packed(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let input = parse_macro_input!(item as ItemStruct);

    let vis = &input.vis;
    let name = &input.ident;
    let fields = &input.fields;

    let expanded = quote! {
        #[repr(C, packed)]
        #[derive(Packed, Clone, Copy)]
        #vis struct #name #fields
    };

    TokenStream::from(expanded)
}

#[proc_macro_derive(Packed)]
pub fn derive_packed(input: TokenStream) -> TokenStream {
    let name = &parse_macro_input!(input as DeriveInput).ident;

    TokenStream::from(quote! {
        impl crate::_Packed_ for #name {}
    })
}

Meta

rustc --version --verbose:

rustc 1.81.0-nightly (c6727fc9b 2024-07-12)
binary: rustc
commit-hash: c6727fc9b5c64cefa7263486497ee95e529bd0f8
commit-date: 2024-07-12
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7

Error output

error: internal compiler error: compiler/rustc_mir_transform/src/check_packed_ref.rs:50:21: builtin derive created an unaligned reference
  --> src/main.rs:11:5
   |
8  | #[derive(Debug)]
   |          ----- in this derive macro expansion
...
11 |     x: u32,
   |     ^^^^^^
   |
   = note: this error: internal compiler error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
Backtrace

``` thread 'rustc' panicked at compiler/rustc_mir_transform/src/check_packed_ref.rs:50:21: Box stack backtrace: 0: 0x7f05ae7459e5 - std::backtrace_rs::backtrace::libunwind::trace::h90ef53671b8f35e2 at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5 1: 0x7f05ae7459e5 - std::backtrace_rs::backtrace::trace_unsynchronized::hd3a497571d63ea87 at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f05ae7459e5 - std::backtrace::Backtrace::create::h8bff861c2af14d3f at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/std/src/backtrace.rs:331:13 3: 0x7f05ae745935 - std::backtrace::Backtrace::force_capture::h84acf91f68ab21d6 at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/std/src/backtrace.rs:312:9 4: 0x7f05b1c0b9ff - std[85c3ca4c170da0bb]::panicking::update_hook::>::{closure#0} 5: 0x7f05ae760aff - as core::ops::function::Fn>::call::hbe29137bf798c82c at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/alloc/src/boxed.rs:2078:9 6: 0x7f05ae760aff - std::panicking::rust_panic_with_hook::h869b30cecbb157c4 at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/std/src/panicking.rs:804:13 7: 0x7f05b1c46151 - std[85c3ca4c170da0bb]::panicking::begin_panic::::{closure#0} 8: 0x7f05b1c39106 - std[85c3ca4c170da0bb]::sys::backtrace::__rust_end_short_backtrace::::{closure#0}, !> 9: 0x7f05b1c390b6 - std[85c3ca4c170da0bb]::panicking::begin_panic:: 10: 0x7f05b1c4f391 - ::emit_producing_guarantee 11: 0x7f05b235dd0d - ::span_bug:: 12: 0x7f05b23905d8 - rustc_middle[aeeec62211ae3a75]::util::bug::opt_span_bug_fmt::::{closure#0} 13: 0x7f05b239060a - rustc_middle[aeeec62211ae3a75]::ty::context::tls::with_opt::::{closure#0}, !>::{closure#0} 14: 0x7f05b237c83b - rustc_middle[aeeec62211ae3a75]::ty::context::tls::with_context_opt::::{closure#0}, !>::{closure#0}, !> 15: 0x7f05b237bbb7 - rustc_middle[aeeec62211ae3a75]::util::bug::span_bug_fmt:: 16: 0x7f05b323fdba - as rustc_middle[aeeec62211ae3a75]::mir::MirPass>::run_pass 17: 0x7f05b3201cbb - rustc_mir_transform[e83149d28cb0f37b]::pass_manager::run_passes_inner 18: 0x7f05b36d526c - rustc_query_impl[1a70eeac4d27cd99]::plumbing::__rust_begin_short_backtrace::> 19: 0x7f05b358c038 - rustc_query_system[a1535082eb398a6e]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[1a70eeac4d27cd99]::plumbing::QueryCtxt, true> 20: 0x7f05b359579c - rustc_query_impl[1a70eeac4d27cd99]::query_impl::mir_built::get_query_incr::__rust_end_short_backtrace 21: 0x7f05b36ce63e - rustc_mir_build[ef33ca6215fc9157]::check_unsafety::check_unsafety 22: 0x7f05b36ce3dd - rustc_query_impl[1a70eeac4d27cd99]::plumbing::__rust_begin_short_backtrace::> 23: 0x7f05b359362e - rustc_query_system[a1535082eb398a6e]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[1a70eeac4d27cd99]::plumbing::QueryCtxt, true> 24: 0x7f05b3593141 - rustc_query_impl[1a70eeac4d27cd99]::query_impl::check_unsafety::get_query_incr::__rust_end_short_backtrace 25: 0x7f05b357cdb8 - rustc_interface[a6452cf60fc426fe]::passes::analysis 26: 0x7f05b357bf07 - rustc_query_impl[1a70eeac4d27cd99]::plumbing::__rust_begin_short_backtrace::> 27: 0x7f05b40ac804 - rustc_query_system[a1535082eb398a6e]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[1a70eeac4d27cd99]::plumbing::QueryCtxt, true> 28: 0x7f05b40ac4ba - rustc_query_impl[1a70eeac4d27cd99]::query_impl::analysis::get_query_incr::__rust_end_short_backtrace 29: 0x7f05b3fc2745 - rustc_interface[a6452cf60fc426fe]::interface::run_compiler::, rustc_driver_impl[dc4b00a9cfefcaac]::run_compiler::{closure#0}>::{closure#1} 30: 0x7f05b3f8f589 - std[85c3ca4c170da0bb]::sys::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[dc4b00a9cfefcaac]::run_compiler::{closure#0}>::{closure#1}, core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>>::{closure#0}, core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>> 31: 0x7f05b3f8f33a - <::spawn_unchecked_, rustc_driver_impl[dc4b00a9cfefcaac]::run_compiler::{closure#0}>::{closure#1}, core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>>::{closure#0}, core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[2213ecb858fb81d5]::result::Result<(), rustc_span[67819806738ee56d]::ErrorGuaranteed>>::{closure#2} as core[2213ecb858fb81d5]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 32: 0x7f05ae76a95b - as core::ops::function::FnOnce>::call_once::h7aa527871fe5008e at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/alloc/src/boxed.rs:2064:9 33: 0x7f05ae76a95b - as core::ops::function::FnOnce>::call_once::h661f413d6ffad84b at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/alloc/src/boxed.rs:2064:9 34: 0x7f05ae76a95b - std::sys::pal::unix::thread::Thread::new::thread_start::h77623da8b368e6ce at /rustc/c6727fc9b5c64cefa7263486497ee95e529bd0f8/library/std/src/sys/pal/unix/thread.rs:108:17 35: 0x7f05ae53ca42 - start_thread 36: 0x7f05ae5bc05c - clone3 37: 0x0 - rustc version: 1.81.0-nightly (c6727fc9b 2024-07-12) platform: x86_64-unknown-linux-gnu query stack during panic: #0 [mir_built] building MIR for `::fmt` #1 [check_unsafety] unsafety-checking `::fmt` #2 [analysis] running analysis passes on this crate end of query stack ```

kjughx commented 1 month ago

The error disappears when moving the order of the macros, i.e. this works fine:

#[packed]
#[derive(Debug)]
pub struct Struct {
    x: u32,
}
petrochenkov commented 1 month ago

This is similar to https://github.com/rust-lang/rust/issues/120873.

GrigorenkoPV commented 1 month ago

Regression in nightly-2023-01-31 (e972bc8083d5228536dfd42913c8778b6bb04c8e...001a77fac33f6560ff361ff38f661ff5f1c6bf85) If you change #[derive(Debug)] to #[derive(Copy, Debug)], then it regresses only a year later, in #120847.