Skip to content

Try out local var id in thir mirroring #98130

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
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
29 changes: 16 additions & 13 deletions compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//!
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/thir.html

use hir::HirId;
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
use rustc_hir as hir;
use rustc_hir::def::CtorKind;
Expand Down Expand Up @@ -90,10 +91,16 @@ macro_rules! thir_with_elements {
}
}

#[derive(HashStable, Debug, Clone)]
pub enum LocalVarInfo {
Hir(HirId),
}

thir_with_elements! {
arms: ArmId => Arm<'tcx>,
exprs: ExprId => Expr<'tcx>,
stmts: StmtId => Stmt<'tcx>,
local_vars: LocalVarId => LocalVarInfo,
}

#[derive(Copy, Clone, Debug, HashStable)]
Expand Down Expand Up @@ -191,19 +198,15 @@ 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);
newtype_index! {
/// A THIR-specific index into a registry of variables,
/// either directly derived from HIR IDs of local variables or
/// variables generated while building THIR
#[derive(HashStable)]
pub struct LocalVarId {
DEBUG_FORMAT = "LocalVarId({})"
}
}

/// A THIR expression.
#[derive(Clone, Debug, HashStable)]
Expand Down
46 changes: 33 additions & 13 deletions compiler/rustc_mir_build/src/build/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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, LocalDefId};
use rustc_hir::HirId;
use rustc_middle::hir::place::Projection as HirProjection;
use rustc_middle::hir::place::ProjectionKind as HirProjectionKind;
use rustc_middle::middle::region;
Expand All @@ -15,7 +16,7 @@ use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt, Variance};
use rustc_span::Span;
use rustc_target::abi::VariantIdx;

use rustc_index::vec::Idx;
use rustc_index::vec::{Idx, IndexVec};

use std::iter;

Expand Down Expand Up @@ -150,12 +151,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: LocalVarId,
var_hir_id: HirId,
root_var_idx: usize,
) -> usize {
let mut res = 0;
for (var_id, capture_list) in closure_min_captures {
if *var_id == var_hir_id.0 {
if *var_id == var_hir_id {
res += root_var_idx;
break;
} else {
Expand All @@ -175,12 +176,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: LocalVarId,
var_hir_id: HirId,
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.0)?;
let root_variable_min_captures = closure_min_captures.get(&var_hir_id)?;

let hir_projections = convert_to_hir_projections_and_truncate_for_capture(projections);

Expand All @@ -205,6 +206,7 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
from_builder: PlaceBuilder<'tcx>,
tcx: TyCtxt<'tcx>,
typeck_results: &'a ty::TypeckResults<'tcx>,
local_var_defs: &IndexVec<LocalVarId, LocalVarInfo>,
) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> {
match from_builder.base {
PlaceBase::Local(_) => Ok(from_builder),
Expand All @@ -216,6 +218,9 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
}
ty::ClosureKind::FnOnce => {}
}
let var_hir_id = match &local_var_defs[var_hir_id] {
LocalVarInfo::Hir(id) => *id,
};

let Some((capture_index, capture)) =
find_capture_matching_projections(
Expand Down Expand Up @@ -319,20 +324,26 @@ impl<'tcx> PlaceBuilder<'tcx> {
self,
tcx: TyCtxt<'tcx>,
typeck_results: &'a ty::TypeckResults<'tcx>,
local_var_defs: &IndexVec<LocalVarId, LocalVarInfo>,
) -> Place<'tcx> {
if let PlaceBase::Local(local) = self.base {
Place { local, projection: tcx.intern_place_elems(&self.projection) }
} else {
self.expect_upvars_resolved(tcx, typeck_results).into_place(tcx, typeck_results)
self.expect_upvars_resolved(tcx, typeck_results, local_var_defs).into_place(
tcx,
typeck_results,
local_var_defs,
)
}
}

fn expect_upvars_resolved<'a>(
self,
tcx: TyCtxt<'tcx>,
typeck_results: &'a ty::TypeckResults<'tcx>,
local_var_defs: &IndexVec<LocalVarId, LocalVarInfo>,
) -> PlaceBuilder<'tcx> {
to_upvars_resolved_place_builder(self, tcx, typeck_results).unwrap()
to_upvars_resolved_place_builder(self, tcx, typeck_results, local_var_defs).unwrap()
}

/// Attempts to resolve the `PlaceBuilder`.
Expand All @@ -350,8 +361,9 @@ impl<'tcx> PlaceBuilder<'tcx> {
self,
tcx: TyCtxt<'tcx>,
typeck_results: &'a ty::TypeckResults<'tcx>,
local_var_defs: &IndexVec<LocalVarId, LocalVarInfo>,
) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> {
to_upvars_resolved_place_builder(self, tcx, typeck_results)
to_upvars_resolved_place_builder(self, tcx, typeck_results, local_var_defs)
}

pub(crate) fn base(&self) -> PlaceBase {
Expand Down Expand Up @@ -411,7 +423,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
expr: &Expr<'tcx>,
) -> BlockAnd<Place<'tcx>> {
let place_builder = unpack!(block = self.as_place_builder(block, expr));
block.and(place_builder.into_place(self.tcx, self.typeck_results))
block.and(place_builder.into_place(self.tcx, self.typeck_results, &self.thir.local_vars))
}

/// This is used when constructing a compound `Place`, so that we can avoid creating
Expand All @@ -435,7 +447,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
expr: &Expr<'tcx>,
) -> BlockAnd<Place<'tcx>> {
let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
block.and(place_builder.into_place(self.tcx, self.typeck_results))
block.and(place_builder.into_place(self.tcx, self.typeck_results, &self.thir.local_vars))
}

/// This is used when constructing a compound `Place`, so that we can avoid creating
Expand Down Expand Up @@ -530,7 +542,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
inferred_ty: expr.ty,
});

let place = place_builder.clone().into_place(this.tcx, this.typeck_results);
let place = place_builder.clone().into_place(
this.tcx,
this.typeck_results,
&this.thir.local_vars,
);
this.cfg.push(
block,
Statement {
Expand Down Expand Up @@ -681,7 +697,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
if is_outermost_index {
self.read_fake_borrows(block, fake_borrow_temps, source_info)
} else {
base_place = base_place.expect_upvars_resolved(self.tcx, self.typeck_results);
base_place = base_place.expect_upvars_resolved(
self.tcx,
self.typeck_results,
&self.thir.local_vars,
);
self.add_fake_borrows_of_base(
&base_place,
block,
Expand Down Expand Up @@ -713,7 +733,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block,
source_info,
len,
Rvalue::Len(slice.into_place(self.tcx, self.typeck_results)),
Rvalue::Len(slice.into_place(self.tcx, self.typeck_results, &self.thir.local_vars)),
);
// lt = idx < len
self.cfg.push_assign(
Expand Down
25 changes: 17 additions & 8 deletions compiler/rustc_mir_build/src/build/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let place_builder =
unpack!(block = this.as_place_builder(block, &this.thir[*thir_place]));

if let Ok(place_builder_resolved) =
place_builder.try_upvars_resolved(this.tcx, this.typeck_results)
{
let mir_place =
place_builder_resolved.into_place(this.tcx, this.typeck_results);
if let Ok(place_builder_resolved) = place_builder.try_upvars_resolved(
this.tcx,
this.typeck_results,
&this.thir.local_vars,
) {
let mir_place = place_builder_resolved.into_place(
this.tcx,
this.typeck_results,
&this.thir.local_vars,
);
this.cfg.push_fake_read(
block,
this.source_info(this.tcx.hir().span(*hir_id)),
Expand Down Expand Up @@ -594,8 +599,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// by the parent itself. The mutability of the current capture
// is same as that of the capture in the parent closure.
PlaceBase::Upvar { .. } => {
let enclosing_upvars_resolved =
arg_place_builder.clone().into_place(this.tcx, this.typeck_results);
let enclosing_upvars_resolved = arg_place_builder.clone().into_place(
this.tcx,
this.typeck_results,
&this.thir.local_vars,
);

match enclosing_upvars_resolved.as_ref() {
PlaceRef {
Expand Down Expand Up @@ -632,7 +640,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
};

let arg_place = arg_place_builder.into_place(this.tcx, this.typeck_results);
let arg_place =
arg_place_builder.into_place(this.tcx, this.typeck_results, &this.thir.local_vars);

this.cfg.push_assign(
block,
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_mir_build/src/build/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
None => {
let place_builder = place_builder.clone();
this.consume_by_copy_or_move(
place_builder
.field(n, *ty)
.into_place(this.tcx, this.typeck_results),
place_builder.field(n, *ty).into_place(
this.tcx,
this.typeck_results,
&this.thir.local_vars,
),
)
}
})
Expand Down
50 changes: 33 additions & 17 deletions compiler/rustc_mir_build/src/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let cause_matched_place = FakeReadCause::ForMatchedPlace(None);
let source_info = self.source_info(scrutinee_span);

if let Ok(scrutinee_builder) =
scrutinee_place_builder.clone().try_upvars_resolved(self.tcx, self.typeck_results)
{
let scrutinee_place = scrutinee_builder.into_place(self.tcx, self.typeck_results);
if let Ok(scrutinee_builder) = scrutinee_place_builder.clone().try_upvars_resolved(
self.tcx,
self.typeck_results,
&self.thir.local_vars,
) {
let scrutinee_place =
scrutinee_builder.into_place(self.tcx, self.typeck_results, &self.thir.local_vars);
self.cfg.push_fake_read(block, source_info, cause_matched_place, scrutinee_place);
}

Expand Down Expand Up @@ -345,10 +348,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let scrutinee_place: Place<'tcx>;
if let Ok(scrutinee_builder) = scrutinee_place_builder
.clone()
.try_upvars_resolved(this.tcx, this.typeck_results)
.try_upvars_resolved(this.tcx, this.typeck_results, &this.thir.local_vars)
{
scrutinee_place =
scrutinee_builder.into_place(this.tcx, this.typeck_results);
scrutinee_place = scrutinee_builder.into_place(
this.tcx,
this.typeck_results,
&this.thir.local_vars,
);
opt_scrutinee_place = Some((Some(&scrutinee_place), scrutinee_span));
}
let scope = this.declare_bindings(
Expand Down Expand Up @@ -617,10 +623,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// let (v1, v2) = foo;
// };
// ```
if let Ok(match_pair_resolved) =
initializer.clone().try_upvars_resolved(self.tcx, self.typeck_results)
{
let place = match_pair_resolved.into_place(self.tcx, self.typeck_results);
if let Ok(match_pair_resolved) = initializer.clone().try_upvars_resolved(
self.tcx,
self.typeck_results,
&self.thir.local_vars,
) {
let place = match_pair_resolved.into_place(
self.tcx,
self.typeck_results,
&self.thir.local_vars,
);
*match_place = Some(place);
}
}
Expand Down Expand Up @@ -712,6 +724,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
for_guard: ForGuard,
) {
let local_id = self.var_local_id(var, for_guard);
// @dingxiangfei2009: We need HirId here to schedule a drop
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 Down Expand Up @@ -1600,9 +1613,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

// Insert a Shallow borrow of any places that is switched on.
if let Some(fb) = fake_borrows && let Ok(match_place_resolved) =
match_place.clone().try_upvars_resolved(self.tcx, self.typeck_results)
match_place.clone().try_upvars_resolved(self.tcx, self.typeck_results, &self.thir.local_vars)
{
let resolved_place = match_place_resolved.into_place(self.tcx, self.typeck_results);
let resolved_place = match_place_resolved.into_place(self.tcx, self.typeck_results, &self.thir.local_vars);
fb.insert(resolved_place);
}

Expand Down Expand Up @@ -1788,10 +1801,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
);
let mut opt_expr_place: Option<(Option<&Place<'tcx>>, Span)> = None;
let expr_place: Place<'tcx>;
if let Ok(expr_builder) =
expr_place_builder.try_upvars_resolved(self.tcx, self.typeck_results)
{
expr_place = expr_builder.into_place(self.tcx, self.typeck_results);
if let Ok(expr_builder) = expr_place_builder.try_upvars_resolved(
self.tcx,
self.typeck_results,
&self.thir.local_vars,
) {
expr_place =
expr_builder.into_place(self.tcx, self.typeck_results, &self.thir.local_vars);
opt_expr_place = Some((Some(&expr_place), expr_span));
}
let otherwise_post_guard_block = otherwise_candidate.pre_binding_block.unwrap();
Expand Down
Loading