Skip to content

Commit bf2de9b

Browse files
committed
return a TempLifetime from ScopeTree::temporary_scope
This means less boilerplate when building the THIR and less possibility for confusion about what to do with the returned scopes from `ScopeTree::temporary_scope`. Possibly migrating `TempLifetime` to `rustc_middle::middle::region` and tweaking its doc comments is left for future work.
1 parent 9336c1b commit bf2de9b

File tree

2 files changed

+29
-59
lines changed

2 files changed

+29
-59
lines changed

compiler/rustc_middle/src/middle/region.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_macros::{HashStable, TyDecodable, TyEncodable};
1616
use rustc_span::{DUMMY_SP, Span};
1717
use tracing::debug;
1818

19+
use crate::thir::TempLifetime;
1920
use crate::ty::{self, TyCtxt};
2021

2122
/// Represents a statically-describable scope that can be used to
@@ -330,16 +331,16 @@ impl ScopeTree {
330331
/// Returns the scope when the temp created by `expr_id` will be cleaned up.
331332
/// It also emits a lint on potential backwards incompatible change to the temporary scope
332333
/// which is *for now* always shortening.
333-
pub fn temporary_scope(&self, expr_id: hir::ItemLocalId) -> (Option<Scope>, Option<Scope>) {
334+
pub fn temporary_scope(&self, expr_id: hir::ItemLocalId) -> TempLifetime {
334335
// Check for a designated rvalue scope.
335336
if let Some(&s) = self.rvalue_scopes.get(&expr_id) {
336337
debug!("temporary_scope({expr_id:?}) = {s:?} [custom]");
337-
return (s, None);
338+
return TempLifetime { temp_lifetime: s, backwards_incompatible: None };
338339
}
339340

340341
// Otherwise, locate the innermost terminating scope.
341-
let (scope, backward_incompatible) =
342+
let (scope, backwards_incompatible) =
342343
self.default_temporary_scope(Scope { local_id: expr_id, data: ScopeData::Node });
343-
(Some(scope), backward_incompatible)
344+
TempLifetime { temp_lifetime: Some(scope), backwards_incompatible }
344345
}
345346
}

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
336336
fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx> {
337337
let tcx = self.tcx;
338338
let expr_ty = self.typeck_results.expr_ty(expr);
339-
let (temp_lifetime, backwards_incompatible) =
340-
self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
339+
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
341340

342341
let kind = match expr.kind {
343342
// Here comes the interesting stuff:
@@ -372,7 +371,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
372371
let arg_tys = args.iter().map(|e| self.typeck_results.expr_ty_adjusted(e));
373372
let tupled_args = Expr {
374373
ty: Ty::new_tup_from_iter(tcx, arg_tys),
375-
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
374+
temp_lifetime,
376375
span: expr.span,
377376
kind: ExprKind::Tuple { fields: self.mirror_exprs(args) },
378377
};
@@ -398,7 +397,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
398397
}
399398
let value = &args[0];
400399
return Expr {
401-
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
400+
temp_lifetime,
402401
ty: expr_ty,
403402
span: expr.span,
404403
kind: ExprKind::Box { value: self.mirror_expr(value) },
@@ -502,17 +501,17 @@ impl<'tcx> ThirBuildCx<'tcx> {
502501
expr: Some(arg),
503502
safety_mode: BlockSafety::Safe,
504503
});
505-
let (temp_lifetime, backwards_incompatible) =
506-
self.region_scope_tree.temporary_scope(arg_expr.hir_id.local_id);
507504
arg = self.thir.exprs.push(Expr {
508-
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
505+
temp_lifetime: self
506+
.region_scope_tree
507+
.temporary_scope(arg_expr.hir_id.local_id),
509508
ty: arg_ty,
510509
span: arg_expr.span,
511510
kind: ExprKind::Block { block },
512511
});
513512
}
514513
let expr = self.thir.exprs.push(Expr {
515-
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
514+
temp_lifetime,
516515
ty,
517516
span: expr.span,
518517
kind: ExprKind::Borrow { borrow_kind: mutbl.to_borrow_kind(), arg },
@@ -995,12 +994,10 @@ impl<'tcx> ThirBuildCx<'tcx> {
995994
}
996995
} else {
997996
let block_ty = self.typeck_results.node_type(body.hir_id);
998-
let (temp_lifetime, backwards_incompatible) =
999-
self.region_scope_tree.temporary_scope(body.hir_id.local_id);
1000997
let block = self.mirror_block(body);
1001998
let body = self.thir.exprs.push(Expr {
1002999
ty: block_ty,
1003-
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1000+
temp_lifetime: self.region_scope_tree.temporary_scope(body.hir_id.local_id),
10041001
span: self.thir[block].span,
10051002
kind: ExprKind::Block { block },
10061003
});
@@ -1022,17 +1019,13 @@ impl<'tcx> ThirBuildCx<'tcx> {
10221019
expr, cast_ty.hir_id, user_ty,
10231020
);
10241021

1025-
let cast = self.mirror_expr_cast(
1026-
source,
1027-
TempLifetime { temp_lifetime, backwards_incompatible },
1028-
expr.span,
1029-
);
1022+
let cast = self.mirror_expr_cast(source, temp_lifetime, expr.span);
10301023

10311024
if let Some(user_ty) = user_ty {
10321025
// NOTE: Creating a new Expr and wrapping a Cast inside of it may be
10331026
// inefficient, revisit this when performance becomes an issue.
10341027
let cast_expr = self.thir.exprs.push(Expr {
1035-
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1028+
temp_lifetime,
10361029
ty: expr_ty,
10371030
span: expr.span,
10381031
kind: cast,
@@ -1091,12 +1084,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
10911084
hir::ExprKind::Err(_) => unreachable!("cannot lower a `hir::ExprKind::Err` to THIR"),
10921085
};
10931086

1094-
Expr {
1095-
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1096-
ty: expr_ty,
1097-
span: expr.span,
1098-
kind,
1099-
}
1087+
Expr { temp_lifetime, ty: expr_ty, span: expr.span, kind }
11001088
}
11011089

11021090
fn user_args_applied_to_res(
@@ -1140,8 +1128,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
11401128
span: Span,
11411129
overloaded_callee: Option<Ty<'tcx>>,
11421130
) -> Expr<'tcx> {
1143-
let (temp_lifetime, backwards_incompatible) =
1144-
self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
1131+
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
11451132
let (ty, user_ty) = match overloaded_callee {
11461133
Some(fn_def) => (fn_def, None),
11471134
None => {
@@ -1157,12 +1144,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
11571144
)
11581145
}
11591146
};
1160-
Expr {
1161-
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1162-
ty,
1163-
span,
1164-
kind: ExprKind::ZstLiteral { user_ty },
1165-
}
1147+
Expr { temp_lifetime, ty, span, kind: ExprKind::ZstLiteral { user_ty } }
11661148
}
11671149

11681150
fn convert_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) -> ArmId {
@@ -1235,21 +1217,15 @@ impl<'tcx> ThirBuildCx<'tcx> {
12351217
Res::Def(DefKind::Static { .. }, id) => {
12361218
// this is &raw for extern static or static mut, and & for other statics
12371219
let ty = self.tcx.static_ptr_ty(id, self.typing_env);
1238-
let (temp_lifetime, backwards_incompatible) =
1239-
self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
1220+
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
12401221
let kind = if self.tcx.is_thread_local_static(id) {
12411222
ExprKind::ThreadLocalRef(id)
12421223
} else {
12431224
let alloc_id = self.tcx.reserve_and_set_static_alloc(id);
12441225
ExprKind::StaticRef { alloc_id, ty, def_id: id }
12451226
};
12461227
ExprKind::Deref {
1247-
arg: self.thir.exprs.push(Expr {
1248-
ty,
1249-
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1250-
span: expr.span,
1251-
kind,
1252-
}),
1228+
arg: self.thir.exprs.push(Expr { ty, temp_lifetime, span: expr.span, kind }),
12531229
}
12541230
}
12551231

@@ -1317,13 +1293,12 @@ impl<'tcx> ThirBuildCx<'tcx> {
13171293

13181294
// construct the complete expression `foo()` for the overloaded call,
13191295
// which will yield the &T type
1320-
let (temp_lifetime, backwards_incompatible) =
1321-
self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
1296+
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
13221297
let fun = self.method_callee(expr, span, overloaded_callee);
13231298
let fun = self.thir.exprs.push(fun);
13241299
let fun_ty = self.thir[fun].ty;
13251300
let ref_expr = self.thir.exprs.push(Expr {
1326-
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1301+
temp_lifetime,
13271302
ty: ref_ty,
13281303
span,
13291304
kind: ExprKind::Call { ty: fun_ty, fun, args, from_hir_call: false, fn_span: span },
@@ -1338,8 +1313,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
13381313
closure_expr: &'tcx hir::Expr<'tcx>,
13391314
place: HirPlace<'tcx>,
13401315
) -> Expr<'tcx> {
1341-
let (temp_lifetime, backwards_incompatible) =
1342-
self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
1316+
let temp_lifetime = self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
13431317
let var_ty = place.base_ty;
13441318

13451319
// The result of capture analysis in `rustc_hir_typeck/src/upvar.rs` represents a captured path
@@ -1353,7 +1327,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
13531327
};
13541328

13551329
let mut captured_place_expr = Expr {
1356-
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1330+
temp_lifetime,
13571331
ty: var_ty,
13581332
span: closure_expr.span,
13591333
kind: self.convert_var(var_hir_id),
@@ -1381,12 +1355,8 @@ impl<'tcx> ThirBuildCx<'tcx> {
13811355
}
13821356
};
13831357

1384-
captured_place_expr = Expr {
1385-
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1386-
ty: proj.ty,
1387-
span: closure_expr.span,
1388-
kind,
1389-
};
1358+
captured_place_expr =
1359+
Expr { temp_lifetime, ty: proj.ty, span: closure_expr.span, kind };
13901360
}
13911361

13921362
captured_place_expr
@@ -1401,8 +1371,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
14011371
let upvar_capture = captured_place.info.capture_kind;
14021372
let captured_place_expr =
14031373
self.convert_captured_hir_place(closure_expr, captured_place.place.clone());
1404-
let (temp_lifetime, backwards_incompatible) =
1405-
self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
1374+
let temp_lifetime = self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
14061375

14071376
match upvar_capture {
14081377
ty::UpvarCapture::ByValue => captured_place_expr,
@@ -1411,7 +1380,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
14111380
let expr_id = self.thir.exprs.push(captured_place_expr);
14121381

14131382
Expr {
1414-
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1383+
temp_lifetime,
14151384
ty: upvar_ty,
14161385
span: closure_expr.span,
14171386
kind: ExprKind::ByUse { expr: expr_id, span },
@@ -1428,7 +1397,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
14281397
}
14291398
};
14301399
Expr {
1431-
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1400+
temp_lifetime,
14321401
ty: upvar_ty,
14331402
span: closure_expr.span,
14341403
kind: ExprKind::Borrow {

0 commit comments

Comments
 (0)