Skip to content

Commit ceeaa8a

Browse files
authored
Rollup merge of #120517 - Nadrieril:lower-never-as-wildcard, r=compiler-errors
never patterns: It is correct to lower `!` to `_`. This is just a comment update but a non-trivial one: it is correct to lower `!` patterns as `_`. The reasoning is that `!` matches all the possible values of the type, since the type is empty. Moreover, we do want to warn that the `Err` is redundant in: ```rust match x { !, Err(!), } ``` which is consistent with `!` behaving like a wildcard. I did try to introduce `Constructor::Never` and it ended up needing to behave exactly like `Constructor::Wildcard`. r? ```@compiler-errors```
2 parents f3ebf1e + ee2cddd commit ceeaa8a

File tree

1 file changed

+3
-2
lines changed
  • compiler/rustc_pattern_analysis/src

1 file changed

+3
-2
lines changed

compiler/rustc_pattern_analysis/src/rustc.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,9 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
675675
cx.pattern_arena.alloc_from_iter(pats.into_iter().map(|p| self.lower_pat(p)))
676676
}
677677
PatKind::Never => {
678-
// FIXME(never_patterns): handle `!` in exhaustiveness. This is a sane default
679-
// in the meantime.
678+
// A never pattern matches all the values of its type (namely none). Moreover it
679+
// must be compatible with other constructors, since we can use `!` on a type like
680+
// `Result<!, !>` which has other constructors. Hence we lower it as a wildcard.
680681
ctor = Wildcard;
681682
fields = &[];
682683
}

0 commit comments

Comments
 (0)