Add some documentation to pattern/const conversions

This commit is contained in:
Oliver Schneider 2018-02-06 09:52:56 +01:00
parent b985399831
commit 1f9d846a58
No known key found for this signature in database
GPG Key ID: A69F8D225B3AD7D9
1 changed files with 12 additions and 2 deletions

View File

@ -667,6 +667,9 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
}
}
/// 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 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
}
}
/// 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 @@ impl<'a, 'tcx> PatternContext<'a, '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 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
},
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),