Skip to content

Commit f0beba0

Browse files
committed
Moved and refactored ThinAttributes
1 parent 2a8f358 commit f0beba0

File tree

11 files changed

+143
-139
lines changed

11 files changed

+143
-139
lines changed

src/librustc/lint/context.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ use syntax::ast_util::{self, IdVisitingOperation};
4141
use syntax::attr::{self, AttrMetaMethods};
4242
use syntax::codemap::Span;
4343
use syntax::parse::token::InternedString;
44-
use syntax::ast::{self, ThinAttributesExt};
44+
use syntax::ast;
45+
use syntax::attr::ThinAttributesExt;
4546
use rustc_front::hir;
4647
use rustc_front::util;
4748
use rustc_front::intravisit as hir_visit;
@@ -674,7 +675,7 @@ impl<'a, 'tcx, 'v> hir_visit::Visitor<'v> for LateContext<'a, 'tcx> {
674675
}
675676

676677
fn visit_expr(&mut self, e: &hir::Expr) {
677-
self.with_lint_attrs(e.attrs.as_attrs(), |cx| {
678+
self.with_lint_attrs(e.attrs.as_attr_slice(), |cx| {
678679
run_lints!(cx, check_expr, late_passes, e);
679680
hir_visit::walk_expr(cx, e);
680681
})
@@ -737,7 +738,7 @@ impl<'a, 'tcx, 'v> hir_visit::Visitor<'v> for LateContext<'a, 'tcx> {
737738
}
738739

739740
fn visit_local(&mut self, l: &hir::Local) {
740-
self.with_lint_attrs(l.attrs.as_attrs(), |cx| {
741+
self.with_lint_attrs(l.attrs.as_attr_slice(), |cx| {
741742
run_lints!(cx, check_local, late_passes, l);
742743
hir_visit::walk_local(cx, l);
743744
})

src/librustc_front/fold.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
1414
use hir::*;
1515
use syntax::ast::{Ident, Name, NodeId, DUMMY_NODE_ID, Attribute, Attribute_, MetaItem};
16-
use syntax::ast::{MetaWord, MetaList, MetaNameValue, ThinAttributesExt};
16+
use syntax::ast::{MetaWord, MetaList, MetaNameValue};
17+
use syntax::attr::ThinAttributesExt;
1718
use hir;
1819
use syntax::codemap::{respan, Span, Spanned};
1920
use syntax::owned_slice::OwnedSlice;
@@ -508,7 +509,7 @@ pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
508509
pat: fld.fold_pat(pat),
509510
init: init.map(|e| fld.fold_expr(e)),
510511
span: fld.new_span(span),
511-
attrs: attrs.map_opt_attrs(|attrs| fold_attrs(attrs, fld)),
512+
attrs: attrs.map_thin_attrs(|attrs| fold_attrs(attrs, fld)),
512513
}
513514
})
514515
}
@@ -1172,7 +1173,7 @@ pub fn noop_fold_expr<T: Folder>(Expr { id, node, span, attrs }: Expr, folder: &
11721173
}
11731174
},
11741175
span: folder.new_span(span),
1175-
attrs: attrs.map_opt_attrs(|attrs| fold_attrs(attrs, folder)),
1176+
attrs: attrs.map_thin_attrs(|attrs| fold_attrs(attrs, folder)),
11761177
}
11771178
}
11781179

src/librustc_front/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use syntax::codemap::{self, Span, Spanned, DUMMY_SP, ExpnId};
4141
use syntax::abi::Abi;
4242
use syntax::ast::{Name, Ident, NodeId, DUMMY_NODE_ID, TokenTree, AsmDialect};
4343
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, CrateConfig};
44-
use syntax::ast::ThinAttributes;
44+
use syntax::attr::ThinAttributes;
4545
use syntax::owned_slice::OwnedSlice;
4646
use syntax::parse::token::InternedString;
4747
use syntax::ptr::P;

src/librustc_front/lowering.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ use hir;
6666
use std::collections::BTreeMap;
6767
use std::collections::HashMap;
6868
use syntax::ast::*;
69+
use syntax::attr::{ThinAttributes, ThinAttributesExt};
6970
use syntax::ptr::P;
7071
use syntax::codemap::{respan, Spanned, Span};
7172
use syntax::owned_slice::OwnedSlice;
@@ -1219,8 +1220,7 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
12191220
// merge attributes into the inner expression.
12201221
return lower_expr(lctx, ex).map(|mut ex| {
12211222
ex.attrs.update(|attrs| {
1222-
// FIXME: Badly named
1223-
attrs.prepend_outer(e.attrs.clone())
1223+
attrs.prepend(e.attrs.clone())
12241224
});
12251225
ex
12261226
});

src/libsyntax/ast.rs

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub use self::ViewPath_::*;
4545
pub use self::Visibility::*;
4646
pub use self::PathParameters::*;
4747

48+
use attr::ThinAttributes;
4849
use codemap::{Span, Spanned, DUMMY_SP, ExpnId};
4950
use abi::Abi;
5051
use ast_util;
@@ -1952,98 +1953,6 @@ pub struct MacroDef {
19521953
pub body: Vec<TokenTree>,
19531954
}
19541955

1955-
/// A list of attributes, behind a optional box as
1956-
/// a space optimization.
1957-
pub type ThinAttributes = Option<Box<Vec<Attribute>>>;
1958-
1959-
pub trait ThinAttributesExt {
1960-
fn map_opt_attrs<F>(self, f: F) -> Self
1961-
where F: FnOnce(Vec<Attribute>) -> Vec<Attribute>;
1962-
fn prepend_outer(mut self, attrs: Self) -> Self;
1963-
fn append_inner(mut self, attrs: Self) -> Self;
1964-
fn update<F>(&mut self, f: F)
1965-
where Self: Sized,
1966-
F: FnOnce(Self) -> Self;
1967-
fn as_attrs(&self) -> &[Attribute];
1968-
fn into_attrs(self) -> Vec<Attribute>;
1969-
}
1970-
1971-
// FIXME: Rename inner/outer
1972-
// FIXME: Rename opt_attrs
1973-
1974-
impl ThinAttributesExt for ThinAttributes {
1975-
fn map_opt_attrs<F>(self, f: F) -> Self
1976-
where F: FnOnce(Vec<Attribute>) -> Vec<Attribute> {
1977-
1978-
// This is kinda complicated... Ensure the function is
1979-
// always called, and that None inputs or results are
1980-
// correctly handled.
1981-
if let Some(mut b) = self {
1982-
use std::mem::replace;
1983-
1984-
let vec = replace(&mut *b, Vec::new());
1985-
let vec = f(vec);
1986-
if vec.len() == 0 {
1987-
None
1988-
} else {
1989-
replace(&mut*b, vec);
1990-
Some(b)
1991-
}
1992-
} else {
1993-
f(Vec::new()).into_opt_attrs()
1994-
}
1995-
}
1996-
1997-
fn prepend_outer(self, attrs: ThinAttributes) -> Self {
1998-
attrs.map_opt_attrs(|mut attrs| {
1999-
attrs.extend(self.into_attrs());
2000-
attrs
2001-
})
2002-
}
2003-
2004-
fn append_inner(self, attrs: ThinAttributes) -> Self {
2005-
self.map_opt_attrs(|mut self_| {
2006-
self_.extend(attrs.into_attrs());
2007-
self_
2008-
})
2009-
}
2010-
2011-
fn update<F>(&mut self, f: F)
2012-
where Self: Sized,
2013-
F: FnOnce(ThinAttributes) -> ThinAttributes
2014-
{
2015-
let self_ = f(self.take());
2016-
*self = self_;
2017-
}
2018-
2019-
fn as_attrs(&self) -> &[Attribute] {
2020-
match *self {
2021-
Some(ref b) => b,
2022-
None => &[],
2023-
}
2024-
}
2025-
2026-
fn into_attrs(self) -> Vec<Attribute> {
2027-
match self {
2028-
Some(b) => *b,
2029-
None => Vec::new(),
2030-
}
2031-
}
2032-
}
2033-
2034-
pub trait AttributesExt {
2035-
fn into_opt_attrs(self) -> ThinAttributes;
2036-
}
2037-
impl AttributesExt for Vec<Attribute> {
2038-
fn into_opt_attrs(self) -> ThinAttributes {
2039-
if self.len() == 0 {
2040-
None
2041-
} else {
2042-
Some(Box::new(self))
2043-
}
2044-
}
2045-
}
2046-
20471956
#[cfg(test)]
20481957
mod tests {
20491958
use serialize;

src/libsyntax/attr.rs

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ pub use self::IntType::*;
1616

1717
use ast;
1818
use ast::{AttrId, Attribute, Attribute_, MetaItem, MetaWord, MetaNameValue, MetaList};
19-
use ast::{Stmt, StmtDecl, StmtExpr, StmtMac, StmtSemi, DeclItem, DeclLocal, ThinAttributes};
20-
use ast::{Expr, ThinAttributesExt, Item, Local, Decl};
19+
use ast::{Stmt, StmtDecl, StmtExpr, StmtMac, StmtSemi, DeclItem, DeclLocal};
20+
use ast::{Expr, Item, Local, Decl};
2121
use codemap::{Span, Spanned, spanned, dummy_spanned};
2222
use codemap::BytePos;
2323
use diagnostic::SpanHandler;
@@ -723,6 +723,96 @@ impl IntType {
723723
}
724724
}
725725

726+
/// A list of attributes, behind a optional box as
727+
/// a space optimization.
728+
pub type ThinAttributes = Option<Box<Vec<Attribute>>>;
729+
730+
pub trait ThinAttributesExt {
731+
fn map_thin_attrs<F>(self, f: F) -> Self
732+
where F: FnOnce(Vec<Attribute>) -> Vec<Attribute>;
733+
fn prepend(mut self, attrs: Self) -> Self;
734+
fn append(mut self, attrs: Self) -> Self;
735+
fn update<F>(&mut self, f: F)
736+
where Self: Sized,
737+
F: FnOnce(Self) -> Self;
738+
fn as_attr_slice(&self) -> &[Attribute];
739+
fn into_attr_vec(self) -> Vec<Attribute>;
740+
}
741+
742+
impl ThinAttributesExt for ThinAttributes {
743+
fn map_thin_attrs<F>(self, f: F) -> Self
744+
where F: FnOnce(Vec<Attribute>) -> Vec<Attribute> {
745+
746+
// This is kinda complicated... Ensure the function is
747+
// always called, and that None inputs or results are
748+
// correctly handled.
749+
if let Some(mut b) = self {
750+
use std::mem::replace;
751+
752+
let vec = replace(&mut *b, Vec::new());
753+
let vec = f(vec);
754+
if vec.len() == 0 {
755+
None
756+
} else {
757+
replace(&mut*b, vec);
758+
Some(b)
759+
}
760+
} else {
761+
f(Vec::new()).into_thin_attrs()
762+
}
763+
}
764+
765+
fn prepend(self, attrs: ThinAttributes) -> Self {
766+
attrs.map_thin_attrs(|mut attrs| {
767+
attrs.extend(self.into_attr_vec());
768+
attrs
769+
})
770+
}
771+
772+
fn append(self, attrs: ThinAttributes) -> Self {
773+
self.map_thin_attrs(|mut self_| {
774+
self_.extend(attrs.into_attr_vec());
775+
self_
776+
})
777+
}
778+
779+
fn update<F>(&mut self, f: F)
780+
where Self: Sized,
781+
F: FnOnce(ThinAttributes) -> ThinAttributes
782+
{
783+
let self_ = f(self.take());
784+
*self = self_;
785+
}
786+
787+
fn as_attr_slice(&self) -> &[Attribute] {
788+
match *self {
789+
Some(ref b) => b,
790+
None => &[],
791+
}
792+
}
793+
794+
fn into_attr_vec(self) -> Vec<Attribute> {
795+
match self {
796+
Some(b) => *b,
797+
None => Vec::new(),
798+
}
799+
}
800+
}
801+
802+
pub trait AttributesExt {
803+
fn into_thin_attrs(self) -> ThinAttributes;
804+
}
805+
806+
impl AttributesExt for Vec<Attribute> {
807+
fn into_thin_attrs(self) -> ThinAttributes {
808+
if self.len() == 0 {
809+
None
810+
} else {
811+
Some(Box::new(self))
812+
}
813+
}
814+
}
815+
726816
/// A cheap way to add Attributes to an AST node.
727817
pub trait WithAttrs {
728818
// FIXME: Could be extended to anything IntoIter<Item=Attribute>
@@ -732,7 +822,7 @@ pub trait WithAttrs {
732822
impl WithAttrs for P<Expr> {
733823
fn with_attrs(self, attrs: ThinAttributes) -> Self {
734824
self.map(|mut e| {
735-
e.attrs.update(|a| a.append_inner(attrs));
825+
e.attrs.update(|a| a.append(attrs));
736826
e
737827
})
738828
}
@@ -741,7 +831,7 @@ impl WithAttrs for P<Expr> {
741831
impl WithAttrs for P<Item> {
742832
fn with_attrs(self, attrs: ThinAttributes) -> Self {
743833
self.map(|Item { ident, attrs: mut ats, id, node, vis, span }| {
744-
ats.extend(attrs.into_attrs());
834+
ats.extend(attrs.into_attr_vec());
745835
Item {
746836
ident: ident,
747837
attrs: ats,
@@ -757,7 +847,7 @@ impl WithAttrs for P<Item> {
757847
impl WithAttrs for P<Local> {
758848
fn with_attrs(self, attrs: ThinAttributes) -> Self {
759849
self.map(|Local { pat, ty, init, id, span, attrs: mut ats }| {
760-
ats.update(|a| a.append_inner(attrs));
850+
ats.update(|a| a.append(attrs));
761851
Local {
762852
pat: pat,
763853
ty: ty,
@@ -794,7 +884,7 @@ impl WithAttrs for P<Stmt> {
794884
StmtExpr(expr, id) => StmtExpr(expr.with_attrs(attrs), id),
795885
StmtSemi(expr, id) => StmtSemi(expr.with_attrs(attrs), id),
796886
StmtMac(mac, style, mut ats) => {
797-
ats.update(|a| a.append_inner(attrs));
887+
ats.update(|a| a.append(attrs));
798888
StmtMac(mac, style, ats)
799889
}
800890
},

src/libsyntax/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'a, F> fold::Folder for Context<'a, F> where F: FnMut(&[ast::Attribute]) ->
5454
// Anything else is always required, and thus has to error out
5555
// in case of a cfg attr.
5656
//
57-
// NB: This intentionally not part of the fold_expr() function
57+
// NB: This is intentionally not part of the fold_expr() function
5858
// in order for fold_opt_expr() to be able to avoid this check
5959
if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
6060
self.diagnostic.span_err(attr.span,

src/libsyntax/fold.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2121
use ast::*;
2222
use ast;
23+
use attr::ThinAttributesExt;
2324
use ast_util;
2425
use codemap::{respan, Span, Spanned};
2526
use owned_slice::OwnedSlice;
@@ -522,7 +523,7 @@ pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
522523
pat: fld.fold_pat(pat),
523524
init: init.map(|e| fld.fold_expr(e)),
524525
span: fld.new_span(span),
525-
attrs: attrs.map_opt_attrs(|v| fold_attrs(v, fld)),
526+
attrs: attrs.map_thin_attrs(|v| fold_attrs(v, fld)),
526527
})
527528
}
528529

@@ -1339,7 +1340,7 @@ pub fn noop_fold_expr<T: Folder>(Expr {id, node, span, attrs}: Expr, folder: &mu
13391340
ExprParen(ex) => ExprParen(folder.fold_expr(ex))
13401341
},
13411342
span: folder.new_span(span),
1342-
attrs: attrs.map_opt_attrs(|v| fold_attrs(v, folder)),
1343+
attrs: attrs.map_thin_attrs(|v| fold_attrs(v, folder)),
13431344
}
13441345
}
13451346

@@ -1388,7 +1389,7 @@ pub fn noop_fold_stmt<T: Folder>(Spanned {node, span}: Stmt, folder: &mut T)
13881389
StmtMac(mac, semi, attrs) => SmallVector::one(P(Spanned {
13891390
node: StmtMac(mac.map(|m| folder.fold_mac(m)),
13901391
semi,
1391-
attrs.map_opt_attrs(|v| fold_attrs(v, folder))),
1392+
attrs.map_thin_attrs(|v| fold_attrs(v, folder))),
13921393
span: span
13931394
}))
13941395
}

0 commit comments

Comments
 (0)