]> git.lizzy.rs Git - rust.git/commitdiff
Add some documentation to pattern/const conversions
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 6 Feb 2018 08:52:56 +0000 (09:52 +0100)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Thu, 8 Mar 2018 07:34:16 +0000 (08:34 +0100)
src/librustc_mir/hair/pattern/mod.rs

index d5dde0164fb5db2457efe3f46add40122fcb44be..072b28850f0cfe325496c57924db12c6a7858d5a 100644 (file)
@@ -667,6 +667,9 @@ fn lower_variant_or_leaf(
         }
     }
 
+    /// Takes a HIR Path. If the path is a constant, evaluates it and feeds
+    /// it to `const_to_pat`. Any other path (like enum variants without fields)
+    /// is converted to the corresponding pattern via `lower_variant_or_leaf`
     fn lower_path(&mut self,
                   qpath: &hir::QPath,
                   id: hir::HirId,
@@ -722,6 +725,10 @@ fn lower_path(&mut self,
         }
     }
 
+    /// Converts literals, paths and negation of literals to patterns.
+    /// The special case for negation exists to allow things like -128i8
+    /// which would overflow if we tried to evaluate 128i8 and then negate
+    /// afterwards.
     fn lower_lit(&mut self, expr: &'tcx hir::Expr) -> PatternKind<'tcx> {
         match expr.node {
             hir::ExprLit(ref lit) => {
@@ -767,6 +774,9 @@ fn lower_lit(&mut self, expr: &'tcx hir::Expr) -> PatternKind<'tcx> {
         }
     }
 
+    /// Converts an evaluated constant to a pattern (if possible).
+    /// This means aggregate values (like structs and enums) are converted
+    /// to a pattern that matches the value (as if you'd compare via eq).
     fn const_to_pat(
         &self,
         instance: ty::Instance<'tcx>,
@@ -844,14 +854,14 @@ fn const_to_pat(
                     },
                     ConstVal::Unevaluated(..) =>
                         span_bug!(span, "{:#?} is not a valid enum constant", cv),
-                    }
+                }
             },
             ty::TyAdt(adt_def, _) => {
                 let struct_var = adt_def.non_enum_variant();
                 PatternKind::Leaf {
                     subpatterns: adt_subpatterns(struct_var.fields.len(), None),
-                        }
                 }
+            }
             ty::TyTuple(fields, _) => {
                 PatternKind::Leaf {
                     subpatterns: adt_subpatterns(fields.len(), None),