Skip to content
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
14 changes: 4 additions & 10 deletions compiler/rustc_middle/src/thir.rs
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option here would be to use ItemLocalId instead of HirId to maybe make thir::StmtKind and thir::Arm a bit smaller. I'm not really familiar with layout computation so I don't actually know for sure if it'd do anything. I don't think it'd affect thir::ExprKind's size.

LocalVarId is also a whole HirId despite local variables' scopes being stored by ItemLocalId in the ScopeTree, so maybe there's a stylistic reason to use HirIds instead of ItemLocalIds in the THIR? Not sure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally feel in favor of using ItemLocalId in the THIR, the same as for TypeckResults. Could defer that to a later PR though. Feels like an unrelated change

Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,6 @@ pub struct Param<'tcx> {
pub hir_id: Option<HirId>,
}

#[derive(Copy, Clone, Debug, HashStable)]
pub enum LintLevel {
Inherited,
Explicit(HirId),
}

#[derive(Clone, Debug, HashStable)]
pub struct Block {
/// Whether the block itself has a label. Used by `label: {}`
Expand Down Expand Up @@ -236,8 +230,8 @@ pub enum StmtKind<'tcx> {
/// `let pat: ty = <INIT> else { <ELSE> }`
else_block: Option<BlockId>,

/// The lint level for this `let` statement.
lint_level: LintLevel,
/// The [`HirId`] for this `let` statement.
hir_id: HirId,

/// Span of the `let <PAT> = <INIT>` part.
span: Span,
Expand Down Expand Up @@ -271,7 +265,7 @@ pub enum ExprKind<'tcx> {
/// and to track the `HirId` of the expressions within the scope.
Scope {
region_scope: region::Scope,
lint_level: LintLevel,
hir_id: HirId,
value: ExprId,
},
/// A `box <value>` expression.
Expand Down Expand Up @@ -579,7 +573,7 @@ pub struct Arm<'tcx> {
pub pattern: Box<Pat<'tcx>>,
pub guard: Option<ExprId>,
pub body: ExprId,
pub lint_level: LintLevel,
pub hir_id: HirId,
pub scope: region::Scope,
pub span: Span,
}
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_middle/src/thir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ pub fn walk_expr<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
use ExprKind::*;
let Expr { kind, ty: _, temp_scope_id: _, span: _ } = expr;
match *kind {
Scope { value, region_scope: _, lint_level: _ } => {
visitor.visit_expr(&visitor.thir()[value])
}
Scope { value, region_scope: _, hir_id: _ } => visitor.visit_expr(&visitor.thir()[value]),
Box { value } => visitor.visit_expr(&visitor.thir()[value]),
If { cond, then, else_opt, if_then_scope: _ } => {
visitor.visit_expr(&visitor.thir()[cond]);
Expand Down Expand Up @@ -205,7 +203,7 @@ pub fn walk_stmt<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
remainder_scope: _,
init_scope: _,
pattern,
lint_level: _,
hir_id: _,
else_block,
span: _,
} => {
Expand Down Expand Up @@ -238,7 +236,7 @@ pub fn walk_arm<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
visitor: &mut V,
arm: &'thir Arm<'tcx>,
) {
let Arm { guard, pattern, body, lint_level: _, span: _, scope: _ } = arm;
let Arm { guard, pattern, body, hir_id: _, span: _, scope: _ } = arm;
if let Some(expr) = guard {
visitor.visit_expr(&visitor.thir()[*expr])
}
Expand Down
13 changes: 8 additions & 5 deletions compiler/rustc_mir_build/src/builder/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use tracing::debug;

use crate::builder::ForGuard::OutsideGuard;
use crate::builder::matches::{DeclareLetBindings, ScheduleDrops};
use crate::builder::scope::LintLevel;
use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder};

impl<'a, 'tcx> Builder<'a, 'tcx> {
Expand Down Expand Up @@ -83,7 +84,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
init_scope,
pattern,
initializer: Some(initializer),
lint_level,
hir_id,
else_block: Some(else_block),
span: _,
} => {
Expand Down Expand Up @@ -191,7 +192,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

let initializer_span = this.thir[*initializer].span;
let scope = (*init_scope, source_info);
let failure_and_block = this.in_scope(scope, *lint_level, |this| {
let lint_level = LintLevel::Explicit(*hir_id);
let failure_and_block = this.in_scope(scope, lint_level, |this| {
this.declare_bindings(
visibility_scope,
remainder_span,
Expand Down Expand Up @@ -232,7 +234,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
init_scope,
pattern,
initializer,
lint_level,
hir_id,
else_block: None,
span: _,
} => {
Expand All @@ -250,12 +252,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Some(this.new_source_scope(remainder_span, LintLevel::Inherited));

// Evaluate the initializer, if present.
let lint_level = LintLevel::Explicit(*hir_id);
if let Some(init) = *initializer {
let initializer_span = this.thir[init].span;
let scope = (*init_scope, source_info);

block = this
.in_scope(scope, *lint_level, |this| {
.in_scope(scope, lint_level, |this| {
this.declare_bindings(
visibility_scope,
remainder_span,
Expand All @@ -269,7 +272,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
.into_block();
} else {
let scope = (*init_scope, source_info);
let _: BlockAnd<()> = this.in_scope(scope, *lint_level, |this| {
let _: BlockAnd<()> = this.in_scope(scope, lint_level, |this| {
this.declare_bindings(
visibility_scope,
remainder_span,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/builder/expr/as_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let tcx = this.tcx;
let Expr { ty, temp_scope_id: _, span, ref kind } = *expr;
match kind {
ExprKind::Scope { region_scope: _, lint_level: _, value } => {
ExprKind::Scope { region_scope: _, hir_id: _, value } => {
this.as_constant(&this.thir[*value])
}
_ => as_constant_inner(
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_mir_build/src/builder/expr/as_operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rustc_middle::thir::*;
use tracing::{debug, instrument};

use crate::builder::expr::category::Category;
use crate::builder::scope::LintLevel;
use crate::builder::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary};

impl<'a, 'tcx> Builder<'a, 'tcx> {
Expand Down Expand Up @@ -122,10 +123,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let this = self; // See "LET_THIS_SELF".

let expr = &this.thir[expr_id];
if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
if let ExprKind::Scope { region_scope, hir_id, value } = expr.kind {
let source_info = this.source_info(expr.span);
let region_scope = (region_scope, source_info);
return this.in_scope(region_scope, lint_level, |this| {
return this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
this.as_operand(block, scope, value, local_info, needs_temporary)
});
}
Expand Down Expand Up @@ -165,10 +166,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let expr = &this.thir[expr_id];
debug!("as_call_operand(block={:?}, expr={:?})", block, expr);

if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
if let ExprKind::Scope { region_scope, hir_id, value } = expr.kind {
let source_info = this.source_info(expr.span);
let region_scope = (region_scope, source_info);
return this.in_scope(region_scope, lint_level, |this| {
return this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
this.as_call_operand(block, scope, value)
});
}
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_mir_build/src/builder/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tracing::{debug, instrument, trace};

use crate::builder::ForGuard::{OutsideGuard, RefWithinGuard};
use crate::builder::expr::category::Category;
use crate::builder::scope::LintLevel;
use crate::builder::{BlockAnd, BlockAndExtension, Builder, Capture, CaptureMap};

/// The "outermost" place that holds this value.
Expand Down Expand Up @@ -427,8 +428,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let expr_span = expr.span;
let source_info = this.source_info(expr_span);
match expr.kind {
ExprKind::Scope { region_scope, lint_level, value } => {
this.in_scope((region_scope, source_info), lint_level, |this| {
ExprKind::Scope { region_scope, hir_id, value } => {
this.in_scope((region_scope, source_info), LintLevel::Explicit(hir_id), |this| {
this.expr_as_place(block, value, mutability, fake_borrow_temps)
})
}
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use tracing::debug;

use crate::builder::expr::as_place::PlaceBase;
use crate::builder::expr::category::{Category, RvalueFunc};
use crate::builder::scope::LintLevel;
use crate::builder::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary};

impl<'a, 'tcx> Builder<'a, 'tcx> {
Expand Down Expand Up @@ -56,9 +57,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

match expr.kind {
ExprKind::ThreadLocalRef(did) => block.and(Rvalue::ThreadLocalRef(did)),
ExprKind::Scope { region_scope, lint_level, value } => {
ExprKind::Scope { region_scope, hir_id, value } => {
let region_scope = (region_scope, source_info);
this.in_scope(region_scope, lint_level, |this| this.as_rvalue(block, scope, value))
this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
this.as_rvalue(block, scope, value)
})
}
ExprKind::Repeat { value, count } => {
if Some(0) == count.try_to_target_usize(this.tcx) {
Expand Down Expand Up @@ -657,7 +660,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
source: eid,
is_from_as_cast: _,
}
| &ExprKind::Scope { region_scope: _, lint_level: _, value: eid } => {
| &ExprKind::Scope { region_scope: _, hir_id: _, value: eid } => {
kind = &self.thir[eid].kind
}
_ => return matches!(Category::of(&kind), Some(Category::Constant)),
Expand Down
12 changes: 7 additions & 5 deletions compiler/rustc_mir_build/src/builder/expr/as_temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_middle::mir::*;
use rustc_middle::thir::*;
use tracing::{debug, instrument};

use crate::builder::scope::DropKind;
use crate::builder::scope::{DropKind, LintLevel};
use crate::builder::{BlockAnd, BlockAndExtension, Builder};

impl<'a, 'tcx> Builder<'a, 'tcx> {
Expand Down Expand Up @@ -39,10 +39,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let expr = &this.thir[expr_id];
let expr_span = expr.span;
let source_info = this.source_info(expr_span);
if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
return this.in_scope((region_scope, source_info), lint_level, |this| {
this.as_temp(block, temp_lifetime, value, mutability)
});
if let ExprKind::Scope { region_scope, hir_id, value } = expr.kind {
return this.in_scope(
(region_scope, source_info),
LintLevel::Explicit(hir_id),
|this| this.as_temp(block, temp_lifetime, value, mutability),
);
}

let expr_ty = expr.ty;
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_mir_build/src/builder/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tracing::{debug, instrument};

use crate::builder::expr::category::{Category, RvalueFunc};
use crate::builder::matches::{DeclareLetBindings, HasMatchGuard};
use crate::builder::scope::LintLevel;
use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder, NeedsTemporary};
use crate::errors::{LoopMatchArmWithGuard, LoopMatchUnsupportedType};

Expand Down Expand Up @@ -45,10 +46,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}

let block_and = match expr.kind {
ExprKind::Scope { region_scope, lint_level, value } => {
ExprKind::Scope { region_scope, hir_id, value } => {
let region_scope = (region_scope, source_info);
ensure_sufficient_stack(|| {
this.in_scope(region_scope, lint_level, |this| {
this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
this.expr_into_dest(destination, block, value)
})
})
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_mir_build/src/builder/expr/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_middle::thir::*;
use rustc_span::source_map::Spanned;
use tracing::debug;

use crate::builder::scope::BreakableTarget;
use crate::builder::scope::{BreakableTarget, LintLevel};
use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder};

impl<'a, 'tcx> Builder<'a, 'tcx> {
Expand All @@ -25,8 +25,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// Handle a number of expressions that don't need a destination at all. This
// avoids needing a mountain of temporary `()` variables.
match expr.kind {
ExprKind::Scope { region_scope, lint_level, value } => {
this.in_scope((region_scope, source_info), lint_level, |this| {
ExprKind::Scope { region_scope, hir_id, value } => {
this.in_scope((region_scope, source_info), LintLevel::Explicit(hir_id), |this| {
this.stmt_expr(block, value, statement_scope)
})
}
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
ExprKind::Become { value } => {
let v = &this.thir[value];
let ExprKind::Scope { value, lint_level, region_scope } = v.kind else {
let ExprKind::Scope { value, hir_id, region_scope } = v.kind else {
span_bug!(v.span, "`thir_check_tail_calls` should have disallowed this {v:?}")
};

Expand All @@ -115,7 +115,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
span_bug!(v.span, "`thir_check_tail_calls` should have disallowed this {v:?}")
};

this.in_scope((region_scope, source_info), lint_level, |this| {
this.in_scope((region_scope, source_info), LintLevel::Explicit(hir_id), |this| {
let fun = unpack!(block = this.as_local_operand(block, fun));
let args: Box<[_]> = args
.into_iter()
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_mir_build/src/builder/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::builder::ForGuard::{self, OutsideGuard, RefWithinGuard};
use crate::builder::expr::as_place::PlaceBuilder;
use crate::builder::matches::buckets::PartitionedCandidates;
use crate::builder::matches::user_ty::ProjectedUserTypesNode;
use crate::builder::scope::DropKind;
use crate::builder::scope::{DropKind, LintLevel};
use crate::builder::{
BlockAnd, BlockAndExtension, Builder, GuardFrame, GuardFrameLocal, LocalsForNode,
};
Expand Down Expand Up @@ -182,9 +182,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
this.break_for_else(success_block, args.variable_source_info);
failure_block.unit()
}
ExprKind::Scope { region_scope, lint_level, value } => {
ExprKind::Scope { region_scope, hir_id, value } => {
let region_scope = (region_scope, this.source_info(expr_span));
this.in_scope(region_scope, lint_level, |this| {
this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
this.then_else_break_inner(block, value, args)
})
}
Expand Down Expand Up @@ -434,7 +434,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let guard_scope = arm
.guard
.map(|_| region::Scope { data: region::ScopeData::MatchGuard, ..arm.scope });
self.in_scope(arm_scope, arm.lint_level, |this| {
self.in_scope(arm_scope, LintLevel::Explicit(arm.hir_id), |this| {
this.opt_in_scope(guard_scope.map(|scope| (scope, arm_source_info)), |this| {
// `if let` guard temps needing deduplicating will be in the guard scope.
let old_dedup_scope =
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 @@ -38,14 +38,14 @@ use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
use rustc_middle::middle::region;
use rustc_middle::mir::*;
use rustc_middle::thir::{self, ExprId, LintLevel, LocalVarId, Param, ParamId, PatKind, Thir};
use rustc_middle::thir::{self, ExprId, LocalVarId, Param, ParamId, PatKind, Thir};
use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt, TypeVisitableExt, TypingMode};
use rustc_middle::{bug, span_bug};
use rustc_session::lint;
use rustc_span::{Span, Symbol, sym};

use crate::builder::expr::as_place::PlaceBuilder;
use crate::builder::scope::DropKind;
use crate::builder::scope::{DropKind, LintLevel};
use crate::errors;

pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
Expand Down
10 changes: 9 additions & 1 deletion 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_hir::HirId;
use rustc_index::{IndexSlice, IndexVec};
use rustc_middle::middle::region;
use rustc_middle::mir::{self, *};
use rustc_middle::thir::{AdtExpr, AdtExprBase, ArmId, ExprId, ExprKind, LintLevel};
use rustc_middle::thir::{AdtExpr, AdtExprBase, ArmId, ExprId, ExprKind};
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, ValTree};
use rustc_middle::{bug, span_bug};
use rustc_pattern_analysis::rustc::RustcPatCtxt;
Expand Down Expand Up @@ -522,6 +522,14 @@ impl<'tcx> Scopes<'tcx> {
}
}

/// Used by [`Builder::in_scope`] to create source scopes mapping from MIR back to HIR at points
/// where lint levels change.
#[derive(Copy, Clone, Debug)]
pub(crate) enum LintLevel {
Inherited,
Explicit(HirId),
}

impl<'a, 'tcx> Builder<'a, 'tcx> {
// Adding and removing scopes
// ==========================
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
}
};
match expr.kind {
ExprKind::Scope { value, lint_level: LintLevel::Explicit(hir_id), region_scope: _ } => {
ExprKind::Scope { value, hir_id, region_scope: _ } => {
let prev_id = self.hir_context;
self.hir_context = hir_id;
ensure_sufficient_stack(|| {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/thir/cx/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
pattern,
initializer: local.init.map(|init| self.mirror_expr(init)),
else_block,
lint_level: LintLevel::Explicit(local.hir_id),
hir_id: local.hir_id,
span,
},
};
Expand Down
Loading
Loading