Skip to content

Commit 0232744

Browse files
committed
Changed check_stability to take an Option<NodeId> instead of NodeId.
This clarifies the intent of whether to emit deprecated lint or not.
1 parent abf4d8b commit 0232744

File tree

6 files changed

+43
-33
lines changed

6 files changed

+43
-33
lines changed

src/librustc/middle/stability.rs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -527,17 +527,21 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
527527

528528
/// Evaluates the stability of an item.
529529
///
530-
/// Returns `None` if the item is stable, or unstable but the corresponding `#![feature]` has
531-
/// been provided. Returns the tuple `Some((feature, reason, issue))` of the offending unstable
532-
/// feature otherwise.
533-
pub fn eval_stability(self, def_id: DefId, id: NodeId, span: Span) -> EvalResult {
530+
/// Returns `EvalResult::Allow` if the item is stable, or unstable but the corresponding
531+
/// `#![feature]` has been provided. Returns `EvalResult::Deny` which describes the offending
532+
/// unstable feature otherwise.
533+
///
534+
/// If `id` is `Some(_)`, this function will also check if the item at `def_id` has been
535+
/// deprecated. If the item is indeed deprecated, we will emit a deprecation lint attached to
536+
/// `id`.
537+
pub fn eval_stability(self, def_id: DefId, id: Option<NodeId>, span: Span) -> EvalResult {
534538
if span.allows_unstable() {
535539
debug!("stability: \
536540
skipping span={:?} since it is internal", span);
537541
return EvalResult::Allow;
538542
}
539543

540-
let lint_deprecated = |def_id: DefId, note: Option<Symbol>| {
544+
let lint_deprecated = |def_id: DefId, id: NodeId, note: Option<Symbol>| {
541545
let path = self.item_path_str(def_id);
542546

543547
let msg = if let Some(note) = note {
@@ -547,22 +551,21 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
547551
};
548552

549553
self.lint_node(lint::builtin::DEPRECATED, id, span, &msg);
554+
if id == ast::DUMMY_NODE_ID {
555+
span_bug!(span, "emitted a deprecated lint with dummy node id: {:?}", def_id);
556+
}
550557
};
551558

552559
// Deprecated attributes apply in-crate and cross-crate.
553-
if let Some(depr_entry) = self.lookup_deprecation_entry(def_id) {
554-
let skip = if id == ast::DUMMY_NODE_ID {
555-
true
556-
} else {
560+
if let Some(id) = id {
561+
if let Some(depr_entry) = self.lookup_deprecation_entry(def_id) {
557562
let parent_def_id = self.hir.local_def_id(self.hir.get_parent(id));
558-
self.lookup_deprecation_entry(parent_def_id).map_or(false, |parent_depr| {
559-
parent_depr.same_origin(&depr_entry)
560-
})
563+
let skip = self.lookup_deprecation_entry(parent_def_id)
564+
.map_or(false, |parent_depr| parent_depr.same_origin(&depr_entry));
565+
if !skip {
566+
lint_deprecated(def_id, id, depr_entry.attr.note);
567+
}
561568
};
562-
563-
if !skip {
564-
lint_deprecated(def_id, depr_entry.attr.note);
565-
}
566569
}
567570

568571
let is_staged_api = self.lookup_stability(DefId {
@@ -579,8 +582,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
579582

580583
if let Some(&Stability{rustc_depr: Some(attr::RustcDeprecation { reason, .. }), ..})
581584
= stability {
582-
if id != ast::DUMMY_NODE_ID {
583-
lint_deprecated(def_id, Some(reason));
585+
if let Some(id) = id {
586+
lint_deprecated(def_id, id, Some(reason));
584587
}
585588
}
586589

@@ -629,7 +632,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
629632
}
630633
}
631634

632-
pub fn check_stability(self, def_id: DefId, id: NodeId, span: Span) {
635+
/// Checks if an item is stable or error out.
636+
///
637+
/// If the item defined by `def_id` is unstable and the corresponding `#![feature]` does not
638+
/// exist, emits an error.
639+
///
640+
/// Additionally, this function will also check if the item is deprecated. If so, and `id` is
641+
/// not `None`, a deprecated lint attached to `id` will be emitted.
642+
pub fn check_stability(self, def_id: DefId, id: Option<NodeId>, span: Span) {
633643
match self.eval_stability(def_id, id, span) {
634644
EvalResult::Allow => {}
635645
EvalResult::Deny { feature, reason, issue } => {
@@ -687,7 +697,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
687697
None => return,
688698
};
689699
let def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
690-
self.tcx.check_stability(def_id, item.id, item.span);
700+
self.tcx.check_stability(def_id, Some(item.id), item.span);
691701
}
692702

693703
// For implementations of traits, check the stability of each item
@@ -700,8 +710,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
700710
let trait_item_def_id = self.tcx.associated_items(trait_did)
701711
.find(|item| item.name == impl_item.name).map(|item| item.def_id);
702712
if let Some(def_id) = trait_item_def_id {
703-
// Pass `DUMMY_NODE_ID` to skip deprecation warnings.
704-
self.tcx.check_stability(def_id, ast::DUMMY_NODE_ID, impl_item.span);
713+
// Pass `None` to skip deprecation warnings.
714+
self.tcx.check_stability(def_id, None, impl_item.span);
705715
}
706716
}
707717
}
@@ -737,7 +747,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
737747
match path.def {
738748
Def::Local(..) | Def::Upvar(..) |
739749
Def::PrimTy(..) | Def::SelfTy(..) | Def::Err => {}
740-
_ => self.tcx.check_stability(path.def.def_id(), id, path.span)
750+
_ => self.tcx.check_stability(path.def.def_id(), Some(id), path.span)
741751
}
742752
intravisit::walk_path(self, path)
743753
}

src/librustc_typeck/astconv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
530530
let msg = format!("associated type `{}` is private", binding.item_name);
531531
tcx.sess.span_err(binding.span, &msg);
532532
}
533-
tcx.check_stability(assoc_ty.def_id, ref_id, binding.span);
533+
tcx.check_stability(assoc_ty.def_id, Some(ref_id), binding.span);
534534

535535
Ok(candidate.map_bound(|trait_ref| {
536536
ty::ProjectionPredicate {
@@ -868,7 +868,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
868868
let msg = format!("{} `{}` is private", def.kind_name(), assoc_name);
869869
tcx.sess.span_err(span, &msg);
870870
}
871-
tcx.check_stability(item.def_id, ref_id, span);
871+
tcx.check_stability(item.def_id, Some(ref_id), span);
872872

873873
(ty, def)
874874
}

src/librustc_typeck/check/_match.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
861861
let field_ty = self.field_ty(subpat.span, &variant.fields[i], substs);
862862
self.check_pat_walk(&subpat, field_ty, def_bm, true);
863863

864-
self.tcx.check_stability(variant.fields[i].did, pat.id, subpat.span);
864+
self.tcx.check_stability(variant.fields[i].did, Some(pat.id), subpat.span);
865865
}
866866
} else {
867867
let subpats_ending = if subpats.len() == 1 { "" } else { "s" };
@@ -923,7 +923,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
923923
vacant.insert(span);
924924
field_map.get(&field.name)
925925
.map(|f| {
926-
self.tcx.check_stability(f.did, pat_id, span);
926+
self.tcx.check_stability(f.did, Some(pat_id), span);
927927

928928
self.field_ty(span, f, substs)
929929
})

src/librustc_typeck/check/method/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
171171
.unwrap().insert(import_def_id);
172172
}
173173

174-
self.tcx.check_stability(pick.item.def_id, call_expr.id, span);
174+
self.tcx.check_stability(pick.item.def_id, Some(call_expr.id), span);
175175

176176
let result = self.confirm_method(span,
177177
self_expr,
@@ -371,7 +371,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
371371
}
372372

373373
let def = pick.item.def();
374-
self.tcx.check_stability(def.def_id(), expr_id, span);
374+
self.tcx.check_stability(def.def_id(), Some(expr_id), span);
375375

376376
Ok(def)
377377
}

src/librustc_typeck/check/method/probe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
10081008
if let Some(uc) = unstable_candidates {
10091009
applicable_candidates.retain(|&(p, _)| {
10101010
if let stability::EvalResult::Deny { feature, .. } =
1011-
self.tcx.eval_stability(p.item.def_id, ast::DUMMY_NODE_ID, self.span)
1011+
self.tcx.eval_stability(p.item.def_id, None, self.span)
10121012
{
10131013
uc.push((p, feature));
10141014
return false;

src/librustc_typeck/check/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,7 +3078,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
30783078
self.apply_adjustments(base, adjustments);
30793079
autoderef.finalize();
30803080

3081-
self.tcx.check_stability(field.did, expr.id, expr.span);
3081+
self.tcx.check_stability(field.did, Some(expr.id), expr.span);
30823082

30833083
return field_ty;
30843084
}
@@ -3219,7 +3219,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
32193219
if let Some(field) = fields.iter().find(|f| f.name.to_ident() == ident) {
32203220
let field_ty = self.field_ty(expr.span, field, substs);
32213221
if field.vis.is_accessible_from(def_scope, self.tcx) {
3222-
self.tcx.check_stability(field.did, expr.id, expr.span);
3222+
self.tcx.check_stability(field.did, Some(expr.id), expr.span);
32233223
Some(field_ty)
32243224
} else {
32253225
private_candidate = Some((base_def.did, field_ty));
@@ -3364,7 +3364,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
33643364
// struct-like enums (yet...), but it's definitely not
33653365
// a bug to have construct one.
33663366
if adt_kind != ty::AdtKind::Enum {
3367-
tcx.check_stability(v_field.did, expr_id, field.span);
3367+
tcx.check_stability(v_field.did, Some(expr_id), field.span);
33683368
}
33693369

33703370
self.field_ty(field.span, v_field, substs)

0 commit comments

Comments
 (0)