Skip to content

Commit b2df63b

Browse files
committed
Add generics field to LateContext
1 parent 2e6a1a9 commit b2df63b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/librustc/hir/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1854,6 +1854,19 @@ impl Item_ {
18541854
_ => None,
18551855
}
18561856
}
1857+
1858+
pub fn generics(&self) -> Option<&Generics> {
1859+
Some(match *self {
1860+
ItemFn(_, _, _, _, ref generics, _) |
1861+
ItemTy(_, ref generics) |
1862+
ItemEnum(_, ref generics) |
1863+
ItemStruct(_, ref generics) |
1864+
ItemUnion(_, ref generics) |
1865+
ItemTrait(_, ref generics, _, _) |
1866+
ItemImpl(_, _, _, ref generics, _, _, _)=> generics,
1867+
_ => return None
1868+
})
1869+
}
18571870
}
18581871

18591872
/// A reference from an trait to one of its associated items. This

src/librustc/lint/context.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ pub struct LateContext<'a, 'tcx: 'a> {
352352
lint_sess: LintSession<'tcx, LateLintPassObject>,
353353

354354
last_ast_node_with_lint_attrs: ast::NodeId,
355+
356+
/// Generic type parameters in scope for the item we are in.
357+
pub generics: Option<&'tcx hir::Generics>,
355358
}
356359

357360
/// Context for lint checking of the AST, after expansion, before lowering to
@@ -646,13 +649,16 @@ impl<'a, 'tcx> hir_visit::Visitor<'tcx> for LateContext<'a, 'tcx> {
646649
}
647650

648651
fn visit_item(&mut self, it: &'tcx hir::Item) {
652+
let generics = self.generics.take();
653+
self.generics = it.node.generics();
649654
self.with_lint_attrs(it.id, &it.attrs, |cx| {
650655
cx.with_param_env(it.id, |cx| {
651656
run_lints!(cx, check_item, late_passes, it);
652657
hir_visit::walk_item(cx, it);
653658
run_lints!(cx, check_item_post, late_passes, it);
654659
});
655-
})
660+
});
661+
self.generics = generics;
656662
}
657663

658664
fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem) {
@@ -774,23 +780,29 @@ impl<'a, 'tcx> hir_visit::Visitor<'tcx> for LateContext<'a, 'tcx> {
774780
}
775781

776782
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
783+
let generics = self.generics.take();
784+
self.generics = Some(&trait_item.generics);
777785
self.with_lint_attrs(trait_item.id, &trait_item.attrs, |cx| {
778786
cx.with_param_env(trait_item.id, |cx| {
779787
run_lints!(cx, check_trait_item, late_passes, trait_item);
780788
hir_visit::walk_trait_item(cx, trait_item);
781789
run_lints!(cx, check_trait_item_post, late_passes, trait_item);
782790
});
783791
});
792+
self.generics = generics;
784793
}
785794

786795
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
796+
let generics = self.generics.take();
797+
self.generics = Some(&impl_item.generics);
787798
self.with_lint_attrs(impl_item.id, &impl_item.attrs, |cx| {
788799
cx.with_param_env(impl_item.id, |cx| {
789800
run_lints!(cx, check_impl_item, late_passes, impl_item);
790801
hir_visit::walk_impl_item(cx, impl_item);
791802
run_lints!(cx, check_impl_item_post, late_passes, impl_item);
792803
});
793804
});
805+
self.generics = generics;
794806
}
795807

796808
fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) {
@@ -991,6 +1003,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
9911003
access_levels,
9921004
lint_sess: LintSession::new(&tcx.sess.lint_store),
9931005
last_ast_node_with_lint_attrs: ast::CRATE_NODE_ID,
1006+
generics: None,
9941007
};
9951008

9961009
// Visit the whole crate.

0 commit comments

Comments
 (0)