Skip to content

Commit

Permalink
Rollup merge of #118598 - Nadrieril:remove_precise_pointer_size_match…
Browse files Browse the repository at this point in the history
…ing, r=davidtwco

Remove the `precise_pointer_size_matching` feature gate

`usize` and `isize` are special for pattern matching because their range might depend on the platform. To make code portable across platforms, the following is never considered exhaustive:
```rust
let x: usize = ...;
match x {
    0..=18446744073709551615 => {}
}
```
Because of how rust handles constants, this also unfortunately counts `0..=usize::MAX` as non-exhaustive. The [`precise_pointer_size_matching`](#56354) feature gate was introduced both for this convenience and for the possibility that the lang team could decide to allow the above.

Since then, [half-open range patterns](#67264) have been implemented, and since #116692 they correctly support `usize`/`isize`:
```rust
match 0usize { // exhaustive!
    0..5 => {}
    5.. => {}
}
```
I believe this subsumes all the use cases of the feature gate. Moreover no attempt has been made to stabilize it in the 5 years of its existence. I therefore propose we retire this feature gate.

Closes #56354
  • Loading branch information
matthiaskrgr committed Dec 5, 2023
2 parents fddda14 + 5e470db commit 81b6263
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 86 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/removed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ declare_features! (
/// Allows using `#[plugin_registrar]` on functions.
(removed, plugin_registrar, "1.54.0", Some(29597), None,
Some("plugins are no longer supported")),
/// Allows exhaustive integer pattern matching with `usize::MAX`/`isize::MIN`/`isize::MAX`.
(removed, precise_pointer_size_matching, "1.32.0", Some(56354), None,
Some("removed in favor of half-open ranges")),
(removed, proc_macro_expr, "1.27.0", Some(54727), None,
Some("subsumed by `#![feature(proc_macro_hygiene)]`")),
(removed, proc_macro_gen, "1.27.0", Some(54727), None,
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,6 @@ declare_features! (
(unstable, offset_of_enum, "1.75.0", Some(106655), None),
/// Allows using `#[optimize(X)]`.
(unstable, optimize_attribute, "1.34.0", Some(54882), None),
/// Allows exhaustive integer pattern matching on `usize` and `isize`.
(unstable, precise_pointer_size_matching, "1.32.0", Some(56354), None),
/// Allows macro attributes on expressions, statements and non-inline modules.
(unstable, proc_macro_hygiene, "1.30.0", Some(54727), None),
/// Allows `&raw const $place_expr` and `&raw mut $place_expr` expressions.
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,12 +867,6 @@ fn report_non_exhaustive_match<'p, 'tcx>(
exhaustively",
));
}
if cx.tcx.sess.is_nightly_build() {
err.help(format!(
"add `#![feature(precise_pointer_size_matching)]` to the crate attributes to \
enable precise `{ty}` matching",
));
}
} else if ty == cx.tcx.types.str_ {
err.note("`&str` cannot be matched exhaustively, so a wildcard `_` is necessary");
} else if cx.is_foreign_non_exhaustive_enum(ty) {
Expand Down
19 changes: 6 additions & 13 deletions compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,7 @@ impl IntRange {
/// `NegInfinity..PosInfinity`. In other words, as far as `IntRange` is concerned, there are
/// values before `isize::MIN` and after `usize::MAX`/`isize::MAX`.
/// This is to avoid e.g. `0..(u32::MAX as usize)` from being exhaustive on one architecture and
/// not others. See discussions around the `precise_pointer_size_matching` feature for more
/// details.
/// not others. This was decided in <https://github.com/rust-lang/rfcs/pull/2591>.
///
/// These infinities affect splitting subtly: it is possible to get `NegInfinity..0` and
/// `usize::MAX+1..PosInfinity` in the output. Diagnostics must be careful to handle these
Expand Down Expand Up @@ -380,7 +379,7 @@ impl IntRange {
/// Whether the range denotes the fictitious values before `isize::MIN` or after
/// `usize::MAX`/`isize::MAX` (see doc of [`IntRange::split`] for why these exist).
pub(crate) fn is_beyond_boundaries<'tcx>(&self, ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
ty.is_ptr_sized_integral() && !tcx.features().precise_pointer_size_matching && {
ty.is_ptr_sized_integral() && {
// The two invalid ranges are `NegInfinity..isize::MIN` (represented as
// `NegInfinity..0`), and `{u,i}size::MAX+1..PosInfinity`. `to_diagnostic_pat_range_bdy`
// converts `MAX+1` to `PosInfinity`, and we couldn't have `PosInfinity` in `self.lo`
Expand Down Expand Up @@ -941,11 +940,8 @@ impl ConstructorSet {
}
}
&ty::Int(ity) => {
let range = if ty.is_ptr_sized_integral()
&& !cx.tcx.features().precise_pointer_size_matching
{
// The min/max values of `isize` are not allowed to be observed unless the
// `precise_pointer_size_matching` feature is enabled.
let range = if ty.is_ptr_sized_integral() {
// The min/max values of `isize` are not allowed to be observed.
IntRange { lo: NegInfinity, hi: PosInfinity }
} else {
let bits = Integer::from_int_ty(&cx.tcx, ity).size().bits() as u128;
Expand All @@ -956,11 +952,8 @@ impl ConstructorSet {
Self::Integers { range_1: range, range_2: None }
}
&ty::Uint(uty) => {
let range = if ty.is_ptr_sized_integral()
&& !cx.tcx.features().precise_pointer_size_matching
{
// The max value of `usize` is not allowed to be observed unless the
// `precise_pointer_size_matching` feature is enabled.
let range = if ty.is_ptr_sized_integral() {
// The max value of `usize` is not allowed to be observed.
let lo = MaybeInfiniteInt::new_finite(cx.tcx, ty, 0);
IntRange { lo, hi: PosInfinity }
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ LL | match 0usize {
|
= note: the matched value is of type `usize`
= note: `usize` does not have a fixed maximum value, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL ~ 0..=usize::MAX => {},
Expand All @@ -21,7 +20,6 @@ LL | match 0isize {
|
= note: the matched value is of type `isize`
= note: `isize` does not have fixed minimum and maximum values, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
|
LL ~ isize::MIN..=isize::MAX => {},
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,161 +1,150 @@
error[E0004]: non-exhaustive patterns: `usize::MAX..` not covered
--> $DIR/pointer-sized-int.rs:14:11
--> $DIR/pointer-sized-int.rs:13:11
|
LL | match 0usize {
| ^^^^^^ pattern `usize::MAX..` not covered
|
= note: the matched value is of type `usize`
= note: `usize` does not have a fixed maximum value, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL ~ 0..=usize::MAX => {},
LL + usize::MAX.. => todo!()
|

error[E0004]: non-exhaustive patterns: `..isize::MIN` and `isize::MAX..` not covered
--> $DIR/pointer-sized-int.rs:19:11
--> $DIR/pointer-sized-int.rs:18:11
|
LL | match 0isize {
| ^^^^^^ patterns `..isize::MIN` and `isize::MAX..` not covered
|
= note: the matched value is of type `isize`
= note: `isize` does not have fixed minimum and maximum values, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
|
LL ~ isize::MIN..=isize::MAX => {},
LL + ..isize::MIN | isize::MAX.. => todo!()
|

error[E0004]: non-exhaustive patterns: `usize::MAX..` not covered
--> $DIR/pointer-sized-int.rs:24:8
--> $DIR/pointer-sized-int.rs:23:8
|
LL | m!(0usize, 0..=usize::MAX);
| ^^^^^^ pattern `usize::MAX..` not covered
|
= note: the matched value is of type `usize`
= note: `usize` does not have a fixed maximum value, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL | match $s { $($t)+ => {}, usize::MAX.. => todo!() }
| +++++++++++++++++++++++++

error[E0004]: non-exhaustive patterns: `usize::MAX..` not covered
--> $DIR/pointer-sized-int.rs:26:8
--> $DIR/pointer-sized-int.rs:25:8
|
LL | m!(0usize, 0..5 | 5..=usize::MAX);
| ^^^^^^ pattern `usize::MAX..` not covered
|
= note: the matched value is of type `usize`
= note: `usize` does not have a fixed maximum value, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL | match $s { $($t)+ => {}, usize::MAX.. => todo!() }
| +++++++++++++++++++++++++

error[E0004]: non-exhaustive patterns: `usize::MAX..` not covered
--> $DIR/pointer-sized-int.rs:28:8
--> $DIR/pointer-sized-int.rs:27:8
|
LL | m!(0usize, 0..usize::MAX | usize::MAX);
| ^^^^^^ pattern `usize::MAX..` not covered
|
= note: the matched value is of type `usize`
= note: `usize` does not have a fixed maximum value, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL | match $s { $($t)+ => {}, usize::MAX.. => todo!() }
| +++++++++++++++++++++++++

error[E0004]: non-exhaustive patterns: `(usize::MAX.., _)` not covered
--> $DIR/pointer-sized-int.rs:30:8
--> $DIR/pointer-sized-int.rs:29:8
|
LL | m!((0usize, true), (0..5, true) | (5..=usize::MAX, true) | (0..=usize::MAX, false));
| ^^^^^^^^^^^^^^ pattern `(usize::MAX.., _)` not covered
|
= note: the matched value is of type `(usize, bool)`
= note: `usize` does not have a fixed maximum value, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL | match $s { $($t)+ => {}, (usize::MAX.., _) => todo!() }
| ++++++++++++++++++++++++++++++

error[E0004]: non-exhaustive patterns: `..isize::MIN` and `isize::MAX..` not covered
--> $DIR/pointer-sized-int.rs:39:8
--> $DIR/pointer-sized-int.rs:38:8
|
LL | m!(0isize, isize::MIN..=isize::MAX);
| ^^^^^^ patterns `..isize::MIN` and `isize::MAX..` not covered
|
= note: the matched value is of type `isize`
= note: `isize` does not have fixed minimum and maximum values, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
|
LL | match $s { $($t)+ => {}, ..isize::MIN | isize::MAX.. => todo!() }
| ++++++++++++++++++++++++++++++++++++++++

error[E0004]: non-exhaustive patterns: `..isize::MIN` and `isize::MAX..` not covered
--> $DIR/pointer-sized-int.rs:41:8
--> $DIR/pointer-sized-int.rs:40:8
|
LL | m!(0isize, isize::MIN..5 | 5..=isize::MAX);
| ^^^^^^ patterns `..isize::MIN` and `isize::MAX..` not covered
|
= note: the matched value is of type `isize`
= note: `isize` does not have fixed minimum and maximum values, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
|
LL | match $s { $($t)+ => {}, ..isize::MIN | isize::MAX.. => todo!() }
| ++++++++++++++++++++++++++++++++++++++++

error[E0004]: non-exhaustive patterns: `..isize::MIN` and `isize::MAX..` not covered
--> $DIR/pointer-sized-int.rs:43:8
--> $DIR/pointer-sized-int.rs:42:8
|
LL | m!(0isize, isize::MIN..=-1 | 0 | 1..=isize::MAX);
| ^^^^^^ patterns `..isize::MIN` and `isize::MAX..` not covered
|
= note: the matched value is of type `isize`
= note: `isize` does not have fixed minimum and maximum values, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
|
LL | match $s { $($t)+ => {}, ..isize::MIN | isize::MAX.. => todo!() }
| ++++++++++++++++++++++++++++++++++++++++

error[E0004]: non-exhaustive patterns: `..isize::MIN` and `isize::MAX..` not covered
--> $DIR/pointer-sized-int.rs:45:8
--> $DIR/pointer-sized-int.rs:44:8
|
LL | m!(0isize, isize::MIN..isize::MAX | isize::MAX);
| ^^^^^^ patterns `..isize::MIN` and `isize::MAX..` not covered
|
= note: the matched value is of type `isize`
= note: `isize` does not have fixed minimum and maximum values, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
|
LL | match $s { $($t)+ => {}, ..isize::MIN | isize::MAX.. => todo!() }
| ++++++++++++++++++++++++++++++++++++++++

error[E0004]: non-exhaustive patterns: `(..isize::MIN, _)` and `(isize::MAX.., _)` not covered
--> $DIR/pointer-sized-int.rs:48:9
--> $DIR/pointer-sized-int.rs:47:9
|
LL | (0isize, true),
| ^^^^^^^^^^^^^^ patterns `(..isize::MIN, _)` and `(isize::MAX.., _)` not covered
|
= note: the matched value is of type `(isize, bool)`
= note: `isize` does not have fixed minimum and maximum values, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
|
LL | match $s { $($t)+ => {}, (..isize::MIN, _) | (isize::MAX.., _) => todo!() }
| ++++++++++++++++++++++++++++++++++++++++++++++++++

error[E0004]: non-exhaustive patterns: type `usize` is non-empty
--> $DIR/pointer-sized-int.rs:59:11
--> $DIR/pointer-sized-int.rs:58:11
|
LL | match 7usize {}
| ^^^^^^
Expand Down
25 changes: 12 additions & 13 deletions tests/ui/pattern/usefulness/integer-ranges/pointer-sized-int.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// revisions: allow deny
// revisions: deny
#![feature(exclusive_range_pattern)]
#![cfg_attr(allow, feature(precise_pointer_size_matching))]
#![allow(overlapping_range_endpoints)]

macro_rules! m {
Expand All @@ -12,23 +11,23 @@ macro_rules! m {
#[rustfmt::skip]
fn main() {
match 0usize {
//[deny]~^ ERROR non-exhaustive patterns
//~^ ERROR non-exhaustive patterns
0..=usize::MAX => {}
}

match 0isize {
//[deny]~^ ERROR non-exhaustive patterns
//~^ ERROR non-exhaustive patterns
isize::MIN..=isize::MAX => {}
}

m!(0usize, 0..=usize::MAX);
//[deny]~^ ERROR non-exhaustive patterns
//~^ ERROR non-exhaustive patterns
m!(0usize, 0..5 | 5..=usize::MAX);
//[deny]~^ ERROR non-exhaustive patterns
//~^ ERROR non-exhaustive patterns
m!(0usize, 0..usize::MAX | usize::MAX);
//[deny]~^ ERROR non-exhaustive patterns
//~^ ERROR non-exhaustive patterns
m!((0usize, true), (0..5, true) | (5..=usize::MAX, true) | (0..=usize::MAX, false));
//[deny]~^ ERROR non-exhaustive patterns
//~^ ERROR non-exhaustive patterns

m!(0usize, 0..);
m!(0usize, 0..5 | 5..);
Expand All @@ -37,18 +36,18 @@ fn main() {
m!(0usize, 0..=usize::MAX | usize::MAX..);

m!(0isize, isize::MIN..=isize::MAX);
//[deny]~^ ERROR non-exhaustive patterns
//~^ ERROR non-exhaustive patterns
m!(0isize, isize::MIN..5 | 5..=isize::MAX);
//[deny]~^ ERROR non-exhaustive patterns
//~^ ERROR non-exhaustive patterns
m!(0isize, isize::MIN..=-1 | 0 | 1..=isize::MAX);
//[deny]~^ ERROR non-exhaustive patterns
//~^ ERROR non-exhaustive patterns
m!(0isize, isize::MIN..isize::MAX | isize::MAX);
//[deny]~^ ERROR non-exhaustive patterns
//~^ ERROR non-exhaustive patterns
m!(
(0isize, true),
(isize::MIN..5, true) | (5..=isize::MAX, true) | (isize::MIN..=isize::MAX, false)
);
//[deny]~^^^ ERROR non-exhaustive patterns
//~^^^ ERROR non-exhaustive patterns

m!(0isize, ..0 | 0..);
m!(0isize, ..5 | 5..);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ LL | match 0usize {
|
= note: the matched value is of type `usize`
= note: `usize` does not have a fixed maximum value, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL ~ 0..=usize::MAX => {},
Expand All @@ -21,7 +20,6 @@ LL | match 0isize {
|
= note: the matched value is of type `isize`
= note: `isize` does not have fixed minimum and maximum values, so half-open ranges are necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
|
LL ~ isize::MIN..=isize::MAX => {},
Expand Down

0 comments on commit 81b6263

Please sign in to comment.