rust-lang / rust

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

ICE: rustc_abi `the len is 0 but the index is 0` calculating offset #123917

Closed matthiaskrgr closed 3 weeks ago

matthiaskrgr commented 4 months ago

auto-reduced (treereduce-rust) + some manual reduction

use std::marker::PhantomData;

pub struct Id<'id>();

pub struct Item<'life, T> {
    data: T,
}

pub struct Token<'life, 'borrow, 'compact, 'reborrow, T>
where
    'life: 'reborrow,
    T: Tokenize,
{
    ptr: *mut <T as Tokenize>::Tokenized,
    ptr: core::ptr::NonNull<T::Tokenized>,
    _phantom: PhantomData<Id<'life>>,
}

impl<'life> Arena<'life> {
    pub fn tokenize<'before, 'compact, 'borrow, 'reborrow, T, U>(
        item: Item<'life, &'before mut T>,
    ) -> Token<'life, 'borrow, 'compact, 'reborrow, U>
    where
        T: Tokenize<'life, 'borrow, 'compact, 'reborrow, Untokenized = U>,
        T::Untokenized: Tokenize<'life, 'borrow, 'compact, 'reborrow>,
    {
        let dst = item.data as *mut T as *mut T::Tokenized;
        Token {
            ptr: core::ptr::NonNull::new(dst as *mut _).unwrap(),
            _phantom: PhantomData,
        }
    }
}

pub trait Tokenize {
    type Tokenized;
    type Untokenized;
}
original code

original: ````rust //@ check-pass #![feature(inline_const)] #![feature(generic_const_exprs)] #![allow(incomplete_features)] use std::marker::PhantomData; pub struct Equal(); pub trait True {} impl True for Equal {} // replacement for generativity pub struct Id<'id>(PhantomData &'id ()>); pub struct Guard<'id>(Id<'id>); fn make_guard<'id>(i: &'id Id<'id>) -> Guard<'id> { Guard(Id(PhantomData)) } impl<'id> Into> for Guard<'id> { fn into(self) -> Id<'id> { self.0 } } pub struct Arena<'life> { bytes: *mut [u8], //bitmap: RefCell, _token: PhantomData>, } #[repr(transparent)] pub struct Item<'life, T> { data: T, _phantom: PhantomData>, } #[repr(transparent)] pub struct Token<'life, 'borrow, 'compact, 'reborrow, T> where 'life: 'reborrow, T: Tokenize<'life, 'borrow, 'compact, 'reborrow>, { ptr: *mut ::Tokenized, ptr: core::ptr::NonNull, _phantom: PhantomData>, _compact: PhantomData<&'borrow Guard<'compact>>, _result: PhantomData<&'reborrow T::Untokenized>, } impl<'life> Arena<'life> { pub fn tokenize<'before, 'compact, 'borrow, 'reborrow, T, U>( &self, guard: &'borrow Guard<'compact>, item: Item<'life, &'before mut T>, ) -> Token<'life, 'borrow, 'compact, 'reborrow, U> where T: Tokenize<'life, 'borrow, 'compact, 'reborrow, Untokenized = U>, T::Untokenized: Tokenize<'life, 'borrow, 'compact, 'reborrow>, Equal<{ core::mem::size_of::() }, { core::mem::size_of::() }>: True, 'compact: 'borrow, 'life: 'reborrow, 'life: 'compact, 'life: 'borrow, // 'borrow: 'before ?? { let dst = item.data as *mut T as *mut T::Tokenized; Token { ptr: core::ptr::NonNull::new(dst as *mut _).unwrap(), _phantom: PhantomData, _compact: PhantomData, _result: PhantomData, } } } pub trait Tokenize<'life, 'borrow, 'compact, 'reborrow> where 'compact: 'borrow, 'life: 'reborrow, 'life: 'borrow, 'life: 'compact, { type Tokenized; type Untokenized; const TO: fn(&Arena<'life>, &'borrow Guard<'compact>, Self) -> Self::Tokenized; const FROM: fn(&'reborrow Arena<'life>, Self::Tokenized) -> Self::Untokenized; } macro_rules! tokenize { ($to:expr, $from:expr) => { const TO: fn(&Arena<'life>, &'borrow Guard<'compact>, Self) -> Self::Tokenized = $to; const FROM: fn(&'reborrow Arena<'life>, Self::Tokenized) -> Self::Untokenized = $from; }; } struct Foo<'life, 'borrow>(Option>); struct UnitVariant<'life, 'borrow, 'compact, 'reborrow>( Option>, ); struct Bar(u8); impl<'life, 'before, 'borrow, 'compact, 'reborrow> Tokenize<'life, 'borrow, 'compact, 'reborrow> for Foo<'life, 'before> where 'compact: 'borrow, 'life: 'reborrow, 'life: 'borrow, 'life: 'compact, { type Tokenized = TokenFoo<'life, 'borrow, 'compact, 'reborrow>; type Untokenized = Foo<'life, 'reborrow>; tokenize!(foo_to, foo_from); } impl<'life, 'borrow, 'compact, 'reborrow> Tokenize<'life, 'borrow, 'compact, 'reborrow> for Bar where 'compact: 'borrow, 'life: 'reborrow, 'life: 'borrow, 'life: 'compact, { type Tokenized = Bar; type Untokenized = Bar; tokenize!(bar_to, bar_from); } fn bar_to<'life, 'borrow, 'compact>( arena: &Arena<'life>, guard: &'borrow Guard<'compact>, s: Bar, ) -> Bar { s } fn bar_from<'life, 'reborrow>(arena: &'reborrow Arena<'life>, s: Bar) -> Bar { s } fn foo_to<'life, 'borrow, 'compact, 'reborrow, 'before>( arena: &'before Arena<'life>, guard: &'borrow Guard<'compact>, s: Foo<'life, 'before>, ) -> TokenFoo<'life, 'borrow, 'compact, 'reborrow> { let Foo(bar) = s; TokenFoo(bar.map(|bar| arena.tokenize(guard, bar))) } fn foo_from<'life, 'borrow, 'compact, 'reborrow>( arena: &'reborrow Arena<'life>, s: TokenFoo<'life, 'borrow, 'compact, 'reborrow>, ) -> Foo<'life, 'reborrow> { Foo(s.0.map(|bar| panic!())) } fn main() {} ````

Version information

rustc 1.79.0-nightly (f3c660886 2024-04-14)
binary: rustc
commit-hash: f3c66088610c1b80110297c2d9a8b5f9265b013f
commit-date: 2024-04-14
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.3

Command: /home/matthias/.rustup/toolchains/master/bin/rustc -Zmir-opt-level=5 -Zpolymorphize=on

Program output

``` error[E0412]: cannot find type `Arena` in this scope --> /tmp/icemaker_global_tempdir.iy1H39B5cCGu/rustc_testrunner_tmpdir_reporting.szJD7axysECi/mvce.rs:19:13 | 19 | impl<'life> Arena<'life> { | ^^^^^ not found in this scope error[E0601]: `main` function not found in crate `mvce` --> /tmp/icemaker_global_tempdir.iy1H39B5cCGu/rustc_testrunner_tmpdir_reporting.szJD7axysECi/mvce.rs:38:2 | 38 | } | ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.iy1H39B5cCGu/rustc_testrunner_tmpdir_reporting.szJD7axysECi/mvce.rs` error[E0124]: field `ptr` is already declared --> /tmp/icemaker_global_tempdir.iy1H39B5cCGu/rustc_testrunner_tmpdir_reporting.szJD7axysECi/mvce.rs:15:5 | 14 | ptr: *mut ::Tokenized, | ------------------------------------ `ptr` first declared here 15 | ptr: core::ptr::NonNull, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ field already declared error[E0392]: lifetime parameter `'id` is never used --> /tmp/icemaker_global_tempdir.iy1H39B5cCGu/rustc_testrunner_tmpdir_reporting.szJD7axysECi/mvce.rs:3:15 | 3 | pub struct Id<'id>(); | ^^^ unused lifetime parameter | = help: consider removing `'id`, referring to it in a field, or using a marker such as `PhantomData` error[E0392]: lifetime parameter `'life` is never used --> /tmp/icemaker_global_tempdir.iy1H39B5cCGu/rustc_testrunner_tmpdir_reporting.szJD7axysECi/mvce.rs:5:17 | 5 | pub struct Item<'life, T> { | ^^^^^ unused lifetime parameter | = help: consider removing `'life`, referring to it in a field, or using a marker such as `PhantomData` error[E0392]: lifetime parameter `'life` is never used --> /tmp/icemaker_global_tempdir.iy1H39B5cCGu/rustc_testrunner_tmpdir_reporting.szJD7axysECi/mvce.rs:9:18 | 9 | pub struct Token<'life, 'borrow, 'compact, 'reborrow, T> | ^^^^^ unused lifetime parameter | = help: consider removing `'life`, referring to it in a field, or using a marker such as `PhantomData` error[E0392]: lifetime parameter `'borrow` is never used --> /tmp/icemaker_global_tempdir.iy1H39B5cCGu/rustc_testrunner_tmpdir_reporting.szJD7axysECi/mvce.rs:9:25 | 9 | pub struct Token<'life, 'borrow, 'compact, 'reborrow, T> | ^^^^^^^ unused lifetime parameter | = help: consider removing `'borrow`, referring to it in a field, or using a marker such as `PhantomData` error[E0392]: lifetime parameter `'compact` is never used --> /tmp/icemaker_global_tempdir.iy1H39B5cCGu/rustc_testrunner_tmpdir_reporting.szJD7axysECi/mvce.rs:9:34 | 9 | pub struct Token<'life, 'borrow, 'compact, 'reborrow, T> | ^^^^^^^^ unused lifetime parameter | = help: consider removing `'compact`, referring to it in a field, or using a marker such as `PhantomData` error[E0392]: lifetime parameter `'reborrow` is never used --> /tmp/icemaker_global_tempdir.iy1H39B5cCGu/rustc_testrunner_tmpdir_reporting.szJD7axysECi/mvce.rs:9:44 | 9 | pub struct Token<'life, 'borrow, 'compact, 'reborrow, T> | ^^^^^^^^^ unused lifetime parameter | = help: consider removing `'reborrow`, referring to it in a field, or using a marker such as `PhantomData` error[E0107]: trait takes 0 lifetime arguments but 4 lifetime arguments were supplied --> /tmp/icemaker_global_tempdir.iy1H39B5cCGu/rustc_testrunner_tmpdir_reporting.szJD7axysECi/mvce.rs:24:12 | 24 | T: Tokenize<'life, 'borrow, 'compact, 'reborrow, Untokenized = U>, | ^^^^^^^^------------------------------------------------------ help: remove these generics | | | expected 0 lifetime arguments | note: trait defined here, with 0 lifetime parameters --> /tmp/icemaker_global_tempdir.iy1H39B5cCGu/rustc_testrunner_tmpdir_reporting.szJD7axysECi/mvce.rs:35:11 | 35 | pub trait Tokenize { | ^^^^^^^^ error[E0107]: trait takes 0 lifetime arguments but 4 lifetime arguments were supplied --> /tmp/icemaker_global_tempdir.iy1H39B5cCGu/rustc_testrunner_tmpdir_reporting.szJD7axysECi/mvce.rs:25:25 | 25 | T::Untokenized: Tokenize<'life, 'borrow, 'compact, 'reborrow>, | ^^^^^^^^------------------------------------- help: remove these generics | | | expected 0 lifetime arguments | note: trait defined here, with 0 lifetime parameters --> /tmp/icemaker_global_tempdir.iy1H39B5cCGu/rustc_testrunner_tmpdir_reporting.szJD7axysECi/mvce.rs:35:11 | 35 | pub trait Tokenize { | ^^^^^^^^ thread 'rustc' panicked at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/compiler/rustc_abi/src/lib.rs:1201:66: index out of bounds: the len is 0 but the index is 0 stack backtrace: 0: 0x7f75db283b55 - std::backtrace_rs::backtrace::libunwind::trace::h03dc4c75a602abd6 at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5 1: 0x7f75db283b55 - std::backtrace_rs::backtrace::trace_unsynchronized::h1821d384db4b0e26 at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f75db283b55 - std::sys_common::backtrace::_print_fmt::h1412fd631a460d8d at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/std/src/sys_common/backtrace.rs:68:5 3: 0x7f75db283b55 - ::fmt::h585bce00fd030aee at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/std/src/sys_common/backtrace.rs:44:22 4: 0x7f75db2d2e3b - core::fmt::rt::Argument::fmt::h51a122f876b01508 at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/core/src/fmt/rt.rs:165:63 5: 0x7f75db2d2e3b - core::fmt::write::h4dfb720a2f630255 at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/core/src/fmt/mod.rs:1157:21 6: 0x7f75db27875f - std::io::Write::write_fmt::hb827ffc1239a96a6 at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/std/src/io/mod.rs:1832:15 7: 0x7f75db28392e - std::sys_common::backtrace::_print::h34ff464c9a4e054c at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/std/src/sys_common/backtrace.rs:47:5 8: 0x7f75db28392e - std::sys_common::backtrace::print::hb47d1eaca16fac84 at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/std/src/sys_common/backtrace.rs:34:9 9: 0x7f75db2862a9 - std::panicking::default_hook::{{closure}}::h7fc91e7cfecf6096 10: 0x7f75db285fed - std::panicking::default_hook::h677e761d182faf87 at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/std/src/panicking.rs:291:9 11: 0x7f75d7c7731c - std[184458c9395fed62]::panicking::update_hook::>::{closure#0} 12: 0x7f75db2869ac - as core::ops::function::Fn>::call::ha779244beb256d6f at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/alloc/src/boxed.rs:2032:9 13: 0x7f75db2869ac - std::panicking::rust_panic_with_hook::h74b58e16e7d0027a at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/std/src/panicking.rs:792:13 14: 0x7f75db286756 - std::panicking::begin_panic_handler::{{closure}}::h3945bb507775f2bc at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/std/src/panicking.rs:657:13 15: 0x7f75db284019 - std::sys_common::backtrace::__rust_end_short_backtrace::hc7e0fa976db9fff0 at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/std/src/sys_common/backtrace.rs:171:18 16: 0x7f75db286487 - rust_begin_unwind at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/std/src/panicking.rs:645:5 17: 0x7f75db2cf2e6 - core::panicking::panic_fmt::ha9dbe3d7803e94c3 at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/core/src/panicking.rs:72:14 18: 0x7f75db2cf507 - core::panicking::panic_bounds_check::h6b123be699451bc5 at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/core/src/panicking.rs:268:5 19: 0x7f75da677ac8 - >::offset.cold 20: 0x7f75d9973082 - >::project_field:: 21: 0x7f75d82fb944 - ::assign_constant::{closure#0} 22: 0x7f75d8259929 - ::for_each_projection_value::::assign_constant::{closure#0}, ::assign_constant::{closure#1}> 23: 0x7f75d82fb8f6 - ::assign_constant 24: 0x7f75d82cac17 - ::assign_operand 25: 0x7f75d82c9b27 - ::handle_assign 26: 0x7f75d82f9cce - ::run_pass 27: 0x7f75d900188d - rustc_mir_transform[560a276af31fbb04]::pass_manager::run_passes_inner 28: 0x7f75d96259b1 - rustc_mir_transform[560a276af31fbb04]::optimized_mir 29: 0x7f75d9624b9b - rustc_query_impl[8a57831cea33f912]::plumbing::__rust_begin_short_backtrace::> 30: 0x7f75d91041de - rustc_query_system[698870ff4aca75a8]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[8a57831cea33f912]::plumbing::QueryCtxt, false> 31: 0x7f75d910391e - rustc_query_impl[8a57831cea33f912]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace 32: 0x7f75d92f261e - rustc_middle[55da801b9dd73f34]::query::plumbing::query_get_at::>> 33: 0x7f75d958ebc7 - rustc_query_impl[8a57831cea33f912]::plumbing::__rust_begin_short_backtrace::> 34: 0x7f75d958e399 - rustc_query_system[698870ff4aca75a8]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[8a57831cea33f912]::plumbing::QueryCtxt, false> 35: 0x7f75d958e08e - rustc_query_impl[8a57831cea33f912]::query_impl::unused_generic_params::get_query_non_incr::__rust_end_short_backtrace 36: 0x7f75d9589bc0 - rustc_interface[e2c21f0ab22f590a]::passes::analysis 37: 0x7f75d95888c7 - rustc_query_impl[8a57831cea33f912]::plumbing::__rust_begin_short_backtrace::> 38: 0x7f75d9e61ea5 - rustc_query_system[698870ff4aca75a8]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[8a57831cea33f912]::plumbing::QueryCtxt, false> 39: 0x7f75d9e61c09 - rustc_query_impl[8a57831cea33f912]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace 40: 0x7f75d9cc0793 - rustc_interface[e2c21f0ab22f590a]::interface::run_compiler::, rustc_driver_impl[f047c15e8130c94f]::run_compiler::{closure#0}>::{closure#0} 41: 0x7f75d9d93d1d - std[184458c9395fed62]::sys_common::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[f047c15e8130c94f]::run_compiler::{closure#0}>::{closure#0}, core[cfe0013222fec553]::result::Result<(), rustc_span[27037ef1f63d3fc0]::ErrorGuaranteed>>::{closure#0}, core[cfe0013222fec553]::result::Result<(), rustc_span[27037ef1f63d3fc0]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[cfe0013222fec553]::result::Result<(), rustc_span[27037ef1f63d3fc0]::ErrorGuaranteed>> 42: 0x7f75d9d93b2a - <::spawn_unchecked_, rustc_driver_impl[f047c15e8130c94f]::run_compiler::{closure#0}>::{closure#0}, core[cfe0013222fec553]::result::Result<(), rustc_span[27037ef1f63d3fc0]::ErrorGuaranteed>>::{closure#0}, core[cfe0013222fec553]::result::Result<(), rustc_span[27037ef1f63d3fc0]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[cfe0013222fec553]::result::Result<(), rustc_span[27037ef1f63d3fc0]::ErrorGuaranteed>>::{closure#2} as core[cfe0013222fec553]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 43: 0x7f75db29089b - as core::ops::function::FnOnce>::call_once::h5913e9e2478013f2 at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/alloc/src/boxed.rs:2018:9 44: 0x7f75db29089b - as core::ops::function::FnOnce>::call_once::hc7951dd75ee74cd3 at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/alloc/src/boxed.rs:2018:9 45: 0x7f75db29089b - std::sys::pal::unix::thread::Thread::new::thread_start::h59778d43da0379b7 at /rustc/f3c66088610c1b80110297c2d9a8b5f9265b013f/library/std/src/sys/pal/unix/thread.rs:108:17 46: 0x7f75d4aa955a - 47: 0x7f75d4b26a3c - 48: 0x0 - 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: rustc 1.79.0-nightly (f3c660886 2024-04-14) running on x86_64-unknown-linux-gnu note: compiler flags: -Z mir-opt-level=5 -Z polymorphize=on -Z dump-mir-dir=dir query stack during panic: #0 [optimized_mir] optimizing MIR for `::tokenize` #1 [unused_generic_params] determining which generic parameters are unused by `::tokenize` #2 [analysis] running analysis passes on this crate end of query stack error: aborting due to 11 previous errors Some errors have detailed explanations: E0107, E0124, E0392, E0412, E0601. For more information about an error, try `rustc --explain E0107`. ```

matthiaskrgr commented 4 months ago

Another one for #120550

Noratrieb commented 4 months ago

would be great if someone could minimize it further (remove as many of the errors as possible, I doubt all of them are necessary)

Luk-ESC commented 4 months ago

Minimized further:

struct NonNull<T>(*mut T);

struct Token<T> {
    ptr: *mut T,
    ptr: NonNull<T>,
    _extra_field: ()
}

fn tokenize<T>(item: *mut T) -> Token<T> {
    Token {
        ptr: NonNull(item),
        _extra_field: ()
    }
}
adwinwhite commented 3 weeks ago

Cannot reproduce. May be fixed by #126045.

matthiaskrgr commented 3 weeks ago

This was fixed by 2e3842b6d024943dfb06c73dd15e980f6e69fcb7 but the ticket wasn't closed :crying_cat_face: cc @oli-obk