Skip to content

Commit fba903a

Browse files
committed
Make the fields of RangeInclusive private.
Added new()/start()/end() methods to RangeInclusive. Changed the lowering of `..=` to use RangeInclusive::new().
1 parent 64e6dda commit fba903a

File tree

6 files changed

+78
-6
lines changed

6 files changed

+78
-6
lines changed

src/liballoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
#![feature(on_unimplemented)]
123123
#![feature(exact_chunks)]
124124
#![feature(pointer_methods)]
125-
#![feature(inclusive_range_fields)]
125+
#![feature(inclusive_range_methods)]
126126
#![cfg_attr(stage0, feature(generic_param_attrs))]
127127

128128
#![cfg_attr(not(test), feature(fn_traits, i128))]

src/liballoc/tests/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#![feature(try_reserve)]
2626
#![feature(unboxed_closures)]
2727
#![feature(exact_chunks)]
28-
#![feature(inclusive_range_fields)]
28+
#![feature(inclusive_range_methods)]
2929

3030
extern crate alloc_system;
3131
extern crate core;

src/libcore/ops/range.rs

+59-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
320320
/// ```
321321
/// #![feature(inclusive_range_fields)]
322322
///
323-
/// assert_eq!((3..=5), std::ops::RangeInclusive { start: 3, end: 5 });
323+
/// assert_eq!((3..=5), std::ops::RangeInclusive::new(3, 5));
324324
/// assert_eq!(3 + 4 + 5, (3..=5).sum());
325325
///
326326
/// let arr = [0, 1, 2, 3];
@@ -331,14 +331,72 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
331331
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
332332
#[stable(feature = "inclusive_range", since = "1.26.0")]
333333
pub struct RangeInclusive<Idx> {
334+
// FIXME: The current representation follows RFC 1980,
335+
// but it is known that LLVM is not able to optimize loops following that RFC.
336+
// Consider adding an extra `bool` field to indicate emptiness of the range.
337+
// See #45222 for performance test cases.
338+
#[cfg(not(stage0))]
339+
pub(crate) start: Idx,
340+
#[cfg(not(stage0))]
341+
pub(crate) end: Idx,
334342
/// The lower bound of the range (inclusive).
343+
#[cfg(stage0)]
335344
#[unstable(feature = "inclusive_range_fields", issue = "49022")]
336345
pub start: Idx,
337346
/// The upper bound of the range (inclusive).
347+
#[cfg(stage0)]
338348
#[unstable(feature = "inclusive_range_fields", issue = "49022")]
339349
pub end: Idx,
340350
}
341351

352+
impl<Idx> RangeInclusive<Idx> {
353+
/// Creates a new inclusive range. Equivalent to writing `start..=end`.
354+
///
355+
/// # Examples
356+
///
357+
/// ```
358+
/// #![feature(inclusive_range_methods)]
359+
/// use std::ops::RangeInclusive;
360+
///
361+
/// assert_eq!(3..=5, RangeInclusive::new(3, 5));
362+
/// ```
363+
#[unstable(feature = "inclusive_range_methods", issue = "49022")]
364+
#[inline]
365+
pub fn new(start: Idx, end: Idx) -> Self {
366+
Self { start, end }
367+
}
368+
369+
/// Returns the lower bound of the range (inclusive).
370+
///
371+
/// # Examples
372+
///
373+
/// ```
374+
/// #![feature(inclusive_range_methods)]
375+
///
376+
/// assert_eq!((3..=5).start(), &3);
377+
/// ```
378+
#[unstable(feature = "inclusive_range_methods", issue = "49022")]
379+
#[inline]
380+
pub fn start(&self) -> &Idx {
381+
&self.start
382+
}
383+
384+
/// Returns the upper bound of the range (inclusive).
385+
///
386+
/// # Examples
387+
///
388+
/// ```
389+
/// #![feature(inclusive_range_methods)]
390+
///
391+
/// assert_eq!((3..=5).end(), &5);
392+
/// ```
393+
#[unstable(feature = "inclusive_range_methods", issue = "49022")]
394+
#[inline]
395+
pub fn end(&self) -> &Idx {
396+
&self.end
397+
}
398+
}
399+
342400
#[stable(feature = "inclusive_range", since = "1.26.0")]
343401
impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
344402
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {

src/libcore/tests/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#![feature(exact_chunks)]
4545
#![cfg_attr(stage0, feature(atomic_nand))]
4646
#![feature(reverse_bits)]
47-
#![feature(inclusive_range_fields)]
47+
#![feature(inclusive_range_methods)]
4848
#![feature(iterator_find_map)]
4949

5050
extern crate core;

src/librustc/hir/lowering.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -3119,6 +3119,20 @@ impl<'a> LoweringContext<'a> {
31193119
ExprKind::Index(ref el, ref er) => {
31203120
hir::ExprIndex(P(self.lower_expr(el)), P(self.lower_expr(er)))
31213121
}
3122+
// Desugar `<start>..=<end>` to `std::ops::RangeInclusive::new(<start>, <end>)`
3123+
ExprKind::Range(Some(ref e1), Some(ref e2), RangeLimits::Closed) => {
3124+
// FIXME: Use head_sp directly after RangeInclusive::new() is stabilized in stage0.
3125+
let span = self.allow_internal_unstable(CompilerDesugaringKind::DotFill, e.span);
3126+
let id = self.lower_node_id(e.id);
3127+
let e1 = self.lower_expr(e1);
3128+
let e2 = self.lower_expr(e2);
3129+
let ty_path = P(self.std_path(span, &["ops", "RangeInclusive"], false));
3130+
let ty = self.ty_path(id, span, hir::QPath::Resolved(None, ty_path));
3131+
let new_seg = P(hir::PathSegment::from_name(Symbol::intern("new")));
3132+
let new_path = hir::QPath::TypeRelative(ty, new_seg);
3133+
let new = P(self.expr(span, hir::ExprPath(new_path), ThinVec::new()));
3134+
hir::ExprCall(new, hir_vec![e1, e2])
3135+
}
31223136
ExprKind::Range(ref e1, ref e2, lims) => {
31233137
use syntax::ast::RangeLimits::*;
31243138

@@ -3128,7 +3142,7 @@ impl<'a> LoweringContext<'a> {
31283142
(&None, &Some(..), HalfOpen) => "RangeTo",
31293143
(&Some(..), &Some(..), HalfOpen) => "Range",
31303144
(&None, &Some(..), Closed) => "RangeToInclusive",
3131-
(&Some(..), &Some(..), Closed) => "RangeInclusive",
3145+
(&Some(..), &Some(..), Closed) => unreachable!(),
31323146
(_, &None, Closed) => self.diagnostic()
31333147
.span_fatal(e.span, "inclusive range with no end")
31343148
.raise(),

src/librustc_trans/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#![feature(rustc_diagnostic_macros)]
3030
#![feature(slice_sort_by_cached_key)]
3131
#![feature(optin_builtin_traits)]
32-
#![feature(inclusive_range_fields)]
32+
#![feature(inclusive_range_methods)]
3333

3434
use rustc::dep_graph::WorkProduct;
3535
use syntax_pos::symbol::Symbol;

0 commit comments

Comments
 (0)