rust-lang / rustc_codegen_cranelift

Cranelift based backend for rustc
Apache License 2.0
1.52k stars 94 forks source link

ICE: `Can't write value with incompatible type` #1429

Closed matthiaskrgr closed 7 months ago

matthiaskrgr commented 7 months ago

snippet:

#![feature(adt_const_params, coroutines, coroutine_trait)]

use std::ops::Coroutine;
use std::pin::Pin;

fn main() {
    GenericStruct::<i32, i32>::impl_function();

    // Closure
    let closure = || TestStruct1;
    closure();
}

struct TestStruct1;

struct GenericStruct<T1, T2>(std::marker::PhantomData<(T1, T2)>);

impl<T1, T2> GenericStruct<T1, T2> {
    pub fn impl_function() {
        // Closure
        let closure = || TestStruct1;
        closure();

        let mut coroutine = || {
            yield;
            return;
        };
        Pin::new(&mut coroutine).resume(());
    }
}

Version information

rustc 1.76.0-nightly (739d55682 2023-11-22)
binary: rustc
commit-hash: 739d55682659e4a81db6fd83f52b2491505030ea
commit-date: 2023-11-22
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.5

Command: /home/matthias/.rustup/toolchains/master/bin/rustc -Zpolymorphize=on -Zcodegen-backend=cranelift

Program output

``` warning: the feature `adt_const_params` is incomplete and may not be safe to use and/or cause compiler crashes --> /tmp/icemaker_global_tempdir.P7Hp73Jhct5x/rustc_testrunner_tmpdir_reporting.djVsk4t1fURb/mvce.rs:1:12 | 1 | #![feature(adt_const_params, coroutines, coroutine_trait)] | ^^^^^^^^^^^^^^^^ | = note: see issue #95174 for more information = note: `#[warn(incomplete_features)]` on by default thread 'rustc' panicked at src/abi/pass_mode.rs:221:5: assertion `left == right` failed: Can't write value with incompatible type Coroutine(DefId(0:19 ~ mvce[cafd]::{impl#0}::impl_function::{closure#1}), [i32, i32, (), (), (), CoroutineWitness(DefId(0:19 ~ mvce[cafd]::{impl#0}::impl_function::{closure#1}), [i32, i32]), ()], Movable) to place with type Coroutine(DefId(0:19 ~ mvce[cafd]::{impl#0}::impl_function::{closure#1}), [T1/#0, T2/#1, (), (), (), CoroutineWitness(DefId(0:19 ~ mvce[cafd]::{impl#0}::impl_function::{closure#1}), [T1/#0, T2/#1]), ()], Movable) [i32, i32] [CPlace { inner: Addr(Pointer { base: Dangling(Align(8 bytes)), offset: Offset32(0) }, None), layout: TyAndLayout { ty: (), layout: Layout { size: Size(0 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, abi: Aggregate { sized: true }, fields: Arbitrary { offsets: [], memory_index: [] }, largest_niche: None, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes) } } }, CPlace { inner: Addr(Pointer { base: Dangling(Align(8 bytes)), offset: Offset32(0) }, None), layout: TyAndLayout { ty: {closure@/tmp/icemaker_global_tempdir.P7Hp73Jhct5x/rustc_testrunner_tmpdir_reporting.djVsk4t1fURb/mvce.rs:21:23: 21:25}, layout: Layout { size: Size(0 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, abi: Aggregate { sized: true }, fields: Arbitrary { offsets: [], memory_index: [] }, largest_niche: None, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes) } } }, CPlace { inner: Addr(Pointer { base: Dangling(Align(8 bytes)), offset: Offset32(0) }, None), layout: TyAndLayout { ty: TestStruct1, layout: Layout { size: Size(0 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, abi: Aggregate { sized: true }, fields: Arbitrary { offsets: [], memory_index: [] }, largest_niche: None, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes) } } }, CPlace { inner: Var(_3, var0), layout: TyAndLayout { ty: &{closure@/tmp/icemaker_global_tempdir.P7Hp73Jhct5x/rustc_testrunner_tmpdir_reporting.djVsk4t1fURb/mvce.rs:21:23: 21:25}, layout: Layout { size: Size(8 bytes), align: AbiAndPrefAlign { abi: Align(8 bytes), pref: Align(8 bytes) }, abi: Scalar(Initialized { value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), fields: Primitive, largest_niche: Some(Niche { offset: Size(0 bytes), value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes) } } }, CPlace { inner: Addr(Pointer { base: Stack(ss0), offset: Offset32(0) }, None), layout: TyAndLayout { ty: {coroutine@/tmp/icemaker_global_tempdir.P7Hp73Jhct5x/rustc_testrunner_tmpdir_reporting.djVsk4t1fURb/mvce.rs:24:29: 24:31}, layout: Layout { size: Size(1 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, abi: Aggregate { sized: true }, fields: Arbitrary { offsets: [Size(0 bytes)], memory_index: [0] }, largest_niche: Some(Niche { offset: Size(0 bytes), value: Int(I8, false), valid_range: 0..=3 }), variants: Multiple { tag: Initialized { value: Int(I8, false), valid_range: 0..=3 }, tag_encoding: Direct, tag_field: 0, variants: [Layout { size: Size(1 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, abi: Aggregate { sized: true }, fields: Arbitrary { offsets: [], memory_index: [] }, largest_niche: None, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes) }, Layout { size: Size(1 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, abi: Aggregate { sized: true }, fields: Arbitrary { offsets: [], memory_index: [] }, largest_niche: None, variants: Single { index: 1 }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes) }, Layout { size: Size(1 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, abi: Aggregate { sized: true }, fields: Arbitrary { offsets: [], memory_index: [] }, largest_niche: None, variants: Single { index: 2 }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes) }, Layout { size: Size(1 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, abi: Aggregate { sized: true }, fields: Arbitrary { offsets: [], memory_index: [] }, largest_niche: None, variants: Single { index: 3 }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes) }] }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes) } } }, CPlace { inner: Addr(Pointer { base: Stack(ss1), offset: Offset32(0) }, None), layout: TyAndLayout { ty: CoroutineState<(), ()>, layout: Layout { size: Size(1 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, abi: Scalar(Initialized { value: Int(I8, false), valid_range: 0..=1 }), fields: Arbitrary { offsets: [Size(0 bytes)], memory_index: [0] }, largest_niche: Some(Niche { offset: Size(0 bytes), value: Int(I8, false), valid_range: 0..=1 }), variants: Multiple { tag: Initialized { value: Int(I8, false), valid_range: 0..=1 }, tag_encoding: Direct, tag_field: 0, variants: [Layout { size: Size(1 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, abi: Scalar(Initialized { value: Int(I8, false), valid_range: 0..=1 }), fields: Arbitrary { offsets: [Size(1 bytes)], memory_index: [0] }, largest_niche: None, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes) }, Layout { size: Size(1 bytes), align: AbiAndPrefAlign { abi: Align(1 bytes), pref: Align(8 bytes) }, abi: Scalar(Initialized { value: Int(I8, false), valid_range: 0..=1 }), fields: Arbitrary { offsets: [Size(1 bytes)], memory_index: [0] }, largest_niche: None, variants: Single { index: 1 }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes) }] }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes) } } }, CPlace { inner: Addr(Pointer { base: Stack(ss2), offset: Offset32(0) }, None), layout: TyAndLayout { ty: Pin<&mut {coroutine@/tmp/icemaker_global_tempdir.P7Hp73Jhct5x/rustc_testrunner_tmpdir_reporting.djVsk4t1fURb/mvce.rs:24:29: 24:31}>, layout: Layout { size: Size(8 bytes), align: AbiAndPrefAlign { abi: Align(8 bytes), pref: Align(8 bytes) }, abi: Scalar(Initialized { value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), fields: Arbitrary { offsets: [Size(0 bytes)], memory_index: [0] }, largest_niche: Some(Niche { offset: Size(0 bytes), value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes) } } }, CPlace { inner: Var(_7, var1), layout: TyAndLayout { ty: &mut {coroutine@/tmp/icemaker_global_tempdir.P7Hp73Jhct5x/rustc_testrunner_tmpdir_reporting.djVsk4t1fURb/mvce.rs:24:29: 24:31}, layout: Layout { size: Size(8 bytes), align: AbiAndPrefAlign { abi: Align(8 bytes), pref: Align(8 bytes) }, abi: Scalar(Initialized { value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), fields: Primitive, largest_niche: Some(Niche { offset: Size(0 bytes), value: Pointer(AddressSpace(0)), valid_range: 1..=18446744073709551615 }), variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes) } } }] function u0:12() system_v { ss0 = explicit_slot 16 ss1 = explicit_slot 16 ss2 = explicit_slot 16 sig0 = (i64) system_v sig1 = (i64) -> i64 system_v sig2 = (i64) -> i8 system_v fn0 = colocated u0:13 sig0 fn1 = colocated u0:6 sig1 fn2 = colocated u0:14 sig2 block0: nop jump block1 block1: nop v0 = iconst.i64 8 call fn0(v0) ; v0 = 8 jump block2 block2: nop v1 = iconst.i8 0 stack_store v1, ss0 ; v1 = 0 v2 = stack_addr.i64 ss0 v3 = call fn1(v2) stack_store v3, ss2 jump block3 block3: nop } left: Coroutine(DefId(0:19 ~ mvce[cafd]::{impl#0}::impl_function::{closure#1}), [i32, i32, (), (), (), CoroutineWitness(DefId(0:19 ~ mvce[cafd]::{impl#0}::impl_function::{closure#1}), [i32, i32]), ()], Movable) right: Coroutine(DefId(0:19 ~ mvce[cafd]::{impl#0}::impl_function::{closure#1}), [T1/#0, T2/#1, (), (), (), CoroutineWitness(DefId(0:19 ~ mvce[cafd]::{impl#0}::impl_function::{closure#1}), [T1/#0, T2/#1]), ()], Movable) stack backtrace: 0: 0x7f1bc038aafc - std::backtrace_rs::backtrace::libunwind::trace::hdd99412eaf13ca97 at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5 1: 0x7f1bc038aafc - std::backtrace_rs::backtrace::trace_unsynchronized::h1f1df4c313dbe1c3 at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f1bc038aafc - std::sys_common::backtrace::_print_fmt::h1ad713b770c28acc at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/std/src/sys_common/backtrace.rs:68:5 3: 0x7f1bc038aafc - ::fmt::h0dda65360afed24d at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/std/src/sys_common/backtrace.rs:44:22 4: 0x7f1bc03ddc80 - core::fmt::rt::Argument::fmt::h566cd6272da8873e at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/core/src/fmt/rt.rs:142:9 5: 0x7f1bc03ddc80 - core::fmt::write::h2788119addd26744 at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/core/src/fmt/mod.rs:1120:17 6: 0x7f1bc037e9af - std::io::Write::write_fmt::h8d10386af4d8151e at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/std/src/io/mod.rs:1762:15 7: 0x7f1bc038a8e4 - std::sys_common::backtrace::_print::h1feaac9889d696b8 at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/std/src/sys_common/backtrace.rs:47:5 8: 0x7f1bc038a8e4 - std::sys_common::backtrace::print::h7dc96e4a48e84da8 at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/std/src/sys_common/backtrace.rs:34:9 9: 0x7f1bc038d577 - std::panicking::default_hook::{{closure}}::h1d54e187062bebc3 10: 0x7f1bc038d2df - std::panicking::default_hook::h05d19a5a0b9d9910 at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/std/src/panicking.rs:292:9 11: 0x7f1bc30f37e0 - std[6f3898e8c87bd7ff]::panicking::update_hook::>::{closure#0} 12: 0x7f1bc038dcb8 - as core::ops::function::Fn>::call::h8185fc714826c3ad at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/alloc/src/boxed.rs:2021:9 13: 0x7f1bc038dcb8 - std::panicking::rust_panic_with_hook::h6acfd9a9aa64c3be at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/std/src/panicking.rs:783:13 14: 0x7f1bc038da0e - std::panicking::begin_panic_handler::{{closure}}::ha6f952772803f55f at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/std/src/panicking.rs:657:13 15: 0x7f1bc038afc6 - std::sys_common::backtrace::__rust_end_short_backtrace::hc30e18b3c4279ee2 at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/std/src/sys_common/backtrace.rs:171:18 16: 0x7f1bc038d772 - rust_begin_unwind at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/std/src/panicking.rs:645:5 17: 0x7f1bc03da365 - core::panicking::panic_fmt::h4196180d8f686081 at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/core/src/panicking.rs:72:14 18: 0x7f1bc03da9c1 - core::panicking::assert_failed_inner::h4cbe769fe7511016 19: 0x7f1bb69b647f - core[2feb293b23a7df61]::panicking::assert_failed:: 20: 0x7f1bb6a4cac2 - rustc_codegen_cranelift[7be5e50b9264a0ba]::value_and_place::assert_assignable 21: 0x7f1bb6a4bf38 - rustc_codegen_cranelift[7be5e50b9264a0ba]::value_and_place::assert_assignable 22: 0x7f1bb69f202b - rustc_codegen_cranelift[7be5e50b9264a0ba]::abi::pass_mode::adjust_arg_for_abi 23: 0x7f1bb69e7453 - , core[2feb293b23a7df61]::option::IntoIter>, core[2feb293b23a7df61]::iter::adapters::flatten::FlatMap>>, smallvec[de4025a24e926840]::IntoIter<[cranelift_codegen[10832528869623d1]::ir::entities::Value; 2usize]>, rustc_codegen_cranelift[7be5e50b9264a0ba]::abi::codegen_terminator_call::{closure#4}::{closure#0}>> as core[2feb293b23a7df61]::iter::traits::iterator::Iterator>::next 24: 0x7f1bb69f95cf - rustc_codegen_cranelift[7be5e50b9264a0ba]::abi::codegen_terminator_call 25: 0x7f1bb6a01f4d - rustc_codegen_cranelift[7be5e50b9264a0ba]::base::codegen_fn_body 26: 0x7f1bb6a17774 - rustc_codegen_cranelift[7be5e50b9264a0ba]::driver::aot::module_codegen::{closure#0} 27: 0x7f1bb6a14a85 - rustc_codegen_cranelift[7be5e50b9264a0ba]::driver::aot::module_codegen 28: 0x7f1bb6a2108c - rustc_codegen_cranelift[7be5e50b9264a0ba]::driver::aot::run_aot 29: 0x7f1bb6a4d3da - ::codegen_crate 30: 0x7f1bc4d4ed57 - rustc_interface[3b5e8f3f619d352f]::passes::start_codegen 31: 0x7f1bc4d4e729 - ::ongoing_codegen 32: 0x7f1bc5415947 - rustc_interface[3b5e8f3f619d352f]::interface::run_compiler::, rustc_driver_impl[83f0b3fb8e850017]::run_compiler::{closure#0}>::{closure#0} 33: 0x7f1bc53a6114 - std[6f3898e8c87bd7ff]::sys_common::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[83f0b3fb8e850017]::run_compiler::{closure#0}>::{closure#0}, core[2feb293b23a7df61]::result::Result<(), rustc_span[4c1ba234228835d]::ErrorGuaranteed>>::{closure#0}, core[2feb293b23a7df61]::result::Result<(), rustc_span[4c1ba234228835d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[2feb293b23a7df61]::result::Result<(), rustc_span[4c1ba234228835d]::ErrorGuaranteed>> 34: 0x7f1bc53a5f3d - <::spawn_unchecked_, rustc_driver_impl[83f0b3fb8e850017]::run_compiler::{closure#0}>::{closure#0}, core[2feb293b23a7df61]::result::Result<(), rustc_span[4c1ba234228835d]::ErrorGuaranteed>>::{closure#0}, core[2feb293b23a7df61]::result::Result<(), rustc_span[4c1ba234228835d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[2feb293b23a7df61]::result::Result<(), rustc_span[4c1ba234228835d]::ErrorGuaranteed>>::{closure#1} as core[2feb293b23a7df61]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 35: 0x7f1bc0397b75 - as core::ops::function::FnOnce>::call_once::hb2144691085ad32b at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/alloc/src/boxed.rs:2007:9 36: 0x7f1bc0397b75 - as core::ops::function::FnOnce>::call_once::hf94bff3cb4476bdb at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/alloc/src/boxed.rs:2007:9 37: 0x7f1bc0397b75 - std::sys::unix::thread::Thread::new::thread_start::hde682e6c21a35c03 at /rustc/739d55682659e4a81db6fd83f52b2491505030ea/library/std/src/sys/unix/thread.rs:108:17 38: 0x7f1bc01539eb - 39: 0x7f1bc01d77cc - 40: 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: rustc 1.76.0-nightly (739d55682 2023-11-22) running on x86_64-unknown-linux-gnu note: compiler flags: -Z polymorphize=on -Z codegen-backend=cranelift -Z dump-mir-dir=dir query stack during panic: end of query stack warning: 1 warning emitted ```

bjorn3 commented 7 months ago

Reduced to

#![feature(coroutines, coroutine_trait)]

use std::ops::Coroutine;
use std::pin::Pin;

fn main() {
    run_coroutine::<i32>();
}

fn run_coroutine<T>() {
    let mut coroutine = || {
        yield;
        return;
    };
    Pin::new(&mut coroutine).resume(());
}
bjorn3 commented 7 months ago

What is happening here is that coroutine has a type with the type parameter T substituted with i32, while Coroutine::resume for it expects the self parameter to have T be polymorphized, thus causing an assignment of a coroutine value with T=i32 to a place with T=T/#0 (a type parameter). The assert_assignable check rejects this as it didn't recurse into Coroutine and CoroutineWitness to hit the code path where assigning any type to a type parameter (and vice versa) is allowed when polymorphization is enabled.