Skip to content

Commit c7bf3a0

Browse files
authored
Unrolled build for rust-lang#136167
Rollup merge of rust-lang#136167 - pitaj:new_range, r=Nadrieril Implement unstable `new_range` feature Switches `a..b`, `a..`, and `a..=b` to resolve to the new range types. For rust-lang/rfcs#3550 Tracking issue rust-lang#123741 also adds the re-export that was missed in the original implementation of `new_range_api`
2 parents 019fc4d + f530a29 commit c7bf3a0

29 files changed

+252
-54
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
284284
ExprKind::Index(el, er, brackets_span) => {
285285
hir::ExprKind::Index(self.lower_expr(el), self.lower_expr(er), *brackets_span)
286286
}
287-
ExprKind::Range(Some(e1), Some(e2), RangeLimits::Closed) => {
288-
self.lower_expr_range_closed(e.span, e1, e2)
289-
}
290287
ExprKind::Range(e1, e2, lims) => {
291288
self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), *lims)
292289
}
@@ -1512,15 +1509,39 @@ impl<'hir> LoweringContext<'_, 'hir> {
15121509

15131510
let lang_item = match (e1, e2, lims) {
15141511
(None, None, HalfOpen) => hir::LangItem::RangeFull,
1515-
(Some(..), None, HalfOpen) => hir::LangItem::RangeFrom,
1512+
(Some(..), None, HalfOpen) => {
1513+
if self.tcx.features().new_range() {
1514+
hir::LangItem::RangeFromCopy
1515+
} else {
1516+
hir::LangItem::RangeFrom
1517+
}
1518+
}
15161519
(None, Some(..), HalfOpen) => hir::LangItem::RangeTo,
1517-
(Some(..), Some(..), HalfOpen) => hir::LangItem::Range,
1520+
(Some(..), Some(..), HalfOpen) => {
1521+
if self.tcx.features().new_range() {
1522+
hir::LangItem::RangeCopy
1523+
} else {
1524+
hir::LangItem::Range
1525+
}
1526+
}
15181527
(None, Some(..), Closed) => hir::LangItem::RangeToInclusive,
1519-
(Some(..), Some(..), Closed) => unreachable!(),
1528+
(Some(e1), Some(e2), Closed) => {
1529+
if self.tcx.features().new_range() {
1530+
hir::LangItem::RangeInclusiveCopy
1531+
} else {
1532+
return self.lower_expr_range_closed(span, e1, e2);
1533+
}
1534+
}
15201535
(start, None, Closed) => {
15211536
self.dcx().emit_err(InclusiveRangeWithNoEnd { span });
15221537
match start {
1523-
Some(..) => hir::LangItem::RangeFrom,
1538+
Some(..) => {
1539+
if self.tcx.features().new_range() {
1540+
hir::LangItem::RangeFromCopy
1541+
} else {
1542+
hir::LangItem::RangeFrom
1543+
}
1544+
}
15241545
None => hir::LangItem::RangeFull,
15251546
}
15261547
}

compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,8 @@ declare_features! (
570570
(unstable, never_type, "1.13.0", Some(35121)),
571571
/// Allows diverging expressions to fall back to `!` rather than `()`.
572572
(unstable, never_type_fallback, "1.41.0", Some(65992)),
573+
/// Switch `..` syntax to use the new (`Copy + IntoIterator`) range types.
574+
(unstable, new_range, "CURRENT_RUSTC_VERSION", Some(123741)),
573575
/// Allows `#![no_core]`.
574576
(unstable, no_core, "1.3.0", Some(29639)),
575577
/// Allows the use of `no_sanitize` attribute.

compiler/rustc_hir/src/hir.rs

+40-1
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,18 @@ impl Expr<'_> {
23132313
[val2],
23142314
StructTailExpr::None,
23152315
),
2316+
)
2317+
| (
2318+
ExprKind::Struct(
2319+
QPath::LangItem(LangItem::RangeFromCopy, _),
2320+
[val1],
2321+
StructTailExpr::None,
2322+
),
2323+
ExprKind::Struct(
2324+
QPath::LangItem(LangItem::RangeFromCopy, _),
2325+
[val2],
2326+
StructTailExpr::None,
2327+
),
23162328
) => val1.expr.equivalent_for_indexing(val2.expr),
23172329
(
23182330
ExprKind::Struct(
@@ -2325,6 +2337,30 @@ impl Expr<'_> {
23252337
[val2, val4],
23262338
StructTailExpr::None,
23272339
),
2340+
)
2341+
| (
2342+
ExprKind::Struct(
2343+
QPath::LangItem(LangItem::RangeCopy, _),
2344+
[val1, val3],
2345+
StructTailExpr::None,
2346+
),
2347+
ExprKind::Struct(
2348+
QPath::LangItem(LangItem::RangeCopy, _),
2349+
[val2, val4],
2350+
StructTailExpr::None,
2351+
),
2352+
)
2353+
| (
2354+
ExprKind::Struct(
2355+
QPath::LangItem(LangItem::RangeInclusiveCopy, _),
2356+
[val1, val3],
2357+
StructTailExpr::None,
2358+
),
2359+
ExprKind::Struct(
2360+
QPath::LangItem(LangItem::RangeInclusiveCopy, _),
2361+
[val2, val4],
2362+
StructTailExpr::None,
2363+
),
23282364
) => {
23292365
val1.expr.equivalent_for_indexing(val2.expr)
23302366
&& val3.expr.equivalent_for_indexing(val4.expr)
@@ -2354,7 +2390,10 @@ pub fn is_range_literal(expr: &Expr<'_>) -> bool {
23542390
| LangItem::RangeTo
23552391
| LangItem::RangeFrom
23562392
| LangItem::RangeFull
2357-
| LangItem::RangeToInclusive,
2393+
| LangItem::RangeToInclusive
2394+
| LangItem::RangeCopy
2395+
| LangItem::RangeFromCopy
2396+
| LangItem::RangeInclusiveCopy,
23582397
..
23592398
)
23602399
),

compiler/rustc_hir/src/lang_items.rs

+5
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@ language_item_table! {
416416
RangeToInclusive, sym::RangeToInclusive, range_to_inclusive_struct, Target::Struct, GenericRequirement::None;
417417
RangeTo, sym::RangeTo, range_to_struct, Target::Struct, GenericRequirement::None;
418418

419+
// `new_range` types that are `Copy + IntoIterator`
420+
RangeFromCopy, sym::RangeFromCopy, range_from_copy_struct, Target::Struct, GenericRequirement::None;
421+
RangeCopy, sym::RangeCopy, range_copy_struct, Target::Struct, GenericRequirement::None;
422+
RangeInclusiveCopy, sym::RangeInclusiveCopy, range_inclusive_copy_struct, Target::Struct, GenericRequirement::None;
423+
419424
String, sym::String, string, Target::Struct, GenericRequirement::None;
420425
CStr, sym::CStr, c_str, Target::Struct, GenericRequirement::None;
421426
}

compiler/rustc_hir_typeck/src/method/suggest.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2395,6 +2395,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23952395
let lang_item = match parent_expr.kind {
23962396
ExprKind::Struct(qpath, _, _) => match *qpath {
23972397
QPath::LangItem(LangItem::Range, ..) => Some(LangItem::Range),
2398+
QPath::LangItem(LangItem::RangeCopy, ..) => Some(LangItem::RangeCopy),
2399+
QPath::LangItem(LangItem::RangeInclusiveCopy, ..) => {
2400+
Some(LangItem::RangeInclusiveCopy)
2401+
}
23982402
QPath::LangItem(LangItem::RangeTo, ..) => Some(LangItem::RangeTo),
23992403
QPath::LangItem(LangItem::RangeToInclusive, ..) => {
24002404
Some(LangItem::RangeToInclusive)

compiler/rustc_span/src/symbol.rs

+4
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,12 @@ symbols! {
294294
ProceduralMasqueradeDummyType,
295295
Range,
296296
RangeBounds,
297+
RangeCopy,
297298
RangeFrom,
299+
RangeFromCopy,
298300
RangeFull,
299301
RangeInclusive,
302+
RangeInclusiveCopy,
300303
RangeTo,
301304
RangeToInclusive,
302305
Rc,
@@ -1364,6 +1367,7 @@ symbols! {
13641367
new_lower_hex,
13651368
new_octal,
13661369
new_pointer,
1370+
new_range,
13671371
new_unchecked,
13681372
new_upper_exp,
13691373
new_upper_hex,

library/core/src/range.rs

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub use crate::ops::{Bound, OneSidedRange, RangeBounds, RangeFull, RangeTo, Rang
4848
/// assert_eq!(Range::from(3..5), Range { start: 3, end: 5 });
4949
/// assert_eq!(3 + 4 + 5, Range::from(3..6).into_iter().sum());
5050
/// ```
51+
#[cfg_attr(not(bootstrap), lang = "RangeCopy")]
5152
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
5253
#[unstable(feature = "new_range_api", issue = "125687")]
5354
pub struct Range<Idx> {
@@ -205,6 +206,7 @@ impl<T> From<legacy::Range<T>> for Range<T> {
205206
/// assert_eq!(RangeInclusive::from(3..=5), RangeInclusive { start: 3, end: 5 });
206207
/// assert_eq!(3 + 4 + 5, RangeInclusive::from(3..=5).into_iter().sum());
207208
/// ```
209+
#[cfg_attr(not(bootstrap), lang = "RangeInclusiveCopy")]
208210
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
209211
#[unstable(feature = "new_range_api", issue = "125687")]
210212
pub struct RangeInclusive<Idx> {
@@ -388,6 +390,7 @@ impl<T> From<legacy::RangeInclusive<T>> for RangeInclusive<T> {
388390
/// assert_eq!(RangeFrom::from(2..), core::range::RangeFrom { start: 2 });
389391
/// assert_eq!(2 + 3 + 4, RangeFrom::from(2..).into_iter().take(3).sum());
390392
/// ```
393+
#[cfg_attr(not(bootstrap), lang = "RangeFromCopy")]
391394
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
392395
#[unstable(feature = "new_range_api", issue = "125687")]
393396
pub struct RangeFrom<Idx> {

library/std/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,8 @@ pub use core::option;
530530
pub use core::pin;
531531
#[stable(feature = "rust1", since = "1.0.0")]
532532
pub use core::ptr;
533+
#[unstable(feature = "new_range_api", issue = "125687")]
534+
pub use core::range;
533535
#[stable(feature = "rust1", since = "1.0.0")]
534536
pub use core::result;
535537
#[stable(feature = "rust1", since = "1.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# `new_range`
2+
3+
The tracking issue for this feature is: [#123741]
4+
5+
[#123741]: https://github.com/rust-lang/rust/issues/123741
6+
7+
---
8+
9+
Switch the syntaxes `a..`, `a..b`, and `a..=b` to resolve the new range types.

tests/mir-opt/pre-codegen/range_iter.inclusive_loop.PreCodegen.after.panic-abort.mir

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ fn inclusive_loop(_1: u32, _2: u32, _3: impl Fn(u32)) -> () {
1919
scope 2 {
2020
debug x => _9;
2121
}
22-
scope 5 (inlined iter::range::<impl Iterator for RangeInclusive<u32>>::next) {
22+
scope 5 (inlined iter::range::<impl Iterator for std::ops::RangeInclusive<u32>>::next) {
2323
}
2424
}
25-
scope 3 (inlined RangeInclusive::<u32>::new) {
25+
scope 3 (inlined std::ops::RangeInclusive::<u32>::new) {
2626
}
27-
scope 4 (inlined <RangeInclusive<u32> as IntoIterator>::into_iter) {
27+
scope 4 (inlined <std::ops::RangeInclusive<u32> as IntoIterator>::into_iter) {
2828
}
2929

3030
bb0: {
31-
_4 = RangeInclusive::<u32> { start: copy _1, end: copy _2, exhausted: const false };
31+
_4 = std::ops::RangeInclusive::<u32> { start: copy _1, end: copy _2, exhausted: const false };
3232
StorageLive(_5);
3333
_5 = copy _4;
3434
goto -> bb1;
@@ -37,7 +37,7 @@ fn inclusive_loop(_1: u32, _2: u32, _3: impl Fn(u32)) -> () {
3737
bb1: {
3838
StorageLive(_7);
3939
_6 = &mut _5;
40-
_7 = <RangeInclusive<u32> as iter::range::RangeInclusiveIteratorImpl>::spec_next(move _6) -> [return: bb2, unwind unreachable];
40+
_7 = <std::ops::RangeInclusive<u32> as iter::range::RangeInclusiveIteratorImpl>::spec_next(move _6) -> [return: bb2, unwind unreachable];
4141
}
4242

4343
bb2: {

tests/mir-opt/pre-codegen/range_iter.inclusive_loop.PreCodegen.after.panic-unwind.mir

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ fn inclusive_loop(_1: u32, _2: u32, _3: impl Fn(u32)) -> () {
1919
scope 2 {
2020
debug x => _9;
2121
}
22-
scope 5 (inlined iter::range::<impl Iterator for RangeInclusive<u32>>::next) {
22+
scope 5 (inlined iter::range::<impl Iterator for std::ops::RangeInclusive<u32>>::next) {
2323
}
2424
}
25-
scope 3 (inlined RangeInclusive::<u32>::new) {
25+
scope 3 (inlined std::ops::RangeInclusive::<u32>::new) {
2626
}
27-
scope 4 (inlined <RangeInclusive<u32> as IntoIterator>::into_iter) {
27+
scope 4 (inlined <std::ops::RangeInclusive<u32> as IntoIterator>::into_iter) {
2828
}
2929

3030
bb0: {
31-
_4 = RangeInclusive::<u32> { start: copy _1, end: copy _2, exhausted: const false };
31+
_4 = std::ops::RangeInclusive::<u32> { start: copy _1, end: copy _2, exhausted: const false };
3232
StorageLive(_5);
3333
_5 = copy _4;
3434
goto -> bb1;
@@ -37,7 +37,7 @@ fn inclusive_loop(_1: u32, _2: u32, _3: impl Fn(u32)) -> () {
3737
bb1: {
3838
StorageLive(_7);
3939
_6 = &mut _5;
40-
_7 = <RangeInclusive<u32> as iter::range::RangeInclusiveIteratorImpl>::spec_next(move _6) -> [return: bb2, unwind: bb8];
40+
_7 = <std::ops::RangeInclusive<u32> as iter::range::RangeInclusiveIteratorImpl>::spec_next(move _6) -> [return: bb2, unwind: bb8];
4141
}
4242

4343
bb2: {

tests/mir-opt/pre-codegen/range_iter.range_inclusive_iter_next.PreCodegen.after.panic-abort.mir

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// MIR for `range_inclusive_iter_next` after PreCodegen
22

3-
fn range_inclusive_iter_next(_1: &mut RangeInclusive<u32>) -> Option<u32> {
3+
fn range_inclusive_iter_next(_1: &mut std::ops::RangeInclusive<u32>) -> Option<u32> {
44
debug it => _1;
55
let mut _0: std::option::Option<u32>;
6-
scope 1 (inlined iter::range::<impl Iterator for RangeInclusive<u32>>::next) {
6+
scope 1 (inlined iter::range::<impl Iterator for std::ops::RangeInclusive<u32>>::next) {
77
}
88

99
bb0: {
10-
_0 = <RangeInclusive<u32> as iter::range::RangeInclusiveIteratorImpl>::spec_next(move _1) -> [return: bb1, unwind unreachable];
10+
_0 = <std::ops::RangeInclusive<u32> as iter::range::RangeInclusiveIteratorImpl>::spec_next(move _1) -> [return: bb1, unwind unreachable];
1111
}
1212

1313
bb1: {

tests/mir-opt/pre-codegen/range_iter.range_inclusive_iter_next.PreCodegen.after.panic-unwind.mir

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// MIR for `range_inclusive_iter_next` after PreCodegen
22

3-
fn range_inclusive_iter_next(_1: &mut RangeInclusive<u32>) -> Option<u32> {
3+
fn range_inclusive_iter_next(_1: &mut std::ops::RangeInclusive<u32>) -> Option<u32> {
44
debug it => _1;
55
let mut _0: std::option::Option<u32>;
6-
scope 1 (inlined iter::range::<impl Iterator for RangeInclusive<u32>>::next) {
6+
scope 1 (inlined iter::range::<impl Iterator for std::ops::RangeInclusive<u32>>::next) {
77
}
88

99
bb0: {
10-
_0 = <RangeInclusive<u32> as iter::range::RangeInclusiveIteratorImpl>::spec_next(move _1) -> [return: bb1, unwind continue];
10+
_0 = <std::ops::RangeInclusive<u32> as iter::range::RangeInclusiveIteratorImpl>::spec_next(move _1) -> [return: bb1, unwind continue];
1111
}
1212

1313
bb1: {

tests/ui/const-generics/std/const-generics-range.full.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0741]: `std::ops::Range<usize>` must implement `ConstParamTy` to be used
44
LL | struct _Range<const R: std::ops::Range<usize>>;
55
| ^^^^^^^^^^^^^^^^^^^^^^
66

7-
error[E0741]: `RangeFrom<usize>` must implement `ConstParamTy` to be used as the type of a const generic parameter
7+
error[E0741]: `std::ops::RangeFrom<usize>` must implement `ConstParamTy` to be used as the type of a const generic parameter
88
--> $DIR/const-generics-range.rs:13:28
99
|
1010
LL | struct _RangeFrom<const R: std::ops::RangeFrom<usize>>;
@@ -16,7 +16,7 @@ error[E0741]: `RangeFull` must implement `ConstParamTy` to be used as the type o
1616
LL | struct _RangeFull<const R: std::ops::RangeFull>;
1717
| ^^^^^^^^^^^^^^^^^^^
1818

19-
error[E0741]: `RangeInclusive<usize>` must implement `ConstParamTy` to be used as the type of a const generic parameter
19+
error[E0741]: `std::ops::RangeInclusive<usize>` must implement `ConstParamTy` to be used as the type of a const generic parameter
2020
--> $DIR/const-generics-range.rs:24:33
2121
|
2222
LL | struct _RangeInclusive<const R: std::ops::RangeInclusive<usize>>;

tests/ui/const-generics/std/const-generics-range.min.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ help: add `#![feature(adt_const_params)]` to the crate attributes to enable more
1010
LL + #![feature(adt_const_params)]
1111
|
1212

13-
error: `RangeFrom<usize>` is forbidden as the type of a const generic parameter
13+
error: `std::ops::RangeFrom<usize>` is forbidden as the type of a const generic parameter
1414
--> $DIR/const-generics-range.rs:13:28
1515
|
1616
LL | struct _RangeFrom<const R: std::ops::RangeFrom<usize>>;
@@ -34,7 +34,7 @@ help: add `#![feature(adt_const_params)]` to the crate attributes to enable more
3434
LL + #![feature(adt_const_params)]
3535
|
3636

37-
error: `RangeInclusive<usize>` is forbidden as the type of a const generic parameter
37+
error: `std::ops::RangeInclusive<usize>` is forbidden as the type of a const generic parameter
3838
--> $DIR/const-generics-range.rs:24:33
3939
|
4040
LL | struct _RangeInclusive<const R: std::ops::RangeInclusive<usize>>;

tests/ui/const-generics/std/const-generics-range.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const RANGE : _Range<{ 0 .. 1000 }> = _Range;
1111

1212
// `RangeFrom` should be usable within const generics:
1313
struct _RangeFrom<const R: std::ops::RangeFrom<usize>>;
14-
//[min]~^ ERROR `RangeFrom<usize>` is forbidden
14+
//[min]~^ ERROR `std::ops::RangeFrom<usize>` is forbidden
1515
const RANGE_FROM : _RangeFrom<{ 0 .. }> = _RangeFrom;
1616

1717
// `RangeFull` should be usable within const generics:
@@ -22,7 +22,7 @@ const RANGE_FULL : _RangeFull<{ .. }> = _RangeFull;
2222
// Regression test for #70155
2323
// `RangeInclusive` should be usable within const generics:
2424
struct _RangeInclusive<const R: std::ops::RangeInclusive<usize>>;
25-
//[min]~^ ERROR `RangeInclusive<usize>` is forbidden
25+
//[min]~^ ERROR `std::ops::RangeInclusive<usize>` is forbidden
2626
const RANGE_INCLUSIVE : _RangeInclusive<{ 0 ..= 999 }> = _RangeInclusive;
2727

2828
// `RangeTo` should be usable within const generics:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(new_range_api)]
2+
3+
fn main() {
4+
let a: core::range::RangeFrom<u8> = 1..;
5+
//~^ mismatched types
6+
let b: core::range::Range<u8> = 2..3;
7+
//~^ mismatched types
8+
let c: core::range::RangeInclusive<u8> = 4..=5;
9+
//~^ mismatched types
10+
}

0 commit comments

Comments
 (0)