Skip to content

Commit f530a29

Browse files
committed
implement unstable new_range feature
for RFC 3550, tracking issue #123741
1 parent 95eaadc commit f530a29

File tree

15 files changed

+210
-14
lines changed

15 files changed

+210
-14
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
@@ -568,6 +568,8 @@ declare_features! (
568568
(unstable, never_type, "1.13.0", Some(35121)),
569569
/// Allows diverging expressions to fall back to `!` rather than `()`.
570570
(unstable, never_type_fallback, "1.41.0", Some(65992)),
571+
/// Switch `..` syntax to use the new (`Copy + IntoIterator`) range types.
572+
(unstable, new_range, "CURRENT_RUSTC_VERSION", Some(123741)),
571573
/// Allows `#![no_core]`.
572574
(unstable, no_core, "1.3.0", Some(29639)),
573575
/// 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
@@ -415,6 +415,11 @@ language_item_table! {
415415
RangeToInclusive, sym::RangeToInclusive, range_to_inclusive_struct, Target::Struct, GenericRequirement::None;
416416
RangeTo, sym::RangeTo, range_to_struct, Target::Struct, GenericRequirement::None;
417417

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

compiler/rustc_hir_typeck/src/method/suggest.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2410,6 +2410,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24102410
let lang_item = match parent_expr.kind {
24112411
ExprKind::Struct(qpath, _, _) => match *qpath {
24122412
QPath::LangItem(LangItem::Range, ..) => Some(LangItem::Range),
2413+
QPath::LangItem(LangItem::RangeCopy, ..) => Some(LangItem::RangeCopy),
2414+
QPath::LangItem(LangItem::RangeInclusiveCopy, ..) => {
2415+
Some(LangItem::RangeInclusiveCopy)
2416+
}
24132417
QPath::LangItem(LangItem::RangeTo, ..) => Some(LangItem::RangeTo),
24142418
QPath::LangItem(LangItem::RangeToInclusive, ..) => {
24152419
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,
@@ -1367,6 +1370,7 @@ symbols! {
13671370
new_lower_hex,
13681371
new_octal,
13691372
new_pointer,
1373+
new_range,
13701374
new_unchecked,
13711375
new_upper_exp,
13721376
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> {
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.
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/feature-gate-new_range.rs:4:41
3+
|
4+
LL | let a: core::range::RangeFrom<u8> = 1..;
5+
| -------------------------- ^^^ expected `RangeFrom<u8>`, found `RangeFrom<{integer}>`
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected struct `std::range::RangeFrom<u8>`
10+
found struct `std::ops::RangeFrom<{integer}>`
11+
help: call `Into::into` on this expression to convert `std::ops::RangeFrom<{integer}>` into `std::range::RangeFrom<u8>`
12+
|
13+
LL | let a: core::range::RangeFrom<u8> = 1...into();
14+
| +++++++
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/feature-gate-new_range.rs:6:37
18+
|
19+
LL | let b: core::range::Range<u8> = 2..3;
20+
| ---------------------- ^^^^ expected `Range<u8>`, found `Range<{integer}>`
21+
| |
22+
| expected due to this
23+
|
24+
= note: expected struct `std::range::Range<u8>`
25+
found struct `std::ops::Range<{integer}>`
26+
help: call `Into::into` on this expression to convert `std::ops::Range<{integer}>` into `std::range::Range<u8>`
27+
|
28+
LL | let b: core::range::Range<u8> = 2..3.into();
29+
| +++++++
30+
31+
error[E0308]: mismatched types
32+
--> $DIR/feature-gate-new_range.rs:8:46
33+
|
34+
LL | let c: core::range::RangeInclusive<u8> = 4..=5;
35+
| ------------------------------- ^^^^^ expected `RangeInclusive<u8>`, found `RangeInclusive<{integer}>`
36+
| |
37+
| expected due to this
38+
|
39+
= note: expected struct `std::range::RangeInclusive<u8>`
40+
found struct `std::ops::RangeInclusive<{integer}>`
41+
help: call `Into::into` on this expression to convert `std::ops::RangeInclusive<{integer}>` into `std::range::RangeInclusive<u8>`
42+
|
43+
LL | let c: core::range::RangeInclusive<u8> = 4..=5.into();
44+
| +++++++
45+
46+
error: aborting due to 3 previous errors
47+
48+
For more information about this error, try `rustc --explain E0308`.

tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.current.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ fn main() {
99
let _ = (-10..=10).find(|x: &i32| x.signum() == 0);
1010
//[current]~^ ERROR type mismatch in closure arguments
1111
//[next]~^^ ERROR expected `RangeInclusive<{integer}>` to be an iterator that yields `&&i32`, but it yields `{integer}`
12-
//[next]~| ERROR expected a `FnMut(&<RangeInclusive<{integer}> as Iterator>::Item)` closure, found
12+
//[next]~| ERROR expected a `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found
1313
}

tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.next.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ error[E0271]: expected `RangeInclusive<{integer}>` to be an iterator that yields
1818
LL | let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
1919
| ^^^^ expected `&&i32`, found integer
2020

21-
error[E0277]: expected a `FnMut(&<RangeInclusive<{integer}> as Iterator>::Item)` closure, found `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:9:29: 9:40}`
21+
error[E0277]: expected a `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:9:29: 9:40}`
2222
--> $DIR/closure-arg-type-mismatch-issue-45727.rs:9:29
2323
|
2424
LL | let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
25-
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnMut(&<RangeInclusive<{integer}> as Iterator>::Item)` closure, found `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:9:29: 9:40}`
25+
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:9:29: 9:40}`
2626
| |
2727
| required by a bound introduced by this call
2828
|
29-
= help: the trait `for<'a> FnMut(&'a <RangeInclusive<{integer}> as Iterator>::Item)` is not implemented for closure `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:9:29: 9:40}`
29+
= help: the trait `for<'a> FnMut(&'a <std::ops::RangeInclusive<{integer}> as Iterator>::Item)` is not implemented for closure `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:9:29: 9:40}`
3030
= note: expected a closure with arguments `(&&&i32,)`
31-
found a closure with arguments `(&<RangeInclusive<{integer}> as Iterator>::Item,)`
31+
found a closure with arguments `(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item,)`
3232
note: required by a bound in `find`
3333
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
3434

tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ fn main() {
99
let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
1010
//[current]~^ ERROR type mismatch in closure arguments
1111
//[next]~^^ ERROR expected `RangeInclusive<{integer}>` to be an iterator that yields `&&i32`, but it yields `{integer}`
12-
//[next]~| ERROR expected a `FnMut(&<RangeInclusive<{integer}> as Iterator>::Item)` closure, found
12+
//[next]~| ERROR expected a `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found
1313
}

tests/ui/new-range/disabled.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ check-pass
2+
3+
#![feature(new_range_api)]
4+
5+
fn main() {
6+
// Unchanged
7+
let a: core::range::RangeFull = ..;
8+
let b: core::range::RangeTo<u8> = ..2;
9+
let c: core::range::RangeToInclusive<u8> = ..=3;
10+
11+
let _: core::ops::RangeFull = a;
12+
let _: core::ops::RangeTo<u8> = b;
13+
let _: core::ops::RangeToInclusive<u8> = c;
14+
15+
// Changed
16+
let a: core::range::legacy::RangeFrom<u8> = 1..;
17+
let b: core::range::legacy::Range<u8> = 2..3;
18+
let c: core::range::legacy::RangeInclusive<u8> = 4..=5;
19+
20+
let a: core::ops::RangeFrom<u8> = a;
21+
let b: core::ops::Range<u8> = b;
22+
let c: core::ops::RangeInclusive<u8> = c;
23+
24+
let _: core::ops::RangeFrom<u8> = a.into_iter();
25+
let _: core::ops::Range<u8> = b.into_iter();
26+
let _: core::ops::RangeInclusive<u8> = c.into_iter();
27+
}

tests/ui/new-range/enabled.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ check-pass
2+
3+
#![feature(new_range_api)]
4+
#![feature(new_range)]
5+
6+
fn main() {
7+
// Unchanged
8+
let a: core::range::RangeFull = ..;
9+
let b: core::range::RangeTo<u8> = ..2;
10+
let c: core::range::RangeToInclusive<u8> = ..=3;
11+
12+
let _: core::ops::RangeFull = a;
13+
let _: core::ops::RangeTo<u8> = b;
14+
let _: core::ops::RangeToInclusive<u8> = c;
15+
16+
// Changed
17+
let a: core::range::RangeFrom<u8> = 1..;
18+
let b: core::range::Range<u8> = 2..3;
19+
let c: core::range::RangeInclusive<u8> = 4..=5;
20+
21+
let _: core::range::IterRangeFrom<u8> = a.into_iter();
22+
let _: core::range::IterRange<u8> = b.into_iter();
23+
let _: core::range::IterRangeInclusive<u8> = c.into_iter();
24+
}

0 commit comments

Comments
 (0)