Skip to content

Commit eb58ac1

Browse files
committed
Lint stability now checks macro arguments.
Closes #17185.
1 parent 3907a13 commit eb58ac1

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/librustc/lint/builtin.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,8 +1490,27 @@ impl LintPass for Stability {
14901490
}
14911491

14921492
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
1493-
// if the expression was produced by a macro expansion,
1494-
if e.span.expn_id != NO_EXPANSION { return }
1493+
// skip if `e` is not from macro arguments
1494+
let skip = cx.tcx.sess.codemap().with_expn_info(e.span.expn_id, |expninfo| {
1495+
match expninfo {
1496+
Some(ref info) => {
1497+
if info.call_site.expn_id != NO_EXPANSION ||
1498+
!( e.span.lo > info.call_site.lo && e.span.hi < info.call_site.hi ) {
1499+
// This code is not from the arguments,
1500+
// or this macro call was generated by an other macro
1501+
// We can't handle it.
1502+
true
1503+
} else if info.callee.span.is_none() {
1504+
// We don't want to mess with compiler builtins.
1505+
true
1506+
} else {
1507+
false
1508+
}
1509+
},
1510+
_ => { false }
1511+
}
1512+
});
1513+
if skip { return; }
14951514

14961515
let id = match e.node {
14971516
ast::ExprPath(..) | ast::ExprStruct(..) => {

src/test/auxiliary/lint_stability.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,13 @@ pub struct LockedTupleStruct(pub int);
181181
macro_rules! macro_test(
182182
() => (deprecated());
183183
)
184+
185+
#[macro_export]
186+
macro_rules! macro_test_arg(
187+
($func:expr) => ($func);
188+
)
189+
190+
#[macro_export]
191+
macro_rules! macro_test_arg_nested(
192+
($func:ident) => (macro_test_arg!($func()));
193+
)

src/test/compile-fail/lint-stability.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,14 @@ mod cross_crate {
109109
let _ = FrozenTupleStruct (1);
110110
let _ = LockedTupleStruct (1);
111111

112-
// At the moment, the following just checks that the stability
113-
// level of expanded code does not trigger the
114-
// lint. Eventually, we will want to lint the contents of the
112+
// At the moment, the lint checker only checks stability in
113+
// in the arguments of macros.
114+
// Eventually, we will want to lint the contents of the
115115
// macro in the module *defining* it. Also, stability levels
116116
// on macros themselves are not yet linted.
117117
macro_test!();
118+
macro_test_arg!(deprecated_text()); //~ ERROR use of deprecated item: text
119+
macro_test_arg_nested!(deprecated_text);
118120
}
119121

120122
fn test_method_param<F: Trait>(foo: F) {

0 commit comments

Comments
 (0)