From 2d56abfbebdc905dafc9cf9edc0a6f58e4de7cbd Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Tue, 16 Jan 2018 01:44:32 +0300 Subject: [PATCH] AST/HIR: Add a separate structure for labels --- src/librustc/hir/intravisit.rs | 52 ++++++++++---------- src/librustc/hir/lowering.rs | 48 +++++++++---------- src/librustc/hir/mod.rs | 20 ++++++-- src/librustc/hir/print.rs | 24 +++++----- src/librustc/ich/impls_hir.rs | 7 ++- src/librustc/middle/resolve_lifetime.rs | 2 +- src/librustc_passes/ast_validation.rs | 13 ++--- src/librustc_resolve/lib.rs | 14 +++--- src/libsyntax/ast.rs | 24 +++++++--- src/libsyntax/fold.rs | 43 +++++++++-------- src/libsyntax/parse/parser.rs | 64 +++++++++---------------- src/libsyntax/print/pprust.rs | 36 +++++++------- src/libsyntax/visit.rs | 58 ++++++++++------------ 13 files changed, 203 insertions(+), 202 deletions(-) diff --git a/src/librustc/hir/intravisit.rs b/src/librustc/hir/intravisit.rs index ce35e6552ca..97cf9b01410 100644 --- a/src/librustc/hir/intravisit.rs +++ b/src/librustc/hir/intravisit.rs @@ -43,7 +43,6 @@ use syntax::abi::Abi; use syntax::ast::{NodeId, CRATE_NODE_ID, Name, Attribute}; -use syntax::codemap::Spanned; use syntax_pos::Span; use hir::*; use hir::def::Def; @@ -336,6 +335,9 @@ fn visit_enum_def(&mut self, fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics, item_id: NodeId) { walk_variant(self, v, g, item_id) } + fn visit_label(&mut self, label: &'v Label) { + walk_label(self, label) + } fn visit_lifetime(&mut self, lifetime: &'v Lifetime) { walk_lifetime(self, lifetime) } @@ -370,18 +372,6 @@ fn visit_defaultness(&mut self, defaultness: &'v Defaultness) { } } -pub fn walk_opt_name<'v, V: Visitor<'v>>(visitor: &mut V, span: Span, opt_name: Option) { - if let Some(name) = opt_name { - visitor.visit_name(span, name); - } -} - -pub fn walk_opt_sp_name<'v, V: Visitor<'v>>(visitor: &mut V, opt_sp_name: &Option>) { - if let Some(ref sp_name) = *opt_sp_name { - visitor.visit_name(sp_name.span, sp_name.node); - } -} - /// Walks the contents of a crate. See also `Crate::visit_all_items`. pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) { visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID); @@ -420,6 +410,10 @@ pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) { walk_list!(visitor, visit_ty, &local.ty); } +pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) { + visitor.visit_name(label.span, label.name); +} + pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) { visitor.visit_id(lifetime.id); match lifetime.name { @@ -452,7 +446,9 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) { match item.node { ItemExternCrate(opt_name) => { visitor.visit_id(item.id); - walk_opt_name(visitor, item.span, opt_name) + if let Some(name) = opt_name { + visitor.visit_name(item.span, name); + } } ItemUse(ref path, _) => { visitor.visit_id(item.id); @@ -993,14 +989,14 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) { visitor.visit_expr(if_block); walk_list!(visitor, visit_expr, optional_else); } - ExprWhile(ref subexpression, ref block, ref opt_sp_name) => { + ExprWhile(ref subexpression, ref block, ref opt_label) => { + walk_list!(visitor, visit_label, opt_label); visitor.visit_expr(subexpression); visitor.visit_block(block); - walk_opt_sp_name(visitor, opt_sp_name); } - ExprLoop(ref block, ref opt_sp_name, _) => { + ExprLoop(ref block, ref opt_label, _) => { + walk_list!(visitor, visit_label, opt_label); visitor.visit_block(block); - walk_opt_sp_name(visitor, opt_sp_name); } ExprMatch(ref subexpression, ref arms, _) => { visitor.visit_expr(subexpression); @@ -1036,28 +1032,28 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) { ExprPath(ref qpath) => { visitor.visit_qpath(qpath, expression.id, expression.span); } - ExprBreak(label, ref opt_expr) => { - label.ident.map(|ident| { - match label.target_id { + ExprBreak(ref destination, ref opt_expr) => { + if let Some(ref label) = destination.label { + visitor.visit_label(label); + match destination.target_id { ScopeTarget::Block(node_id) | ScopeTarget::Loop(LoopIdResult::Ok(node_id)) => visitor.visit_def_mention(Def::Label(node_id)), ScopeTarget::Loop(LoopIdResult::Err(_)) => {}, }; - visitor.visit_name(ident.span, ident.node.name); - }); + } walk_list!(visitor, visit_expr, opt_expr); } - ExprAgain(label) => { - label.ident.map(|ident| { - match label.target_id { + ExprAgain(ref destination) => { + if let Some(ref label) = destination.label { + visitor.visit_label(label); + match destination.target_id { ScopeTarget::Block(_) => bug!("can't `continue` to a non-loop block"), ScopeTarget::Loop(LoopIdResult::Ok(node_id)) => visitor.visit_def_mention(Def::Label(node_id)), ScopeTarget::Loop(LoopIdResult::Err(_)) => {}, }; - visitor.visit_name(ident.span, ident.node.name); - }); + } } ExprRet(ref optional_expression) => { walk_list!(visitor, visit_expr, optional_expression); diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 32b55a05124..adfcf42d85b 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -768,22 +768,22 @@ fn lower_ident(&mut self, ident: Ident) -> Name { *self.name_map.entry(ident).or_insert_with(|| Symbol::from_ident(ident)) } - fn lower_opt_sp_ident(&mut self, o_id: Option>) -> Option> { - o_id.map(|sp_ident| respan(sp_ident.span, sp_ident.node.name)) + fn lower_label(&mut self, label: Option