Skip to content

Commit e64f64a

Browse files
committed
rustc: separate bodies for static/(associated)const and embedded constants.
1 parent 8649282 commit e64f64a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+637
-655
lines changed

src/librustc/cfg/construct.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,6 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
327327
self.opt_expr(base, field_cfg)
328328
}
329329

330-
hir::ExprRepeat(ref elem, ref count) => {
331-
self.straightline(expr, pred, [elem, count].iter().map(|&e| &**e))
332-
}
333-
334330
hir::ExprAssign(ref l, ref r) |
335331
hir::ExprAssignOp(_, ref l, ref r) => {
336332
self.straightline(expr, pred, [r, l].iter().map(|&e| &**e))
@@ -347,7 +343,8 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
347343
hir::ExprType(ref e, _) |
348344
hir::ExprUnary(_, ref e) |
349345
hir::ExprField(ref e, _) |
350-
hir::ExprTupField(ref e, _) => {
346+
hir::ExprTupField(ref e, _) |
347+
hir::ExprRepeat(ref e, _) => {
351348
self.straightline(expr, pred, Some(&**e).into_iter())
352349
}
353350

src/librustc/dep_graph/visit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ pub fn visit_all_item_likes_in_krate<'a, 'tcx, V, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>
5050
let task_id = (self.dep_node_fn)(trait_item_def_id);
5151
let _task = self.tcx.dep_graph.in_task(task_id.clone());
5252
debug!("Started task {:?}", task_id);
53-
assert!(!self.tcx.map.is_inlined_def_id(trait_item_def_id));
5453
self.tcx.dep_graph.read(DepNode::Hir(trait_item_def_id));
5554
self.visitor.visit_trait_item(i);
5655
debug!("Ended task {:?}", task_id);

src/librustc/hir/intravisit.rs

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ pub trait Visitor<'v> : Sized {
203203
/// visit_nested_item, does nothing by default unless you override
204204
/// `nested_visit_map` to return `Some(_)`, in which case it will walk the
205205
/// body.
206-
fn visit_body(&mut self, id: ExprId) {
207-
let opt_expr = self.nested_visit_map().intra().map(|map| map.expr(id));
208-
if let Some(expr) = opt_expr {
209-
self.visit_expr(expr);
206+
fn visit_nested_body(&mut self, id: BodyId) {
207+
let opt_body = self.nested_visit_map().intra().map(|map| map.body(id));
208+
if let Some(body) = opt_body {
209+
self.visit_body(body);
210210
}
211211
}
212212

@@ -216,6 +216,10 @@ pub trait Visitor<'v> : Sized {
216216
walk_item(self, i)
217217
}
218218

219+
fn visit_body(&mut self, b: &'v Body) {
220+
walk_body(self, b);
221+
}
222+
219223
/// When invoking `visit_all_item_likes()`, you need to supply an
220224
/// item-like visitor. This method converts a "intra-visit"
221225
/// visitor into an item-like visitor that walks the entire tree.
@@ -264,8 +268,6 @@ pub trait Visitor<'v> : Sized {
264268
fn visit_expr(&mut self, ex: &'v Expr) {
265269
walk_expr(self, ex)
266270
}
267-
fn visit_expr_post(&mut self, _ex: &'v Expr) {
268-
}
269271
fn visit_ty(&mut self, t: &'v Ty) {
270272
walk_ty(self, t)
271273
}
@@ -278,7 +280,7 @@ pub trait Visitor<'v> : Sized {
278280
fn visit_fn_decl(&mut self, fd: &'v FnDecl) {
279281
walk_fn_decl(self, fd)
280282
}
281-
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: ExprId, s: Span, id: NodeId) {
283+
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: BodyId, s: Span, id: NodeId) {
282284
walk_fn(self, fk, fd, b, s, id)
283285
}
284286
fn visit_trait_item(&mut self, ti: &'v TraitItem) {
@@ -392,6 +394,10 @@ pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod, mod_node_i
392394
}
393395
}
394396

397+
pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body) {
398+
visitor.visit_expr(&body.value);
399+
}
400+
395401
pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
396402
visitor.visit_id(local.id);
397403
visitor.visit_pat(&local.pat);
@@ -437,11 +443,11 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
437443
visitor.visit_id(item.id);
438444
visitor.visit_path(path, item.id);
439445
}
440-
ItemStatic(ref typ, _, ref expr) |
441-
ItemConst(ref typ, ref expr) => {
446+
ItemStatic(ref typ, _, body) |
447+
ItemConst(ref typ, body) => {
442448
visitor.visit_id(item.id);
443449
visitor.visit_ty(typ);
444-
visitor.visit_expr(expr);
450+
visitor.visit_nested_body(body);
445451
}
446452
ItemFn(ref declaration, unsafety, constness, abi, ref generics, body_id) => {
447453
visitor.visit_fn(FnKind::ItemFn(item.name,
@@ -523,7 +529,7 @@ pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
523529
generics,
524530
parent_item_id,
525531
variant.span);
526-
walk_list!(visitor, visit_expr, &variant.node.disr_expr);
532+
walk_list!(visitor, visit_nested_body, variant.node.disr_expr);
527533
walk_list!(visitor, visit_attribute, &variant.node.attrs);
528534
}
529535

@@ -556,18 +562,18 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
556562
visitor.visit_ty(ty);
557563
walk_list!(visitor, visit_ty_param_bound, bounds);
558564
}
559-
TyArray(ref ty, ref expression) => {
565+
TyArray(ref ty, length) => {
560566
visitor.visit_ty(ty);
561-
visitor.visit_expr(expression)
567+
visitor.visit_nested_body(length)
562568
}
563569
TyPolyTraitRef(ref bounds) => {
564570
walk_list!(visitor, visit_ty_param_bound, bounds);
565571
}
566572
TyImplTrait(ref bounds) => {
567573
walk_list!(visitor, visit_ty_param_bound, bounds);
568574
}
569-
TyTypeof(ref expression) => {
570-
visitor.visit_expr(expression)
575+
TyTypeof(expression) => {
576+
visitor.visit_nested_body(expression)
571577
}
572578
TyInfer => {}
573579
}
@@ -775,35 +781,23 @@ pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'
775781
pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
776782
function_kind: FnKind<'v>,
777783
function_declaration: &'v FnDecl,
778-
body_id: ExprId,
784+
body_id: BodyId,
779785
_span: Span,
780786
id: NodeId) {
781787
visitor.visit_id(id);
782788
visitor.visit_fn_decl(function_declaration);
783789
walk_fn_kind(visitor, function_kind);
784-
visitor.visit_body(body_id)
785-
}
786-
787-
pub fn walk_fn_with_body<'v, V: Visitor<'v>>(visitor: &mut V,
788-
function_kind: FnKind<'v>,
789-
function_declaration: &'v FnDecl,
790-
body: &'v Expr,
791-
_span: Span,
792-
id: NodeId) {
793-
visitor.visit_id(id);
794-
visitor.visit_fn_decl(function_declaration);
795-
walk_fn_kind(visitor, function_kind);
796-
visitor.visit_expr(body)
790+
visitor.visit_nested_body(body_id)
797791
}
798792

799793
pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
800794
visitor.visit_name(trait_item.span, trait_item.name);
801795
walk_list!(visitor, visit_attribute, &trait_item.attrs);
802796
match trait_item.node {
803-
TraitItemKind::Const(ref ty, ref default) => {
797+
TraitItemKind::Const(ref ty, default) => {
804798
visitor.visit_id(trait_item.id);
805799
visitor.visit_ty(ty);
806-
walk_list!(visitor, visit_expr, default);
800+
walk_list!(visitor, visit_nested_body, default);
807801
}
808802
TraitItemKind::Method(ref sig, None) => {
809803
visitor.visit_id(trait_item.id);
@@ -846,10 +840,10 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
846840
visitor.visit_defaultness(defaultness);
847841
walk_list!(visitor, visit_attribute, attrs);
848842
match *node {
849-
ImplItemKind::Const(ref ty, ref expr) => {
843+
ImplItemKind::Const(ref ty, body) => {
850844
visitor.visit_id(impl_item.id);
851845
visitor.visit_ty(ty);
852-
visitor.visit_expr(expr);
846+
visitor.visit_nested_body(body);
853847
}
854848
ImplItemKind::Method(ref sig, body_id) => {
855849
visitor.visit_fn(FnKind::Method(impl_item.name,
@@ -928,9 +922,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
928922
ExprArray(ref subexpressions) => {
929923
walk_list!(visitor, visit_expr, subexpressions);
930924
}
931-
ExprRepeat(ref element, ref count) => {
925+
ExprRepeat(ref element, count) => {
932926
visitor.visit_expr(element);
933-
visitor.visit_expr(count)
927+
visitor.visit_nested_body(count)
934928
}
935929
ExprStruct(ref qpath, ref fields, ref optional_base) => {
936930
visitor.visit_qpath(qpath, expression.id, expression.span);
@@ -1037,8 +1031,6 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
10371031
}
10381032
}
10391033
}
1040-
1041-
visitor.visit_expr_post(expression)
10421034
}
10431035

10441036
pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
@@ -1125,12 +1117,12 @@ impl<'a, 'ast> Visitor<'ast> for IdRangeComputingVisitor<'a, 'ast> {
11251117
/// Computes the id range for a single fn body, ignoring nested items.
11261118
pub fn compute_id_range_for_fn_body<'v>(fk: FnKind<'v>,
11271119
decl: &'v FnDecl,
1128-
body: &'v Expr,
1120+
body: BodyId,
11291121
sp: Span,
11301122
id: NodeId,
11311123
map: &map::Map<'v>)
11321124
-> IdRange {
11331125
let mut visitor = IdRangeComputingVisitor::new(map);
1134-
walk_fn_with_body(&mut visitor, fk, decl, body, sp, id);
1126+
visitor.visit_fn(fk, decl, body, sp, id);
11351127
visitor.result()
11361128
}

src/librustc/hir/lowering.rs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ use hir::map::definitions::DefPathData;
4646
use hir::def_id::{DefIndex, DefId};
4747
use hir::def::{Def, PathResolution};
4848
use session::Session;
49-
use util::nodemap::NodeMap;
50-
use rustc_data_structures::fnv::FnvHashMap;
49+
use util::nodemap::{NodeMap, FxHashMap};
5150

5251
use std::collections::BTreeMap;
5352
use std::iter;
@@ -70,14 +69,14 @@ pub struct LoweringContext<'a> {
7069
// the form of a DefIndex) so that if we create a new node which introduces
7170
// a definition, then we can properly create the def id.
7271
parent_def: Option<DefIndex>,
73-
exprs: FnvHashMap<hir::ExprId, hir::Expr>,
7472
resolver: &'a mut Resolver,
7573

7674
/// The items being lowered are collected here.
7775
items: BTreeMap<NodeId, hir::Item>,
7876

7977
trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem>,
8078
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem>,
79+
bodies: FxHashMap<hir::BodyId, hir::Body>,
8180
}
8281

8382
pub trait Resolver {
@@ -105,11 +104,11 @@ pub fn lower_crate(sess: &Session,
105104
crate_root: std_inject::injected_crate_name(krate),
106105
sess: sess,
107106
parent_def: None,
108-
exprs: FnvHashMap(),
109107
resolver: resolver,
110108
items: BTreeMap::new(),
111109
trait_items: BTreeMap::new(),
112110
impl_items: BTreeMap::new(),
111+
bodies: FxHashMap(),
113112
}.lower_crate(krate)
114113
}
115114

@@ -136,7 +135,7 @@ impl<'a> LoweringContext<'a> {
136135
items: self.items,
137136
trait_items: self.trait_items,
138137
impl_items: self.impl_items,
139-
exprs: self.exprs,
138+
bodies: self.bodies,
140139
}
141140
}
142141

@@ -171,9 +170,12 @@ impl<'a> LoweringContext<'a> {
171170
visit::walk_crate(&mut item_lowerer, c);
172171
}
173172

174-
fn record_expr(&mut self, expr: hir::Expr) -> hir::ExprId {
175-
let id = hir::ExprId(expr.id);
176-
self.exprs.insert(id, expr);
173+
fn record_body(&mut self, value: hir::Expr) -> hir::BodyId {
174+
let body = hir::Body {
175+
value: value
176+
};
177+
let id = body.id();
178+
self.bodies.insert(id, body);
177179
id
178180
}
179181

@@ -305,11 +307,14 @@ impl<'a> LoweringContext<'a> {
305307
TyKind::ObjectSum(ref ty, ref bounds) => {
306308
hir::TyObjectSum(self.lower_ty(ty), self.lower_bounds(bounds))
307309
}
308-
TyKind::Array(ref ty, ref e) => {
309-
hir::TyArray(self.lower_ty(ty), P(self.lower_expr(e)))
310+
TyKind::Array(ref ty, ref length) => {
311+
let length = self.lower_expr(length);
312+
hir::TyArray(self.lower_ty(ty),
313+
self.record_body(length))
310314
}
311315
TyKind::Typeof(ref expr) => {
312-
hir::TyTypeof(P(self.lower_expr(expr)))
316+
let expr = self.lower_expr(expr);
317+
hir::TyTypeof(self.record_body(expr))
313318
}
314319
TyKind::PolyTraitRef(ref bounds) => {
315320
hir::TyPolyTraitRef(self.lower_bounds(bounds))
@@ -336,7 +341,10 @@ impl<'a> LoweringContext<'a> {
336341
name: v.node.name.name,
337342
attrs: self.lower_attrs(&v.node.attrs),
338343
data: self.lower_variant_data(&v.node.data),
339-
disr_expr: v.node.disr_expr.as_ref().map(|e| P(self.lower_expr(e))),
344+
disr_expr: v.node.disr_expr.as_ref().map(|e| {
345+
let e = self.lower_expr(e);
346+
self.record_body(e)
347+
}),
340348
},
341349
span: v.span,
342350
}
@@ -858,17 +866,20 @@ impl<'a> LoweringContext<'a> {
858866
hir::ItemUse(path, kind)
859867
}
860868
ItemKind::Static(ref t, m, ref e) => {
869+
let value = self.lower_expr(e);
861870
hir::ItemStatic(self.lower_ty(t),
862871
self.lower_mutability(m),
863-
P(self.lower_expr(e)))
872+
self.record_body(value))
864873
}
865874
ItemKind::Const(ref t, ref e) => {
866-
hir::ItemConst(self.lower_ty(t), P(self.lower_expr(e)))
875+
let value = self.lower_expr(e);
876+
hir::ItemConst(self.lower_ty(t),
877+
self.record_body(value))
867878
}
868879
ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
869880
let body = self.lower_block(body);
870881
let body = self.expr_block(body, ThinVec::new());
871-
let body_id = self.record_expr(body);
882+
let body_id = self.record_body(body);
872883
hir::ItemFn(self.lower_fn_decl(decl),
873884
self.lower_unsafety(unsafety),
874885
self.lower_constness(constness),
@@ -935,14 +946,17 @@ impl<'a> LoweringContext<'a> {
935946
node: match i.node {
936947
TraitItemKind::Const(ref ty, ref default) => {
937948
hir::TraitItemKind::Const(this.lower_ty(ty),
938-
default.as_ref().map(|x| P(this.lower_expr(x))))
949+
default.as_ref().map(|x| {
950+
let value = this.lower_expr(x);
951+
this.record_body(value)
952+
}))
939953
}
940954
TraitItemKind::Method(ref sig, ref body) => {
941955
hir::TraitItemKind::Method(this.lower_method_sig(sig),
942956
body.as_ref().map(|x| {
943957
let body = this.lower_block(x);
944958
let expr = this.expr_block(body, ThinVec::new());
945-
this.record_expr(expr)
959+
this.record_body(expr)
946960
}))
947961
}
948962
TraitItemKind::Type(ref bounds, ref default) => {
@@ -990,13 +1004,15 @@ impl<'a> LoweringContext<'a> {
9901004
defaultness: this.lower_defaultness(i.defaultness, true /* [1] */),
9911005
node: match i.node {
9921006
ImplItemKind::Const(ref ty, ref expr) => {
993-
hir::ImplItemKind::Const(this.lower_ty(ty), P(this.lower_expr(expr)))
1007+
let value = this.lower_expr(expr);
1008+
let body_id = this.record_body(value);
1009+
hir::ImplItemKind::Const(this.lower_ty(ty), body_id)
9941010
}
9951011
ImplItemKind::Method(ref sig, ref body) => {
9961012
let body = this.lower_block(body);
9971013
let expr = this.expr_block(body, ThinVec::new());
998-
let expr_id = this.record_expr(expr);
999-
hir::ImplItemKind::Method(this.lower_method_sig(sig), expr_id)
1014+
let body_id = this.record_body(expr);
1015+
hir::ImplItemKind::Method(this.lower_method_sig(sig), body_id)
10001016
}
10011017
ImplItemKind::Type(ref ty) => hir::ImplItemKind::Type(this.lower_ty(ty)),
10021018
ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
@@ -1350,8 +1366,8 @@ impl<'a> LoweringContext<'a> {
13501366
}
13511367
ExprKind::Repeat(ref expr, ref count) => {
13521368
let expr = P(self.lower_expr(expr));
1353-
let count = P(self.lower_expr(count));
1354-
hir::ExprRepeat(expr, count)
1369+
let count = self.lower_expr(count);
1370+
hir::ExprRepeat(expr, self.record_body(count))
13551371
}
13561372
ExprKind::Tup(ref elts) => {
13571373
hir::ExprTup(elts.iter().map(|x| self.lower_expr(x)).collect())
@@ -1434,7 +1450,7 @@ impl<'a> LoweringContext<'a> {
14341450
let expr = this.lower_expr(body);
14351451
hir::ExprClosure(this.lower_capture_clause(capture_clause),
14361452
this.lower_fn_decl(decl),
1437-
this.record_expr(expr),
1453+
this.record_body(expr),
14381454
fn_decl_span)
14391455
})
14401456
}

0 commit comments

Comments
 (0)