Skip to content

Wrap HirIds of locals into LocalVarIds for THIR nodes #97913

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

Merged
merged 1 commit into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@ pub enum StmtKind<'tcx> {
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
rustc_data_structures::static_assert_size!(Expr<'_>, 104);

#[derive(
Clone,
Debug,
Copy,
PartialEq,
Eq,
Hash,
HashStable,
TyEncodable,
TyDecodable,
TypeFoldable
)]
pub struct LocalVarId(pub hir::HirId);

/// A THIR expression.
#[derive(Clone, Debug, HashStable)]
pub struct Expr<'tcx> {
Expand Down Expand Up @@ -332,15 +346,15 @@ pub enum ExprKind<'tcx> {
},
/// A local variable.
VarRef {
id: hir::HirId,
id: LocalVarId,
},
/// Used to represent upvars mentioned in a closure/generator
UpvarRef {
/// DefId of the closure/generator
closure_def_id: DefId,

/// HirId of the root variable
var_hir_id: hir::HirId,
var_hir_id: LocalVarId,
},
/// A borrow, e.g. `&arg`.
Borrow {
Expand Down Expand Up @@ -596,7 +610,7 @@ pub enum PatKind<'tcx> {
mutability: Mutability,
name: Symbol,
mode: BindingMode,
var: hir::HirId,
var: LocalVarId,
ty: Ty<'tcx>,
subpattern: Option<Pat<'tcx>>,
/// Is this the leftmost occurrence of the binding, i.e., is `var` the
Expand Down
28 changes: 13 additions & 15 deletions compiler/rustc_mir_build/src/build/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use crate::build::expr::category::Category;
use crate::build::ForGuard::{OutsideGuard, RefWithinGuard};
use crate::build::{BlockAnd, BlockAndExtension, Builder};
use rustc_hir::def_id::DefId;
use rustc_hir::HirId;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::hir::place::Projection as HirProjection;
use rustc_middle::hir::place::ProjectionKind as HirProjectionKind;
use rustc_middle::middle::region;
Expand Down Expand Up @@ -57,7 +56,7 @@ pub(crate) enum PlaceBase {
/// figure out that it is captured until all the `Field` projections are applied.
Upvar {
/// HirId of the upvar
var_hir_id: HirId,
var_hir_id: LocalVarId,
/// DefId of the closure
closure_def_id: DefId,
/// The trait closure implements, `Fn`, `FnMut`, `FnOnce`
Expand Down Expand Up @@ -151,12 +150,12 @@ fn is_ancestor_or_same_capture(
/// `ty::MinCaptureList` of the root variable `var_hir_id`.
fn compute_capture_idx<'tcx>(
closure_min_captures: &ty::RootVariableMinCaptureList<'tcx>,
var_hir_id: HirId,
var_hir_id: LocalVarId,
root_var_idx: usize,
) -> usize {
let mut res = 0;
for (var_id, capture_list) in closure_min_captures {
if *var_id == var_hir_id {
if *var_id == var_hir_id.0 {
res += root_var_idx;
break;
} else {
Expand All @@ -176,12 +175,12 @@ fn compute_capture_idx<'tcx>(
/// Returns None, when the ancestor is not found.
fn find_capture_matching_projections<'a, 'tcx>(
typeck_results: &'a ty::TypeckResults<'tcx>,
var_hir_id: HirId,
var_hir_id: LocalVarId,
closure_def_id: DefId,
projections: &[PlaceElem<'tcx>],
) -> Option<(usize, &'a ty::CapturedPlace<'tcx>)> {
let closure_min_captures = typeck_results.closure_min_captures.get(&closure_def_id)?;
let root_variable_min_captures = closure_min_captures.get(&var_hir_id)?;
let root_variable_min_captures = closure_min_captures.get(&var_hir_id.0)?;

let hir_projections = convert_to_hir_projections_and_truncate_for_capture(projections);

Expand Down Expand Up @@ -500,8 +499,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
source_info,
),
ExprKind::UpvarRef { closure_def_id, var_hir_id } => {
let upvar_id = ty::UpvarId::new(var_hir_id, closure_def_id.expect_local());
this.lower_captured_upvar(block, upvar_id)
this.lower_captured_upvar(block, closure_def_id.expect_local(), var_hir_id)
}

ExprKind::VarRef { id } => {
Expand Down Expand Up @@ -627,11 +625,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fn lower_captured_upvar(
&mut self,
block: BasicBlock,
upvar_id: ty::UpvarId,
closure_expr_id: LocalDefId,
var_hir_id: LocalVarId,
) -> BlockAnd<PlaceBuilder<'tcx>> {
let closure_ty = self
.typeck_results
.node_type(self.tcx.hir().local_def_id_to_hir_id(upvar_id.closure_expr_id));
let closure_ty =
self.typeck_results.node_type(self.tcx.hir().local_def_id_to_hir_id(closure_expr_id));

let closure_kind = if let ty::Closure(_, closure_substs) = closure_ty.kind() {
self.infcx.closure_kind(closure_substs).unwrap()
Expand All @@ -641,8 +639,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
};

block.and(PlaceBuilder::from(PlaceBase::Upvar {
var_hir_id: upvar_id.var_path.hir_id,
closure_def_id: upvar_id.closure_expr_id.to_def_id(),
var_hir_id,
closure_def_id: closure_expr_id.to_def_id(),
closure_kind,
}))
}
Expand Down
15 changes: 7 additions & 8 deletions compiler/rustc_mir_build/src/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use rustc_data_structures::{
fx::{FxHashSet, FxIndexMap, FxIndexSet},
stack::ensure_sufficient_stack,
};
use rustc_hir::HirId;
use rustc_index::bit_set::BitSet;
use rustc_middle::middle::region;
use rustc_middle::mir::*;
Expand Down Expand Up @@ -690,7 +689,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
pub(crate) fn storage_live_binding(
&mut self,
block: BasicBlock,
var: HirId,
var: LocalVarId,
span: Span,
for_guard: ForGuard,
schedule_drop: bool,
Expand All @@ -700,20 +699,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
self.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(local_id) });
// Altough there is almost always scope for given variable in corner cases
// like #92893 we might get variable with no scope.
if let Some(region_scope) = self.region_scope_tree.var_scope(var.local_id) && schedule_drop{
if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id) && schedule_drop{
self.schedule_drop(span, region_scope, local_id, DropKind::Storage);
}
Place::from(local_id)
}

pub(crate) fn schedule_drop_for_binding(
&mut self,
var: HirId,
var: LocalVarId,
span: Span,
for_guard: ForGuard,
) {
let local_id = self.var_local_id(var, for_guard);
if let Some(region_scope) = self.region_scope_tree.var_scope(var.local_id) {
if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id) {
self.schedule_drop(span, region_scope, local_id, DropKind::Value);
}
}
Expand All @@ -730,7 +729,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Mutability,
Symbol,
BindingMode,
HirId,
LocalVarId,
Span,
Ty<'tcx>,
UserTypeProjections,
Expand Down Expand Up @@ -917,7 +916,7 @@ fn traverse_candidate<'pat, 'tcx: 'pat, C, T, I>(
struct Binding<'tcx> {
span: Span,
source: Place<'tcx>,
var_id: HirId,
var_id: LocalVarId,
binding_mode: BindingMode,
}

Expand Down Expand Up @@ -2184,7 +2183,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
mutability: Mutability,
name: Symbol,
mode: BindingMode,
var_id: HirId,
var_id: LocalVarId,
var_ty: Ty<'tcx>,
user_ty: UserTypeProjections,
has_guard: ArmHasGuard,
Expand Down
15 changes: 8 additions & 7 deletions compiler/rustc_mir_build/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ use crate::build::scope::DropKind;
use crate::thir::constant::parse_float;
use crate::thir::pattern::pat_from_hir;
use rustc_ast as ast;
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::lang_items::LangItem;
use rustc_hir::{GeneratorKind, HirIdMap, Node};
use rustc_hir::{GeneratorKind, Node};
use rustc_index::vec::{Idx, IndexVec};
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
use rustc_middle::middle::region;
use rustc_middle::mir::interpret::Allocation;
use rustc_middle::mir::interpret::{ConstValue, LitToConstError, LitToConstInput, Scalar};
use rustc_middle::mir::*;
use rustc_middle::thir::{BindingMode, Expr, ExprId, LintLevel, PatKind, Thir};
use rustc_middle::thir::{BindingMode, Expr, ExprId, LintLevel, LocalVarId, PatKind, Thir};
use rustc_middle::ty::subst::Subst;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeckResults};
use rustc_span::symbol::sym;
Expand Down Expand Up @@ -445,7 +446,7 @@ struct Builder<'a, 'tcx> {

/// Maps `HirId`s of variable bindings to the `Local`s created for them.
/// (A match binding can have two locals; the 2nd is for the arm's guard.)
var_indices: HirIdMap<LocalsForNode>,
var_indices: FxHashMap<LocalVarId, LocalsForNode>,
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
canonical_user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
upvar_mutbls: Vec<Mutability>,
Expand All @@ -455,11 +456,11 @@ struct Builder<'a, 'tcx> {
}

impl<'a, 'tcx> Builder<'a, 'tcx> {
fn is_bound_var_in_guard(&self, id: hir::HirId) -> bool {
fn is_bound_var_in_guard(&self, id: LocalVarId) -> bool {
self.guard_context.iter().any(|frame| frame.locals.iter().any(|local| local.id == id))
}

fn var_local_id(&self, id: hir::HirId, for_guard: ForGuard) -> Local {
fn var_local_id(&self, id: LocalVarId, for_guard: ForGuard) -> Local {
self.var_indices[&id].local_id(for_guard)
}
}
Expand Down Expand Up @@ -543,11 +544,11 @@ enum LocalsForNode {

#[derive(Debug)]
struct GuardFrameLocal {
id: hir::HirId,
id: LocalVarId,
}

impl GuardFrameLocal {
fn new(id: hir::HirId, _binding_mode: BindingMode) -> Self {
fn new(id: LocalVarId, _binding_mode: BindingMode) -> Self {
GuardFrameLocal { id }
}
}
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,9 +903,12 @@ impl<'tcx> Cx<'tcx> {
);

if is_upvar {
ExprKind::UpvarRef { closure_def_id: self.body_owner, var_hir_id }
ExprKind::UpvarRef {
closure_def_id: self.body_owner,
var_hir_id: LocalVarId(var_hir_id),
}
} else {
ExprKind::VarRef { id: var_hir_id }
ExprKind::VarRef { id: LocalVarId(var_hir_id) }
}
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_mir_build/src/thir/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustc_middle::mir::interpret::{get_slice_bytes, ConstValue};
use rustc_middle::mir::interpret::{ErrorHandled, LitToConstError, LitToConstInput};
use rustc_middle::mir::{self, UserTypeProjection};
use rustc_middle::mir::{BorrowKind, Field, Mutability};
use rustc_middle::thir::{Ascription, BindingMode, FieldPat, Pat, PatKind, PatRange};
use rustc_middle::thir::{Ascription, BindingMode, FieldPat, LocalVarId, Pat, PatKind, PatRange};
use rustc_middle::ty::subst::{GenericArg, SubstsRef};
use rustc_middle::ty::CanonicalUserTypeAnnotation;
use rustc_middle::ty::{self, AdtDef, ConstKind, DefIdTree, Region, Ty, TyCtxt, UserType};
Expand Down Expand Up @@ -288,7 +288,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
mutability,
mode,
name: ident.name,
var: id,
var: LocalVarId(id),
ty: var_ty,
subpattern: self.lower_opt_pattern(sub),
is_primary: id == pat.hir_id,
Expand Down Expand Up @@ -664,7 +664,7 @@ macro_rules! ClonePatternFoldableImpls {
}

ClonePatternFoldableImpls! { <'tcx>
Span, Field, Mutability, Symbol, hir::HirId, usize, ty::Const<'tcx>,
Span, Field, Mutability, Symbol, LocalVarId, usize, ty::Const<'tcx>,
Region<'tcx>, Ty<'tcx>, BindingMode, AdtDef<'tcx>,
SubstsRef<'tcx>, &'tcx GenericArg<'tcx>, UserType<'tcx>,
UserTypeProjection, CanonicalUserTypeAnnotation<'tcx>
Expand Down