Skip to content

Commit e49e5b9

Browse files
committed
Start a new version of generator liveness.
1 parent b0ee495 commit e49e5b9

File tree

6 files changed

+58
-8
lines changed

6 files changed

+58
-8
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4506,6 +4506,7 @@ dependencies = [
45064506
"rustc_lint",
45074507
"rustc_macros",
45084508
"rustc_middle",
4509+
"rustc_passes",
45094510
"rustc_session",
45104511
"rustc_span",
45114512
"rustc_target",

compiler/rustc_passes/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mod intrinsicck;
3232
mod lang_items;
3333
pub mod layout_test;
3434
mod lib_features;
35-
mod liveness;
35+
pub mod liveness;
3636
pub mod loops;
3737
mod naked_functions;
3838
mod reachable;

compiler/rustc_passes/src/liveness.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,24 @@ struct CaptureInfo {
176176
}
177177

178178
#[derive(Copy, Clone, Debug)]
179-
struct LocalInfo {
179+
pub struct LocalInfo {
180180
id: HirId,
181181
name: Symbol,
182182
is_shorthand: bool,
183183
}
184184

185185
#[derive(Copy, Clone, Debug)]
186-
enum VarKind {
186+
pub enum VarKind {
187187
Param(HirId, Symbol),
188188
Local(LocalInfo),
189189
Upvar(HirId, Symbol),
190+
/// A virtual variable for a temporary expression.
191+
///
192+
/// Used primarily by generator_interior.
193+
Temporary(HirId),
190194
}
191195

192-
struct IrMaps<'tcx> {
196+
pub struct IrMaps<'tcx> {
193197
tcx: TyCtxt<'tcx>,
194198
live_node_map: HirIdMap<LiveNode>,
195199
variable_map: HirIdMap<Variable>,
@@ -225,11 +229,14 @@ impl IrMaps<'tcx> {
225229
debug!("{:?} is node {:?}", ln, hir_id);
226230
}
227231

228-
fn add_variable(&mut self, vk: VarKind) -> Variable {
232+
pub fn add_variable(&mut self, vk: VarKind) -> Variable {
229233
let v = self.var_kinds.push(vk);
230234

231235
match vk {
232-
Local(LocalInfo { id: node_id, .. }) | Param(node_id, _) | Upvar(node_id, _) => {
236+
Local(LocalInfo { id: node_id, .. })
237+
| Param(node_id, _)
238+
| Upvar(node_id, _)
239+
| Temporary(node_id) => {
233240
self.variable_map.insert(node_id, v);
234241
}
235242
}
@@ -251,13 +258,14 @@ impl IrMaps<'tcx> {
251258
fn variable_name(&self, var: Variable) -> Symbol {
252259
match self.var_kinds[var] {
253260
Local(LocalInfo { name, .. }) | Param(_, name) | Upvar(_, name) => name,
261+
Temporary(..) => Symbol::intern("[temporary]"),
254262
}
255263
}
256264

257265
fn variable_is_shorthand(&self, var: Variable) -> bool {
258266
match self.var_kinds[var] {
259267
Local(LocalInfo { is_shorthand, .. }) => is_shorthand,
260-
Param(..) | Upvar(..) => false,
268+
Param(..) | Upvar(..) | Temporary(..) => false,
261269
}
262270
}
263271

@@ -485,7 +493,7 @@ const ACC_READ: u32 = 1;
485493
const ACC_WRITE: u32 = 2;
486494
const ACC_USE: u32 = 4;
487495

488-
struct Liveness<'a, 'tcx> {
496+
pub struct Liveness<'a, 'tcx> {
489497
ir: &'a mut IrMaps<'tcx>,
490498
typeck_results: &'a ty::TypeckResults<'tcx>,
491499
param_env: ty::ParamEnv<'tcx>,

compiler/rustc_typeck/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ rustc_data_structures = { path = "../rustc_data_structures" }
1717
rustc_errors = { path = "../rustc_errors" }
1818
rustc_hir = { path = "../rustc_hir" }
1919
rustc_hir_pretty = { path = "../rustc_hir_pretty" }
20+
rustc_passes = { path = "../rustc_passes" }
2021
rustc_target = { path = "../rustc_target" }
2122
rustc_session = { path = "../rustc_session" }
2223
smallvec = { version = "1.6.1", features = ["union", "may_dangle"] }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use crate::expr_use_visitor;
2+
use rustc_passes::liveness::{IrMaps, VarKind};
3+
4+
use VarKind::*;
5+
6+
impl<'tcx> expr_use_visitor::Delegate<'tcx> for IrMaps<'tcx> {
7+
fn consume(
8+
&mut self,
9+
place_with_id: &rustc_middle::hir::place::PlaceWithHirId<'tcx>,
10+
_diag_expr_id: rustc_hir::HirId,
11+
) {
12+
self.add_variable(Temporary(place_with_id.hir_id));
13+
}
14+
15+
fn borrow(
16+
&mut self,
17+
place_with_id: &rustc_middle::hir::place::PlaceWithHirId<'tcx>,
18+
_diag_expr_id: rustc_hir::HirId,
19+
_bk: rustc_middle::ty::BorrowKind,
20+
) {
21+
self.add_variable(Temporary(place_with_id.hir_id));
22+
}
23+
24+
fn mutate(
25+
&mut self,
26+
assignee_place: &rustc_middle::hir::place::PlaceWithHirId<'tcx>,
27+
_diag_expr_id: rustc_hir::HirId,
28+
) {
29+
self.add_variable(Temporary(assignee_place.hir_id));
30+
}
31+
32+
fn fake_read(
33+
&mut self,
34+
_place: rustc_middle::hir::place::Place<'tcx>,
35+
_cause: rustc_middle::mir::FakeReadCause,
36+
_diag_expr_id: rustc_hir::HirId,
37+
) {
38+
}
39+
}

compiler/rustc_typeck/src/check/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ mod fallback;
7979
mod fn_ctxt;
8080
mod gather_locals;
8181
mod generator_interior;
82+
mod generator_liveness;
8283
mod inherited;
8384
pub mod intrinsic;
8485
pub mod method;

0 commit comments

Comments
 (0)