From 2ec5eab368b3320950007b07b00600b9a981827a Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 22 Jun 2018 12:03:44 +0200 Subject: [PATCH] Also place method impl trait into the surrounding module --- src/librustc/hir/lowering.rs | 62 ++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index a21d2b691351..b7260dded920 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -3076,6 +3076,45 @@ impl<'a> LoweringContext<'a> { } } + /// Lowers `impl Trait` items and appends them to the list + fn lower_impl_trait_ids( + &mut self, + decl: &FnDecl, + ids: &mut SmallVector, + ) { + struct IdVisitor<'a> { ids: &'a mut SmallVector } + impl<'a, 'b> Visitor<'a> for IdVisitor<'b> { + fn visit_ty(&mut self, ty: &'a Ty) { + match ty.node { + | TyKind::Typeof(_) + | TyKind::BareFn(_) + => return, + + TyKind::ImplTrait(id, _) => self.ids.push(hir::ItemId { id }), + _ => {}, + } + visit::walk_ty(self, ty); + } + fn visit_path_segment( + &mut self, + path_span: Span, + path_segment: &'v PathSegment, + ) { + if let Some(ref p) = path_segment.parameters { + if let PathParameters::Parenthesized(..) = **p { + return; + } + } + visit::walk_path_segment(self, path_span, path_segment) + } + } + let mut visitor = IdVisitor { ids }; + match decl.output { + FunctionRetTy::Default(_) => {}, + FunctionRetTy::Ty(ref ty) => visitor.visit_ty(ty), + } + } + fn lower_item_id(&mut self, i: &Item) -> SmallVector { match i.node { ItemKind::Use(ref use_tree) => { @@ -3085,21 +3124,18 @@ impl<'a> LoweringContext<'a> { } ItemKind::MacroDef(..) => SmallVector::new(), ItemKind::Fn(ref decl, ..) => { - struct IdVisitor { ids: SmallVector } - impl<'a> Visitor<'a> for IdVisitor { - fn visit_ty(&mut self, ty: &'a Ty) { - if let TyKind::ImplTrait(id, _) = ty.node { - self.ids.push(hir::ItemId { id }); - } - visit::walk_ty(self, ty); + let mut ids = SmallVector::one(hir::ItemId { id: i.id }); + self.lower_impl_trait_ids(decl, &mut ids); + ids + }, + ItemKind::Impl(.., ref items) => { + let mut ids = SmallVector::one(hir::ItemId { id: i.id }); + for item in items { + if let ImplItemKind::Method(ref sig, _) = item.node { + self.lower_impl_trait_ids(&sig.decl, &mut ids); } } - let mut visitor = IdVisitor { ids: SmallVector::one(hir::ItemId { id: i.id }) }; - match decl.output { - FunctionRetTy::Default(_) => {}, - FunctionRetTy::Ty(ref ty) => visitor.visit_ty(ty), - } - visitor.ids + ids }, _ => SmallVector::one(hir::ItemId { id: i.id }), }