rust/compiler
bors 0d76b73745 Auto merge of #83918 - workingjubilee:stable-rangefrom-pat, r=joshtriplett
Stabilize "RangeFrom" patterns in 1.55

Implements a partial stabilization of #67264 and #37854.
Reference PR: https://github.com/rust-lang/reference/pull/900

# Stabilization Report

This stabilizes the `X..` pattern, shown as such, offering an exhaustive match for unsigned integers:
```rust
match x as u32 {
      0 => println!("zero!"),
      1.. => println!("positive number!"),
}
```

Currently if a Rust author wants to write such a match on an integer, they must use `1..={integer}::MAX` . By allowing a "RangeFrom" style pattern, this simplifies the match to not require the MAX path and thus not require specifically repeating the type inside the match, allowing for easier refactoring. This is particularly useful for instances like the above case, where different behavior on "0" vs. "1 or any positive number" is desired, and the actual MAX is unimportant.

Notably, this excepts slice patterns which include half-open ranges from stabilization, as the wisdom of those is still subject to some debate.

## Practical Applications

Instances of this specific usage have appeared in the compiler:
16143d1067/compiler/rustc_middle/src/ty/inhabitedness/mod.rs (L219)
673d0db5e3/compiler/rustc_ty_utils/src/ty.rs (L524)

And I have noticed there are also a handful of "in the wild" users who have deployed it to similar effect, especially in the case of rejecting any value of a certain number or greater. It simply makes it much more ergonomic to write an irrefutable match, as done in Katholieke Universiteit Leuven's [SCALE and MAMBA project](05e5db00d5/WebAssembly/scale_std/src/fixed_point.rs (L685-L695)).

## Tests
There were already many tests in [src/test/ui/half-open-range/patterns](90a2e5e3fe/src/test/ui/half-open-range-patterns), as well as [generic pattern tests that test the `exclusive_range_pattern` feature](673d0db5e3/src/test/ui/pattern/usefulness/integer-ranges/reachability.rs), many dating back to the feature's introduction and remaining standing to this day. However, this stabilization comes with some additional tests to explore the... sometimes interesting behavior of interactions with other patterns. e.g. There is, at least, a mild diagnostic improvement in some edge cases, because before now, the pattern `0..=(5+1)` encounters the `half_open_range_patterns` feature gate and can thus emit the request to enable the feature flag, while also emitting the "inclusive range with no end" diagnostic. There is no intent to allow an `X..=` pattern that I am aware of, so removing the flag request is a strict improvement. The arrival of the `J | K` "or" pattern also enables some odd formations.

Some of the behavior tested for here is derived from experiments in this [Playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=58777b3c715c85165ac4a70d93efeefc) example, linked at https://github.com/rust-lang/rust/issues/67264#issuecomment-812770692, which may be useful to reference to observe the current behavior more closely.

In addition tests constituting an explanation of the "slicing range patterns" syntax issue are included in this PR.

## Desiderata

The exclusive range patterns and half-open range patterns are fairly strongly requested by many authors, as they make some patterns much more natural to write, but there is disagreement regarding the "closed" exclusive range pattern or the "RangeTo" pattern, especially where it creates "off by one" gaps in the presence of a "catch-all" wildcard case. Also, there are obviously no range analyses in place that will force diagnostics for e.g. highly overlapping matches. I believe these should be warned on, ideally, and I think it would be reasonable to consider such a blocker to stabilizing this feature, but there is no technical issue with the feature as-is from the purely syntactic perspective as such overlapping or missed matches can already be generated today with such a catch-all case. And part of the "point" of the feature, at least from my view, is to make it easier to omit wildcard matches: a pattern with such an "open" match produces an irrefutable match and does not need the wild card case, making it easier to benefit from exhaustiveness checking.

## History

- Implemented:
  - Partially via exclusive ranges: https://github.com/rust-lang/rust/pull/35712
  - Fully with half-open ranges: https://github.com/rust-lang/rust/pull/67258
- Unresolved Questions:
  - The precedence concerns of https://github.com/rust-lang/rust/pull/48501 were considered as likely requiring adjustment but probably wanting a uniform consistent change across all pattern styles, given https://github.com/rust-lang/rust/issues/67264#issuecomment-720711656, but it is still unknown what changes might be desired
  - How we want to handle slice patterns in ranges seems to be an open question still, as witnessed in the discussion of this PR!

I checked but I couldn't actually find an RFC for this, and given "approved provisionally by lang team without an RFC", I believe this might require an RFC before it can land? Unsure of procedure here, on account of this being stabilizing a subset of a feature of syntax.

r? `@scottmcm`
2021-07-11 06:31:42 +00:00
..
rustc
rustc_apfloat
rustc_arena
rustc_ast Auto merge of #83918 - workingjubilee:stable-rangefrom-pat, r=joshtriplett 2021-07-11 06:31:42 +00:00
rustc_ast_lowering Auto merge of #86416 - Amanieu:asm_clobber_only, r=nagisa 2021-07-11 01:06:58 +00:00
rustc_ast_passes Auto merge of #83918 - workingjubilee:stable-rangefrom-pat, r=joshtriplett 2021-07-11 06:31:42 +00:00
rustc_ast_pretty Rework SESSION_GLOBALS API to prevent overwriting it 2021-07-08 16:16:28 +02:00
rustc_attr Enhance well-formedness checks for #[repr(...)] attributes 2021-07-09 22:03:48 +02:00
rustc_builtin_macros
rustc_codegen_cranelift Use cranelift's Type::int instead of doing the match myself 2021-07-08 14:55:58 -07:00
rustc_codegen_llvm Auto merge of #86416 - Amanieu:asm_clobber_only, r=nagisa 2021-07-11 01:06:58 +00:00
rustc_codegen_ssa Auto merge of #86873 - nikic:opaque-ptrs, r=nagisa 2021-07-10 19:01:41 +00:00
rustc_data_structures
rustc_driver
rustc_error_codes Rollup merge of #86838 - lambinoo:I-69630-rust_const_unstable_check_const, r=oli-obk 2021-07-08 18:30:34 +02:00
rustc_errors Rework SESSION_GLOBALS API to prevent overwriting it 2021-07-08 16:16:28 +02:00
rustc_expand Rework SESSION_GLOBALS API to prevent overwriting it 2021-07-08 16:16:28 +02:00
rustc_feature remove const_raw_ptr_to_usize_cast feature 2021-07-10 12:08:58 +02:00
rustc_fs_util
rustc_graphviz
rustc_hir Rollup merge of #86726 - sexxi-goose:use-diagnostic-item-for-rfc2229-migration, r=nikomatsakis 2021-07-08 18:30:33 +02:00
rustc_hir_pretty
rustc_incremental
rustc_index
rustc_infer
rustc_interface Rework SESSION_GLOBALS API to prevent overwriting it 2021-07-08 16:16:28 +02:00
rustc_lexer
rustc_lint Auto merge of #86968 - inquisitivecrystal:missing-docs-v2, r=oli-obk 2021-07-10 03:32:42 +00:00
rustc_lint_defs rename rust_2021_token_prefixes to rust_2021_prefixes_incompatible_syntax 2021-07-06 20:13:36 +02:00
rustc_llvm Pass type when creating atomic load 2021-07-09 22:00:19 +02:00
rustc_macros
rustc_metadata Add support for raw-dylib with stdcall, fastcall functions on i686-pc-windows-msvc. 2021-07-09 12:04:54 -07:00
rustc_middle Auto merge of #87029 - JohnTitor:rollup-0yapv7z, r=JohnTitor 2021-07-10 16:41:26 +00:00
rustc_mir Rollup merge of #87028 - aDotInTheVoid:patch-1, r=petrochenkov 2021-07-11 01:15:43 +09:00
rustc_mir_build remove const_raw_ptr_to_usize_cast feature 2021-07-10 12:08:58 +02:00
rustc_parse Auto merge of #83918 - workingjubilee:stable-rangefrom-pat, r=joshtriplett 2021-07-11 06:31:42 +00:00
rustc_parse_format Rework SESSION_GLOBALS API to prevent overwriting it 2021-07-08 16:16:28 +02:00
rustc_passes Rollup merge of #86838 - lambinoo:I-69630-rust_const_unstable_check_const, r=oli-obk 2021-07-08 18:30:34 +02:00
rustc_plugin_impl
rustc_privacy
rustc_query_impl
rustc_query_system
rustc_resolve
rustc_save_analysis
rustc_serialize
rustc_session Auto merge of #86572 - rylev:force-warnings-always, r=nikomatsakis 2021-07-06 16:50:33 +00:00
rustc_span Auto merge of #86416 - Amanieu:asm_clobber_only, r=nagisa 2021-07-11 01:06:58 +00:00
rustc_symbol_mangling
rustc_target Auto merge of #86416 - Amanieu:asm_clobber_only, r=nagisa 2021-07-11 01:06:58 +00:00
rustc_trait_selection Rollup merge of #86726 - sexxi-goose:use-diagnostic-item-for-rfc2229-migration, r=nikomatsakis 2021-07-08 18:30:33 +02:00
rustc_traits
rustc_ty_utils
rustc_type_ir
rustc_typeck Auto merge of #86965 - sexxi-goose:rfc2229-improve-lint, r=nikomatsakis,lqd 2021-07-11 03:50:28 +00:00