Skip to content

Commit 1f9d846

Browse files
committed
Add some documentation to pattern/const conversions
1 parent b985399 commit 1f9d846

File tree

1 file changed

+12
-2
lines changed
  • src/librustc_mir/hair/pattern

1 file changed

+12
-2
lines changed

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,9 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
667667
}
668668
}
669669

670+
/// Takes a HIR Path. If the path is a constant, evaluates it and feeds
671+
/// it to `const_to_pat`. Any other path (like enum variants without fields)
672+
/// is converted to the corresponding pattern via `lower_variant_or_leaf`
670673
fn lower_path(&mut self,
671674
qpath: &hir::QPath,
672675
id: hir::HirId,
@@ -722,6 +725,10 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
722725
}
723726
}
724727

728+
/// Converts literals, paths and negation of literals to patterns.
729+
/// The special case for negation exists to allow things like -128i8
730+
/// which would overflow if we tried to evaluate 128i8 and then negate
731+
/// afterwards.
725732
fn lower_lit(&mut self, expr: &'tcx hir::Expr) -> PatternKind<'tcx> {
726733
match expr.node {
727734
hir::ExprLit(ref lit) => {
@@ -767,6 +774,9 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
767774
}
768775
}
769776

777+
/// Converts an evaluated constant to a pattern (if possible).
778+
/// This means aggregate values (like structs and enums) are converted
779+
/// to a pattern that matches the value (as if you'd compare via eq).
770780
fn const_to_pat(
771781
&self,
772782
instance: ty::Instance<'tcx>,
@@ -844,14 +854,14 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
844854
},
845855
ConstVal::Unevaluated(..) =>
846856
span_bug!(span, "{:#?} is not a valid enum constant", cv),
847-
}
857+
}
848858
},
849859
ty::TyAdt(adt_def, _) => {
850860
let struct_var = adt_def.non_enum_variant();
851861
PatternKind::Leaf {
852862
subpatterns: adt_subpatterns(struct_var.fields.len(), None),
853-
}
854863
}
864+
}
855865
ty::TyTuple(fields, _) => {
856866
PatternKind::Leaf {
857867
subpatterns: adt_subpatterns(fields.len(), None),

0 commit comments

Comments
 (0)