Auto merge of #51622 - kennytm:three-field-range-inclusive, r=SimonSapin

Change RangeInclusive to a three-field struct.

Fix #45222.

This PR also reverts #48012 (i.e. removed the `try_fold`/`try_rfold` specialization for `RangeInclusive`) because LLVM no longer has trouble recognizing a RangeInclusive loop.
This commit is contained in:
bors 2018-07-13 10:18:38 +00:00
commit c0955a34bc
7 changed files with 208 additions and 112 deletions

View file

@ -0,0 +1,74 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// compile-flags: -O
// min-llvm-version 6.0
#![crate_type = "lib"]
// verify that LLVM recognizes a loop involving 0..=n and will const-fold it.
//------------------------------------------------------------------------------
// Example from original issue #45222
fn foo2(n: u64) -> u64 {
let mut count = 0;
for _ in 0..n {
for j in (0..=n).rev() {
count += j;
}
}
count
}
// CHECK-LABEL: @check_foo2
#[no_mangle]
pub fn check_foo2() -> u64 {
// CHECK: ret i64 500005000000000
foo2(100000)
}
//------------------------------------------------------------------------------
// Simplified example of #45222
fn triangle_inc(n: u64) -> u64 {
let mut count = 0;
for j in 0 ..= n {
count += j;
}
count
}
// CHECK-LABEL: @check_triangle_inc
#[no_mangle]
pub fn check_triangle_inc() -> u64 {
// CHECK: ret i64 5000050000
triangle_inc(100000)
}
//------------------------------------------------------------------------------
// Demo in #48012
fn foo3r(n: u64) -> u64 {
let mut count = 0;
(0..n).for_each(|_| {
(0 ..= n).rev().for_each(|j| {
count += j;
})
});
count
}
// CHECK-LABEL: @check_foo3r
#[no_mangle]
pub fn check_foo3r() -> u64 {
// CHECK: ret i64 500005000000000
foo3r(100000)
}

View file

@ -10,12 +10,18 @@
// Test inclusive range syntax.
use std::ops::{RangeInclusive, RangeToInclusive};
#![feature(range_is_empty)]
#![allow(unused_comparisons)]
use std::ops::RangeToInclusive;
fn foo() -> isize { 42 }
// Test that range syntax works in return statements
fn return_range_to() -> RangeToInclusive<i32> { return ..=1; }
pub fn return_range_to() -> RangeToInclusive<i32> { return ..=1; }
#[derive(Debug)]
struct P(u8);
pub fn main() {
let mut count = 0;
@ -26,7 +32,7 @@ pub fn main() {
assert_eq!(count, 55);
let mut count = 0;
let mut range = 0_usize..=10;
let range = 0_usize..=10;
for i in range {
assert!(i >= 0 && i <= 10);
count += i;
@ -80,7 +86,7 @@ pub fn main() {
short.next();
assert_eq!(long.size_hint(), (255, Some(255)));
assert_eq!(short.size_hint(), (0, Some(0)));
assert_eq!(short, 1..=0);
assert!(short.is_empty());
assert_eq!(long.len(), 255);
assert_eq!(short.len(), 0);
@ -95,28 +101,31 @@ pub fn main() {
for i in 3..=251 {
assert_eq!(long.next(), Some(i));
}
assert_eq!(long, 1..=0);
assert!(long.is_empty());
// check underflow
let mut narrow = 1..=0;
assert_eq!(narrow.next_back(), None);
assert_eq!(narrow, 1..=0);
assert!(narrow.is_empty());
let mut zero = 0u8..=0;
assert_eq!(zero.next_back(), Some(0));
assert_eq!(zero.next_back(), None);
assert_eq!(zero, 1..=0);
assert!(zero.is_empty());
let mut high = 255u8..=255;
assert_eq!(high.next_back(), Some(255));
assert_eq!(high.next_back(), None);
assert_eq!(high, 1..=0);
assert!(high.is_empty());
// what happens if you have a nonsense range?
let mut nonsense = 10..=5;
assert_eq!(nonsense.next(), None);
assert_eq!(nonsense, 10..=5);
assert!(nonsense.is_empty());
// output
assert_eq!(format!("{:?}", 0..=10), "0..=10");
assert_eq!(format!("{:?}", ..=10), "..=10");
assert_eq!(format!("{:?}", long), "1..=0");
assert_eq!(format!("{:?}", 9..=6), "9..=6");
// ensure that constructing a RangeInclusive does not need PartialOrd bound
assert_eq!(format!("{:?}", P(1)..=P(2)), "P(1)..=P(2)");
}