Skip to content

Commit

Permalink
Remove some unnecessary indirection from HIR structures
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Dec 7, 2015
1 parent ac0e845 commit ca88e9c
Show file tree
Hide file tree
Showing 26 changed files with 176 additions and 193 deletions.
2 changes: 1 addition & 1 deletion src/librustc/front/map/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
for v in &enum_definition.variants {
let variant_def_index =
self.insert_def(v.node.data.id(),
NodeVariant(&**v),
NodeVariant(v),
DefPathData::EnumVariant(v.node.name));

for field in v.node.data.fields() {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/front/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,13 +852,13 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
II::Item(i) => II::Item(i.map(|i| fld.fold_item(i))),
II::TraitItem(d, ti) => {
II::TraitItem(fld.fold_ops.new_def_id(d),
fld.fold_trait_item(ti))
ti.map(|ti| fld.fold_trait_item(ti)))
}
II::ImplItem(d, ii) => {
II::ImplItem(fld.fold_ops.new_def_id(d),
fld.fold_impl_item(ii))
ii.map(|ii| fld.fold_impl_item(ii)))
}
II::Foreign(i) => II::Foreign(fld.fold_foreign_item(i))
II::Foreign(i) => II::Foreign(i.map(|i| fld.fold_foreign_item(i)))
};

let ii_parent = map.forest.inlined_items.alloc(InlinedParent {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/cfg/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
fn block(&mut self, blk: &hir::Block, pred: CFGIndex) -> CFGIndex {
let mut stmts_exit = pred;
for stmt in &blk.stmts {
stmts_exit = self.stmt(&**stmt, stmts_exit);
stmts_exit = self.stmt(stmt, stmts_exit);
}

let expr_exit = self.opt_expr(&blk.expr, stmts_exit);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn lookup_variant_by_id<'a>(tcx: &'a ty::ctxt,
enum_def: DefId,
variant_def: DefId)
-> Option<&'a Expr> {
fn variant_expr<'a>(variants: &'a [P<hir::Variant>], id: ast::NodeId)
fn variant_expr<'a>(variants: &'a [hir::Variant], id: ast::NodeId)
-> Option<&'a Expr> {
for variant in variants {
if variant.node.data.id() == id {
Expand All @@ -77,7 +77,7 @@ fn lookup_variant_by_id<'a>(tcx: &'a ty::ctxt,
None => None,
Some(ast_map::NodeItem(it)) => match it.node {
hir::ItemEnum(hir::EnumDef { ref variants }, _) => {
variant_expr(&variants[..], variant_node_id)
variant_expr(variants, variant_node_id)
}
_ => None
},
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
debug!("walk_block(blk.id={})", blk.id);

for stmt in &blk.stmts {
self.walk_stmt(&**stmt);
self.walk_stmt(stmt);
}

if let Some(ref tail_expr) = blk.expr {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1501,15 +1501,15 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
self.rebuild_arg_ty_or_output(&**t, lifetime, anon_nums, region_names)
});
let new_bindings = data.bindings.map(|b| {
P(hir::TypeBinding {
hir::TypeBinding {
id: b.id,
name: b.name,
ty: self.rebuild_arg_ty_or_output(&*b.ty,
lifetime,
anon_nums,
region_names),
span: b.span
})
}
});
hir::AngleBracketedParameters(hir::AngleBracketedParameterData {
lifetimes: new_lts,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
-> LiveNode {
let succ = self.propagate_through_opt_expr(blk.expr.as_ref().map(|e| &**e), succ);
blk.stmts.iter().rev().fold(succ, |succ, stmt| {
self.propagate_through_stmt(&**stmt, succ)
self.propagate_through_stmt(stmt, succ)
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ fn resolve_block(visitor: &mut RegionResolutionVisitor, blk: &hir::Block) {
parent: stmt_extent,
};
}
visitor.visit_stmt(&**statement)
visitor.visit_stmt(statement)
}
walk_list!(visitor, visit_expr, &blk.expr);
}
Expand Down
209 changes: 98 additions & 111 deletions src/librustc_front/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub trait Folder : Sized {
noop_fold_view_path(view_path, self)
}

fn fold_foreign_item(&mut self, ni: P<ForeignItem>) -> P<ForeignItem> {
fn fold_foreign_item(&mut self, ni: ForeignItem) -> ForeignItem {
noop_fold_foreign_item(ni, self)
}

Expand All @@ -67,11 +67,11 @@ pub trait Folder : Sized {
noop_fold_item_underscore(i, self)
}

fn fold_trait_item(&mut self, i: P<TraitItem>) -> P<TraitItem> {
fn fold_trait_item(&mut self, i: TraitItem) -> TraitItem {
noop_fold_trait_item(i, self)
}

fn fold_impl_item(&mut self, i: P<ImplItem>) -> P<ImplItem> {
fn fold_impl_item(&mut self, i: ImplItem) -> ImplItem {
noop_fold_impl_item(i, self)
}

Expand All @@ -83,7 +83,7 @@ pub trait Folder : Sized {
noop_fold_block(b, self)
}

fn fold_stmt(&mut self, s: P<Stmt>) -> P<Stmt> {
fn fold_stmt(&mut self, s: Stmt) -> Stmt {
noop_fold_stmt(s, self)
}

Expand All @@ -107,7 +107,7 @@ pub trait Folder : Sized {
noop_fold_ty(t, self)
}

fn fold_ty_binding(&mut self, t: P<TypeBinding>) -> P<TypeBinding> {
fn fold_ty_binding(&mut self, t: TypeBinding) -> TypeBinding {
noop_fold_ty_binding(t, self)
}

Expand All @@ -119,7 +119,7 @@ pub trait Folder : Sized {
noop_fold_foreign_mod(nm, self)
}

fn fold_variant(&mut self, v: P<Variant>) -> P<Variant> {
fn fold_variant(&mut self, v: Variant) -> Variant {
noop_fold_variant(v, self)
}

Expand Down Expand Up @@ -333,15 +333,13 @@ pub fn noop_fold_decl<T: Folder>(d: P<Decl>, fld: &mut T) -> P<Decl> {
})
}

pub fn noop_fold_ty_binding<T: Folder>(b: P<TypeBinding>, fld: &mut T) -> P<TypeBinding> {
b.map(|TypeBinding { id, name, ty, span }| {
TypeBinding {
id: fld.new_id(id),
name: name,
ty: fld.fold_ty(ty),
span: fld.new_span(span),
}
})
pub fn noop_fold_ty_binding<T: Folder>(b: TypeBinding, fld: &mut T) -> TypeBinding {
TypeBinding {
id: fld.new_id(b.id),
name: b.name,
ty: fld.fold_ty(b.ty),
span: fld.new_span(b.span),
}
}

pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
Expand Down Expand Up @@ -402,18 +400,16 @@ pub fn noop_fold_foreign_mod<T: Folder>(ForeignMod { abi, items }: ForeignMod,
}
}

pub fn noop_fold_variant<T: Folder>(v: P<Variant>, fld: &mut T) -> P<Variant> {
v.map(|Spanned { node: Variant_ { name, attrs, data, disr_expr }, span }| {
Spanned {
node: Variant_ {
name: name,
attrs: fold_attrs(attrs, fld),
data: fld.fold_variant_data(data),
disr_expr: disr_expr.map(|e| fld.fold_expr(e)),
},
span: fld.new_span(span),
}
})
pub fn noop_fold_variant<T: Folder>(v: Variant, fld: &mut T) -> Variant {
Spanned {
node: Variant_ {
name: v.node.name,
attrs: fold_attrs(v.node.attrs, fld),
data: fld.fold_variant_data(v.node.data),
disr_expr: v.node.disr_expr.map(|e| fld.fold_expr(e)),
},
span: fld.new_span(v.span),
}
}

pub fn noop_fold_name<T: Folder>(n: Name, _: &mut T) -> Name {
Expand Down Expand Up @@ -814,51 +810,47 @@ pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
}
}

pub fn noop_fold_trait_item<T: Folder>(i: P<TraitItem>,
pub fn noop_fold_trait_item<T: Folder>(i: TraitItem,
folder: &mut T)
-> P<TraitItem> {
i.map(|TraitItem { id, name, attrs, node, span }| {
TraitItem {
id: folder.new_id(id),
name: folder.fold_name(name),
attrs: fold_attrs(attrs, folder),
node: match node {
ConstTraitItem(ty, default) => {
ConstTraitItem(folder.fold_ty(ty), default.map(|x| folder.fold_expr(x)))
}
MethodTraitItem(sig, body) => {
MethodTraitItem(noop_fold_method_sig(sig, folder),
body.map(|x| folder.fold_block(x)))
}
TypeTraitItem(bounds, default) => {
TypeTraitItem(folder.fold_bounds(bounds),
default.map(|x| folder.fold_ty(x)))
}
},
span: folder.new_span(span),
}
})
-> TraitItem {
TraitItem {
id: folder.new_id(i.id),
name: folder.fold_name(i.name),
attrs: fold_attrs(i.attrs, folder),
node: match i.node {
ConstTraitItem(ty, default) => {
ConstTraitItem(folder.fold_ty(ty), default.map(|x| folder.fold_expr(x)))
}
MethodTraitItem(sig, body) => {
MethodTraitItem(noop_fold_method_sig(sig, folder),
body.map(|x| folder.fold_block(x)))
}
TypeTraitItem(bounds, default) => {
TypeTraitItem(folder.fold_bounds(bounds),
default.map(|x| folder.fold_ty(x)))
}
},
span: folder.new_span(i.span),
}
}

pub fn noop_fold_impl_item<T: Folder>(i: P<ImplItem>, folder: &mut T) -> P<ImplItem> {
i.map(|ImplItem { id, name, attrs, node, vis, span }| {
ImplItem {
id: folder.new_id(id),
name: folder.fold_name(name),
attrs: fold_attrs(attrs, folder),
vis: vis,
node: match node {
ImplItemKind::Const(ty, expr) => {
ImplItemKind::Const(folder.fold_ty(ty), folder.fold_expr(expr))
}
ImplItemKind::Method(sig, body) => {
ImplItemKind::Method(noop_fold_method_sig(sig, folder), folder.fold_block(body))
}
ImplItemKind::Type(ty) => ImplItemKind::Type(folder.fold_ty(ty)),
},
span: folder.new_span(span),
}
})
pub fn noop_fold_impl_item<T: Folder>(i: ImplItem, folder: &mut T) -> ImplItem {
ImplItem {
id: folder.new_id(i.id),
name: folder.fold_name(i.name),
attrs: fold_attrs(i.attrs, folder),
vis: i.vis,
node: match i.node {
ImplItemKind::Const(ty, expr) => {
ImplItemKind::Const(folder.fold_ty(ty), folder.fold_expr(expr))
}
ImplItemKind::Method(sig, body) => {
ImplItemKind::Method(noop_fold_method_sig(sig, folder), folder.fold_block(body))
}
ImplItemKind::Type(ty) => ImplItemKind::Type(folder.fold_ty(ty)),
},
span: folder.new_span(i.span),
}
}

pub fn noop_fold_mod<T: Folder>(Mod { inner, item_ids }: Mod, folder: &mut T) -> Mod {
Expand Down Expand Up @@ -935,24 +927,22 @@ pub fn noop_fold_item<T: Folder>(item: Item, folder: &mut T) -> Item {
}
}

pub fn noop_fold_foreign_item<T: Folder>(ni: P<ForeignItem>, folder: &mut T) -> P<ForeignItem> {
ni.map(|ForeignItem { id, name, attrs, node, span, vis }| {
ForeignItem {
id: folder.new_id(id),
name: folder.fold_name(name),
attrs: fold_attrs(attrs, folder),
node: match node {
ForeignItemFn(fdec, generics) => {
ForeignItemFn(folder.fold_fn_decl(fdec), folder.fold_generics(generics))
}
ForeignItemStatic(t, m) => {
ForeignItemStatic(folder.fold_ty(t), m)
}
},
vis: vis,
span: folder.new_span(span),
}
})
pub fn noop_fold_foreign_item<T: Folder>(ni: ForeignItem, folder: &mut T) -> ForeignItem {
ForeignItem {
id: folder.new_id(ni.id),
name: folder.fold_name(ni.name),
attrs: fold_attrs(ni.attrs, folder),
node: match ni.node {
ForeignItemFn(fdec, generics) => {
ForeignItemFn(folder.fold_fn_decl(fdec), folder.fold_generics(generics))
}
ForeignItemStatic(t, m) => {
ForeignItemStatic(folder.fold_ty(t), m)
}
},
vis: ni.vis,
span: folder.new_span(ni.span),
}
}

pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> MethodSig {
Expand Down Expand Up @@ -1147,32 +1137,29 @@ pub fn noop_fold_expr<T: Folder>(Expr { id, node, span, attrs }: Expr, folder: &
}
}

pub fn noop_fold_stmt<T: Folder>(stmt: P<Stmt>, folder: &mut T)
-> P<Stmt> {
stmt.map(|Spanned { node, span }| {
let span = folder.new_span(span);
match node {
StmtDecl(d, id) => {
let id = folder.new_id(id);
Spanned {
node: StmtDecl(folder.fold_decl(d), id),
span: span
}
pub fn noop_fold_stmt<T: Folder>(stmt: Stmt, folder: &mut T) -> Stmt {
let span = folder.new_span(stmt.span);
match stmt.node {
StmtDecl(d, id) => {
let id = folder.new_id(id);
Spanned {
node: StmtDecl(folder.fold_decl(d), id),
span: span
}
StmtExpr(e, id) => {
let id = folder.new_id(id);
Spanned {
node: StmtExpr(folder.fold_expr(e), id),
span: span,
}
}
StmtExpr(e, id) => {
let id = folder.new_id(id);
Spanned {
node: StmtExpr(folder.fold_expr(e), id),
span: span,
}
StmtSemi(e, id) => {
let id = folder.new_id(id);
Spanned {
node: StmtSemi(folder.fold_expr(e), id),
span: span,
}
}
StmtSemi(e, id) => {
let id = folder.new_id(id);
Spanned {
node: StmtSemi(folder.fold_expr(e), id),
span: span,
}
}
})
}
}
Loading

0 comments on commit ca88e9c

Please sign in to comment.