Skip to content

Commit 8eddf02

Browse files
committed
Rollup merge of rust-lang#34339 - jseyfried:thin_vec, r=petrochenkov,Manishearth
Generalize and abstract `ThinAttributes` to `ThinVec<Attribute>`.
2 parents 8748cd9 + 5033eca commit 8eddf02

File tree

27 files changed

+274
-317
lines changed

27 files changed

+274
-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_pos::Span;
2019
use syntax::codemap::{respan, Spanned};
@@ -293,8 +292,11 @@ pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<
293292
})
294293
}
295294

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

300302
pub fn noop_fold_arm<T: Folder>(Arm { attrs, pats, guard, body }: Arm, fld: &mut T) -> Arm {
@@ -462,7 +464,7 @@ pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
462464
pat: fld.fold_pat(pat),
463465
init: init.map(|e| fld.fold_expr(e)),
464466
span: fld.new_span(span),
465-
attrs: attrs.map_thin_attrs(|attrs| fold_attrs(attrs.into(), fld).into()),
467+
attrs: fold_attrs(attrs, fld),
466468
}
467469
})
468470
}
@@ -1079,7 +1081,7 @@ pub fn noop_fold_expr<T: Folder>(Expr { id, node, span, attrs }: Expr, folder: &
10791081
}
10801082
},
10811083
span: folder.new_span(span),
1082-
attrs: attrs.map_thin_attrs(|attrs| fold_attrs(attrs.into(), folder).into()),
1084+
attrs: fold_attrs(attrs, folder),
10831085
}
10841086
}
10851087

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::Spanned;
3231
use syntax_pos::Span;
3332
use hir::*;
@@ -757,7 +756,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
757756
walk_list!(visitor, visit_arm, arms);
758757
}
759758
ExprClosure(_, ref function_declaration, ref body, _fn_decl_span) => {
760-
visitor.visit_fn(FnKind::Closure(expression.attrs.as_attr_slice()),
759+
visitor.visit_fn(FnKind::Closure(&expression.attrs),
761760
function_declaration,
762761
body,
763762
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_pos::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::Spanned;
2827
use syntax::visit;
2928
use syntax_pos::Span;
@@ -578,7 +577,7 @@ impl<'ast> Map<'ast> {
578577
Some(NodeTraitItem(ref ti)) => Some(&ti.attrs[..]),
579578
Some(NodeImplItem(ref ii)) => Some(&ii.attrs[..]),
580579
Some(NodeVariant(ref v)) => Some(&v.node.attrs[..]),
581-
Some(NodeExpr(ref e)) => Some(e.attrs.as_attr_slice()),
580+
Some(NodeExpr(ref e)) => Some(&*e.attrs),
582581
Some(NodeStmt(ref s)) => Some(s.node.attrs()),
583582
// unit/tuple structs take the attributes straight from
584583
// the struct definition.

src/librustc/hir/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ use syntax::codemap::{self, respan, Spanned};
4141
use syntax::abi::Abi;
4242
use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, AsmDialect};
4343
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
44-
use syntax::attr::{ThinAttributes, ThinAttributesExt};
4544
use syntax::parse::token::{keywords, InternedString};
4645
use syntax::ptr::P;
4746
use syntax::tokenstream::TokenTree;
47+
use syntax::util::ThinVec;
4848

4949
use std::collections::BTreeMap;
5050
use std::fmt;
@@ -734,7 +734,7 @@ impl Stmt_ {
734734
match *self {
735735
StmtDecl(ref d, _) => d.node.attrs(),
736736
StmtExpr(ref e, _) |
737-
StmtSemi(ref e, _) => e.attrs.as_attr_slice(),
737+
StmtSemi(ref e, _) => &e.attrs,
738738
}
739739
}
740740

@@ -758,7 +758,7 @@ pub struct Local {
758758
pub init: Option<P<Expr>>,
759759
pub id: NodeId,
760760
pub span: Span,
761-
pub attrs: ThinAttributes,
761+
pub attrs: ThinVec<Attribute>,
762762
}
763763

764764
pub type Decl = Spanned<Decl_>;
@@ -774,7 +774,7 @@ pub enum Decl_ {
774774
impl Decl_ {
775775
pub fn attrs(&self) -> &[Attribute] {
776776
match *self {
777-
DeclLocal(ref l) => l.attrs.as_attr_slice(),
777+
DeclLocal(ref l) => &l.attrs,
778778
DeclItem(_) => &[]
779779
}
780780
}
@@ -819,7 +819,7 @@ pub struct Expr {
819819
pub id: NodeId,
820820
pub node: Expr_,
821821
pub span: Span,
822-
pub attrs: ThinAttributes,
822+
pub attrs: ThinVec<Attribute>,
823823
}
824824

825825
impl fmt::Debug for Expr {

src/librustc/lint/context.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ use std::mem;
4242
use syntax::attr::{self, AttrMetaMethods};
4343
use syntax::parse::token::InternedString;
4444
use syntax::ast;
45-
use syntax::attr::ThinAttributesExt;
4645
use syntax_pos::Span;
4746
use errors::DiagnosticBuilder;
4847
use hir;
@@ -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> ast_visit::Visitor 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> ast_visit::Visitor 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
@@ -452,7 +452,7 @@ fn const_val_to_expr(value: &ConstVal) -> P<hir::Expr> {
452452
id: 0,
453453
node: hir::ExprLit(P(Spanned { node: node, span: DUMMY_SP })),
454454
span: DUMMY_SP,
455-
attrs: None,
455+
attrs: ast::ThinVec::new(),
456456
})
457457
}
458458

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: syntax_pos::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: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ 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;
1919
use syntax_pos::{mk_sp, Span, DUMMY_SP, ExpnId};
2020
use codemap::{respan, Spanned};
2121
use abi::Abi;
@@ -835,10 +835,9 @@ pub enum StmtKind {
835835
/// Expr without trailing semi-colon (must have unit type).
836836
Expr(P<Expr>),
837837

838-
/// Expr with trailing semi-colon (may have any type).
839838
Semi(P<Expr>),
840839

841-
Mac(P<(Mac, MacStmtStyle, ThinAttributes)>),
840+
Mac(P<(Mac, MacStmtStyle, ThinVec<Attribute>)>),
842841
}
843842

844843
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
@@ -865,7 +864,7 @@ pub struct Local {
865864
pub init: Option<P<Expr>>,
866865
pub id: NodeId,
867866
pub span: Span,
868-
pub attrs: ThinAttributes,
867+
pub attrs: ThinVec<Attribute>,
869868
}
870869

871870
/// An arm of a 'match'.
@@ -913,7 +912,7 @@ pub struct Expr {
913912
pub id: NodeId,
914913
pub node: ExprKind,
915914
pub span: Span,
916-
pub attrs: ThinAttributes
915+
pub attrs: ThinVec<Attribute>
917916
}
918917

919918
impl fmt::Debug for Expr {

src/libsyntax/attr.rs

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
2525
use parse::token::InternedString;
2626
use parse::{ParseSess, token};
2727
use ptr::P;
28+
use util::ThinVec;
2829

2930
use std::cell::{RefCell, Cell};
3031
use std::collections::HashSet;
@@ -802,80 +803,6 @@ impl IntType {
802803
}
803804
}
804805

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

893-
impl HasAttrs for ThinAttributes {
820+
impl HasAttrs for ThinVec<Attribute> {
894821
fn attrs(&self) -> &[Attribute] {
895-
self.as_attr_slice()
822+
&self
896823
}
897824
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
898-
self.map_thin_attrs(f)
825+
f(self.into()).into()
899826
}
900827
}
901828

src/libsyntax/ext/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl DummyResult {
380380
id: ast::DUMMY_NODE_ID,
381381
node: ast::ExprKind::Lit(P(codemap::respan(sp, ast::LitKind::Bool(false)))),
382382
span: sp,
383-
attrs: None,
383+
attrs: ast::ThinVec::new(),
384384
})
385385
}
386386

src/libsyntax/ext/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
530530
init: Some(ex),
531531
id: ast::DUMMY_NODE_ID,
532532
span: sp,
533-
attrs: None,
533+
attrs: ast::ThinVec::new(),
534534
});
535535
ast::Stmt {
536536
id: ast::DUMMY_NODE_ID,
@@ -558,7 +558,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
558558
init: Some(ex),
559559
id: ast::DUMMY_NODE_ID,
560560
span: sp,
561-
attrs: None,
561+
attrs: ast::ThinVec::new(),
562562
});
563563
P(ast::Stmt {
564564
id: ast::DUMMY_NODE_ID,
@@ -601,7 +601,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
601601
id: ast::DUMMY_NODE_ID,
602602
node: node,
603603
span: span,
604-
attrs: None,
604+
attrs: ast::ThinVec::new(),
605605
})
606606
}
607607

0 commit comments

Comments
 (0)