about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Jaszkowiak <p.jaszkow@gmail.com>2025-01-27 13:32:10 -0700
committerPeter Jaszkowiak <p.jaszkow@gmail.com>2025-01-30 21:33:11 -0700
commitf530a29944ff1eba9a146704ba2f13b94331be5d (patch)
tree06bea3432752ffd1127f222d618dde4f8dbb2832
parent95eaadc773eb4adab8f46cd77083d1d503fd5bff (diff)
downloadrust-f530a29944ff1eba9a146704ba2f13b94331be5d.tar.gz
rust-f530a29944ff1eba9a146704ba2f13b94331be5d.zip
implement unstable `new_range` feature
for RFC 3550, tracking issue #123741
-rw-r--r--compiler/rustc_ast_lowering/src/expr.rs35
-rw-r--r--compiler/rustc_feature/src/unstable.rs2
-rw-r--r--compiler/rustc_hir/src/hir.rs41
-rw-r--r--compiler/rustc_hir/src/lang_items.rs5
-rw-r--r--compiler/rustc_hir_typeck/src/method/suggest.rs4
-rw-r--r--compiler/rustc_span/src/symbol.rs4
-rw-r--r--library/core/src/range.rs3
-rw-r--r--src/doc/unstable-book/src/language-features/new-range.md9
-rw-r--r--tests/ui/feature-gates/feature-gate-new_range.rs10
-rw-r--r--tests/ui/feature-gates/feature-gate-new_range.stderr48
-rw-r--r--tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.current.fixed2
-rw-r--r--tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.next.stderr8
-rw-r--r--tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.rs2
-rw-r--r--tests/ui/new-range/disabled.rs27
-rw-r--r--tests/ui/new-range/enabled.rs24
15 files changed, 210 insertions, 14 deletions
diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs
index 1267281f73e..98d6372ca89 100644
--- a/compiler/rustc_ast_lowering/src/expr.rs
+++ b/compiler/rustc_ast_lowering/src/expr.rs
@@ -284,9 +284,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
                 ExprKind::Index(el, er, brackets_span) => {
                     hir::ExprKind::Index(self.lower_expr(el), self.lower_expr(er), *brackets_span)
                 }
-                ExprKind::Range(Some(e1), Some(e2), RangeLimits::Closed) => {
-                    self.lower_expr_range_closed(e.span, e1, e2)
-                }
                 ExprKind::Range(e1, e2, lims) => {
                     self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), *lims)
                 }
@@ -1512,15 +1509,39 @@ impl<'hir> LoweringContext<'_, 'hir> {
 
         let lang_item = match (e1, e2, lims) {
             (None, None, HalfOpen) => hir::LangItem::RangeFull,
-            (Some(..), None, HalfOpen) => hir::LangItem::RangeFrom,
+            (Some(..), None, HalfOpen) => {
+                if self.tcx.features().new_range() {
+                    hir::LangItem::RangeFromCopy
+                } else {
+                    hir::LangItem::RangeFrom
+                }
+            }
             (None, Some(..), HalfOpen) => hir::LangItem::RangeTo,
-            (Some(..), Some(..), HalfOpen) => hir::LangItem::Range,
+            (Some(..), Some(..), HalfOpen) => {
+                if self.tcx.features().new_range() {
+                    hir::LangItem::RangeCopy
+                } else {
+                    hir::LangItem::Range
+                }
+            }
             (None, Some(..), Closed) => hir::LangItem::RangeToInclusive,
-            (Some(..), Some(..), Closed) => unreachable!(),
+            (Some(e1), Some(e2), Closed) => {
+                if self.tcx.features().new_range() {
+                    hir::LangItem::RangeInclusiveCopy
+                } else {
+                    return self.lower_expr_range_closed(span, e1, e2);
+                }
+            }
             (start, None, Closed) => {
                 self.dcx().emit_err(InclusiveRangeWithNoEnd { span });
                 match start {
-                    Some(..) => hir::LangItem::RangeFrom,
+                    Some(..) => {
+                        if self.tcx.features().new_range() {
+                            hir::LangItem::RangeFromCopy
+                        } else {
+                            hir::LangItem::RangeFrom
+                        }
+                    }
                     None => hir::LangItem::RangeFull,
                 }
             }
diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs
index 1a216ebf117..7a30c8d4737 100644
--- a/compiler/rustc_feature/src/unstable.rs
+++ b/compiler/rustc_feature/src/unstable.rs
@@ -568,6 +568,8 @@ declare_features! (
     (unstable, never_type, "1.13.0", Some(35121)),
     /// Allows diverging expressions to fall back to `!` rather than `()`.
     (unstable, never_type_fallback, "1.41.0", Some(65992)),
+    /// Switch `..` syntax to use the new (`Copy + IntoIterator`) range types.
+    (unstable, new_range, "CURRENT_RUSTC_VERSION", Some(123741)),
     /// Allows `#![no_core]`.
     (unstable, no_core, "1.3.0", Some(29639)),
     /// Allows the use of `no_sanitize` attribute.
diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs
index af2f86b67e0..5483b97a7f4 100644
--- a/compiler/rustc_hir/src/hir.rs
+++ b/compiler/rustc_hir/src/hir.rs
@@ -2313,6 +2313,18 @@ impl Expr<'_> {
                     [val2],
                     StructTailExpr::None,
                 ),
+            )
+            | (
+                ExprKind::Struct(
+                    QPath::LangItem(LangItem::RangeFromCopy, _),
+                    [val1],
+                    StructTailExpr::None,
+                ),
+                ExprKind::Struct(
+                    QPath::LangItem(LangItem::RangeFromCopy, _),
+                    [val2],
+                    StructTailExpr::None,
+                ),
             ) => val1.expr.equivalent_for_indexing(val2.expr),
             (
                 ExprKind::Struct(
@@ -2325,6 +2337,30 @@ impl Expr<'_> {
                     [val2, val4],
                     StructTailExpr::None,
                 ),
+            )
+            | (
+                ExprKind::Struct(
+                    QPath::LangItem(LangItem::RangeCopy, _),
+                    [val1, val3],
+                    StructTailExpr::None,
+                ),
+                ExprKind::Struct(
+                    QPath::LangItem(LangItem::RangeCopy, _),
+                    [val2, val4],
+                    StructTailExpr::None,
+                ),
+            )
+            | (
+                ExprKind::Struct(
+                    QPath::LangItem(LangItem::RangeInclusiveCopy, _),
+                    [val1, val3],
+                    StructTailExpr::None,
+                ),
+                ExprKind::Struct(
+                    QPath::LangItem(LangItem::RangeInclusiveCopy, _),
+                    [val2, val4],
+                    StructTailExpr::None,
+                ),
             ) => {
                 val1.expr.equivalent_for_indexing(val2.expr)
                     && val3.expr.equivalent_for_indexing(val4.expr)
@@ -2354,7 +2390,10 @@ pub fn is_range_literal(expr: &Expr<'_>) -> bool {
                     | LangItem::RangeTo
                     | LangItem::RangeFrom
                     | LangItem::RangeFull
-                    | LangItem::RangeToInclusive,
+                    | LangItem::RangeToInclusive
+                    | LangItem::RangeCopy
+                    | LangItem::RangeFromCopy
+                    | LangItem::RangeInclusiveCopy,
                 ..
             )
         ),
diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_hir/src/lang_items.rs
index 02bc069fc5f..3edf1370d2b 100644
--- a/compiler/rustc_hir/src/lang_items.rs
+++ b/compiler/rustc_hir/src/lang_items.rs
@@ -415,6 +415,11 @@ language_item_table! {
     RangeToInclusive,        sym::RangeToInclusive,    range_to_inclusive_struct,  Target::Struct,         GenericRequirement::None;
     RangeTo,                 sym::RangeTo,             range_to_struct,            Target::Struct,         GenericRequirement::None;
 
+    // `new_range` types that are `Copy + IntoIterator`
+    RangeFromCopy,           sym::RangeFromCopy,       range_from_copy_struct,     Target::Struct,         GenericRequirement::None;
+    RangeCopy,               sym::RangeCopy,           range_copy_struct,          Target::Struct,         GenericRequirement::None;
+    RangeInclusiveCopy,      sym::RangeInclusiveCopy,  range_inclusive_copy_struct, Target::Struct,         GenericRequirement::None;
+
     String,                  sym::String,              string,                     Target::Struct,         GenericRequirement::None;
     CStr,                    sym::CStr,                c_str,                      Target::Struct,         GenericRequirement::None;
 }
diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs
index e4a6a0fedc5..fb48f5dca89 100644
--- a/compiler/rustc_hir_typeck/src/method/suggest.rs
+++ b/compiler/rustc_hir_typeck/src/method/suggest.rs
@@ -2410,6 +2410,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                     let lang_item = match parent_expr.kind {
                         ExprKind::Struct(qpath, _, _) => match *qpath {
                             QPath::LangItem(LangItem::Range, ..) => Some(LangItem::Range),
+                            QPath::LangItem(LangItem::RangeCopy, ..) => Some(LangItem::RangeCopy),
+                            QPath::LangItem(LangItem::RangeInclusiveCopy, ..) => {
+                                Some(LangItem::RangeInclusiveCopy)
+                            }
                             QPath::LangItem(LangItem::RangeTo, ..) => Some(LangItem::RangeTo),
                             QPath::LangItem(LangItem::RangeToInclusive, ..) => {
                                 Some(LangItem::RangeToInclusive)
diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs
index 6f1d3a74a81..dca96b74174 100644
--- a/compiler/rustc_span/src/symbol.rs
+++ b/compiler/rustc_span/src/symbol.rs
@@ -294,9 +294,12 @@ symbols! {
         ProceduralMasqueradeDummyType,
         Range,
         RangeBounds,
+        RangeCopy,
         RangeFrom,
+        RangeFromCopy,
         RangeFull,
         RangeInclusive,
+        RangeInclusiveCopy,
         RangeTo,
         RangeToInclusive,
         Rc,
@@ -1367,6 +1370,7 @@ symbols! {
         new_lower_hex,
         new_octal,
         new_pointer,
+        new_range,
         new_unchecked,
         new_upper_exp,
         new_upper_hex,
diff --git a/library/core/src/range.rs b/library/core/src/range.rs
index 427526fd14b..6a62928873f 100644
--- a/library/core/src/range.rs
+++ b/library/core/src/range.rs
@@ -48,6 +48,7 @@ pub use crate::ops::{Bound, OneSidedRange, RangeBounds, RangeFull, RangeTo, Rang
 /// assert_eq!(Range::from(3..5), Range { start: 3, end: 5 });
 /// assert_eq!(3 + 4 + 5, Range::from(3..6).into_iter().sum());
 /// ```
+#[cfg_attr(not(bootstrap), lang = "RangeCopy")]
 #[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
 #[unstable(feature = "new_range_api", issue = "125687")]
 pub struct Range<Idx> {
@@ -205,6 +206,7 @@ impl<T> From<legacy::Range<T>> for Range<T> {
 /// assert_eq!(RangeInclusive::from(3..=5), RangeInclusive { start: 3, end: 5 });
 /// assert_eq!(3 + 4 + 5, RangeInclusive::from(3..=5).into_iter().sum());
 /// ```
+#[cfg_attr(not(bootstrap), lang = "RangeInclusiveCopy")]
 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
 #[unstable(feature = "new_range_api", issue = "125687")]
 pub struct RangeInclusive<Idx> {
@@ -388,6 +390,7 @@ impl<T> From<legacy::RangeInclusive<T>> for RangeInclusive<T> {
 /// assert_eq!(RangeFrom::from(2..), core::range::RangeFrom { start: 2 });
 /// assert_eq!(2 + 3 + 4, RangeFrom::from(2..).into_iter().take(3).sum());
 /// ```
+#[cfg_attr(not(bootstrap), lang = "RangeFromCopy")]
 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
 #[unstable(feature = "new_range_api", issue = "125687")]
 pub struct RangeFrom<Idx> {
diff --git a/src/doc/unstable-book/src/language-features/new-range.md b/src/doc/unstable-book/src/language-features/new-range.md
new file mode 100644
index 00000000000..e7464f31e53
--- /dev/null
+++ b/src/doc/unstable-book/src/language-features/new-range.md
@@ -0,0 +1,9 @@
+# `new_range`
+
+The tracking issue for this feature is: [#123741]
+
+[#123741]: https://github.com/rust-lang/rust/issues/123741
+
+---
+
+Switch the syntaxes `a..`, `a..b`, and `a..=b` to resolve the new range types.
diff --git a/tests/ui/feature-gates/feature-gate-new_range.rs b/tests/ui/feature-gates/feature-gate-new_range.rs
new file mode 100644
index 00000000000..ecb73546d6a
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-new_range.rs
@@ -0,0 +1,10 @@
+#![feature(new_range_api)]
+
+fn main() {
+    let a: core::range::RangeFrom<u8> = 1..;
+    //~^ mismatched types
+    let b: core::range::Range<u8> = 2..3;
+    //~^ mismatched types
+    let c: core::range::RangeInclusive<u8> = 4..=5;
+    //~^ mismatched types
+}
diff --git a/tests/ui/feature-gates/feature-gate-new_range.stderr b/tests/ui/feature-gates/feature-gate-new_range.stderr
new file mode 100644
index 00000000000..c4241390418
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-new_range.stderr
@@ -0,0 +1,48 @@
+error[E0308]: mismatched types
+  --> $DIR/feature-gate-new_range.rs:4:41
+   |
+LL |     let a: core::range::RangeFrom<u8> = 1..;
+   |            --------------------------   ^^^ expected `RangeFrom<u8>`, found `RangeFrom<{integer}>`
+   |            |
+   |            expected due to this
+   |
+   = note: expected struct `std::range::RangeFrom<u8>`
+              found struct `std::ops::RangeFrom<{integer}>`
+help: call `Into::into` on this expression to convert `std::ops::RangeFrom<{integer}>` into `std::range::RangeFrom<u8>`
+   |
+LL |     let a: core::range::RangeFrom<u8> = 1...into();
+   |                                            +++++++
+
+error[E0308]: mismatched types
+  --> $DIR/feature-gate-new_range.rs:6:37
+   |
+LL |     let b: core::range::Range<u8> = 2..3;
+   |            ----------------------   ^^^^ expected `Range<u8>`, found `Range<{integer}>`
+   |            |
+   |            expected due to this
+   |
+   = note: expected struct `std::range::Range<u8>`
+              found struct `std::ops::Range<{integer}>`
+help: call `Into::into` on this expression to convert `std::ops::Range<{integer}>` into `std::range::Range<u8>`
+   |
+LL |     let b: core::range::Range<u8> = 2..3.into();
+   |                                         +++++++
+
+error[E0308]: mismatched types
+  --> $DIR/feature-gate-new_range.rs:8:46
+   |
+LL |     let c: core::range::RangeInclusive<u8> = 4..=5;
+   |            -------------------------------   ^^^^^ expected `RangeInclusive<u8>`, found `RangeInclusive<{integer}>`
+   |            |
+   |            expected due to this
+   |
+   = note: expected struct `std::range::RangeInclusive<u8>`
+              found struct `std::ops::RangeInclusive<{integer}>`
+help: call `Into::into` on this expression to convert `std::ops::RangeInclusive<{integer}>` into `std::range::RangeInclusive<u8>`
+   |
+LL |     let c: core::range::RangeInclusive<u8> = 4..=5.into();
+   |                                                   +++++++
+
+error: aborting due to 3 previous errors
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.current.fixed b/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.current.fixed
index fc44c824043..ba46a447802 100644
--- a/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.current.fixed
+++ b/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.current.fixed
@@ -9,5 +9,5 @@ fn main() {
     let _ = (-10..=10).find(|x: &i32| x.signum() == 0);
     //[current]~^ ERROR type mismatch in closure arguments
     //[next]~^^ ERROR expected `RangeInclusive<{integer}>` to be an iterator that yields `&&i32`, but it yields `{integer}`
-    //[next]~| ERROR expected a `FnMut(&<RangeInclusive<{integer}> as Iterator>::Item)` closure, found
+    //[next]~| ERROR expected a `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found
 }
diff --git a/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.next.stderr b/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.next.stderr
index 973fe7ade60..b7161310619 100644
--- a/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.next.stderr
+++ b/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.next.stderr
@@ -18,17 +18,17 @@ error[E0271]: expected `RangeInclusive<{integer}>` to be an iterator that yields
 LL |     let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
    |                        ^^^^ expected `&&i32`, found integer
 
-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}`
+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}`
   --> $DIR/closure-arg-type-mismatch-issue-45727.rs:9:29
    |
 LL |     let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
-   |                        ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnMut(&<RangeInclusive<{integer}> as Iterator>::Item)` closure, found `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:9:29: 9:40}`
+   |                        ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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}`
    |                        |
    |                        required by a bound introduced by this call
    |
-   = 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}`
+   = 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}`
    = note: expected a closure with arguments `(&&&i32,)`
-              found a closure with arguments `(&<RangeInclusive<{integer}> as Iterator>::Item,)`
+              found a closure with arguments `(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item,)`
 note: required by a bound in `find`
   --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
 
diff --git a/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.rs b/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.rs
index a96df10db35..0fd56707763 100644
--- a/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.rs
+++ b/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.rs
@@ -9,5 +9,5 @@ fn main() {
     let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
     //[current]~^ ERROR type mismatch in closure arguments
     //[next]~^^ ERROR expected `RangeInclusive<{integer}>` to be an iterator that yields `&&i32`, but it yields `{integer}`
-    //[next]~| ERROR expected a `FnMut(&<RangeInclusive<{integer}> as Iterator>::Item)` closure, found
+    //[next]~| ERROR expected a `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found
 }
diff --git a/tests/ui/new-range/disabled.rs b/tests/ui/new-range/disabled.rs
new file mode 100644
index 00000000000..1a5fe3f9743
--- /dev/null
+++ b/tests/ui/new-range/disabled.rs
@@ -0,0 +1,27 @@
+//@ check-pass
+
+#![feature(new_range_api)]
+
+fn main() {
+    // Unchanged
+    let a: core::range::RangeFull = ..;
+    let b: core::range::RangeTo<u8> = ..2;
+    let c: core::range::RangeToInclusive<u8> = ..=3;
+
+    let _: core::ops::RangeFull = a;
+    let _: core::ops::RangeTo<u8> = b;
+    let _: core::ops::RangeToInclusive<u8> = c;
+
+    // Changed
+    let a: core::range::legacy::RangeFrom<u8> = 1..;
+    let b: core::range::legacy::Range<u8> = 2..3;
+    let c: core::range::legacy::RangeInclusive<u8> = 4..=5;
+
+    let a: core::ops::RangeFrom<u8> = a;
+    let b: core::ops::Range<u8> = b;
+    let c: core::ops::RangeInclusive<u8> = c;
+
+    let _: core::ops::RangeFrom<u8> = a.into_iter();
+    let _: core::ops::Range<u8> = b.into_iter();
+    let _: core::ops::RangeInclusive<u8> = c.into_iter();
+}
diff --git a/tests/ui/new-range/enabled.rs b/tests/ui/new-range/enabled.rs
new file mode 100644
index 00000000000..a5fb76ad52b
--- /dev/null
+++ b/tests/ui/new-range/enabled.rs
@@ -0,0 +1,24 @@
+//@ check-pass
+
+#![feature(new_range_api)]
+#![feature(new_range)]
+
+fn main() {
+    // Unchanged
+    let a: core::range::RangeFull = ..;
+    let b: core::range::RangeTo<u8> = ..2;
+    let c: core::range::RangeToInclusive<u8> = ..=3;
+
+    let _: core::ops::RangeFull = a;
+    let _: core::ops::RangeTo<u8> = b;
+    let _: core::ops::RangeToInclusive<u8> = c;
+
+    // Changed
+    let a: core::range::RangeFrom<u8> = 1..;
+    let b: core::range::Range<u8> = 2..3;
+    let c: core::range::RangeInclusive<u8> = 4..=5;
+
+    let _: core::range::IterRangeFrom<u8> = a.into_iter();
+    let _: core::range::IterRange<u8> = b.into_iter();
+    let _: core::range::IterRangeInclusive<u8> = c.into_iter();
+}