Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize and abstract ThinAttributes to ThinVec<Attribute> #34339

Merged
merged 1 commit into from
Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/librustc/hir/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use hir::*;
use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, Attribute, Attribute_, MetaItem};
use syntax::ast::MetaItemKind;
use syntax::attr::ThinAttributesExt;
use hir;
use syntax::codemap::{respan, Span, Spanned};
use syntax::ptr::P;
Expand Down Expand Up @@ -292,8 +291,11 @@ pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<
})
}

pub fn fold_attrs<T: Folder>(attrs: HirVec<Attribute>, fld: &mut T) -> HirVec<Attribute> {
attrs.move_flat_map(|x| fld.fold_attribute(x))
pub fn fold_attrs<T, F>(attrs: T, fld: &mut F) -> T
where T: Into<Vec<Attribute>> + From<Vec<Attribute>>,
F: Folder,
{
attrs.into().move_flat_map(|x| fld.fold_attribute(x)).into()
}

pub fn noop_fold_arm<T: Folder>(Arm { attrs, pats, guard, body }: Arm, fld: &mut T) -> Arm {
Expand Down Expand Up @@ -461,7 +463,7 @@ pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
pat: fld.fold_pat(pat),
init: init.map(|e| fld.fold_expr(e)),
span: fld.new_span(span),
attrs: attrs.map_thin_attrs(|attrs| fold_attrs(attrs.into(), fld).into()),
attrs: fold_attrs(attrs, fld),
}
})
}
Expand Down Expand Up @@ -1078,7 +1080,7 @@ pub fn noop_fold_expr<T: Folder>(Expr { id, node, span, attrs }: Expr, folder: &
}
},
span: folder.new_span(span),
attrs: attrs.map_thin_attrs(|attrs| fold_attrs(attrs.into(), folder).into()),
attrs: fold_attrs(attrs, folder),
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

use syntax::abi::Abi;
use syntax::ast::{NodeId, CRATE_NODE_ID, Name, Attribute};
use syntax::attr::ThinAttributesExt;
use syntax::codemap::{Span, Spanned};
use hir::*;

Expand Down Expand Up @@ -756,7 +755,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
walk_list!(visitor, visit_arm, arms);
}
ExprClosure(_, ref function_declaration, ref body, _fn_decl_span) => {
visitor.visit_fn(FnKind::Closure(expression.attrs.as_attr_slice()),
visitor.visit_fn(FnKind::Closure(&expression.attrs),
function_declaration,
body,
expression.span,
Expand Down
156 changes: 72 additions & 84 deletions src/librustc/hir/lowering.rs

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions src/librustc/hir/map/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use hir::map::{self, Node};
use syntax::abi;
use hir::{Block, FnDecl};
use syntax::ast::{Attribute, Name, NodeId};
use syntax::attr::ThinAttributesExt;
use hir as ast;
use syntax::codemap::Span;
use hir::intravisit::FnKind;
Expand Down Expand Up @@ -257,11 +256,7 @@ impl<'a> FnLikeNode<'a> {
}
map::NodeExpr(e) => match e.node {
ast::ExprClosure(_, ref decl, ref block, _fn_decl_span) =>
closure(ClosureParts::new(&decl,
&block,
e.id,
e.span,
e.attrs.as_attr_slice())),
closure(ClosureParts::new(&decl, &block, e.id, e.span, &e.attrs)),
_ => bug!("expr FnLikeNode that is not fn-like"),
},
_ => bug!("other FnLikeNode that is not fn-like"),
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndex};

use syntax::abi::Abi;
use syntax::ast::{self, Name, NodeId, DUMMY_NODE_ID, };
use syntax::attr::ThinAttributesExt;
use syntax::codemap::{Span, Spanned};
use syntax::visit;

Expand Down Expand Up @@ -577,7 +576,7 @@ impl<'ast> Map<'ast> {
Some(NodeTraitItem(ref ti)) => Some(&ti.attrs[..]),
Some(NodeImplItem(ref ii)) => Some(&ii.attrs[..]),
Some(NodeVariant(ref v)) => Some(&v.node.attrs[..]),
Some(NodeExpr(ref e)) => Some(e.attrs.as_attr_slice()),
Some(NodeExpr(ref e)) => Some(&*e.attrs),
Some(NodeStmt(ref s)) => Some(s.node.attrs()),
// unit/tuple structs take the attributes straight from
// the struct definition.
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ use syntax::codemap::{self, mk_sp, respan, Span, Spanned, ExpnId};
use syntax::abi::Abi;
use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, TokenTree, AsmDialect};
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
use syntax::attr::{ThinAttributes, ThinAttributesExt};
use syntax::parse::token::{keywords, InternedString};
use syntax::ptr::P;
use syntax::util::ThinVec;

use std::collections::BTreeMap;
use std::fmt;
Expand Down Expand Up @@ -732,7 +732,7 @@ impl Stmt_ {
match *self {
StmtDecl(ref d, _) => d.node.attrs(),
StmtExpr(ref e, _) |
StmtSemi(ref e, _) => e.attrs.as_attr_slice(),
StmtSemi(ref e, _) => &e.attrs,
}
}

Expand All @@ -756,7 +756,7 @@ pub struct Local {
pub init: Option<P<Expr>>,
pub id: NodeId,
pub span: Span,
pub attrs: ThinAttributes,
pub attrs: ThinVec<Attribute>,
}

pub type Decl = Spanned<Decl_>;
Expand All @@ -772,7 +772,7 @@ pub enum Decl_ {
impl Decl_ {
pub fn attrs(&self) -> &[Attribute] {
match *self {
DeclLocal(ref l) => l.attrs.as_attr_slice(),
DeclLocal(ref l) => &l.attrs,
DeclItem(_) => &[]
}
}
Expand Down Expand Up @@ -817,7 +817,7 @@ pub struct Expr {
pub id: NodeId,
pub node: Expr_,
pub span: Span,
pub attrs: ThinAttributes,
pub attrs: ThinVec<Attribute>,
}

impl fmt::Debug for Expr {
Expand Down
9 changes: 4 additions & 5 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ use syntax::codemap::Span;
use syntax::errors::DiagnosticBuilder;
use syntax::parse::token::InternedString;
use syntax::ast;
use syntax::attr::ThinAttributesExt;
use hir;
use hir::intravisit as hir_visit;
use hir::intravisit::{IdVisitor, IdVisitingOperation};
Expand Down Expand Up @@ -767,7 +766,7 @@ impl<'a, 'tcx, 'v> hir_visit::Visitor<'v> for LateContext<'a, 'tcx> {
}

fn visit_expr(&mut self, e: &hir::Expr) {
self.with_lint_attrs(e.attrs.as_attr_slice(), |cx| {
self.with_lint_attrs(&e.attrs, |cx| {
run_lints!(cx, check_expr, late_passes, e);
hir_visit::walk_expr(cx, e);
})
Expand Down Expand Up @@ -832,7 +831,7 @@ impl<'a, 'tcx, 'v> hir_visit::Visitor<'v> for LateContext<'a, 'tcx> {
}

fn visit_local(&mut self, l: &hir::Local) {
self.with_lint_attrs(l.attrs.as_attr_slice(), |cx| {
self.with_lint_attrs(&l.attrs, |cx| {
run_lints!(cx, check_local, late_passes, l);
hir_visit::walk_local(cx, l);
})
Expand Down Expand Up @@ -928,7 +927,7 @@ impl<'a, 'v> ast_visit::Visitor<'v> for EarlyContext<'a> {
}

fn visit_expr(&mut self, e: &ast::Expr) {
self.with_lint_attrs(e.attrs.as_attr_slice(), |cx| {
self.with_lint_attrs(&e.attrs, |cx| {
run_lints!(cx, check_expr, early_passes, e);
ast_visit::walk_expr(cx, e);
})
Expand Down Expand Up @@ -988,7 +987,7 @@ impl<'a, 'v> ast_visit::Visitor<'v> for EarlyContext<'a> {
}

fn visit_local(&mut self, l: &ast::Local) {
self.with_lint_attrs(l.attrs.as_attr_slice(), |cx| {
self.with_lint_attrs(&l.attrs, |cx| {
run_lints!(cx, check_local, early_passes, l);
ast_visit::walk_local(cx, l);
})
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_const_eval/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ fn const_val_to_expr(value: &ConstVal) -> P<hir::Expr> {
id: 0,
node: hir::ExprLit(P(Spanned { node: node, span: DUMMY_SP })),
span: DUMMY_SP,
attrs: None,
attrs: ast::ThinVec::new(),
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_driver/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ impl fold::Folder for ReplaceBodyWithLoop {
node: ast::ExprKind::Loop(empty_block, None),
id: ast::DUMMY_NODE_ID,
span: codemap::DUMMY_SP,
attrs: None,
attrs: ast::ThinVec::new(),
});

expr_to_block(b.rules, Some(loop_expr))
Expand Down
9 changes: 5 additions & 4 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ pub use self::TyParamBound::*;
pub use self::UnsafeSource::*;
pub use self::ViewPath_::*;
pub use self::PathParameters::*;
pub use util::ThinVec;

use attr::{ThinAttributes, HasAttrs};
use attr::HasAttrs;
use codemap::{mk_sp, respan, Span, Spanned, DUMMY_SP, ExpnId};
use abi::Abi;
use errors;
Expand Down Expand Up @@ -809,7 +810,7 @@ pub enum StmtKind {
/// Expr with trailing semi-colon (may have any type):
Semi(P<Expr>, NodeId),

Mac(P<Mac>, MacStmtStyle, ThinAttributes),
Mac(P<Mac>, MacStmtStyle, ThinVec<Attribute>),
}

impl StmtKind {
Expand Down Expand Up @@ -851,7 +852,7 @@ pub struct Local {
pub init: Option<P<Expr>>,
pub id: NodeId,
pub span: Span,
pub attrs: ThinAttributes,
pub attrs: ThinVec<Attribute>,
}

impl Local {
Expand Down Expand Up @@ -912,7 +913,7 @@ pub struct Expr {
pub id: NodeId,
pub node: ExprKind,
pub span: Span,
pub attrs: ThinAttributes
pub attrs: ThinVec<Attribute>
}

impl Expr {
Expand Down
87 changes: 7 additions & 80 deletions src/libsyntax/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
use parse::token::InternedString;
use parse::{ParseSess, token};
use ptr::P;
use util::ThinVec;

use std::cell::{RefCell, Cell};
use std::collections::HashSet;
Expand Down Expand Up @@ -803,80 +804,6 @@ impl IntType {
}
}

/// A list of attributes, behind a optional box as
/// a space optimization.
pub type ThinAttributes = Option<Box<Vec<Attribute>>>;

pub trait ThinAttributesExt {
fn map_thin_attrs<F>(self, f: F) -> Self
where F: FnOnce(Vec<Attribute>) -> Vec<Attribute>;
fn prepend(mut self, attrs: Self) -> Self;
fn append(mut self, attrs: Self) -> Self;
fn update<F>(&mut self, f: F)
where Self: Sized,
F: FnOnce(Self) -> Self;
fn as_attr_slice(&self) -> &[Attribute];
fn into_attr_vec(self) -> Vec<Attribute>;
}

impl ThinAttributesExt for ThinAttributes {
fn map_thin_attrs<F>(self, f: F) -> Self
where F: FnOnce(Vec<Attribute>) -> Vec<Attribute>
{
f(self.map(|b| *b).unwrap_or(Vec::new())).into_thin_attrs()
}

fn prepend(self, attrs: ThinAttributes) -> Self {
attrs.map_thin_attrs(|mut attrs| {
attrs.extend(self.into_attr_vec());
attrs
})
}

fn append(self, attrs: ThinAttributes) -> Self {
self.map_thin_attrs(|mut self_| {
self_.extend(attrs.into_attr_vec());
self_
})
}

fn update<F>(&mut self, f: F)
where Self: Sized,
F: FnOnce(ThinAttributes) -> ThinAttributes
{
let self_ = f(self.take());
*self = self_;
}

fn as_attr_slice(&self) -> &[Attribute] {
match *self {
Some(ref b) => b,
None => &[],
}
}

fn into_attr_vec(self) -> Vec<Attribute> {
match self {
Some(b) => *b,
None => Vec::new(),
}
}
}

pub trait AttributesExt {
fn into_thin_attrs(self) -> ThinAttributes;
}

impl AttributesExt for Vec<Attribute> {
fn into_thin_attrs(self) -> ThinAttributes {
if self.len() == 0 {
None
} else {
Some(Box::new(self))
}
}
}

pub trait HasAttrs: Sized {
fn attrs(&self) -> &[ast::Attribute];
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self;
Expand All @@ -885,13 +812,13 @@ pub trait HasAttrs: Sized {
/// A cheap way to add Attributes to an AST node.
pub trait WithAttrs {
// FIXME: Could be extended to anything IntoIter<Item=Attribute>
fn with_attrs(self, attrs: ThinAttributes) -> Self;
fn with_attrs(self, attrs: ThinVec<Attribute>) -> Self;
}

impl<T: HasAttrs> WithAttrs for T {
fn with_attrs(self, attrs: ThinAttributes) -> Self {
fn with_attrs(self, attrs: ThinVec<Attribute>) -> Self {
self.map_attrs(|mut orig_attrs| {
orig_attrs.extend(attrs.into_attr_vec());
orig_attrs.extend::<Vec<_>>(attrs.into());
orig_attrs
})
}
Expand All @@ -906,12 +833,12 @@ impl HasAttrs for Vec<Attribute> {
}
}

impl HasAttrs for ThinAttributes {
impl HasAttrs for ThinVec<Attribute> {
fn attrs(&self) -> &[Attribute] {
self.as_attr_slice()
&self
}
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
self.map_thin_attrs(f)
f(self.into()).into()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl DummyResult {
id: ast::DUMMY_NODE_ID,
node: ast::ExprKind::Lit(P(codemap::respan(sp, ast::LitKind::Bool(false)))),
span: sp,
attrs: None,
attrs: ast::ThinVec::new(),
})
}

Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/ext/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
init: Some(ex),
id: ast::DUMMY_NODE_ID,
span: sp,
attrs: None,
attrs: ast::ThinVec::new(),
});
let decl = respan(sp, ast::DeclKind::Local(local));
respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID))
Expand All @@ -550,7 +550,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
init: Some(ex),
id: ast::DUMMY_NODE_ID,
span: sp,
attrs: None,
attrs: ast::ThinVec::new(),
});
let decl = respan(sp, ast::DeclKind::Local(local));
P(respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID)))
Expand Down Expand Up @@ -587,7 +587,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
id: ast::DUMMY_NODE_ID,
node: node,
span: span,
attrs: None,
attrs: ast::ThinVec::new(),
})
}

Expand Down
Loading