Fix bug just discovered

Suggested by matthewjasper here:
https://github.com/rust-lang/rust/pull/71930#issuecomment-626022502
I have no idea what this does but it seems to work.
This commit is contained in:
Nadrieril 2020-05-09 15:17:16 +01:00
parent e5a2cd526a
commit 079400c74b
2 changed files with 3 additions and 21 deletions

View File

@ -186,30 +186,10 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
// Fourth, check for unreachable arms.
let matrix = check_arms(&mut cx, &inlined_arms, source);
// FIXME: getting the type using `node_type` means that if `f` has output type `!`, we
// get `scrut_ty = !` instead of `bool` in the following:
// ```
// fn from(never: !) -> usize {
// match never {
// true => 1,
// false => 0,
// }
// }
// ```
// If we use `expr_ty_adjusted` instead, then the following breaks, because we get
// `scrut_ty = ()` instead of `!`.
// ```
// fn from(never: !) -> usize {
// match never {}
// }
// ```
// As a workaround, we retrieve the type from the match arms when possible.
let scrut_ty = self.tables.node_type(scrut.hir_id);
let scrut_ty = inlined_arms.iter().map(|(p, _, _)| p.ty).next().unwrap_or(scrut_ty);
// Fifth, check if the match is exhaustive.
// Note: An empty match isn't the same as an empty matrix for diagnostics purposes,
// since an empty matrix can occur when there are arms, if those arms all have guards.
let scrut_ty = self.tables.expr_ty_adjusted(scrut);
let is_empty_match = inlined_arms.is_empty();
check_exhaustive(&mut cx, scrut_ty, scrut.span, &matrix, scrut.hir_id, is_empty_match);
}

View File

@ -436,6 +436,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let Some(m) = contains_ref_bindings {
self.check_expr_with_needs(scrut, Needs::maybe_mut_place(m))
} else if arms.is_empty() {
self.check_expr(scrut)
} else {
// ...but otherwise we want to use any supertype of the
// scrutinee. This is sort of a workaround, see note (*) in