Skip to content

ICE: no type for local variable #141844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
matthiaskrgr opened this issue Jun 1, 2025 · 1 comment · Fixed by #141876
Closed

ICE: no type for local variable #141844

matthiaskrgr opened this issue Jun 1, 2025 · 1 comment · Fixed by #141876
Assignees
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

auto-reduced (treereduce-rust):

fn main() {
    

    // #1
    

    // #2
    

    // #3
    

    // #4
    if Some(let x == Some(_)) = x {}
}

original:

fn main() {
    let x = Some(123);

    // #1
    if let Some(_) == x {}

    // #2
    if let x = Some(_) {}

    // #3
    if let x == Some(_) {}

    // #4
    if Some(let x == Some(_)) = x {}
}

Version information

rustc 1.89.0-nightly (337c11e59 2025-06-01)
binary: rustc
commit-hash: 337c11e5932275e7d450c1f2e26f289f0ddfa717
commit-date: 2025-06-01
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Possibly related line of code:

fn tag(&self) -> String {
format!("{self:p}")
}
pub(crate) fn local_ty(&self, span: Span, nid: HirId) -> Ty<'tcx> {
self.locals.borrow().get(&nid).cloned().unwrap_or_else(|| {
span_bug!(span, "no type for local variable {}", self.tcx.hir_id_to_string(nid))
})
}
#[inline]
pub(crate) fn write_ty(&self, id: HirId, ty: Ty<'tcx>) {
debug!("write_ty({:?}, {:?}) in fcx {}", id, self.resolve_vars_if_possible(ty), self.tag());

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

Program output

error: expected expression, found `let` statement
  --> /tmp/icemaker_global_tempdir.qNli0TH4IAHO/rustc_testrunner_tmpdir_reporting.0GrbsUdTQx1S/mvce.rs:14:13
   |
14 |     if Some(let x == Some(_)) = x {}
   |             ^^^
   |
   = note: only supported directly in conditions of `if` and `while` expressions

error: expected `=`, found `==`
  --> /tmp/icemaker_global_tempdir.qNli0TH4IAHO/rustc_testrunner_tmpdir_reporting.0GrbsUdTQx1S/mvce.rs:14:19
   |
14 |     if Some(let x == Some(_)) = x {}
   |                   ^^
   |
help: consider using `=` here
   |
14 -     if Some(let x == Some(_)) = x {}
14 +     if Some(let x = Some(_)) = x {}
   |

error: in expressions, `_` can only be used on the left-hand side of an assignment
  --> /tmp/icemaker_global_tempdir.qNli0TH4IAHO/rustc_testrunner_tmpdir_reporting.0GrbsUdTQx1S/mvce.rs:14:27
   |
14 |     if Some(let x == Some(_)) = x {}
   |                           ^ `_` not allowed here

error: internal compiler error: compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs:141:13: no type for local variable HirId(DefId(0:3 ~ mvce[7859]::main).8) (pat `x`)
  --> /tmp/icemaker_global_tempdir.qNli0TH4IAHO/rustc_testrunner_tmpdir_reporting.0GrbsUdTQx1S/mvce.rs:14:33
   |
14 |     if Some(let x == Some(_)) = x {}
   |                                 ^


thread 'rustc' panicked at compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs:141:13:
Box<dyn Any>
stack backtrace:
   0:     0x706656cde993 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::hf8ee8b6b352ca661
   1:     0x706657403237 - core::fmt::write::hcd9fe40a26b64d70
   2:     0x706656cd4723 - std::io::Write::write_fmt::h5f94532440194dbd
   3:     0x706656cde7f2 - std::sys::backtrace::BacktraceLock::print::hc29614df06844315
   4:     0x706656ce23ea - std::panicking::default_hook::{{closure}}::h47671d985641cc52
   5:     0x706656ce1f6f - std::panicking::default_hook::h0bb111aee907609c
   6:     0x706655dfa3f3 - std[a1f017d000edeeff]::panicking::update_hook::<alloc[776b61ca0bea01d8]::boxed::Box<rustc_driver_impl[a8c06d39e8948878]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x706656ce2c63 - std::panicking::rust_panic_with_hook::h5930974b9fd29227
   8:     0x706655e37121 - std[a1f017d000edeeff]::panicking::begin_panic::<rustc_errors[693c8abe97730fa9]::ExplicitBug>::{closure#0}
   9:     0x706655e2ba16 - std[a1f017d000edeeff]::sys::backtrace::__rust_end_short_backtrace::<std[a1f017d000edeeff]::panicking::begin_panic<rustc_errors[693c8abe97730fa9]::ExplicitBug>::{closure#0}, !>
  10:     0x706655e2826b - std[a1f017d000edeeff]::panicking::begin_panic::<rustc_errors[693c8abe97730fa9]::ExplicitBug>
  11:     0x706655e40c11 - <rustc_errors[693c8abe97730fa9]::diagnostic::BugAbort as rustc_errors[693c8abe97730fa9]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x706656350c9c - <rustc_errors[693c8abe97730fa9]::DiagCtxtHandle>::span_bug::<rustc_span[14520476a1722fe1]::span_encoding::Span, alloc[776b61ca0bea01d8]::string::String>
  13:     0x7066563e2257 - rustc_middle[fd2e613c2dd7156e]::util::bug::opt_span_bug_fmt::<rustc_span[14520476a1722fe1]::span_encoding::Span>::{closure#0}
  14:     0x7066563c1cda - rustc_middle[fd2e613c2dd7156e]::ty::context::tls::with_opt::<rustc_middle[fd2e613c2dd7156e]::util::bug::opt_span_bug_fmt<rustc_span[14520476a1722fe1]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  15:     0x7066563c1b4b - rustc_middle[fd2e613c2dd7156e]::ty::context::tls::with_context_opt::<rustc_middle[fd2e613c2dd7156e]::ty::context::tls::with_opt<rustc_middle[fd2e613c2dd7156e]::util::bug::opt_span_bug_fmt<rustc_span[14520476a1722fe1]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  16:     0x706653f74107 - rustc_middle[fd2e613c2dd7156e]::util::bug::span_bug_fmt::<rustc_span[14520476a1722fe1]::span_encoding::Span>
  17:     0x706657daf2ab - <rustc_hir_typeck[aaf7cd0e8485b9d]::fn_ctxt::FnCtxt>::instantiate_value_path
  18:     0x706657da5da6 - <rustc_hir_typeck[aaf7cd0e8485b9d]::fn_ctxt::FnCtxt>::check_expr_path
  19:     0x706658056720 - <rustc_hir_typeck[aaf7cd0e8485b9d]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  20:     0x70665803ed25 - <rustc_hir_typeck[aaf7cd0e8485b9d]::fn_ctxt::FnCtxt>::check_decl
  21:     0x70665802d729 - <rustc_hir_typeck[aaf7cd0e8485b9d]::fn_ctxt::FnCtxt>::check_expr_block
  22:     0x7066580577b0 - <rustc_hir_typeck[aaf7cd0e8485b9d]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  23:     0x70665805a5e0 - <rustc_hir_typeck[aaf7cd0e8485b9d]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  24:     0x70665804b8e0 - <rustc_hir_typeck[aaf7cd0e8485b9d]::fn_ctxt::FnCtxt>::check_expr_if
  25:     0x70665805a58e - <rustc_hir_typeck[aaf7cd0e8485b9d]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  26:     0x70665802c99c - <rustc_hir_typeck[aaf7cd0e8485b9d]::fn_ctxt::FnCtxt>::check_expr_block
  27:     0x7066580577b0 - <rustc_hir_typeck[aaf7cd0e8485b9d]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  28:     0x7066580383e4 - rustc_hir_typeck[aaf7cd0e8485b9d]::check::check_fn
  29:     0x706657fe9e65 - rustc_hir_typeck[aaf7cd0e8485b9d]::typeck_with_inspect::{closure#0}
  30:     0x706657fe89be - rustc_query_impl[9415a893a92592b2]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9415a893a92592b2]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[fd2e613c2dd7156e]::query::erase::Erased<[u8; 8usize]>>
  31:     0x7066575cb726 - rustc_query_system[b2478213cc4322cf]::query::plumbing::try_execute_query::<rustc_query_impl[9415a893a92592b2]::DynamicConfig<rustc_data_structures[b91cd40af62c43e6]::vec_cache::VecCache<rustc_span[14520476a1722fe1]::def_id::LocalDefId, rustc_middle[fd2e613c2dd7156e]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[b2478213cc4322cf]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[9415a893a92592b2]::plumbing::QueryCtxt, false>
  32:     0x7066575cb189 - rustc_query_impl[9415a893a92592b2]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace
  33:     0x7066575ca0a4 - rustc_hir_analysis[fae52809c300b748]::check_crate
  34:     0x7066575c59b7 - rustc_interface[2840816492762d3c]::passes::run_required_analyses
  35:     0x70665817f3de - rustc_interface[2840816492762d3c]::passes::analysis
  36:     0x70665817f3b3 - rustc_query_impl[9415a893a92592b2]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9415a893a92592b2]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[fd2e613c2dd7156e]::query::erase::Erased<[u8; 0usize]>>
  37:     0x70665818228e - rustc_query_system[b2478213cc4322cf]::query::plumbing::try_execute_query::<rustc_query_impl[9415a893a92592b2]::DynamicConfig<rustc_query_system[b2478213cc4322cf]::query::caches::SingleCache<rustc_middle[fd2e613c2dd7156e]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[9415a893a92592b2]::plumbing::QueryCtxt, false>
  38:     0x706658181e76 - rustc_query_impl[9415a893a92592b2]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  39:     0x7066588fb8ff - rustc_interface[2840816492762d3c]::passes::create_and_enter_global_ctxt::<core[c4bb63f569d6be2b]::option::Option<rustc_interface[2840816492762d3c]::queries::Linker>, rustc_driver_impl[a8c06d39e8948878]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  40:     0x7066586528cc - rustc_interface[2840816492762d3c]::interface::run_compiler::<(), rustc_driver_impl[a8c06d39e8948878]::run_compiler::{closure#0}>::{closure#1}
  41:     0x7066587a1a3e - std[a1f017d000edeeff]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[2840816492762d3c]::util::run_in_thread_with_globals<rustc_interface[2840816492762d3c]::util::run_in_thread_pool_with_globals<rustc_interface[2840816492762d3c]::interface::run_compiler<(), rustc_driver_impl[a8c06d39e8948878]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  42:     0x7066587a1716 - <<std[a1f017d000edeeff]::thread::Builder>::spawn_unchecked_<rustc_interface[2840816492762d3c]::util::run_in_thread_with_globals<rustc_interface[2840816492762d3c]::util::run_in_thread_pool_with_globals<rustc_interface[2840816492762d3c]::interface::run_compiler<(), rustc_driver_impl[a8c06d39e8948878]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[c4bb63f569d6be2b]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  43:     0x7066584fa03d - std::sys::pal::unix::thread::Thread::new::thread_start::h20008e16f7cd9b41
  44:     0x7066520a57eb - <unknown>
  45:     0x70665212918c - <unknown>
  46:                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: please make sure that you have updated to the latest nightly

note: rustc 1.89.0-nightly (337c11e59 2025-06-01) running on x86_64-unknown-linux-gnu

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


@matthiaskrgr matthiaskrgr added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Jun 1, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 1, 2025
@matthiaskrgr
Copy link
Member Author

matthiaskrgr commented Jun 1, 2025

fn main {
  if Some (let x == Some0)
    = x {}
}

, since #140561

@compiler-errors compiler-errors self-assigned this Jun 2, 2025
@bors bors closed this as completed in 69ebe39 Jun 3, 2025
rust-timer added a commit that referenced this issue Jun 3, 2025
Rollup merge of #141876 - compiler-errors:missing-let-ty, r=SparrowLii

Don't declare variables in `ExprKind::Let` in invalid positions

Handle `let` expressions in invalid positions specially during resolve in order to avoid making destructuring-assignment expressions that reference (invalid) variables that have not yet been delcared yet.

See further explanation in test and comment in the source.

Fixes #141844
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants