Skip to content

Commit c6b65ac

Browse files
committed
process trait/impl items directly from the visitor callback
The current setup processes impl/trait items while visiting the impl/trait. This means we basically have this setup: <Lots> -> TypeckItemBody(Impl) -> Tables(ImplItem{0,1,2,3}) But this was largely an artifact of the older code. By moving the processing of items into method dedicated for their use, we produce this setup: <Little> -> TypeckItemBody(ImplItem0) -> Tables(ImplItem0) ... <Little> -> TypeckItemBody(ImplItem3) -> Tables(ImplItem3)
1 parent fc57e40 commit c6b65ac

File tree

2 files changed

+58
-55
lines changed

2 files changed

+58
-55
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -570,16 +570,43 @@ impl<'a, 'tcx> Visitor<'tcx> for CheckItemTypesVisitor<'a, 'tcx> {
570570
}
571571

572572
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for CheckItemBodiesVisitor<'a, 'tcx> {
573-
fn visit_item(&mut self, i: &'tcx hir::Item) {
574-
check_item_body(self.ccx, i);
573+
fn visit_item(&mut self, item: &'tcx hir::Item) {
574+
match item.node {
575+
hir::ItemFn(ref decl, .., body_id) => {
576+
check_bare_fn(self.ccx, &decl, body_id, item.id, item.span);
577+
}
578+
_ => { }
579+
}
575580
}
576581

577-
fn visit_trait_item(&mut self, _item: &'tcx hir::TraitItem) {
578-
// done as part of `visit_item` above
582+
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
583+
match trait_item.node {
584+
hir::TraitItemKind::Const(_, Some(expr)) => {
585+
check_const(self.ccx, expr, trait_item.id)
586+
}
587+
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body_id)) => {
588+
check_bare_fn(self.ccx, &sig.decl, body_id, trait_item.id, trait_item.span);
589+
}
590+
hir::TraitItemKind::Method(_, hir::TraitMethod::Required(_)) |
591+
hir::TraitItemKind::Const(_, None) |
592+
hir::TraitItemKind::Type(..) => {
593+
// Nothing to do.
594+
}
595+
}
579596
}
580597

581-
fn visit_impl_item(&mut self, _item: &'tcx hir::ImplItem) {
582-
// done as part of `visit_item` above
598+
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
599+
match impl_item.node {
600+
hir::ImplItemKind::Const(_, expr) => {
601+
check_const(self.ccx, expr, impl_item.id)
602+
}
603+
hir::ImplItemKind::Method(ref sig, body_id) => {
604+
check_bare_fn(self.ccx, &sig.decl, body_id, impl_item.id, impl_item.span);
605+
}
606+
hir::ImplItemKind::Type(_) => {
607+
// Nothing to do here.
608+
}
609+
}
583610
}
584611
}
585612

@@ -897,55 +924,6 @@ pub fn check_item_type<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx hir::Item) {
897924
}
898925
}
899926

900-
pub fn check_item_body<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx hir::Item) {
901-
debug!("check_item_body(it.id={}, it.name={})",
902-
it.id,
903-
ccx.tcx.item_path_str(ccx.tcx.map.local_def_id(it.id)));
904-
let _indenter = indenter();
905-
match it.node {
906-
hir::ItemFn(ref decl, .., body_id) => {
907-
check_bare_fn(ccx, &decl, body_id, it.id, it.span);
908-
}
909-
hir::ItemImpl(.., ref impl_item_refs) => {
910-
debug!("ItemImpl {} with id {}", it.name, it.id);
911-
912-
for impl_item_ref in impl_item_refs {
913-
let impl_item = ccx.tcx.map.impl_item(impl_item_ref.id);
914-
match impl_item.node {
915-
hir::ImplItemKind::Const(_, expr) => {
916-
check_const(ccx, expr, impl_item.id)
917-
}
918-
hir::ImplItemKind::Method(ref sig, body_id) => {
919-
check_bare_fn(ccx, &sig.decl, body_id, impl_item.id, impl_item.span);
920-
}
921-
hir::ImplItemKind::Type(_) => {
922-
// Nothing to do here.
923-
}
924-
}
925-
}
926-
}
927-
hir::ItemTrait(.., ref trait_item_refs) => {
928-
for trait_item_ref in trait_item_refs {
929-
let trait_item = ccx.tcx.map.trait_item(trait_item_ref.id);
930-
match trait_item.node {
931-
hir::TraitItemKind::Const(_, Some(expr)) => {
932-
check_const(ccx, expr, trait_item.id)
933-
}
934-
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body_id)) => {
935-
check_bare_fn(ccx, &sig.decl, body_id, trait_item.id, trait_item.span);
936-
}
937-
hir::TraitItemKind::Method(_, hir::TraitMethod::Required(_)) |
938-
hir::TraitItemKind::Const(_, None) |
939-
hir::TraitItemKind::Type(..) => {
940-
// Nothing to do.
941-
}
942-
}
943-
}
944-
}
945-
_ => {/* nothing to do */ }
946-
}
947-
}
948-
949927
fn check_on_unimplemented<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
950928
def_id: DefId,
951929
item: &hir::Item) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![feature(rustc_attrs)]
2+
3+
struct Foo {
4+
x: u8
5+
}
6+
7+
impl Foo {
8+
// Changing the item `new`...
9+
#[rustc_if_this_changed(HirBody)]
10+
fn new() -> Foo {
11+
Foo { x: 0 }
12+
}
13+
14+
// ...should not cause us to recompute the tables for `with`!
15+
#[rustc_then_this_would_need(Tables)] //~ ERROR no path
16+
fn with(x: u8) -> Foo {
17+
Foo { x: x }
18+
}
19+
}
20+
21+
fn main() {
22+
let f = Foo::new();
23+
let g = Foo::with(22);
24+
assert_eq!(f.x, g.x - 22);
25+
}

0 commit comments

Comments
 (0)