From 0626f33aadb7a3a894bf4e871ac48c66faeae896 Mon Sep 17 00:00:00 2001 From: human9000 Date: Mon, 16 Mar 2026 17:48:41 +0500 Subject: [PATCH] Add reference to 'ThirBuildCx' for `PatCtxt` --- compiler/rustc_mir_build/src/thir/cx/block.rs | 3 ++- compiler/rustc_mir_build/src/thir/cx/expr.rs | 2 +- compiler/rustc_mir_build/src/thir/cx/mod.rs | 13 +++++++------ .../src/thir/pattern/const_to_pat.rs | 4 ++-- compiler/rustc_mir_build/src/thir/pattern/mod.rs | 12 ++++++++---- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_mir_build/src/thir/cx/block.rs b/compiler/rustc_mir_build/src/thir/cx/block.rs index b108dff5555f..ea27252ad6ce 100644 --- a/compiler/rustc_mir_build/src/thir/cx/block.rs +++ b/compiler/rustc_mir_build/src/thir/cx/block.rs @@ -83,6 +83,7 @@ fn mirror_stmts( } Some(_) | None => local.span, }; + let initializer = local.init.map(|init| self.mirror_expr(init)); let stmt = Stmt { kind: StmtKind::Let { remainder_scope, @@ -91,7 +92,7 @@ fn mirror_stmts( data: region::ScopeData::Node, }, pattern, - initializer: local.init.map(|init| self.mirror_expr(init)), + initializer, else_block, hir_id: local.hir_id, span, diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index c646b0fc45ea..b79bda204405 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -876,7 +876,7 @@ fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx> }; let indices = self.typeck_results.offset_of_data().get(expr.hir_id).unwrap(); - let mut expr = None::>; + let mut expr = None::>; for &(container, variant, field) in indices.iter() { let next = mk_call(&mut self.thir, container, variant, field); diff --git a/compiler/rustc_mir_build/src/thir/cx/mod.rs b/compiler/rustc_mir_build/src/thir/cx/mod.rs index 60cb509ee9dd..7f4b70b95614 100644 --- a/compiler/rustc_mir_build/src/thir/cx/mod.rs +++ b/compiler/rustc_mir_build/src/thir/cx/mod.rs @@ -13,14 +13,14 @@ use rustc_middle::ty::{self, TyCtxt}; /// Query implementation for [`TyCtxt::thir_body`]. -pub(crate) fn thir_body( - tcx: TyCtxt<'_>, +pub(crate) fn thir_body<'tcx>( + tcx: TyCtxt<'tcx>, owner_def: LocalDefId, -) -> Result<(&Steal>, ExprId), ErrorGuaranteed> { +) -> Result<(&'tcx Steal>, ExprId), ErrorGuaranteed> { debug_assert!(!tcx.is_type_const(owner_def.to_def_id()), "thir_body queried for type_const"); let body = tcx.hir_body_owned_by(owner_def); - let mut cx = ThirBuildCx::new(tcx, owner_def); + let mut cx: ThirBuildCx<'tcx> = ThirBuildCx::new(tcx, owner_def); if let Some(reported) = cx.typeck_results.tainted_by_errors { return Err(reported); } @@ -50,7 +50,7 @@ pub(crate) fn thir_body( } /// Context for lowering HIR to THIR for a single function body (or other kind of body). -struct ThirBuildCx<'tcx> { +pub(crate) struct ThirBuildCx<'tcx> { tcx: TyCtxt<'tcx>, /// The THIR data that this context is building. thir: Thir<'tcx>, @@ -118,6 +118,7 @@ fn pattern_from_hir_with_annotation( let_stmt_type: Option<&hir::Ty<'tcx>>, ) -> Box> { crate::thir::pattern::pat_from_hir( + self, self.tcx, self.typing_env, self.typeck_results, @@ -201,7 +202,7 @@ fn explicit_params( fn_sig.inputs()[index] }; - let pat = self.pattern_from_hir(param.pat); + let pat: Box> = self.pattern_from_hir(param.pat); Param { pat: Some(pat), ty, ty_span, self_kind, hir_id: Some(param.hir_id) } }) } diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs index ff9d456f7e70..ff4778dacc8d 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs @@ -25,7 +25,7 @@ PointerPattern, TypeNotPartialEq, TypeNotStructural, UnionPattern, UnsizedPattern, }; -impl<'tcx> PatCtxt<'tcx> { +impl<'tcx, 'ptcx> PatCtxt<'tcx, 'ptcx> { /// Converts a constant to a pattern (if possible). /// This means aggregate values (like structs and enums) are converted /// to a pattern that matches the value (as if you'd compared via structural equality). @@ -61,7 +61,7 @@ struct ConstToPat<'tcx> { } impl<'tcx> ConstToPat<'tcx> { - fn new(pat_ctxt: &PatCtxt<'tcx>, id: hir::HirId, span: Span, c: ty::Const<'tcx>) -> Self { + fn new(pat_ctxt: &PatCtxt<'tcx, '_>, id: hir::HirId, span: Span, c: ty::Const<'tcx>) -> Self { trace!(?pat_ctxt.typeck_results.hir_owner); ConstToPat { tcx: pat_ctxt.tcx, typing_env: pat_ctxt.typing_env, span, id, c } } diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index 67cde0e2c886..1d305803aa69 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -30,9 +30,11 @@ pub(crate) use self::check_match::check_match; use self::migration::PatMigration; use crate::errors::*; +use crate::thir::cx::ThirBuildCx; /// Context for lowering HIR patterns to THIR patterns. -struct PatCtxt<'tcx> { +struct PatCtxt<'tcx, 'ptcx> { + upper: &'ptcx mut ThirBuildCx<'tcx>, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>, typeck_results: &'tcx ty::TypeckResults<'tcx>, @@ -41,8 +43,9 @@ struct PatCtxt<'tcx> { rust_2024_migration: Option>, } -#[instrument(level = "debug", skip(tcx, typing_env, typeck_results), ret)] -pub(super) fn pat_from_hir<'tcx>( +#[instrument(level = "debug", skip(upper, tcx, typing_env, typeck_results), ret)] +pub(super) fn pat_from_hir<'tcx, 'ptcx>( + upper: &'ptcx mut ThirBuildCx<'tcx>, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>, typeck_results: &'tcx ty::TypeckResults<'tcx>, @@ -51,6 +54,7 @@ pub(super) fn pat_from_hir<'tcx>( let_stmt_type: Option<&hir::Ty<'tcx>>, ) -> Box> { let mut pcx = PatCtxt { + upper, tcx, typing_env, typeck_results, @@ -87,7 +91,7 @@ pub(super) fn pat_from_hir<'tcx>( thir_pat } -impl<'tcx> PatCtxt<'tcx> { +impl<'tcx, 'ptcx> PatCtxt<'tcx, 'ptcx> { fn lower_pattern(&mut self, pat: &'tcx hir::Pat<'tcx>) -> Box> { let adjustments: &[PatAdjustment<'tcx>] = self.typeck_results.pat_adjustments().get(pat.hir_id).map_or(&[], |v| &**v);