Skip to content

Commit

Permalink
Stability lint checker now handles nested macros.
Browse files Browse the repository at this point in the history
Closes #17185.
  • Loading branch information
elinorbgr committed Oct 20, 2014
1 parent 6353465 commit dd55c80
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use syntax::abi;
use syntax::ast_map;
use syntax::attr::AttrMetaMethods;
use syntax::attr;
use syntax::codemap::{Span, NO_EXPANSION};
use syntax::codemap::Span;
use syntax::parse::token;
use syntax::{ast, ast_util, visit};
use syntax::ptr::P;
Expand Down Expand Up @@ -1473,27 +1473,33 @@ impl LintPass for Stability {
}

fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
// skip if `e` is not from macro arguments
let skip = cx.tcx.sess.codemap().with_expn_info(e.span.expn_id, |expninfo| {
// first, check if the given expression was generated by a macro or not
// we need to go back the expn_info tree to check only the arguments
// of the initial macro call, not the nested ones.
let mut expnid = e.span.expn_id;
let mut is_internal = false;
while cx.tcx.sess.codemap().with_expn_info(expnid, |expninfo| {
match expninfo {
Some(ref info) => {
if info.call_site.expn_id != NO_EXPANSION ||
!( e.span.lo > info.call_site.lo && e.span.hi < info.call_site.hi ) {
// This code is not from the arguments,
// or this macro call was generated by an other macro
// We can't handle it.
true
} else if info.callee.span.is_none() {
// We don't want to mess with compiler builtins.
true
// save the parent expn_id for next loop iteration
expnid = info.call_site.expn_id;
if info.callee.span.is_none() {
// it's a compiler built-in, we *really* don't want to mess with it
// so we skip it, unless it was called by a regular macro, in which case
// we will handle the caller macro next turn
is_internal = true;
true // continue looping
} else {
false
// was this expression from the current macro arguments ?
is_internal = !( e.span.lo > info.call_site.lo &&
e.span.hi < info.call_site.hi );
true // continue looping
}
},
_ => { false }
_ => false // stop looping
}
});
if skip { return; }
}) { /* empty while loop body */ }
if is_internal { return; }

let mut span = e.span;

Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/lint-stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ mod cross_crate {
// on macros themselves are not yet linted.
macro_test!();
macro_test_arg!(deprecated_text()); //~ ERROR use of deprecated item: text
macro_test_arg!(macro_test_arg!(deprecated_text())); //~ ERROR use of deprecated item: text
macro_test_arg_nested!(deprecated_text);
}

Expand Down

5 comments on commit dd55c80

@bors
Copy link
Contributor

@bors bors commented on dd55c80 Oct 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pnkfelix
at elinorbgr@dd55c80

@bors
Copy link
Contributor

@bors bors commented on dd55c80 Oct 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging vberger/rust/stability_lint_for_nested_macros = dd55c80 into auto

@bors
Copy link
Contributor

@bors bors commented on dd55c80 Oct 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vberger/rust/stability_lint_for_nested_macros = dd55c80 merged ok, testing candidate = b69cd73

@bors
Copy link
Contributor

@bors bors commented on dd55c80 Oct 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on dd55c80 Oct 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = b69cd73

Please sign in to comment.