]> git.lizzy.rs Git - rust.git/commitdiff
lowering: refactor label/dest -> expr.rs
authorMazdak Farrokhzad <twingoow@gmail.com>
Sat, 10 Aug 2019 15:37:10 +0000 (17:37 +0200)
committerMazdak Farrokhzad <twingoow@gmail.com>
Sat, 10 Aug 2019 18:24:43 +0000 (20:24 +0200)
src/librustc/hir/lowering.rs
src/librustc/hir/lowering/expr.rs

index 480ab228d9a49a781c706e2657c094c4341e8ea4..1ada058a8bc7dbb981c15a6b47ccd3c9ea45ba27 100644 (file)
@@ -1263,36 +1263,6 @@ fn def_key(&mut self, id: DefId) -> DefKey {
         }
     }
 
-    fn lower_label(&mut self, label: Option<Label>) -> Option<hir::Label> {
-        label.map(|label| hir::Label {
-            ident: label.ident,
-        })
-    }
-
-    fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
-        let target_id = match destination {
-            Some((id, _)) => {
-                if let Some(loop_id) = self.resolver.get_label_res(id) {
-                    Ok(self.lower_node_id(loop_id))
-                } else {
-                    Err(hir::LoopIdError::UnresolvedLabel)
-                }
-            }
-            None => {
-                self.loop_scopes
-                    .last()
-                    .cloned()
-                    .map(|id| Ok(self.lower_node_id(id)))
-                    .unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
-                    .into()
-            }
-        };
-        hir::Destination {
-            label: self.lower_label(destination.map(|(_, label)| label)),
-            target_id,
-        }
-    }
-
     fn lower_attrs_extendable(&mut self, attrs: &[Attribute]) -> Vec<Attribute> {
         attrs
             .iter()
index 718f51adc22c2eea887889b8461159e70214be74..a1c8af8c80dcd8aa3c8b83f0b0e52334885ef100 100644 (file)
@@ -138,28 +138,13 @@ pub(super) fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
                 hir::ExprKind::Path(qpath)
             }
             ExprKind::Break(opt_label, ref opt_expr) => {
-                let destination = if self.is_in_loop_condition && opt_label.is_none() {
-                    hir::Destination {
-                        label: None,
-                        target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
-                    }
-                } else {
-                    self.lower_loop_destination(opt_label.map(|label| (e.id, label)))
-                };
                 hir::ExprKind::Break(
-                    destination,
+                    self.lower_jump_destination(e.id, opt_label),
                     opt_expr.as_ref().map(|x| P(self.lower_expr(x))),
                 )
             }
             ExprKind::Continue(opt_label) => {
-                hir::ExprKind::Continue(if self.is_in_loop_condition && opt_label.is_none() {
-                    hir::Destination {
-                        label: None,
-                        target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
-                    }
-                } else {
-                    self.lower_loop_destination(opt_label.map(|label| (e.id, label)))
-                })
+                hir::ExprKind::Continue(self.lower_jump_destination(e.id, opt_label))
             }
             ExprKind::Ret(ref e) => hir::ExprKind::Ret(e.as_ref().map(|x| P(self.lower_expr(x)))),
             ExprKind::InlineAsm(ref asm) => self.lower_expr_asm(asm),
@@ -818,6 +803,47 @@ fn lower_expr_range(
         }
     }
 
+    fn lower_label(&mut self, label: Option<Label>) -> Option<hir::Label> {
+        label.map(|label| hir::Label {
+            ident: label.ident,
+        })
+    }
+
+    fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
+        let target_id = match destination {
+            Some((id, _)) => {
+                if let Some(loop_id) = self.resolver.get_label_res(id) {
+                    Ok(self.lower_node_id(loop_id))
+                } else {
+                    Err(hir::LoopIdError::UnresolvedLabel)
+                }
+            }
+            None => {
+                self.loop_scopes
+                    .last()
+                    .cloned()
+                    .map(|id| Ok(self.lower_node_id(id)))
+                    .unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
+                    .into()
+            }
+        };
+        hir::Destination {
+            label: self.lower_label(destination.map(|(_, label)| label)),
+            target_id,
+        }
+    }
+
+    fn lower_jump_destination(&mut self, id: NodeId, opt_label: Option<Label>) -> hir::Destination {
+        if self.is_in_loop_condition && opt_label.is_none() {
+            hir::Destination {
+                label: None,
+                target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
+            }
+        } else {
+            self.lower_loop_destination(opt_label.map(|label| (id, label)))
+        }
+    }
+
     fn lower_expr_asm(&mut self, asm: &InlineAsm) -> hir::ExprKind {
         let hir_asm = hir::InlineAsm {
             inputs: asm.inputs.iter().map(|&(ref c, _)| c.clone()).collect(),