Rollup merge of #120009 - Nadrieril:never_patterns_tyck, r=compiler-errors
never_patterns: typecheck never patterns This checks that a `!` pattern is only used on an uninhabited type (modulo match ergonomics, i.e. `!` is allowed on `&Void`). r? `@compiler-errors`
This commit is contained in:
commit
5761c36c0a
8 changed files with 223 additions and 92 deletions
|
@ -788,6 +788,16 @@ pub struct FloatPattern;
|
|||
#[diag(mir_build_pointer_pattern)]
|
||||
pub struct PointerPattern;
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(mir_build_non_empty_never_pattern)]
|
||||
#[note]
|
||||
pub struct NonEmptyNeverPattern<'tcx> {
|
||||
#[primary_span]
|
||||
#[label]
|
||||
pub span: Span,
|
||||
pub ty: Ty<'tcx>,
|
||||
}
|
||||
|
||||
#[derive(LintDiagnostic)]
|
||||
#[diag(mir_build_indirect_structural_match)]
|
||||
#[note(mir_build_type_not_structural_tip)]
|
||||
|
|
|
@ -276,10 +276,13 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
|
|||
} else {
|
||||
// Check the pattern for some things unrelated to exhaustiveness.
|
||||
let refutable = if cx.refutable { Refutable } else { Irrefutable };
|
||||
let mut err = Ok(());
|
||||
pat.walk_always(|pat| {
|
||||
check_borrow_conflicts_in_at_patterns(self, pat);
|
||||
check_for_bindings_named_same_as_variants(self, pat, refutable);
|
||||
err = err.and(check_never_pattern(cx, pat));
|
||||
});
|
||||
err?;
|
||||
Ok(cx.pattern_arena.alloc(cx.lower_pat(pat)))
|
||||
}
|
||||
}
|
||||
|
@ -812,6 +815,19 @@ fn check_for_bindings_named_same_as_variants(
|
|||
}
|
||||
}
|
||||
|
||||
/// Check that never patterns are only used on inhabited types.
|
||||
fn check_never_pattern<'tcx>(
|
||||
cx: &MatchCheckCtxt<'_, 'tcx>,
|
||||
pat: &Pat<'tcx>,
|
||||
) -> Result<(), ErrorGuaranteed> {
|
||||
if let PatKind::Never = pat.kind {
|
||||
if !cx.is_uninhabited(pat.ty) {
|
||||
return Err(cx.tcx.dcx().emit_err(NonEmptyNeverPattern { span: pat.span, ty: pat.ty }));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn report_irrefutable_let_patterns(
|
||||
tcx: TyCtxt<'_>,
|
||||
id: HirId,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue