Skip to content

Commit 5033eca

Browse files
committed
Generalize and abstract ThinAttributes
1 parent 114be1e commit 5033eca

File tree

27 files changed

+278
-317
lines changed

27 files changed

+278
-317
lines changed

src/librustc/hir/fold.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use hir::*;
1515
use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, Attribute, Attribute_, MetaItem};
1616
use syntax::ast::MetaItemKind;
17-
use syntax::attr::ThinAttributesExt;
1817
use hir;
1918
use syntax::codemap::{respan, Span, Spanned};
2019
use syntax::ptr::P;
@@ -292,8 +291,11 @@ pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<
292291
})
293292
}
294293

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

299301
pub fn noop_fold_arm<T: Folder>(Arm { attrs, pats, guard, body }: Arm, fld: &mut T) -> Arm {
@@ -461,7 +463,7 @@ pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
461463
pat: fld.fold_pat(pat),
462464
init: init.map(|e| fld.fold_expr(e)),
463465
span: fld.new_span(span),
464-
attrs: attrs.map_thin_attrs(|attrs| fold_attrs(attrs.into(), fld).into()),
466+
attrs: fold_attrs(attrs, fld),
465467
}
466468
})
467469
}
@@ -1078,7 +1080,7 @@ pub fn noop_fold_expr<T: Folder>(Expr { id, node, span, attrs }: Expr, folder: &
10781080
}
10791081
},
10801082
span: folder.new_span(span),
1081-
attrs: attrs.map_thin_attrs(|attrs| fold_attrs(attrs.into(), folder).into()),
1083+
attrs: fold_attrs(attrs, folder),
10821084
}
10831085
}
10841086

src/librustc/hir/intravisit.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
2828
use syntax::abi::Abi;
2929
use syntax::ast::{NodeId, CRATE_NODE_ID, Name, Attribute};
30-
use syntax::attr::ThinAttributesExt;
3130
use syntax::codemap::{Span, Spanned};
3231
use hir::*;
3332

@@ -756,7 +755,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
756755
walk_list!(visitor, visit_arm, arms);
757756
}
758757
ExprClosure(_, ref function_declaration, ref body, _fn_decl_span) => {
759-
visitor.visit_fn(FnKind::Closure(expression.attrs.as_attr_slice()),
758+
visitor.visit_fn(FnKind::Closure(&expression.attrs),
760759
function_declaration,
761760
body,
762761
expression.span,

src/librustc/hir/lowering.rs

Lines changed: 72 additions & 84 deletions
Large diffs are not rendered by default.

src/librustc/hir/map/blocks.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use hir::map::{self, Node};
2727
use syntax::abi;
2828
use hir::{Block, FnDecl};
2929
use syntax::ast::{Attribute, Name, NodeId};
30-
use syntax::attr::ThinAttributesExt;
3130
use hir as ast;
3231
use syntax::codemap::Span;
3332
use hir::intravisit::FnKind;
@@ -257,11 +256,7 @@ impl<'a> FnLikeNode<'a> {
257256
}
258257
map::NodeExpr(e) => match e.node {
259258
ast::ExprClosure(_, ref decl, ref block, _fn_decl_span) =>
260-
closure(ClosureParts::new(&decl,
261-
&block,
262-
e.id,
263-
e.span,
264-
e.attrs.as_attr_slice())),
259+
closure(ClosureParts::new(&decl, &block, e.id, e.span, &e.attrs)),
265260
_ => bug!("expr FnLikeNode that is not fn-like"),
266261
},
267262
_ => bug!("other FnLikeNode that is not fn-like"),

src/librustc/hir/map/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndex};
2323

2424
use syntax::abi::Abi;
2525
use syntax::ast::{self, Name, NodeId, DUMMY_NODE_ID, };
26-
use syntax::attr::ThinAttributesExt;
2726
use syntax::codemap::{Span, Spanned};
2827
use syntax::visit;
2928

@@ -577,7 +576,7 @@ impl<'ast> Map<'ast> {
577576
Some(NodeTraitItem(ref ti)) => Some(&ti.attrs[..]),
578577
Some(NodeImplItem(ref ii)) => Some(&ii.attrs[..]),
579578
Some(NodeVariant(ref v)) => Some(&v.node.attrs[..]),
580-
Some(NodeExpr(ref e)) => Some(e.attrs.as_attr_slice()),
579+
Some(NodeExpr(ref e)) => Some(&*e.attrs),
581580
Some(NodeStmt(ref s)) => Some(s.node.attrs()),
582581
// unit/tuple structs take the attributes straight from
583582
// the struct definition.

src/librustc/hir/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ use syntax::codemap::{self, mk_sp, respan, Span, Spanned, ExpnId};
4040
use syntax::abi::Abi;
4141
use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, TokenTree, AsmDialect};
4242
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
43-
use syntax::attr::{ThinAttributes, ThinAttributesExt};
4443
use syntax::parse::token::{keywords, InternedString};
4544
use syntax::ptr::P;
45+
use syntax::util::ThinVec;
4646

4747
use std::collections::BTreeMap;
4848
use std::fmt;
@@ -732,7 +732,7 @@ impl Stmt_ {
732732
match *self {
733733
StmtDecl(ref d, _) => d.node.attrs(),
734734
StmtExpr(ref e, _) |
735-
StmtSemi(ref e, _) => e.attrs.as_attr_slice(),
735+
StmtSemi(ref e, _) => &e.attrs,
736736
}
737737
}
738738

@@ -756,7 +756,7 @@ pub struct Local {
756756
pub init: Option<P<Expr>>,
757757
pub id: NodeId,
758758
pub span: Span,
759-
pub attrs: ThinAttributes,
759+
pub attrs: ThinVec<Attribute>,
760760
}
761761

762762
pub type Decl = Spanned<Decl_>;
@@ -772,7 +772,7 @@ pub enum Decl_ {
772772
impl Decl_ {
773773
pub fn attrs(&self) -> &[Attribute] {
774774
match *self {
775-
DeclLocal(ref l) => l.attrs.as_attr_slice(),
775+
DeclLocal(ref l) => &l.attrs,
776776
DeclItem(_) => &[]
777777
}
778778
}
@@ -817,7 +817,7 @@ pub struct Expr {
817817
pub id: NodeId,
818818
pub node: Expr_,
819819
pub span: Span,
820-
pub attrs: ThinAttributes,
820+
pub attrs: ThinVec<Attribute>,
821821
}
822822

823823
impl fmt::Debug for Expr {

src/librustc/lint/context.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ use syntax::codemap::Span;
4444
use syntax::errors::DiagnosticBuilder;
4545
use syntax::parse::token::InternedString;
4646
use syntax::ast;
47-
use syntax::attr::ThinAttributesExt;
4847
use hir;
4948
use hir::intravisit as hir_visit;
5049
use hir::intravisit::{IdVisitor, IdVisitingOperation};
@@ -767,7 +766,7 @@ impl<'a, 'tcx, 'v> hir_visit::Visitor<'v> for LateContext<'a, 'tcx> {
767766
}
768767

769768
fn visit_expr(&mut self, e: &hir::Expr) {
770-
self.with_lint_attrs(e.attrs.as_attr_slice(), |cx| {
769+
self.with_lint_attrs(&e.attrs, |cx| {
771770
run_lints!(cx, check_expr, late_passes, e);
772771
hir_visit::walk_expr(cx, e);
773772
})
@@ -832,7 +831,7 @@ impl<'a, 'tcx, 'v> hir_visit::Visitor<'v> for LateContext<'a, 'tcx> {
832831
}
833832

834833
fn visit_local(&mut self, l: &hir::Local) {
835-
self.with_lint_attrs(l.attrs.as_attr_slice(), |cx| {
834+
self.with_lint_attrs(&l.attrs, |cx| {
836835
run_lints!(cx, check_local, late_passes, l);
837836
hir_visit::walk_local(cx, l);
838837
})
@@ -928,7 +927,7 @@ impl<'a, 'v> ast_visit::Visitor<'v> for EarlyContext<'a> {
928927
}
929928

930929
fn visit_expr(&mut self, e: &ast::Expr) {
931-
self.with_lint_attrs(e.attrs.as_attr_slice(), |cx| {
930+
self.with_lint_attrs(&e.attrs, |cx| {
932931
run_lints!(cx, check_expr, early_passes, e);
933932
ast_visit::walk_expr(cx, e);
934933
})
@@ -988,7 +987,7 @@ impl<'a, 'v> ast_visit::Visitor<'v> for EarlyContext<'a> {
988987
}
989988

990989
fn visit_local(&mut self, l: &ast::Local) {
991-
self.with_lint_attrs(l.attrs.as_attr_slice(), |cx| {
990+
self.with_lint_attrs(&l.attrs, |cx| {
992991
run_lints!(cx, check_local, early_passes, l);
993992
ast_visit::walk_local(cx, l);
994993
})

src/librustc_const_eval/check_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ fn const_val_to_expr(value: &ConstVal) -> P<hir::Expr> {
451451
id: 0,
452452
node: hir::ExprLit(P(Spanned { node: node, span: DUMMY_SP })),
453453
span: DUMMY_SP,
454-
attrs: None,
454+
attrs: ast::ThinVec::new(),
455455
})
456456
}
457457

src/librustc_driver/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ impl fold::Folder for ReplaceBodyWithLoop {
672672
node: ast::ExprKind::Loop(empty_block, None),
673673
id: ast::DUMMY_NODE_ID,
674674
span: codemap::DUMMY_SP,
675-
attrs: None,
675+
attrs: ast::ThinVec::new(),
676676
});
677677

678678
expr_to_block(b.rules, Some(loop_expr))

src/libsyntax/ast.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ pub use self::TyParamBound::*;
1414
pub use self::UnsafeSource::*;
1515
pub use self::ViewPath_::*;
1616
pub use self::PathParameters::*;
17+
pub use util::ThinVec;
1718

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

812-
Mac(P<Mac>, MacStmtStyle, ThinAttributes),
813+
Mac(P<Mac>, MacStmtStyle, ThinVec<Attribute>),
813814
}
814815

815816
impl StmtKind {
@@ -851,7 +852,7 @@ pub struct Local {
851852
pub init: Option<P<Expr>>,
852853
pub id: NodeId,
853854
pub span: Span,
854-
pub attrs: ThinAttributes,
855+
pub attrs: ThinVec<Attribute>,
855856
}
856857

857858
impl Local {
@@ -912,7 +913,7 @@ pub struct Expr {
912913
pub id: NodeId,
913914
pub node: ExprKind,
914915
pub span: Span,
915-
pub attrs: ThinAttributes
916+
pub attrs: ThinVec<Attribute>
916917
}
917918

918919
impl Expr {

src/libsyntax/attr.rs

Lines changed: 7 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
2626
use parse::token::InternedString;
2727
use parse::{ParseSess, token};
2828
use ptr::P;
29+
use util::ThinVec;
2930

3031
use std::cell::{RefCell, Cell};
3132
use std::collections::HashSet;
@@ -803,80 +804,6 @@ impl IntType {
803804
}
804805
}
805806

806-
/// A list of attributes, behind a optional box as
807-
/// a space optimization.
808-
pub type ThinAttributes = Option<Box<Vec<Attribute>>>;
809-
810-
pub trait ThinAttributesExt {
811-
fn map_thin_attrs<F>(self, f: F) -> Self
812-
where F: FnOnce(Vec<Attribute>) -> Vec<Attribute>;
813-
fn prepend(mut self, attrs: Self) -> Self;
814-
fn append(mut self, attrs: Self) -> Self;
815-
fn update<F>(&mut self, f: F)
816-
where Self: Sized,
817-
F: FnOnce(Self) -> Self;
818-
fn as_attr_slice(&self) -> &[Attribute];
819-
fn into_attr_vec(self) -> Vec<Attribute>;
820-
}
821-
822-
impl ThinAttributesExt for ThinAttributes {
823-
fn map_thin_attrs<F>(self, f: F) -> Self
824-
where F: FnOnce(Vec<Attribute>) -> Vec<Attribute>
825-
{
826-
f(self.map(|b| *b).unwrap_or(Vec::new())).into_thin_attrs()
827-
}
828-
829-
fn prepend(self, attrs: ThinAttributes) -> Self {
830-
attrs.map_thin_attrs(|mut attrs| {
831-
attrs.extend(self.into_attr_vec());
832-
attrs
833-
})
834-
}
835-
836-
fn append(self, attrs: ThinAttributes) -> Self {
837-
self.map_thin_attrs(|mut self_| {
838-
self_.extend(attrs.into_attr_vec());
839-
self_
840-
})
841-
}
842-
843-
fn update<F>(&mut self, f: F)
844-
where Self: Sized,
845-
F: FnOnce(ThinAttributes) -> ThinAttributes
846-
{
847-
let self_ = f(self.take());
848-
*self = self_;
849-
}
850-
851-
fn as_attr_slice(&self) -> &[Attribute] {
852-
match *self {
853-
Some(ref b) => b,
854-
None => &[],
855-
}
856-
}
857-
858-
fn into_attr_vec(self) -> Vec<Attribute> {
859-
match self {
860-
Some(b) => *b,
861-
None => Vec::new(),
862-
}
863-
}
864-
}
865-
866-
pub trait AttributesExt {
867-
fn into_thin_attrs(self) -> ThinAttributes;
868-
}
869-
870-
impl AttributesExt for Vec<Attribute> {
871-
fn into_thin_attrs(self) -> ThinAttributes {
872-
if self.len() == 0 {
873-
None
874-
} else {
875-
Some(Box::new(self))
876-
}
877-
}
878-
}
879-
880807
pub trait HasAttrs: Sized {
881808
fn attrs(&self) -> &[ast::Attribute];
882809
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self;
@@ -885,13 +812,13 @@ pub trait HasAttrs: Sized {
885812
/// A cheap way to add Attributes to an AST node.
886813
pub trait WithAttrs {
887814
// FIXME: Could be extended to anything IntoIter<Item=Attribute>
888-
fn with_attrs(self, attrs: ThinAttributes) -> Self;
815+
fn with_attrs(self, attrs: ThinVec<Attribute>) -> Self;
889816
}
890817

891818
impl<T: HasAttrs> WithAttrs for T {
892-
fn with_attrs(self, attrs: ThinAttributes) -> Self {
819+
fn with_attrs(self, attrs: ThinVec<Attribute>) -> Self {
893820
self.map_attrs(|mut orig_attrs| {
894-
orig_attrs.extend(attrs.into_attr_vec());
821+
orig_attrs.extend::<Vec<_>>(attrs.into());
895822
orig_attrs
896823
})
897824
}
@@ -906,12 +833,12 @@ impl HasAttrs for Vec<Attribute> {
906833
}
907834
}
908835

909-
impl HasAttrs for ThinAttributes {
836+
impl HasAttrs for ThinVec<Attribute> {
910837
fn attrs(&self) -> &[Attribute] {
911-
self.as_attr_slice()
838+
&self
912839
}
913840
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
914-
self.map_thin_attrs(f)
841+
f(self.into()).into()
915842
}
916843
}
917844

src/libsyntax/ext/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ impl DummyResult {
350350
id: ast::DUMMY_NODE_ID,
351351
node: ast::ExprKind::Lit(P(codemap::respan(sp, ast::LitKind::Bool(false)))),
352352
span: sp,
353-
attrs: None,
353+
attrs: ast::ThinVec::new(),
354354
})
355355
}
356356

src/libsyntax/ext/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
525525
init: Some(ex),
526526
id: ast::DUMMY_NODE_ID,
527527
span: sp,
528-
attrs: None,
528+
attrs: ast::ThinVec::new(),
529529
});
530530
let decl = respan(sp, ast::DeclKind::Local(local));
531531
respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID))
@@ -550,7 +550,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
550550
init: Some(ex),
551551
id: ast::DUMMY_NODE_ID,
552552
span: sp,
553-
attrs: None,
553+
attrs: ast::ThinVec::new(),
554554
});
555555
let decl = respan(sp, ast::DeclKind::Local(local));
556556
P(respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID)))
@@ -587,7 +587,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
587587
id: ast::DUMMY_NODE_ID,
588588
node: node,
589589
span: span,
590-
attrs: None,
590+
attrs: ast::ThinVec::new(),
591591
})
592592
}
593593

0 commit comments

Comments
 (0)