Skip to content

Commit

Permalink
Add precise_pointer_size_matching feature
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Nov 30, 2018
1 parent f1f6d87 commit e018268
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,13 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
}
}

pub fn is_pointer_sized(&self) -> bool {
match self.sty {
Int(ast::IntTy::Isize) | Uint(ast::UintTy::Usize) => true,
_ => false,
}
}

pub fn is_machine(&self) -> bool {
match self.sty {
Int(ast::IntTy::Isize) | Uint(ast::UintTy::Usize) => false,
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_mir/hair/pattern/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,8 @@ pub fn is_useful<'p, 'a: 'p, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,

// For privately empty and non-exhaustive enums, we work as if there were an "extra"
// `_` constructor for the type, so we can never match over all constructors.
let is_non_exhaustive = is_privately_empty || is_declared_nonexhaustive;
let is_non_exhaustive = is_privately_empty || is_declared_nonexhaustive ||
(pcx.ty.is_pointer_sized() && !cx.tcx.features().precise_pointer_size_matching);

if cheap_missing_ctors == MissingCtors::Empty && !is_non_exhaustive {
split_grouped_constructors(cx.tcx, all_ctors, matrix, pcx.ty).into_iter().map(|c| {
Expand Down Expand Up @@ -1436,7 +1437,7 @@ fn should_treat_range_exhaustively(tcx: TyCtxt<'_, 'tcx, 'tcx>, ctor: &Construct
_ => return false,
};
if let ty::Char | ty::Int(_) | ty::Uint(_) = ty.sty {
true
!ty.is_pointer_sized() || tcx.features().precise_pointer_size_matching
} else {
false
}
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ declare_features! (
// 'a: { break 'a; }
(active, label_break_value, "1.28.0", Some(48594), None),

// Exhaustive pattern matching on `usize` and `isize`.
(active, precise_pointer_size_matching, "1.32.0", Some(56354), None),

// #[doc(keyword = "...")]
(active, doc_keyword, "1.28.0", Some(51315), None),
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/exhaustive_integer_patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(precise_pointer_size_matching)]
#![feature(exclusive_range_pattern)]

#![deny(unreachable_patterns)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![feature(exclusive_range_pattern)]

use std::usize::MAX;

fn main() {
match 0usize { //~ERROR non-exhaustive patterns: `_` not covered
0..=MAX => {}
}

match 0isize { //~ERROR non-exhaustive patterns: `_` not covered
1..=20 => {}
-5..3 => {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0004]: non-exhaustive patterns: `_` not covered
--> $DIR/feature-gate-precise_pointer_size_matching.rs:6:11
|
LL | match 0usize { //~ERROR non-exhaustive patterns: `_` not covered
| ^^^^^^ pattern `_` not covered

error[E0004]: non-exhaustive patterns: `_` not covered
--> $DIR/feature-gate-precise_pointer_size_matching.rs:10:11
|
LL | match 0isize { //~ERROR non-exhaustive patterns: `_` not covered
| ^^^^^^ pattern `_` not covered

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0004`.

0 comments on commit e018268

Please sign in to comment.