Skip to content

Commit eafe40e

Browse files
authored
Rollup merge of #150846 - thir-hir-id, r=lcnr
include `HirId`s directly in the THIR, not wrapped in `LintLevel`s Occurrences of `LintLevel` in the THIR were always `LintLevel::Explicit`, containing a `HirId`, so we don't need to make it possible to put `LintLevel::Inherited` there. Removing the unused case where `HirId`s aren't present in the THIR slightly simplifies diagnostics/lints/tools that want to map from the THIR back to the HIR, e.g. #145569. Since `LintLevel` is no longer present in the THIR, I've moved it in the second commit to live in `rustc_mir_build`; that's where it's actually used. I'm not sure exactly where exactly it should live there, but I put it in the `builder::scope` module since it's used by `Builder::in_scope` for determining when to introduce source scopes. r? lcnr as the reviewer of #145569, since this was discussed there
2 parents a6acf0f + 8868b47 commit eafe40e

File tree

26 files changed

+174
-193
lines changed

26 files changed

+174
-193
lines changed

compiler/rustc_middle/src/thir.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,6 @@ pub struct Param<'tcx> {
116116
pub hir_id: Option<HirId>,
117117
}
118118

119-
#[derive(Copy, Clone, Debug, HashStable)]
120-
pub enum LintLevel {
121-
Inherited,
122-
Explicit(HirId),
123-
}
124-
125119
#[derive(Clone, Debug, HashStable)]
126120
pub struct Block {
127121
/// Whether the block itself has a label. Used by `label: {}`
@@ -236,8 +230,8 @@ pub enum StmtKind<'tcx> {
236230
/// `let pat: ty = <INIT> else { <ELSE> }`
237231
else_block: Option<BlockId>,
238232

239-
/// The lint level for this `let` statement.
240-
lint_level: LintLevel,
233+
/// The [`HirId`] for this `let` statement.
234+
hir_id: HirId,
241235

242236
/// Span of the `let <PAT> = <INIT>` part.
243237
span: Span,
@@ -271,7 +265,7 @@ pub enum ExprKind<'tcx> {
271265
/// and to track the `HirId` of the expressions within the scope.
272266
Scope {
273267
region_scope: region::Scope,
274-
lint_level: LintLevel,
268+
hir_id: HirId,
275269
value: ExprId,
276270
},
277271
/// A `box <value>` expression.
@@ -579,7 +573,7 @@ pub struct Arm<'tcx> {
579573
pub pattern: Box<Pat<'tcx>>,
580574
pub guard: Option<ExprId>,
581575
pub body: ExprId,
582-
pub lint_level: LintLevel,
576+
pub hir_id: HirId,
583577
pub scope: region::Scope,
584578
pub span: Span,
585579
}

compiler/rustc_middle/src/thir/visit.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ pub fn walk_expr<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
4747
use ExprKind::*;
4848
let Expr { kind, ty: _, temp_scope_id: _, span: _ } = expr;
4949
match *kind {
50-
Scope { value, region_scope: _, lint_level: _ } => {
51-
visitor.visit_expr(&visitor.thir()[value])
52-
}
50+
Scope { value, region_scope: _, hir_id: _ } => visitor.visit_expr(&visitor.thir()[value]),
5351
Box { value } => visitor.visit_expr(&visitor.thir()[value]),
5452
If { cond, then, else_opt, if_then_scope: _ } => {
5553
visitor.visit_expr(&visitor.thir()[cond]);
@@ -205,7 +203,7 @@ pub fn walk_stmt<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
205203
remainder_scope: _,
206204
init_scope: _,
207205
pattern,
208-
lint_level: _,
206+
hir_id: _,
209207
else_block,
210208
span: _,
211209
} => {
@@ -238,7 +236,7 @@ pub fn walk_arm<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
238236
visitor: &mut V,
239237
arm: &'thir Arm<'tcx>,
240238
) {
241-
let Arm { guard, pattern, body, lint_level: _, span: _, scope: _ } = arm;
239+
let Arm { guard, pattern, body, hir_id: _, span: _, scope: _ } = arm;
242240
if let Some(expr) = guard {
243241
visitor.visit_expr(&visitor.thir()[*expr])
244242
}

compiler/rustc_mir_build/src/builder/block.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use tracing::debug;
77

88
use crate::builder::ForGuard::OutsideGuard;
99
use crate::builder::matches::{DeclareLetBindings, ScheduleDrops};
10+
use crate::builder::scope::LintLevel;
1011
use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
1112

1213
impl<'a, 'tcx> Builder<'a, 'tcx> {
@@ -83,7 +84,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8384
init_scope,
8485
pattern,
8586
initializer: Some(initializer),
86-
lint_level,
87+
hir_id,
8788
else_block: Some(else_block),
8889
span: _,
8990
} => {
@@ -191,7 +192,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
191192

192193
let initializer_span = this.thir[*initializer].span;
193194
let scope = (*init_scope, source_info);
194-
let failure_and_block = this.in_scope(scope, *lint_level, |this| {
195+
let lint_level = LintLevel::Explicit(*hir_id);
196+
let failure_and_block = this.in_scope(scope, lint_level, |this| {
195197
this.declare_bindings(
196198
visibility_scope,
197199
remainder_span,
@@ -232,7 +234,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
232234
init_scope,
233235
pattern,
234236
initializer,
235-
lint_level,
237+
hir_id,
236238
else_block: None,
237239
span: _,
238240
} => {
@@ -250,12 +252,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
250252
Some(this.new_source_scope(remainder_span, LintLevel::Inherited));
251253

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

257260
block = this
258-
.in_scope(scope, *lint_level, |this| {
261+
.in_scope(scope, lint_level, |this| {
259262
this.declare_bindings(
260263
visibility_scope,
261264
remainder_span,
@@ -269,7 +272,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
269272
.into_block();
270273
} else {
271274
let scope = (*init_scope, source_info);
272-
let _: BlockAnd<()> = this.in_scope(scope, *lint_level, |this| {
275+
let _: BlockAnd<()> = this.in_scope(scope, lint_level, |this| {
273276
this.declare_bindings(
274277
visibility_scope,
275278
remainder_span,

compiler/rustc_mir_build/src/builder/expr/as_constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2323
let tcx = this.tcx;
2424
let Expr { ty, temp_scope_id: _, span, ref kind } = *expr;
2525
match kind {
26-
ExprKind::Scope { region_scope: _, lint_level: _, value } => {
26+
ExprKind::Scope { region_scope: _, hir_id: _, value } => {
2727
this.as_constant(&this.thir[*value])
2828
}
2929
_ => as_constant_inner(

compiler/rustc_mir_build/src/builder/expr/as_operand.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_middle::thir::*;
66
use tracing::{debug, instrument};
77

88
use crate::builder::expr::category::Category;
9+
use crate::builder::scope::LintLevel;
910
use crate::builder::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary};
1011

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

124125
let expr = &this.thir[expr_id];
125-
if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
126+
if let ExprKind::Scope { region_scope, hir_id, value } = expr.kind {
126127
let source_info = this.source_info(expr.span);
127128
let region_scope = (region_scope, source_info);
128-
return this.in_scope(region_scope, lint_level, |this| {
129+
return this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
129130
this.as_operand(block, scope, value, local_info, needs_temporary)
130131
});
131132
}
@@ -165,10 +166,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
165166
let expr = &this.thir[expr_id];
166167
debug!("as_call_operand(block={:?}, expr={:?})", block, expr);
167168

168-
if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
169+
if let ExprKind::Scope { region_scope, hir_id, value } = expr.kind {
169170
let source_info = this.source_info(expr.span);
170171
let region_scope = (region_scope, source_info);
171-
return this.in_scope(region_scope, lint_level, |this| {
172+
return this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
172173
this.as_call_operand(block, scope, value)
173174
});
174175
}

compiler/rustc_mir_build/src/builder/expr/as_place.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use tracing::{debug, instrument, trace};
1616

1717
use crate::builder::ForGuard::{OutsideGuard, RefWithinGuard};
1818
use crate::builder::expr::category::Category;
19+
use crate::builder::scope::LintLevel;
1920
use crate::builder::{BlockAnd, BlockAndExtension, Builder, Capture, CaptureMap};
2021

2122
/// The "outermost" place that holds this value.
@@ -427,8 +428,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
427428
let expr_span = expr.span;
428429
let source_info = this.source_info(expr_span);
429430
match expr.kind {
430-
ExprKind::Scope { region_scope, lint_level, value } => {
431-
this.in_scope((region_scope, source_info), lint_level, |this| {
431+
ExprKind::Scope { region_scope, hir_id, value } => {
432+
this.in_scope((region_scope, source_info), LintLevel::Explicit(hir_id), |this| {
432433
this.expr_as_place(block, value, mutability, fake_borrow_temps)
433434
})
434435
}

compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use tracing::debug;
1818

1919
use crate::builder::expr::as_place::PlaceBase;
2020
use crate::builder::expr::category::{Category, RvalueFunc};
21+
use crate::builder::scope::LintLevel;
2122
use crate::builder::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary};
2223

2324
impl<'a, 'tcx> Builder<'a, 'tcx> {
@@ -56,9 +57,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5657

5758
match expr.kind {
5859
ExprKind::ThreadLocalRef(did) => block.and(Rvalue::ThreadLocalRef(did)),
59-
ExprKind::Scope { region_scope, lint_level, value } => {
60+
ExprKind::Scope { region_scope, hir_id, value } => {
6061
let region_scope = (region_scope, source_info);
61-
this.in_scope(region_scope, lint_level, |this| this.as_rvalue(block, scope, value))
62+
this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
63+
this.as_rvalue(block, scope, value)
64+
})
6265
}
6366
ExprKind::Repeat { value, count } => {
6467
if Some(0) == count.try_to_target_usize(this.tcx) {
@@ -657,7 +660,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
657660
source: eid,
658661
is_from_as_cast: _,
659662
}
660-
| &ExprKind::Scope { region_scope: _, lint_level: _, value: eid } => {
663+
| &ExprKind::Scope { region_scope: _, hir_id: _, value: eid } => {
661664
kind = &self.thir[eid].kind
662665
}
663666
_ => return matches!(Category::of(&kind), Some(Category::Constant)),

compiler/rustc_mir_build/src/builder/expr/as_temp.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::mir::*;
77
use rustc_middle::thir::*;
88
use tracing::{debug, instrument};
99

10-
use crate::builder::scope::DropKind;
10+
use crate::builder::scope::{DropKind, LintLevel};
1111
use crate::builder::{BlockAnd, BlockAndExtension, Builder};
1212

1313
impl<'a, 'tcx> Builder<'a, 'tcx> {
@@ -39,10 +39,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3939
let expr = &this.thir[expr_id];
4040
let expr_span = expr.span;
4141
let source_info = this.source_info(expr_span);
42-
if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
43-
return this.in_scope((region_scope, source_info), lint_level, |this| {
44-
this.as_temp(block, temp_lifetime, value, mutability)
45-
});
42+
if let ExprKind::Scope { region_scope, hir_id, value } = expr.kind {
43+
return this.in_scope(
44+
(region_scope, source_info),
45+
LintLevel::Explicit(hir_id),
46+
|this| this.as_temp(block, temp_lifetime, value, mutability),
47+
);
4648
}
4749

4850
let expr_ty = expr.ty;

compiler/rustc_mir_build/src/builder/expr/into.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use tracing::{debug, instrument};
1616

1717
use crate::builder::expr::category::{Category, RvalueFunc};
1818
use crate::builder::matches::{DeclareLetBindings, HasMatchGuard};
19+
use crate::builder::scope::LintLevel;
1920
use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder, NeedsTemporary};
2021
use crate::errors::{LoopMatchArmWithGuard, LoopMatchUnsupportedType};
2122

@@ -45,10 +46,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4546
}
4647

4748
let block_and = match expr.kind {
48-
ExprKind::Scope { region_scope, lint_level, value } => {
49+
ExprKind::Scope { region_scope, hir_id, value } => {
4950
let region_scope = (region_scope, source_info);
5051
ensure_sufficient_stack(|| {
51-
this.in_scope(region_scope, lint_level, |this| {
52+
this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
5253
this.expr_into_dest(destination, block, value)
5354
})
5455
})

compiler/rustc_mir_build/src/builder/expr/stmt.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_middle::thir::*;
55
use rustc_span::source_map::Spanned;
66
use tracing::debug;
77

8-
use crate::builder::scope::BreakableTarget;
8+
use crate::builder::scope::{BreakableTarget, LintLevel};
99
use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
1010

1111
impl<'a, 'tcx> Builder<'a, 'tcx> {
@@ -25,8 +25,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2525
// Handle a number of expressions that don't need a destination at all. This
2626
// avoids needing a mountain of temporary `()` variables.
2727
match expr.kind {
28-
ExprKind::Scope { region_scope, lint_level, value } => {
29-
this.in_scope((region_scope, source_info), lint_level, |this| {
28+
ExprKind::Scope { region_scope, hir_id, value } => {
29+
this.in_scope((region_scope, source_info), LintLevel::Explicit(hir_id), |this| {
3030
this.stmt_expr(block, value, statement_scope)
3131
})
3232
}
@@ -106,7 +106,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
106106
}
107107
ExprKind::Become { value } => {
108108
let v = &this.thir[value];
109-
let ExprKind::Scope { value, lint_level, region_scope } = v.kind else {
109+
let ExprKind::Scope { value, hir_id, region_scope } = v.kind else {
110110
span_bug!(v.span, "`thir_check_tail_calls` should have disallowed this {v:?}")
111111
};
112112

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

118-
this.in_scope((region_scope, source_info), lint_level, |this| {
118+
this.in_scope((region_scope, source_info), LintLevel::Explicit(hir_id), |this| {
119119
let fun = unpack!(block = this.as_local_operand(block, fun));
120120
let args: Box<[_]> = args
121121
.into_iter()

0 commit comments

Comments
 (0)