Skip to content

ice: ProjectionKinds Deref and Field were mismatched #118144

Closed
@matthiaskrgr

Description

@matthiaskrgr
Member

auto-reduced (treereduce-rust):

//

struct V<'a>((Option<&'a mut i32>,));

fn nested(v: &mut V<'_>) {
    (|| {
        V((_o,)) = v;
        v.0 = (None,);
    })();
}

original:

// Regression test for #89606. Used to ICE.
//
// check-pass
// revisions: twenty_eighteen twenty_twentyone
// [twenty_eighteen]compile-flags: --edition 2018
// [twenty_twentyone]compile-flags: --edition 2021

struct S<'a>(Option<&'a mut i32>);

fn by_ref(s: &mut S<'_>) {
    (|| {
        let S(_o) = s;
        s.0 = None;
    })();
}

fn by_value(s: S<'_>) {
    (|| {
        let S(ref _o) = s;
        let _g = s.0;
    })();
}

struct V<'a>((Option<&'a mut i32>,));

fn nested(v: &mut V<'_>) {
    (|| {
        struct S<'a>(Option<&'a mut i32>); V((_o,)) = v;
        v.0 = (None, );
    })();
}

fn main() {
    let mut s = S(None);
    by_ref(&mut s);
    by_value(s);

    let mut v = V((None, ));
    nested(&mut v);
}

Version information

rustc 1.76.0-nightly (e24e5af78 2023-11-21)
binary: rustc
commit-hash: e24e5af787f7015914cbf316063ed5821f370b71
commit-date: 2023-11-21
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.5

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc

Program output

error[E0425]: cannot find value `_o` in this scope
 --> /tmp/icemaker_global_tempdir.4GO1XXNNDlqm/rustc_testrunner_tmpdir_reporting.Y3wqoQZ3yyr5/mvce.rs:7:12
  |
7 |         V((_o,)) = v;
  |            ^^ not found in this scope

error[E0601]: `main` function not found in crate `mvce`
  --> /tmp/icemaker_global_tempdir.4GO1XXNNDlqm/rustc_testrunner_tmpdir_reporting.Y3wqoQZ3yyr5/mvce.rs:10:2
   |
10 | }
   |  ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.4GO1XXNNDlqm/rustc_testrunner_tmpdir_reporting.Y3wqoQZ3yyr5/mvce.rs`

error[E0308]: mismatched types
 --> /tmp/icemaker_global_tempdir.4GO1XXNNDlqm/rustc_testrunner_tmpdir_reporting.Y3wqoQZ3yyr5/mvce.rs:7:9
  |
7 |         V((_o,)) = v;
  |         ^^^^^^^^   - this expression has type `&mut V<'_>`
  |         |
  |         expected `&mut V<'_>`, found `V<'_>`

error: internal compiler error: compiler/rustc_hir_typeck/src/upvar.rs:704:30: ProjectionKinds Deref and Field were mismatched: (Deref, Field(0, 0))

thread 'rustc' panicked at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/compiler/rustc_errors/src/lib.rs:1660:9:
Box<dyn Any>
stack backtrace:
   0:     0x7efd84810afc - std::backtrace_rs::backtrace::libunwind::trace::h0b040baf97a1a814
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:     0x7efd84810afc - std::backtrace_rs::backtrace::trace_unsynchronized::h3bb4f35c883ef0b7
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7efd84810afc - std::sys_common::backtrace::_print_fmt::h7bc4c6c40e2dea38
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7efd84810afc - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::ha29c192b6f9a05c7
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7efd84863c80 - core::fmt::rt::Argument::fmt::hd5d7e2c681722c4a
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/core/src/fmt/rt.rs:142:9
   5:     0x7efd84863c80 - core::fmt::write::h44f28bd6729c28a7
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/core/src/fmt/mod.rs:1120:17
   6:     0x7efd8480491f - std::io::Write::write_fmt::h34e80b21f77f27fe
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/std/src/io/mod.rs:1762:15
   7:     0x7efd848108e4 - std::sys_common::backtrace::_print::hf6fce87ec4803f0c
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7efd848108e4 - std::sys_common::backtrace::print::h2c7cd590dc94a621
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7efd84813577 - std::panicking::default_hook::{{closure}}::ha2265235df34f52e
  10:     0x7efd848132df - std::panicking::default_hook::ha134722f286c6b3a
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/std/src/panicking.rs:292:9
  11:     0x7efd814f42b0 - std[9e66bc0d43e7428]::panicking::update_hook::<alloc[6053f7fa92048905]::boxed::Box<rustc_driver_impl[6d46853ed3c470a0]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7efd84813cb8 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h3506115e7630eb0e
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/alloc/src/boxed.rs:2021:9
  13:     0x7efd84813cb8 - std::panicking::rust_panic_with_hook::h7694bf37e5dc3efe
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/std/src/panicking.rs:783:13
  14:     0x7efd8193b174 - std[9e66bc0d43e7428]::panicking::begin_panic::<rustc_errors[561fbf43990526ac]::ExplicitBug>::{closure#0}
  15:     0x7efd819346d6 - std[9e66bc0d43e7428]::sys_common::backtrace::__rust_end_short_backtrace::<std[9e66bc0d43e7428]::panicking::begin_panic<rustc_errors[561fbf43990526ac]::ExplicitBug>::{closure#0}, !>
  16:     0x7efd8192edc6 - std[9e66bc0d43e7428]::panicking::begin_panic::<rustc_errors[561fbf43990526ac]::ExplicitBug>
  17:     0x7efd81914f14 - <rustc_errors[561fbf43990526ac]::HandlerInner>::bug::<alloc[6053f7fa92048905]::string::String>
  18:     0x7efd81914c1a - <rustc_errors[561fbf43990526ac]::Handler>::bug::<alloc[6053f7fa92048905]::string::String>
  19:     0x7efd819b692d - rustc_middle[999cff9f20269db6]::util::bug::opt_span_bug_fmt::<rustc_span[433598ff900853dd]::span_encoding::Span>::{closure#0}
  20:     0x7efd8199dc8a - rustc_middle[999cff9f20269db6]::ty::context::tls::with_opt::<rustc_middle[999cff9f20269db6]::util::bug::opt_span_bug_fmt<rustc_span[433598ff900853dd]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  21:     0x7efd8199db28 - rustc_middle[999cff9f20269db6]::ty::context::tls::with_context_opt::<rustc_middle[999cff9f20269db6]::ty::context::tls::with_opt<rustc_middle[999cff9f20269db6]::util::bug::opt_span_bug_fmt<rustc_span[433598ff900853dd]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  22:     0x7efd7f93fbf0 - rustc_middle[999cff9f20269db6]::util::bug::bug_fmt
  23:     0x7efd83e0551c - <[rustc_middle[999cff9f20269db6]::ty::closure::CapturedPlace]>::sort_by::<<rustc_hir_typeck[3784c5e6fa919bfa]::fn_ctxt::FnCtxt>::compute_min_captures::{closure#1}>::{closure#0}.cold.0
  24:     0x7efd8345f5eb - core[4409ec5cd92597ed]::slice::sort::insertion_sort_shift_left::<rustc_middle[999cff9f20269db6]::ty::closure::CapturedPlace, <[rustc_middle[999cff9f20269db6]::ty::closure::CapturedPlace]>::sort_by<<rustc_hir_typeck[3784c5e6fa919bfa]::fn_ctxt::FnCtxt>::compute_min_captures::{closure#1}>::{closure#0}>
  25:     0x7efd8345df5e - <rustc_hir_typeck[3784c5e6fa919bfa]::fn_ctxt::FnCtxt>::compute_min_captures
  26:     0x7efd831e8a25 - <rustc_hir_typeck[3784c5e6fa919bfa]::fn_ctxt::FnCtxt>::analyze_closure
  27:     0x7efd7f664942 - <rustc_hir_typeck[3784c5e6fa919bfa]::upvar::InferBorrowKindVisitor as rustc_hir[b19a94a0ae9cf398]::intravisit::Visitor>::visit_expr
  28:     0x7efd7f66474a - <rustc_hir_typeck[3784c5e6fa919bfa]::upvar::InferBorrowKindVisitor as rustc_hir[b19a94a0ae9cf398]::intravisit::Visitor>::visit_expr
  29:     0x7efd82a91d04 - <rustc_hir_typeck[3784c5e6fa919bfa]::upvar::InferBorrowKindVisitor as rustc_hir[b19a94a0ae9cf398]::intravisit::Visitor>::visit_block
  30:     0x7efd82af877e - rustc_hir_typeck[3784c5e6fa919bfa]::typeck
  31:     0x7efd82af7813 - rustc_query_impl[e3648600a664160f]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[e3648600a664160f]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[999cff9f20269db6]::query::erase::Erased<[u8; 8usize]>>
  32:     0x7efd82994cc3 - rustc_query_system[68ab24f02628e1a4]::query::plumbing::try_execute_query::<rustc_query_impl[e3648600a664160f]::DynamicConfig<rustc_query_system[68ab24f02628e1a4]::query::caches::VecCache<rustc_span[433598ff900853dd]::def_id::LocalDefId, rustc_middle[999cff9f20269db6]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[e3648600a664160f]::plumbing::QueryCtxt, false>
  33:     0x7efd82994950 - rustc_query_impl[e3648600a664160f]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace
  34:     0x7efd82bba240 - rustc_hir_analysis[ac2ddafd5ff93a2f]::check_crate
  35:     0x7efd82b4e4d7 - rustc_interface[79d196cce7f623b3]::passes::analysis
  36:     0x7efd82b4e11d - rustc_query_impl[e3648600a664160f]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[e3648600a664160f]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[999cff9f20269db6]::query::erase::Erased<[u8; 1usize]>>
  37:     0x7efd8378aac0 - rustc_query_system[68ab24f02628e1a4]::query::plumbing::try_execute_query::<rustc_query_impl[e3648600a664160f]::DynamicConfig<rustc_query_system[68ab24f02628e1a4]::query::caches::SingleCache<rustc_middle[999cff9f20269db6]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[e3648600a664160f]::plumbing::QueryCtxt, false>
  38:     0x7efd8378a8c7 - rustc_query_impl[e3648600a664160f]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  39:     0x7efd83813678 - rustc_interface[79d196cce7f623b3]::interface::run_compiler::<core[4409ec5cd92597ed]::result::Result<(), rustc_span[433598ff900853dd]::ErrorGuaranteed>, rustc_driver_impl[6d46853ed3c470a0]::run_compiler::{closure#0}>::{closure#0}
  40:     0x7efd837a3054 - std[9e66bc0d43e7428]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[79d196cce7f623b3]::util::run_in_thread_with_globals<rustc_interface[79d196cce7f623b3]::util::run_in_thread_pool_with_globals<rustc_interface[79d196cce7f623b3]::interface::run_compiler<core[4409ec5cd92597ed]::result::Result<(), rustc_span[433598ff900853dd]::ErrorGuaranteed>, rustc_driver_impl[6d46853ed3c470a0]::run_compiler::{closure#0}>::{closure#0}, core[4409ec5cd92597ed]::result::Result<(), rustc_span[433598ff900853dd]::ErrorGuaranteed>>::{closure#0}, core[4409ec5cd92597ed]::result::Result<(), rustc_span[433598ff900853dd]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[4409ec5cd92597ed]::result::Result<(), rustc_span[433598ff900853dd]::ErrorGuaranteed>>
  41:     0x7efd837a2e7d - <<std[9e66bc0d43e7428]::thread::Builder>::spawn_unchecked_<rustc_interface[79d196cce7f623b3]::util::run_in_thread_with_globals<rustc_interface[79d196cce7f623b3]::util::run_in_thread_pool_with_globals<rustc_interface[79d196cce7f623b3]::interface::run_compiler<core[4409ec5cd92597ed]::result::Result<(), rustc_span[433598ff900853dd]::ErrorGuaranteed>, rustc_driver_impl[6d46853ed3c470a0]::run_compiler::{closure#0}>::{closure#0}, core[4409ec5cd92597ed]::result::Result<(), rustc_span[433598ff900853dd]::ErrorGuaranteed>>::{closure#0}, core[4409ec5cd92597ed]::result::Result<(), rustc_span[433598ff900853dd]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[4409ec5cd92597ed]::result::Result<(), rustc_span[433598ff900853dd]::ErrorGuaranteed>>::{closure#1} as core[4409ec5cd92597ed]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  42:     0x7efd8481db75 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::he2fe898dabd7bf63
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/alloc/src/boxed.rs:2007:9
  43:     0x7efd8481db75 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h2e31e36ca472739b
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/alloc/src/boxed.rs:2007:9
  44:     0x7efd8481db75 - std::sys::unix::thread::Thread::new::thread_start::h72b7daa856b881db
                               at /rustc/e24e5af787f7015914cbf316063ed5821f370b71/library/std/src/sys/unix/thread.rs:108:17
  45:     0x7efd7e6aa9eb - <unknown>
  46:     0x7efd7e72e7cc - <unknown>
  47:                0x0 - <unknown>

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 (e24e5af78 2023-11-21) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [typeck] type-checking `nested`
#1 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 4 previous errors

Some errors have detailed explanations: E0308, E0425, E0601.
For more information about an error, try `rustc --explain E0308`.

Activity

added
I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
C-bugCategory: This is a bug.
on Nov 21, 2023
added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Nov 21, 2023
matthiaskrgr

matthiaskrgr commented on Nov 21, 2023

@matthiaskrgr
MemberAuthor

smaller

struct V(&mut i32);

fn nested(v: &V) {
    || {
        V(_somename) = v;
        v.0 = 0;
    };
}
matthiaskrgr

matthiaskrgr commented on Nov 21, 2023

@matthiaskrgr
MemberAuthor

Regression in nightly-2022-11-06

commit[0] 2022-11-04: Auto merge of #103978 - matthiaskrgr:rollup-iym9kmg, r=matthiaskrgr
commit[1] 2022-11-04: Auto merge of #103298 - ferrocene:pa-compile-flags-last, r=jyn514
commit[2] 2022-11-05: Auto merge of #103991 - matthiaskrgr:rollup-tj53nte, r=matthiaskrgr
commit[3] 2022-11-05: Auto merge of #103992 - weihanglo:update-cargo, r=weihanglo
commit[4] 2022-11-05: Auto merge of #103998 - Dylan-DPC:rollup-2nbmtc9, r=Dylan-DPC
commit[5] 2022-11-05: Auto merge of #103691 - michaelwoerister:consistent-slice-and-str-cpp-like-debuginfo-names, r=wesleywiser
commit[6] 2022-11-05: Auto merge of #103831 - chenyukang:yukang/fix-103751-ice, r=nagisa
commit[7] 2022-11-05: Auto merge of #104017 - matthiaskrgr:rollup-k8i0j9m, r=matthiaskrgr
commit[8] 2022-11-05: Auto merge of #102458 - JohnTitor:stabilize-instruction-set, r=oli-obk
ERROR: no CI builds available between 0950848 and 1286ee2 within last 167 days

added
A-closuresArea: Closures (`|…| { … }`)
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Nov 26, 2023
gurry

gurry commented on Nov 27, 2023

@gurry
Contributor

@rustbot claim

gurry

gurry commented on Dec 3, 2023

@gurry
Contributor

Regression in nightly-2022-11-06

commit[0] 2022-11-04: Auto merge of #103978 - matthiaskrgr:rollup-iym9kmg, r=matthiaskrgr commit[1] 2022-11-04: Auto merge of #103298 - ferrocene:pa-compile-flags-last, r=jyn514 commit[2] 2022-11-05: Auto merge of #103991 - matthiaskrgr:rollup-tj53nte, r=matthiaskrgr commit[3] 2022-11-05: Auto merge of #103992 - weihanglo:update-cargo, r=weihanglo commit[4] 2022-11-05: Auto merge of #103998 - Dylan-DPC:rollup-2nbmtc9, r=Dylan-DPC commit[5] 2022-11-05: Auto merge of #103691 - michaelwoerister:consistent-slice-and-str-cpp-like-debuginfo-names, r=wesleywiser commit[6] 2022-11-05: Auto merge of #103831 - chenyukang:yukang/fix-103751-ice, r=nagisa commit[7] 2022-11-05: Auto merge of #104017 - matthiaskrgr:rollup-k8i0j9m, r=matthiaskrgr commit[8] 2022-11-05: Auto merge of #102458 - JohnTitor:stabilize-instruction-set, r=oli-obk ERROR: no CI builds available between 0950848 and 1286ee2 within last 167 days

The above bisection may be wrong. I got this instead:

Regression in nightly-2021-09-26

found 9 bors merge commits in the specified range
commit[0] 2021-09-24: Auto merge of #88872 - petrochenkov:getmod2, r=cjgillot
commit[1] 2021-09-24: Auto merge of #86246 - FabianWolff:issue-83471, r=estebank
commit[2] 2021-09-25: Auto merge of #89230 - workingjubilee:rollup-1swktdq, r=workingjubilee
commit[3] 2021-09-25: Auto merge of #87220 - petrochenkov:derivecfglimit2, r=Aaron1011
commit[4] 2021-09-25: Auto merge of #89237 - BoxyUwU:trackersMcCaller, r=eddyb
commit[5] 2021-09-25: Auto merge of #89030 - nbdd0121:box2, r=jonas-schievink
commit[6] 2021-09-25: Auto merge of #88243 - nikic:newpm-2, r=nagisa
commit[7] 2021-09-25: Auto merge of #87584 - adamgemmell:dev/asm-tests, r=Amanieu
commit[8] 2021-09-25: Auto merge of #88343 - steffahn:fix_code_spacing, r=jyn514
ERROR: no CI builds available between 7342213 and addb4da within last 167 days

The issue seems to be present since PR #89208 which is part of the rollup #89230

gurry

gurry commented on Dec 3, 2023

@gurry
Contributor

The ICE originates at the below code in the function rustc_hir_typeck::upvar::compute_min_captures():

// We should have either a pair of `Deref`s or a pair of `Field`s.
// Anything else is a bug.
(
l @ (ProjectionKind::Deref | ProjectionKind::Field(..)),
r @ (ProjectionKind::Deref | ProjectionKind::Field(..)),
) => bug!(
"ProjectionKinds Deref and Field were mismatched: ({:?}, {:?})",
l,
r
),

This code expects that all the captures of a given variable should have projection sequences that adhere to the following rules:

  1. They contain only Deref, OpaqueCast or Field projection kinds (Index and Sublice are not allowed)
  2. They all should have the exact same sequence e.g. Capture1: Deref.Field.Field and Capture2: Deref.Field.Field are okay but Capture1: Deref.Field.Deref and Capture2: Deref.Field.Field are not acceptable.

What causes the below snippet to ICE:

struct V(&mut i32);

fn nested(v: &V) {
    || {
        V(_somename) = v;
        v.0 = 0;
    };
}

is that compute_min_captures receives the following projection sequences for the arg v:

Capture1 : Field
Capture2 : Deref.Field

where Capture1 comes from the statement V(_somename) = v; and Capture2 comes from the statement v.0 = 0;

As these captures violate rule 2 above, an ICE ensues.

The code that is ICEing was introduced PR #89208 and has been ICEing ever since.

gurry

gurry commented on Dec 3, 2023

@gurry
Contributor

As for the fix, I feel there's little we can do to make these two captures:

Capture1 : Field
Capture2 : Deref.Field

align with each other as per rule 2. A statement like this V(_somename) = v;, which is destructuring v into an previously declared variable _someone, always results in a single Field projection whilev.0 = 0; always results in a Deref.Field projection sequence.

(Worth noting that if we change V(_somename) = v; to let V(_somename) = v;, i.e. we declare the variable while destructuring, then the ICE goes away. This is because let V(_somename) = v; results in a Deref.Field projection sequence which does not violate rule 2.)

A simple fix in my view would be to remove rule 2 and allow projection sequences like Field and Deref.Field to co-exist. We will just have figure out how to sort them w.r.t. each other. Wonder if just removing all Derefs from the projection sequences before comparing them could work. @wesleywiser might be able to shed some light.

added a commit that references this issue on Dec 18, 2023

Auto merge of rust-lang#118584 - gurry:118144-projection-kind-mismatc…

a7690a3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-closuresArea: Closures (`|…| { … }`)C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️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

    Participants

    @matthiaskrgr@gurry@fmease@rustbot

    Issue actions

      ice: `ProjectionKinds Deref and Field were mismatched` · Issue #118144 · rust-lang/rust