Skip to content

Commit 079400c

Browse files
committed
Fix bug just discovered
Suggested by matthewjasper here: #71930 (comment) I have no idea what this does but it seems to work.
1 parent e5a2cd5 commit 079400c

File tree

2 files changed

+3
-21
lines changed

2 files changed

+3
-21
lines changed

src/librustc_mir_build/hair/pattern/check_match.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -186,30 +186,10 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
186186
// Fourth, check for unreachable arms.
187187
let matrix = check_arms(&mut cx, &inlined_arms, source);
188188

189-
// FIXME: getting the type using `node_type` means that if `f` has output type `!`, we
190-
// get `scrut_ty = !` instead of `bool` in the following:
191-
// ```
192-
// fn from(never: !) -> usize {
193-
// match never {
194-
// true => 1,
195-
// false => 0,
196-
// }
197-
// }
198-
// ```
199-
// If we use `expr_ty_adjusted` instead, then the following breaks, because we get
200-
// `scrut_ty = ()` instead of `!`.
201-
// ```
202-
// fn from(never: !) -> usize {
203-
// match never {}
204-
// }
205-
// ```
206-
// As a workaround, we retrieve the type from the match arms when possible.
207-
let scrut_ty = self.tables.node_type(scrut.hir_id);
208-
let scrut_ty = inlined_arms.iter().map(|(p, _, _)| p.ty).next().unwrap_or(scrut_ty);
209-
210189
// Fifth, check if the match is exhaustive.
211190
// Note: An empty match isn't the same as an empty matrix for diagnostics purposes,
212191
// since an empty matrix can occur when there are arms, if those arms all have guards.
192+
let scrut_ty = self.tables.expr_ty_adjusted(scrut);
213193
let is_empty_match = inlined_arms.is_empty();
214194
check_exhaustive(&mut cx, scrut_ty, scrut.span, &matrix, scrut.hir_id, is_empty_match);
215195
}

src/librustc_typeck/check/_match.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
436436

437437
if let Some(m) = contains_ref_bindings {
438438
self.check_expr_with_needs(scrut, Needs::maybe_mut_place(m))
439+
} else if arms.is_empty() {
440+
self.check_expr(scrut)
439441
} else {
440442
// ...but otherwise we want to use any supertype of the
441443
// scrutinee. This is sort of a workaround, see note (*) in

0 commit comments

Comments
 (0)