Skip to content
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

Some destructor/drop related tweaks #134491

Merged
merged 2 commits into from
Dec 19, 2024
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
22 changes: 11 additions & 11 deletions compiler/rustc_hir_analysis/src/check/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -221,7 +221,7 @@ fn resolve_arm<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, arm: &'tcx hir
}

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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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 { .. }) => {
Expand Down Expand Up @@ -803,9 +803,9 @@ impl<'tcx> RegionResolutionVisitor<'tcx> {
// 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)) {
Expand All @@ -822,8 +822,8 @@ impl<'tcx> RegionResolutionVisitor<'tcx> {
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);

Expand Down
32 changes: 11 additions & 21 deletions compiler/rustc_middle/src/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,23 @@ use crate::ty::TyCtxt;
#[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(),
),
}
Expand Down Expand Up @@ -164,18 +164,8 @@ rustc_index::newtype_index! {
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
Expand Down Expand Up @@ -350,7 +340,7 @@ impl ScopeTree {

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);
}

Expand All @@ -359,7 +349,7 @@ impl ScopeTree {
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)
}
_ => {}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_middle/src/ty/rvalue_scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl RvalueScopes {
// 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) {
Expand All @@ -60,7 +60,7 @@ impl RvalueScopes {
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
Expand All @@ -76,7 +76,7 @@ impl RvalueScopes {
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);
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/builder/expr/as_temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
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 },
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_mir_build/src/builder/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ use rustc_index::{IndexSlice, IndexVec};
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};
Expand Down Expand Up @@ -1119,10 +1119,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
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() {
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_mir_build/src/thir/cx/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl<'tcx> Cx<'tcx> {
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,
Expand Down Expand Up @@ -51,7 +51,7 @@ impl<'tcx> Cx<'tcx> {
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),
Expand All @@ -65,7 +65,7 @@ impl<'tcx> Cx<'tcx> {
}
hir::StmtKind::Let(local) => {
let remainder_scope = region::Scope {
id: block_id,
local_id: block_id,
data: region::ScopeData::Remainder(region::FirstStatementIndex::new(
index,
)),
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<'tcx> Cx<'tcx> {
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,
Expand Down
16 changes: 11 additions & 5 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<'tcx> Cx<'tcx> {
#[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);

Expand Down Expand Up @@ -814,14 +814,20 @@ impl<'tcx> Cx<'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),
},
Expand All @@ -831,7 +837,7 @@ impl<'tcx> Cx<'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
Expand Down Expand Up @@ -1021,7 +1027,7 @@ impl<'tcx> Cx<'tcx> {
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)
Expand Down
Loading