Skip to content

Commit

Permalink
ignore deprecation for items deprecated by the same attribute
Browse files Browse the repository at this point in the history
Whenever a node whould be reported as deprecated:

- check if the parent item is also deprecated

- if it is and both were deprecated by the same attribute

- skip the deprecation warning

fixes rust-lang#35128
closes rust-lang#16490
  • Loading branch information
TimNN committed Aug 4, 2016
1 parent 2ebbb3c commit 4b8c561
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
43 changes: 23 additions & 20 deletions src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ impl<'a, 'v, 'tcx> Visitor<'v> for Checker<'a, 'tcx> {

let old_parent_def = self.enter_item(item.id);

check_item(self.tcx, item, true,
check_item(self.tcx, item, true, self.parent_def(),
&mut |id, sp, stab, depr| self.check(id, sp, stab, depr));
intravisit::walk_item(self, item);

Expand All @@ -500,25 +500,25 @@ impl<'a, 'v, 'tcx> Visitor<'v> for Checker<'a, 'tcx> {
}

fn visit_expr(&mut self, ex: &hir::Expr) {
check_expr(self.tcx, ex,
check_expr(self.tcx, ex, self.parent_def(),
&mut |id, sp, stab, depr| self.check(id, sp, stab, depr));
intravisit::walk_expr(self, ex);
}

fn visit_path(&mut self, path: &hir::Path, id: ast::NodeId) {
check_path(self.tcx, path, id,
check_path(self.tcx, path, id, self.parent_def(),
&mut |id, sp, stab, depr| self.check(id, sp, stab, depr));
intravisit::walk_path(self, path)
}

fn visit_path_list_item(&mut self, prefix: &hir::Path, item: &hir::PathListItem) {
check_path_list_item(self.tcx, item,
check_path_list_item(self.tcx, item, self.parent_def(),
&mut |id, sp, stab, depr| self.check(id, sp, stab, depr));
intravisit::walk_path_list_item(self, prefix, item)
}

fn visit_pat(&mut self, pat: &hir::Pat) {
check_pat(self.tcx, pat,
check_pat(self.tcx, pat, self.parent_def(),
&mut |id, sp, stab, depr| self.check(id, sp, stab, depr));
intravisit::walk_pat(self, pat)
}
Expand All @@ -543,6 +543,7 @@ impl<'a, 'v, 'tcx> Visitor<'v> for Checker<'a, 'tcx> {
pub fn check_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
item: &hir::Item,
warn_about_defns: bool,
parent_def: DefIndex,
cb: &mut FnMut(DefId, Span,
&Option<&Stability>,
&Option<Deprecation>)) {
Expand All @@ -556,7 +557,7 @@ pub fn check_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
None => return,
};
let id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
maybe_do_stability_check(tcx, id, item.span, cb);
maybe_do_stability_check(tcx, id, parent_def, item.span, cb);
}

// For implementations of traits, check the stability of each item
Expand All @@ -571,7 +572,8 @@ pub fn check_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
item.name() == impl_item.name
}).unwrap();
if warn_about_defns {
maybe_do_stability_check(tcx, item.def_id(), impl_item.span, cb);
maybe_do_stability_check(tcx, item.def_id(), item.def_id().index,
impl_item.span, cb);
}
}
}
Expand All @@ -581,7 +583,7 @@ pub fn check_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}

/// Helper for discovering nodes to check for stability
pub fn check_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, e: &hir::Expr,
pub fn check_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, e: &hir::Expr, parent_def: DefIndex,
cb: &mut FnMut(DefId, Span,
&Option<&Stability>,
&Option<Deprecation>)) {
Expand Down Expand Up @@ -620,7 +622,7 @@ pub fn check_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, e: &hir::Expr,
let did = def.struct_variant()
.field_named(field.name.node)
.did;
maybe_do_stability_check(tcx, did, field.span, cb);
maybe_do_stability_check(tcx, did, parent_def, field.span, cb);
}

// we're done.
Expand All @@ -641,11 +643,11 @@ pub fn check_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, e: &hir::Expr,
_ => return
};

maybe_do_stability_check(tcx, id, span, cb);
maybe_do_stability_check(tcx, id, parent_def, span, cb);
}

pub fn check_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
path: &hir::Path, id: ast::NodeId,
pub fn check_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, path: &hir::Path,
id: ast::NodeId, parent_def: DefIndex,
cb: &mut FnMut(DefId, Span,
&Option<&Stability>,
&Option<Deprecation>)) {
Expand All @@ -654,26 +656,27 @@ pub fn check_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
Some(Def::PrimTy(..)) => {}
Some(Def::SelfTy(..)) => {}
Some(def) => {
maybe_do_stability_check(tcx, def.def_id(), path.span, cb);
maybe_do_stability_check(tcx, def.def_id(), parent_def, path.span, cb);
}
None => {}
}
}

pub fn check_path_list_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
item: &hir::PathListItem,
parent_def: DefIndex,
cb: &mut FnMut(DefId, Span,
&Option<&Stability>,
&Option<Deprecation>)) {
match tcx.expect_def(item.node.id()) {
Def::PrimTy(..) => {}
def => {
maybe_do_stability_check(tcx, def.def_id(), item.span, cb);
maybe_do_stability_check(tcx, def.def_id(), parent_def, item.span, cb);
}
}
}

pub fn check_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, pat: &hir::Pat,
pub fn check_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, pat: &hir::Pat, parent_def: DefIndex,
cb: &mut FnMut(DefId, Span,
&Option<&Stability>,
&Option<Deprecation>)) {
Expand All @@ -688,23 +691,23 @@ pub fn check_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, pat: &hir::Pat,
// Foo(a, b, c)
PatKind::TupleStruct(_, ref pat_fields, ddpos) => {
for (i, field) in pat_fields.iter().enumerate_and_adjust(v.fields.len(), ddpos) {
maybe_do_stability_check(tcx, v.fields[i].did, field.span, cb)
maybe_do_stability_check(tcx, v.fields[i].did, parent_def, field.span, cb)
}
}
// Foo { a, b, c }
PatKind::Struct(_, ref pat_fields, _) => {
for field in pat_fields {
let did = v.field_named(field.node.name).did;
maybe_do_stability_check(tcx, did, field.span, cb);
maybe_do_stability_check(tcx, did, parent_def, field.span, cb);
}
}
// everything else is fine.
_ => {}
}
}

fn maybe_do_stability_check<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
id: DefId, span: Span,
fn maybe_do_stability_check<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefId,
parent_def: DefIndex, span: Span,
cb: &mut FnMut(DefId, Span,
&Option<&Stability>,
&Option<Deprecation>)) {
Expand All @@ -716,7 +719,7 @@ fn maybe_do_stability_check<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let (stability, deprecation) = if is_staged_api(tcx, id) {
(tcx.lookup_stability(id), None)
} else {
(None, tcx.lookup_deprecation(id))
(None, tcx.lookup_maybe_deprecation(id, parent_def))
};
debug!("maybe_do_stability_check: \
inspecting id={:?} span={:?} of stability={:?}", id, span, stability);
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ impl LintPass for Deprecated {
impl LateLintPass for Deprecated {
fn check_item(&mut self, cx: &LateContext, item: &hir::Item) {
self.push_item(cx, item.id);
stability::check_item(cx.tcx, item, false,
stability::check_item(cx.tcx, item, false, self.parent_def(),
&mut |id, sp, stab, depr|
self.lint(cx, id, sp, &stab, &depr));
}
Expand All @@ -652,25 +652,25 @@ impl LateLintPass for Deprecated {
}

fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
stability::check_expr(cx.tcx, e,
stability::check_expr(cx.tcx, e, self.parent_def(),
&mut |id, sp, stab, depr|
self.lint(cx, id, sp, &stab, &depr));
}

fn check_path(&mut self, cx: &LateContext, path: &hir::Path, id: ast::NodeId) {
stability::check_path(cx.tcx, path, id,
stability::check_path(cx.tcx, path, id, self.parent_def(),
&mut |id, sp, stab, depr|
self.lint(cx, id, sp, &stab, &depr));
}

fn check_path_list_item(&mut self, cx: &LateContext, item: &hir::PathListItem) {
stability::check_path_list_item(cx.tcx, item,
stability::check_path_list_item(cx.tcx, item, self.parent_def(),
&mut |id, sp, stab, depr|
self.lint(cx, id, sp, &stab, &depr));
}

fn check_pat(&mut self, cx: &LateContext, pat: &hir::Pat) {
stability::check_pat(cx.tcx, pat,
stability::check_pat(cx.tcx, pat, self.parent_def(),
&mut |id, sp, stab, depr|
self.lint(cx, id, sp, &stab, &depr));
}
Expand Down

0 comments on commit 4b8c561

Please sign in to comment.