diff --git a/src/librustc/hir/map/def_collector.rs b/src/librustc/hir/map/def_collector.rs index f946ca2903a..6b93bd96a27 100644 --- a/src/librustc/hir/map/def_collector.rs +++ b/src/librustc/hir/map/def_collector.rs @@ -11,13 +11,13 @@ use syntax_pos::Span; /// Creates `DefId`s for nodes in the AST. pub struct DefCollector<'a> { definitions: &'a mut Definitions, - parent_def: Option, + parent_def: DefIndex, expansion: Mark, } impl<'a> DefCollector<'a> { pub fn new(definitions: &'a mut Definitions, expansion: Mark) -> Self { - let parent_def = Some(definitions.invocation_parent(expansion)); + let parent_def = definitions.invocation_parent(expansion); DefCollector { definitions, parent_def, expansion } } @@ -26,17 +26,15 @@ impl<'a> DefCollector<'a> { data: DefPathData, span: Span) -> DefIndex { - let parent_def = self.parent_def.unwrap(); + let parent_def = self.parent_def; debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def); - self.definitions - .create_def_with_parent(parent_def, node_id, data, self.expansion, span) + self.definitions.create_def_with_parent(parent_def, node_id, data, self.expansion, span) } pub fn with_parent(&mut self, parent_def: DefIndex, f: F) { - let parent = self.parent_def; - self.parent_def = Some(parent_def); + let orig_parent_def = std::mem::replace(&mut self.parent_def, parent_def); f(self); - self.parent_def = parent; + self.parent_def = orig_parent_def; } fn visit_async_fn( @@ -77,7 +75,7 @@ impl<'a> DefCollector<'a> { } fn visit_macro_invoc(&mut self, id: NodeId) { - self.definitions.set_invocation_parent(id.placeholder_to_mark(), self.parent_def.unwrap()); + self.definitions.set_invocation_parent(id.placeholder_to_mark(), self.parent_def); } } @@ -250,36 +248,24 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { } fn visit_expr(&mut self, expr: &'a Expr) { - let parent_def = self.parent_def; - - match expr.node { + let parent_def = match expr.node { ExprKind::Mac(..) => return self.visit_macro_invoc(expr.id), ExprKind::Closure(_, asyncness, ..) => { - let closure_def = self.create_def(expr.id, - DefPathData::ClosureExpr, - expr.span); - self.parent_def = Some(closure_def); - // Async closures desugar to closures inside of closures, so // we must create two defs. - if let IsAsync::Async { closure_id, .. } = asyncness { - let async_def = self.create_def(closure_id, - DefPathData::ClosureExpr, - expr.span); - self.parent_def = Some(async_def); + let closure_def = self.create_def(expr.id, DefPathData::ClosureExpr, expr.span); + match asyncness { + IsAsync::Async { closure_id, .. } => + self.create_def(closure_id, DefPathData::ClosureExpr, expr.span), + IsAsync::NotAsync => closure_def, } } - ExprKind::Async(_, async_id, _) => { - let async_def = self.create_def(async_id, - DefPathData::ClosureExpr, - expr.span); - self.parent_def = Some(async_def); - } - _ => {} + ExprKind::Async(_, async_id, _) => + self.create_def(async_id, DefPathData::ClosureExpr, expr.span), + _ => self.parent_def, }; - visit::walk_expr(self, expr); - self.parent_def = parent_def; + self.with_parent(parent_def, |this| visit::walk_expr(this, expr)); } fn visit_ty(&mut self, ty: &'a Ty) {