Skip to content

Commit 90bfcec

Browse files
committed
rollup merge of rust-lang#18679 : brson/lint-trait
2 parents 29b2b58 + f383ce6 commit 90bfcec

File tree

3 files changed

+72
-27
lines changed

3 files changed

+72
-27
lines changed

src/librustc/lint/builtin.rs

+54-27
Original file line numberDiff line numberDiff line change
@@ -1581,34 +1581,12 @@ impl Stability {
15811581

15821582
cx.span_lint(lint, span, msg.as_slice());
15831583
}
1584-
}
1585-
1586-
impl LintPass for Stability {
1587-
fn get_lints(&self) -> LintArray {
1588-
lint_array!(DEPRECATED, EXPERIMENTAL, UNSTABLE)
1589-
}
1590-
1591-
fn check_view_item(&mut self, cx: &Context, item: &ast::ViewItem) {
1592-
// compiler-generated `extern crate` statements have a dummy span.
1593-
if item.span == DUMMY_SP { return }
1594-
1595-
let id = match item.node {
1596-
ast::ViewItemExternCrate(_, _, id) => id,
1597-
ast::ViewItemUse(..) => return,
1598-
};
1599-
let cnum = match cx.tcx.sess.cstore.find_extern_mod_stmt_cnum(id) {
1600-
Some(cnum) => cnum,
1601-
None => return,
1602-
};
1603-
let id = ast::DefId { krate: cnum, node: ast::CRATE_NODE_ID };
1604-
self.lint(cx, id, item.span);
1605-
}
16061584

1607-
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
1585+
fn is_internal(&self, cx: &Context, span: Span) -> bool {
16081586
// first, check if the given expression was generated by a macro or not
16091587
// we need to go back the expn_info tree to check only the arguments
16101588
// of the initial macro call, not the nested ones.
1611-
let mut expnid = e.span.expn_id;
1589+
let mut expnid = span.expn_id;
16121590
let mut is_internal = false;
16131591
while cx.tcx.sess.codemap().with_expn_info(expnid, |expninfo| {
16141592
match expninfo {
@@ -1623,15 +1601,41 @@ impl LintPass for Stability {
16231601
true // continue looping
16241602
} else {
16251603
// was this expression from the current macro arguments ?
1626-
is_internal = !( e.span.lo > info.call_site.lo &&
1627-
e.span.hi < info.call_site.hi );
1604+
is_internal = !( span.lo > info.call_site.lo &&
1605+
span.hi < info.call_site.hi );
16281606
true // continue looping
16291607
}
16301608
},
16311609
_ => false // stop looping
16321610
}
16331611
}) { /* empty while loop body */ }
1634-
if is_internal { return; }
1612+
return is_internal;
1613+
}
1614+
}
1615+
1616+
impl LintPass for Stability {
1617+
fn get_lints(&self) -> LintArray {
1618+
lint_array!(DEPRECATED, EXPERIMENTAL, UNSTABLE)
1619+
}
1620+
1621+
fn check_view_item(&mut self, cx: &Context, item: &ast::ViewItem) {
1622+
// compiler-generated `extern crate` statements have a dummy span.
1623+
if item.span == DUMMY_SP { return }
1624+
1625+
let id = match item.node {
1626+
ast::ViewItemExternCrate(_, _, id) => id,
1627+
ast::ViewItemUse(..) => return,
1628+
};
1629+
let cnum = match cx.tcx.sess.cstore.find_extern_mod_stmt_cnum(id) {
1630+
Some(cnum) => cnum,
1631+
None => return,
1632+
};
1633+
let id = ast::DefId { krate: cnum, node: ast::CRATE_NODE_ID };
1634+
self.lint(cx, id, item.span);
1635+
}
1636+
1637+
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
1638+
if self.is_internal(cx, e.span) { return; }
16351639

16361640
let mut span = e.span;
16371641

@@ -1677,6 +1681,29 @@ impl LintPass for Stability {
16771681
};
16781682
self.lint(cx, id, span);
16791683
}
1684+
1685+
fn check_item(&mut self, cx: &Context, item: &ast::Item) {
1686+
if self.is_internal(cx, item.span) { return }
1687+
1688+
match item.node {
1689+
ast::ItemTrait(_, _, ref supertraits, _) => {
1690+
for t in supertraits.iter() {
1691+
match *t {
1692+
ast::TraitTyParamBound(ref t) => {
1693+
let id = ty::trait_ref_to_def_id(cx.tcx, t);
1694+
self.lint(cx, id, t.path.span);
1695+
}
1696+
_ => (/* pass */)
1697+
}
1698+
}
1699+
}
1700+
ast::ItemImpl(_, Some(ref t), _, _) => {
1701+
let id = ty::trait_ref_to_def_id(cx.tcx, t);
1702+
self.lint(cx, id, t.path.span);
1703+
}
1704+
_ => (/* pass */)
1705+
}
1706+
}
16801707
}
16811708

16821709
declare_lint!(pub UNUSED_IMPORTS, Warn,

src/test/auxiliary/lint_stability.rs

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ pub trait Trait {
118118

119119
impl Trait for MethodTester {}
120120

121+
#[experimental]
122+
pub trait ExperimentalTrait {}
123+
121124
#[deprecated]
122125
pub struct DeprecatedStruct { pub i: int }
123126
#[experimental]

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

+15
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ mod cross_crate {
141141
foo.trait_unmarked(); //~ ERROR use of unmarked item
142142
foo.trait_stable();
143143
}
144+
145+
struct S;
146+
147+
impl ExperimentalTrait for S { } //~ ERROR use of experimental item
148+
149+
trait LocalTrait : ExperimentalTrait { } //~ ERROR use of experimental item
144150
}
145151

146152
mod inheritance {
@@ -444,6 +450,15 @@ mod this_crate {
444450
foo.trait_unmarked();
445451
foo.trait_stable();
446452
}
453+
454+
#[deprecated]
455+
pub trait DeprecatedTrait {}
456+
457+
struct S;
458+
459+
impl DeprecatedTrait for S { } //~ ERROR use of deprecated item
460+
461+
trait LocalTrait : DeprecatedTrait { } //~ ERROR use of deprecated item
447462
}
448463

449464
fn main() {}

0 commit comments

Comments
 (0)