Skip to content

Commit 2a8f358

Browse files
committed
Add syntax support for attributes on expressions and all syntax
nodes in statement position. Extended #[cfg] folder to allow removal of statements, and of expressions in optional positions like expression lists and trailing block expressions. Extended lint checker to recognize lint levels on expressions and locals.
1 parent 6ef02ef commit 2a8f358

34 files changed

+1602
-416
lines changed

src/librustc/lint/context.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ 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;
44+
use syntax::ast::{self, ThinAttributesExt};
4545
use rustc_front::hir;
4646
use rustc_front::util;
4747
use rustc_front::intravisit as hir_visit;
@@ -674,11 +674,18 @@ impl<'a, 'tcx, 'v> hir_visit::Visitor<'v> for LateContext<'a, 'tcx> {
674674
}
675675

676676
fn visit_expr(&mut self, e: &hir::Expr) {
677-
run_lints!(self, check_expr, late_passes, e);
678-
hir_visit::walk_expr(self, e);
677+
self.with_lint_attrs(e.attrs.as_attrs(), |cx| {
678+
run_lints!(cx, check_expr, late_passes, e);
679+
hir_visit::walk_expr(cx, e);
680+
})
679681
}
680682

681683
fn visit_stmt(&mut self, s: &hir::Stmt) {
684+
// statement attributes are actually just attributes on one of
685+
// - item
686+
// - local
687+
// - expression
688+
// so we keep track of lint levels there
682689
run_lints!(self, check_stmt, late_passes, s);
683690
hir_visit::walk_stmt(self, s);
684691
}
@@ -730,8 +737,10 @@ impl<'a, 'tcx, 'v> hir_visit::Visitor<'v> for LateContext<'a, 'tcx> {
730737
}
731738

732739
fn visit_local(&mut self, l: &hir::Local) {
733-
run_lints!(self, check_local, late_passes, l);
734-
hir_visit::walk_local(self, l);
740+
self.with_lint_attrs(l.attrs.as_attrs(), |cx| {
741+
run_lints!(cx, check_local, late_passes, l);
742+
hir_visit::walk_local(cx, l);
743+
})
735744
}
736745

737746
fn visit_block(&mut self, b: &hir::Block) {

src/librustc/middle/check_match.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,8 @@ fn const_val_to_expr(value: &ConstVal) -> P<hir::Expr> {
409409
P(hir::Expr {
410410
id: 0,
411411
node: hir::ExprLit(P(Spanned { node: node, span: DUMMY_SP })),
412-
span: DUMMY_SP
412+
span: DUMMY_SP,
413+
attrs: None,
413414
})
414415
}
415416

src/librustc_driver/pretty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ impl fold::Folder for ReplaceBodyWithLoop {
654654
node: ast::ExprLoop(empty_block, None),
655655
id: ast::DUMMY_NODE_ID,
656656
span: codemap::DUMMY_SP,
657+
attrs: None,
657658
});
658659

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

src/librustc_front/fold.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
use hir::*;
1515
use syntax::ast::{Ident, Name, NodeId, DUMMY_NODE_ID, Attribute, Attribute_, MetaItem};
16-
use syntax::ast::{MetaWord, MetaList, MetaNameValue};
16+
use syntax::ast::{MetaWord, MetaList, MetaNameValue, ThinAttributesExt};
1717
use hir;
1818
use syntax::codemap::{respan, Span, Spanned};
1919
use syntax::owned_slice::OwnedSlice;
@@ -501,13 +501,14 @@ pub fn noop_fold_parenthesized_parameter_data<T: Folder>(data: ParenthesizedPara
501501
}
502502

503503
pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
504-
l.map(|Local { id, pat, ty, init, span }| {
504+
l.map(|Local { id, pat, ty, init, span, attrs }| {
505505
Local {
506506
id: fld.new_id(id),
507507
ty: ty.map(|t| fld.fold_ty(t)),
508508
pat: fld.fold_pat(pat),
509509
init: init.map(|e| fld.fold_expr(e)),
510510
span: fld.new_span(span),
511+
attrs: attrs.map_opt_attrs(|attrs| fold_attrs(attrs, fld)),
511512
}
512513
})
513514
}
@@ -1048,7 +1049,7 @@ pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {
10481049
})
10491050
}
10501051

1051-
pub fn noop_fold_expr<T: Folder>(Expr { id, node, span }: Expr, folder: &mut T) -> Expr {
1052+
pub fn noop_fold_expr<T: Folder>(Expr { id, node, span, attrs }: Expr, folder: &mut T) -> Expr {
10521053
Expr {
10531054
id: folder.new_id(id),
10541055
node: match node {
@@ -1171,6 +1172,7 @@ pub fn noop_fold_expr<T: Folder>(Expr { id, node, span }: Expr, folder: &mut T)
11711172
}
11721173
},
11731174
span: folder.new_span(span),
1175+
attrs: attrs.map_opt_attrs(|attrs| fold_attrs(attrs, folder)),
11741176
}
11751177
}
11761178

src/librustc_front/hir.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +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;
4445
use syntax::owned_slice::OwnedSlice;
4546
use syntax::parse::token::InternedString;
4647
use syntax::ptr::P;
@@ -558,6 +559,7 @@ pub struct Local {
558559
pub init: Option<P<Expr>>,
559560
pub id: NodeId,
560561
pub span: Span,
562+
pub attrs: ThinAttributes,
561563
}
562564

563565
pub type Decl = Spanned<Decl_>;
@@ -609,6 +611,7 @@ pub struct Expr {
609611
pub id: NodeId,
610612
pub node: Expr_,
611613
pub span: Span,
614+
pub attrs: ThinAttributes,
612615
}
613616

614617
impl fmt::Debug for Expr {

src/librustc_front/lowering.rs

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ pub fn lower_local(lctx: &LoweringContext, l: &Local) -> P<hir::Local> {
331331
pat: lower_pat(lctx, &l.pat),
332332
init: l.init.as_ref().map(|e| lower_expr(lctx, e)),
333333
span: l.span,
334+
attrs: l.attrs.clone(),
334335
})
335336
}
336337

@@ -1215,7 +1216,14 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
12151216
maybe_expr.as_ref().map(|x| lower_expr(lctx, x)))
12161217
}
12171218
ExprParen(ref ex) => {
1218-
return lower_expr(lctx, ex);
1219+
// merge attributes into the inner expression.
1220+
return lower_expr(lctx, ex).map(|mut ex| {
1221+
ex.attrs.update(|attrs| {
1222+
// FIXME: Badly named
1223+
attrs.prepend_outer(e.attrs.clone())
1224+
});
1225+
ex
1226+
});
12191227
}
12201228

12211229
// Desugar ExprIfLet
@@ -1454,6 +1462,7 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
14541462
ExprMac(_) => panic!("Shouldn't exist here"),
14551463
},
14561464
span: e.span,
1465+
attrs: e.attrs.clone(),
14571466
})
14581467
}
14591468

@@ -1552,60 +1561,71 @@ fn arm(pats: Vec<P<hir::Pat>>, expr: P<hir::Expr>) -> hir::Arm {
15521561
}
15531562
}
15541563

1555-
fn expr_break(lctx: &LoweringContext, span: Span) -> P<hir::Expr> {
1556-
expr(lctx, span, hir::ExprBreak(None))
1564+
fn expr_break(lctx: &LoweringContext, span: Span,
1565+
attrs: ThinAttributes) -> P<hir::Expr> {
1566+
expr(lctx, span, hir::ExprBreak(None), attrs)
15571567
}
15581568

15591569
fn expr_call(lctx: &LoweringContext,
15601570
span: Span,
15611571
e: P<hir::Expr>,
1562-
args: Vec<P<hir::Expr>>)
1572+
args: Vec<P<hir::Expr>>,
1573+
attrs: ThinAttributes)
15631574
-> P<hir::Expr> {
1564-
expr(lctx, span, hir::ExprCall(e, args))
1575+
expr(lctx, span, hir::ExprCall(e, args), attrs)
15651576
}
15661577

1567-
fn expr_ident(lctx: &LoweringContext, span: Span, id: Ident) -> P<hir::Expr> {
1568-
expr_path(lctx, path_ident(span, id))
1578+
fn expr_ident(lctx: &LoweringContext, span: Span, id: Ident,
1579+
attrs: ThinAttributes) -> P<hir::Expr> {
1580+
expr_path(lctx, path_ident(span, id), attrs)
15691581
}
15701582

1571-
fn expr_mut_addr_of(lctx: &LoweringContext, span: Span, e: P<hir::Expr>) -> P<hir::Expr> {
1572-
expr(lctx, span, hir::ExprAddrOf(hir::MutMutable, e))
1583+
fn expr_mut_addr_of(lctx: &LoweringContext, span: Span, e: P<hir::Expr>,
1584+
attrs: ThinAttributes) -> P<hir::Expr> {
1585+
expr(lctx, span, hir::ExprAddrOf(hir::MutMutable, e), attrs)
15731586
}
15741587

1575-
fn expr_path(lctx: &LoweringContext, path: hir::Path) -> P<hir::Expr> {
1576-
expr(lctx, path.span, hir::ExprPath(None, path))
1588+
fn expr_path(lctx: &LoweringContext, path: hir::Path,
1589+
attrs: ThinAttributes) -> P<hir::Expr> {
1590+
expr(lctx, path.span, hir::ExprPath(None, path), attrs)
15771591
}
15781592

15791593
fn expr_match(lctx: &LoweringContext,
15801594
span: Span,
15811595
arg: P<hir::Expr>,
15821596
arms: Vec<hir::Arm>,
1583-
source: hir::MatchSource)
1597+
source: hir::MatchSource,
1598+
attrs: ThinAttributes)
15841599
-> P<hir::Expr> {
1585-
expr(lctx, span, hir::ExprMatch(arg, arms, source))
1600+
expr(lctx, span, hir::ExprMatch(arg, arms, source), attrs)
15861601
}
15871602

1588-
fn expr_block(lctx: &LoweringContext, b: P<hir::Block>) -> P<hir::Expr> {
1589-
expr(lctx, b.span, hir::ExprBlock(b))
1603+
fn expr_block(lctx: &LoweringContext, b: P<hir::Block>,
1604+
attrs: ThinAttributes) -> P<hir::Expr> {
1605+
expr(lctx, b.span, hir::ExprBlock(b), attrs)
15901606
}
15911607

1592-
fn expr_tuple(lctx: &LoweringContext, sp: Span, exprs: Vec<P<hir::Expr>>) -> P<hir::Expr> {
1593-
expr(lctx, sp, hir::ExprTup(exprs))
1608+
fn expr_tuple(lctx: &LoweringContext, sp: Span, exprs: Vec<P<hir::Expr>>,
1609+
attrs: ThinAttributes) -> P<hir::Expr> {
1610+
expr(lctx, sp, hir::ExprTup(exprs), attrs)
15941611
}
15951612

1596-
fn expr(lctx: &LoweringContext, span: Span, node: hir::Expr_) -> P<hir::Expr> {
1613+
fn expr(lctx: &LoweringContext, span: Span, node: hir::Expr_,
1614+
attrs: ThinAttributes) -> P<hir::Expr> {
15971615
P(hir::Expr {
15981616
id: lctx.next_id(),
15991617
node: node,
16001618
span: span,
1619+
attrs: attrs,
16011620
})
16021621
}
16031622

16041623
fn stmt_let(lctx: &LoweringContext,
16051624
sp: Span,
16061625
mutbl: bool,
16071626
ident: Ident,
1608-
ex: P<hir::Expr>)
1627+
ex: P<hir::Expr>,
1628+
attrs: ThinAttributes)
16091629
-> P<hir::Stmt> {
16101630
let pat = if mutbl {
16111631
pat_ident_binding_mode(lctx, sp, ident, hir::BindByValue(hir::MutMutable))
@@ -1618,6 +1638,7 @@ fn stmt_let(lctx: &LoweringContext,
16181638
init: Some(ex),
16191639
id: lctx.next_id(),
16201640
span: sp,
1641+
attrs: attrs,
16211642
});
16221643
let decl = respan(sp, hir::DeclLocal(local));
16231644
P(respan(sp, hir::StmtDecl(P(decl), lctx.next_id())))
@@ -1755,7 +1776,8 @@ fn signal_block_expr(lctx: &LoweringContext,
17551776
stmts: Vec<P<hir::Stmt>>,
17561777
expr: P<hir::Expr>,
17571778
span: Span,
1758-
rule: hir::BlockCheckMode)
1779+
rule: hir::BlockCheckMode,
1780+
attrs: ThinAttributes)
17591781
-> P<hir::Expr> {
17601782
let id = lctx.next_id();
17611783
expr_block(lctx,
@@ -1765,7 +1787,8 @@ fn signal_block_expr(lctx: &LoweringContext,
17651787
id: id,
17661788
stmts: stmts,
17671789
expr: Some(expr),
1768-
}))
1790+
}),
1791+
attrs)
17691792
}
17701793

17711794

src/librustc_lint/bad_style.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl LateLintPass for NonCamelCaseTypes {
138138
declare_lint! {
139139
pub NON_SNAKE_CASE,
140140
Warn,
141-
"methods, functions, lifetime parameters and modules should have snake case names"
141+
"variables, methods, functions, lifetime parameters and modules should have snake case names"
142142
}
143143

144144
#[derive(Copy, Clone)]

0 commit comments

Comments
 (0)