Rollup merge of #134491 - compiler-errors:dtor-tweaks, r=lqd

Some destructor/drop related tweaks

Two random tweaks I got from investigating some stuff around drops in edition 2024:
1. Use the `TypingEnv` of the mir builder, rather than making it over again.
2. Rename the `id` field from `Scope` to `local_id`, to reflect that it's a local id, and remove the `item_local_id()` accessor which just returned the id field.
This commit is contained in:
许杰友 Jieyou Xu (Joe)
2024-12-19 16:48:11 +08:00
committed by GitHub
8 changed files with 46 additions and 53 deletions
+11 -11
View File
@@ -129,7 +129,7 @@ fn resolve_block<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, blk: &'tcx h
let mut prev_cx = visitor.cx;
visitor.enter_scope(Scope {
id: blk.hir_id.local_id,
local_id: blk.hir_id.local_id,
data: ScopeData::Remainder(FirstStatementIndex::new(i)),
});
visitor.cx.var_parent = visitor.cx.parent;
@@ -154,7 +154,7 @@ fn resolve_block<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, blk: &'tcx h
// the first such subscope, which has the block itself as a
// parent.
visitor.enter_scope(Scope {
id: blk.hir_id.local_id,
local_id: blk.hir_id.local_id,
data: ScopeData::Remainder(FirstStatementIndex::new(i)),
});
visitor.cx.var_parent = visitor.cx.parent;
@@ -184,7 +184,7 @@ fn resolve_block<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, blk: &'tcx h
visitor
.scope_tree
.backwards_incompatible_scope
.insert(local_id, Scope { id: local_id, data: ScopeData::Node });
.insert(local_id, Scope { local_id, data: ScopeData::Node });
}
visitor.visit_expr(tail_expr);
}
@@ -221,7 +221,7 @@ fn has_let_expr(expr: &Expr<'_>) -> bool {
}
fn resolve_pat<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, pat: &'tcx hir::Pat<'tcx>) {
visitor.record_child_scope(Scope { id: pat.hir_id.local_id, data: ScopeData::Node });
visitor.record_child_scope(Scope { local_id: pat.hir_id.local_id, data: ScopeData::Node });
// If this is a binding then record the lifetime of that binding.
if let PatKind::Binding(..) = pat.kind {
@@ -485,7 +485,7 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
} else {
ScopeData::IfThen
};
visitor.enter_scope(Scope { id: then.hir_id.local_id, data });
visitor.enter_scope(Scope { local_id: then.hir_id.local_id, data });
visitor.cx.var_parent = visitor.cx.parent;
visitor.visit_expr(cond);
visitor.visit_expr(then);
@@ -500,7 +500,7 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
} else {
ScopeData::IfThen
};
visitor.enter_scope(Scope { id: then.hir_id.local_id, data });
visitor.enter_scope(Scope { local_id: then.hir_id.local_id, data });
visitor.cx.var_parent = visitor.cx.parent;
visitor.visit_expr(cond);
visitor.visit_expr(then);
@@ -516,7 +516,7 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
if let hir::ExprKind::Yield(_, source) = &expr.kind {
// Mark this expr's scope and all parent scopes as containing `yield`.
let mut scope = Scope { id: expr.hir_id.local_id, data: ScopeData::Node };
let mut scope = Scope { local_id: expr.hir_id.local_id, data: ScopeData::Node };
loop {
let span = match expr.kind {
hir::ExprKind::Yield(expr, hir::YieldSource::Await { .. }) => {
@@ -803,9 +803,9 @@ fn enter_node_scope_with_dtor(&mut self, id: hir::ItemLocalId) {
// account for the destruction scope representing the scope of
// the destructors that run immediately after it completes.
if self.terminating_scopes.contains(&id) {
self.enter_scope(Scope { id, data: ScopeData::Destruction });
self.enter_scope(Scope { local_id: id, data: ScopeData::Destruction });
}
self.enter_scope(Scope { id, data: ScopeData::Node });
self.enter_scope(Scope { local_id: id, data: ScopeData::Node });
}
fn enter_body(&mut self, hir_id: hir::HirId, f: impl FnOnce(&mut Self)) {
@@ -822,8 +822,8 @@ fn enter_body(&mut self, hir_id: hir::HirId, f: impl FnOnce(&mut Self)) {
let outer_pessimistic_yield = mem::replace(&mut self.pessimistic_yield, false);
self.terminating_scopes.insert(hir_id.local_id);
self.enter_scope(Scope { id: hir_id.local_id, data: ScopeData::CallSite });
self.enter_scope(Scope { id: hir_id.local_id, data: ScopeData::Arguments });
self.enter_scope(Scope { local_id: hir_id.local_id, data: ScopeData::CallSite });
self.enter_scope(Scope { local_id: hir_id.local_id, data: ScopeData::Arguments });
f(self);
+11 -21
View File
@@ -84,23 +84,23 @@
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Copy, TyEncodable, TyDecodable)]
#[derive(HashStable)]
pub struct Scope {
pub id: hir::ItemLocalId,
pub local_id: hir::ItemLocalId,
pub data: ScopeData,
}
impl fmt::Debug for Scope {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.data {
ScopeData::Node => write!(fmt, "Node({:?})", self.id),
ScopeData::CallSite => write!(fmt, "CallSite({:?})", self.id),
ScopeData::Arguments => write!(fmt, "Arguments({:?})", self.id),
ScopeData::Destruction => write!(fmt, "Destruction({:?})", self.id),
ScopeData::IfThen => write!(fmt, "IfThen({:?})", self.id),
ScopeData::IfThenRescope => write!(fmt, "IfThen[edition2024]({:?})", self.id),
ScopeData::Node => write!(fmt, "Node({:?})", self.local_id),
ScopeData::CallSite => write!(fmt, "CallSite({:?})", self.local_id),
ScopeData::Arguments => write!(fmt, "Arguments({:?})", self.local_id),
ScopeData::Destruction => write!(fmt, "Destruction({:?})", self.local_id),
ScopeData::IfThen => write!(fmt, "IfThen({:?})", self.local_id),
ScopeData::IfThenRescope => write!(fmt, "IfThen[edition2024]({:?})", self.local_id),
ScopeData::Remainder(fsi) => write!(
fmt,
"Remainder {{ block: {:?}, first_statement_index: {}}}",
self.id,
self.local_id,
fsi.as_u32(),
),
}
@@ -164,18 +164,8 @@ pub struct FirstStatementIndex {}
rustc_data_structures::static_assert_size!(ScopeData, 4);
impl Scope {
/// Returns an item-local ID associated with this scope.
///
/// N.B., likely to be replaced as API is refined; e.g., pnkfelix
/// anticipates `fn entry_node_id` and `fn each_exit_node_id`.
pub fn item_local_id(&self) -> hir::ItemLocalId {
self.id
}
pub fn hir_id(&self, scope_tree: &ScopeTree) -> Option<HirId> {
scope_tree
.root_body
.map(|hir_id| HirId { owner: hir_id.owner, local_id: self.item_local_id() })
scope_tree.root_body.map(|hir_id| HirId { owner: hir_id.owner, local_id: self.local_id })
}
/// Returns the span of this `Scope`. Note that in general the
@@ -350,7 +340,7 @@ pub fn record_scope_parent(&mut self, child: Scope, parent: Option<(Scope, Scope
pub fn record_var_scope(&mut self, var: hir::ItemLocalId, lifetime: Scope) {
debug!("record_var_scope(sub={:?}, sup={:?})", var, lifetime);
assert!(var != lifetime.item_local_id());
assert!(var != lifetime.local_id);
self.var_map.insert(var, lifetime);
}
@@ -359,7 +349,7 @@ pub fn record_rvalue_candidate(&mut self, var: HirId, candidate_type: RvalueCand
match &candidate_type {
RvalueCandidateType::Borrow { lifetime: Some(lifetime), .. }
| RvalueCandidateType::Pattern { lifetime: Some(lifetime), .. } => {
assert!(var.local_id != lifetime.item_local_id())
assert!(var.local_id != lifetime.local_id)
}
_ => {}
}
@@ -35,7 +35,7 @@ pub fn temporary_scope(
// if there's one. Static items, for instance, won't
// have an enclosing scope, hence no scope will be
// returned.
let mut id = Scope { id: expr_id, data: ScopeData::Node };
let mut id = Scope { local_id: expr_id, data: ScopeData::Node };
let mut backwards_incompatible = None;
while let Some(&(p, _)) = region_scope_tree.parent_map.get(&id) {
@@ -60,7 +60,7 @@ pub fn temporary_scope(
if backwards_incompatible.is_none() {
backwards_incompatible = region_scope_tree
.backwards_incompatible_scope
.get(&p.item_local_id())
.get(&p.local_id)
.copied();
}
id = p
@@ -76,7 +76,7 @@ pub fn temporary_scope(
pub fn record_rvalue_scope(&mut self, var: hir::ItemLocalId, lifetime: Option<Scope>) {
debug!("record_rvalue_scope(var={var:?}, lifetime={lifetime:?})");
if let Some(lifetime) = lifetime {
assert!(var != lifetime.item_local_id());
assert!(var != lifetime.local_id);
}
self.map.insert(var, lifetime);
}
@@ -75,11 +75,11 @@ fn as_temp_inner(
LocalInfo::BlockTailTemp(tail_info)
}
_ if let Some(Scope { data: ScopeData::IfThenRescope, id }) =
_ if let Some(Scope { data: ScopeData::IfThenRescope, local_id }) =
temp_lifetime.temp_lifetime =>
{
LocalInfo::IfThenRescopeTemp {
if_then: HirId { owner: this.hir_id.owner, local_id: id },
if_then: HirId { owner: this.hir_id.owner, local_id },
}
}
+2 -2
View File
@@ -531,9 +531,9 @@ fn construct_fn<'tcx>(
);
let call_site_scope =
region::Scope { id: body.id().hir_id.local_id, data: region::ScopeData::CallSite };
region::Scope { local_id: body.id().hir_id.local_id, data: region::ScopeData::CallSite };
let arg_scope =
region::Scope { id: body.id().hir_id.local_id, data: region::ScopeData::Arguments };
region::Scope { local_id: body.id().hir_id.local_id, data: region::ScopeData::Arguments };
let source_info = builder.source_info(span);
let call_site_s = (call_site_scope, source_info);
let _: BlockAnd<()> = builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
@@ -89,7 +89,7 @@
use rustc_middle::middle::region;
use rustc_middle::mir::*;
use rustc_middle::thir::{ExprId, LintLevel};
use rustc_middle::{bug, span_bug, ty};
use rustc_middle::{bug, span_bug};
use rustc_session::lint::Level;
use rustc_span::source_map::Spanned;
use rustc_span::{DUMMY_SP, Span};
@@ -1119,10 +1119,7 @@ pub(crate) fn schedule_backwards_incompatible_drop(
region_scope: region::Scope,
local: Local,
) {
if !self.local_decls[local].ty.has_significant_drop(self.tcx, ty::TypingEnv {
typing_mode: ty::TypingMode::non_body_analysis(),
param_env: self.param_env,
}) {
if !self.local_decls[local].ty.has_significant_drop(self.tcx, self.typing_env()) {
return;
}
for scope in self.scopes.scopes.iter_mut().rev() {
@@ -16,7 +16,7 @@ pub(crate) fn mirror_block(&mut self, block: &'tcx hir::Block<'tcx>) -> BlockId
let block = Block {
targeted_by_break: block.targeted_by_break,
region_scope: region::Scope {
id: block.hir_id.local_id,
local_id: block.hir_id.local_id,
data: region::ScopeData::Node,
},
span: block.span,
@@ -51,7 +51,7 @@ fn mirror_stmts(
let stmt = Stmt {
kind: StmtKind::Expr {
scope: region::Scope {
id: hir_id.local_id,
local_id: hir_id.local_id,
data: region::ScopeData::Node,
},
expr: self.mirror_expr(expr),
@@ -65,7 +65,7 @@ fn mirror_stmts(
}
hir::StmtKind::Let(local) => {
let remainder_scope = region::Scope {
id: block_id,
local_id: block_id,
data: region::ScopeData::Remainder(region::FirstStatementIndex::new(
index,
)),
@@ -108,7 +108,7 @@ fn mirror_stmts(
kind: StmtKind::Let {
remainder_scope,
init_scope: region::Scope {
id: hir_id.local_id,
local_id: hir_id.local_id,
data: region::ScopeData::Node,
},
pattern,
+11 -5
View File
@@ -45,7 +45,7 @@ pub(crate) fn mirror_exprs(&mut self, exprs: &'tcx [hir::Expr<'tcx>]) -> Box<[Ex
#[instrument(level = "trace", skip(self, hir_expr))]
pub(super) fn mirror_expr_inner(&mut self, hir_expr: &'tcx hir::Expr<'tcx>) -> ExprId {
let expr_scope =
region::Scope { id: hir_expr.hir_id.local_id, data: region::ScopeData::Node };
region::Scope { local_id: hir_expr.hir_id.local_id, data: region::ScopeData::Node };
trace!(?hir_expr.hir_id, ?hir_expr.span);
@@ -814,14 +814,20 @@ fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx>
hir::ExprKind::Become(call) => ExprKind::Become { value: self.mirror_expr(call) },
hir::ExprKind::Break(dest, ref value) => match dest.target_id {
Ok(target_id) => ExprKind::Break {
label: region::Scope { id: target_id.local_id, data: region::ScopeData::Node },
label: region::Scope {
local_id: target_id.local_id,
data: region::ScopeData::Node,
},
value: value.map(|value| self.mirror_expr(value)),
},
Err(err) => bug!("invalid loop id for break: {}", err),
},
hir::ExprKind::Continue(dest) => match dest.target_id {
Ok(loop_id) => ExprKind::Continue {
label: region::Scope { id: loop_id.local_id, data: region::ScopeData::Node },
label: region::Scope {
local_id: loop_id.local_id,
data: region::ScopeData::Node,
},
},
Err(err) => bug!("invalid loop id for continue: {}", err),
},
@@ -831,7 +837,7 @@ fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx>
},
hir::ExprKind::If(cond, then, else_opt) => ExprKind::If {
if_then_scope: region::Scope {
id: then.hir_id.local_id,
local_id: then.hir_id.local_id,
data: {
if expr.span.at_least_rust_2024() {
region::ScopeData::IfThenRescope
@@ -1021,7 +1027,7 @@ fn convert_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) -> ArmId {
guard: arm.guard.as_ref().map(|g| self.mirror_expr(g)),
body: self.mirror_expr(arm.body),
lint_level: LintLevel::Explicit(arm.hir_id),
scope: region::Scope { id: arm.hir_id.local_id, data: region::ScopeData::Node },
scope: region::Scope { local_id: arm.hir_id.local_id, data: region::ScopeData::Node },
span: arm.span,
};
self.thir.arms.push(arm)