Add support for ..= syntax

Add ..= to the parser

Add ..= to libproc_macro

Add ..= to ICH

Highlight ..= in rustdoc

Update impl Debug for RangeInclusive to ..=

Replace `...` to `..=` in range docs

Make the dotdoteq warning point to the ...

Add warning for ... in expressions

Updated more tests to the ..= syntax

Updated even more tests to the ..= syntax

Updated the inclusive_range entry in unstable book
This commit is contained in:
Alex Burka 2017-09-19 05:40:04 +00:00 committed by Badel2
parent 3eb19bf9b1
commit e64efc91f4
33 changed files with 244 additions and 182 deletions

View file

@ -10,5 +10,5 @@
fn main() {
let tmp = vec![0, 1, 2, 3, 4, 4, 3, 3, 2, 1];
let x = &tmp[1...]; //~ ERROR E0586
let x = &tmp[1..=]; //~ ERROR E0586
}

View file

@ -18,12 +18,12 @@ pub fn main() {
..1;
0..1;
...; //~ERROR inclusive range with no end
..=; //~ERROR inclusive range with no end
//~^HELP bounded at the end
0...; //~ERROR inclusive range with no end
0..=; //~ERROR inclusive range with no end
//~^HELP bounded at the end
...1;
0...1;
..=1;
0..=1;
}

View file

@ -14,7 +14,7 @@
// #![feature(inclusive_range)]
pub fn main() {
let _: std::ops::RangeInclusive<_> = { use std::intrinsics; 1 } ... { use std::intrinsics; 2 };
let _: std::ops::RangeInclusive<_> = { use std::intrinsics; 1 } ..= { use std::intrinsics; 2 };
//~^ ERROR use of unstable library feature 'inclusive_range'
//~| ERROR core_intrinsics
//~| ERROR core_intrinsics

View file

@ -153,5 +153,5 @@ fn exclusive_to_inclusive_range(slice: &[u32]) -> &[u32] {
#[rustc_metadata_clean(cfg="cfail2")]
#[rustc_metadata_clean(cfg="cfail3")]
fn exclusive_to_inclusive_range(slice: &[u32]) -> &[u32] {
&slice[3...7]
&slice[3..=7]
}

View file

@ -11,5 +11,5 @@
// Parsing of range patterns
fn main() {
let macropus!() ... 11 = 12; //~ error: expected one of `:`, `;`, or `=`, found `...`
let macropus!() ..= 11 = 12; //~ error: expected one of `:`, `;`, or `=`, found `..=`
}

View file

@ -11,5 +11,5 @@
// Parsing of range patterns
fn main() {
let 10 ... makropulos!() = 12; //~ error: expected one of `::`, `:`, `;`, or `=`, found `!`
let 10 ..= makropulos!() = 12; //~ error: expected one of `::`, `:`, `;`, or `=`, found `!`
}

View file

@ -11,5 +11,5 @@
// Parsing of range patterns
fn main() {
let 10 ... 10 + 3 = 12; //~ expected one of `:`, `;`, or `=`, found `+`
let 10 ..= 10 + 3 = 12; //~ expected one of `:`, `;`, or `=`, found `+`
}

View file

@ -11,5 +11,6 @@
// Parsing of range patterns
fn main() {
let 10 - 3 ... 10 = 8; //~ error: expected one of `...`, `..`, `:`, `;`, or `=`, found `-`
let 10 - 3 ..= 10 = 8;
//~^ error: expected one of `...`, `..=`, `..`, `:`, `;`, or `=`, found `-`
}

View file

@ -13,7 +13,7 @@
#![feature(inclusive_range_syntax, inclusive_range)]
pub fn main() {
for _ in 1... {} //~ERROR inclusive range with no end
for _ in 1..= {} //~ERROR inclusive range with no end
//~^HELP bounded at the end
}

View file

@ -15,21 +15,21 @@
// #![feature(inclusive_range_syntax, inclusive_range)]
macro_rules! m {
() => { for _ in 1...10 {} } //~ ERROR inclusive range syntax is experimental
() => { for _ in 1..=10 {} } //~ ERROR inclusive range syntax is experimental
}
#[cfg(nope)]
fn f() {}
#[cfg(not(nope))]
fn f() {
for _ in 1...10 {} //~ ERROR inclusive range syntax is experimental
for _ in 1..=10 {} //~ ERROR inclusive range syntax is experimental
}
#[cfg(nope)]
macro_rules! n { () => {} }
#[cfg(not(nope))]
macro_rules! n {
() => { for _ in 1...10 {} } //~ ERROR inclusive range syntax is experimental
() => { for _ in 1..=10 {} } //~ ERROR inclusive range syntax is experimental
}
macro_rules! o {
@ -38,7 +38,7 @@ macro_rules! o {
fn g() {}
#[cfg(not(nope))]
fn g() {
for _ in 1...10 {} //~ ERROR inclusive range syntax is experimental
for _ in 1..=10 {} //~ ERROR inclusive range syntax is experimental
}
g();
@ -54,7 +54,7 @@ macro_rules! p {
fn h() {}
#[cfg(not(nope))]
fn h() {
for _ in 1...10 {} //~ ERROR inclusive range syntax is experimental
for _ in 1..=10 {} //~ ERROR inclusive range syntax is experimental
}
h();
@ -62,8 +62,8 @@ macro_rules! p {
}
pub fn main() {
for _ in 1...10 {} //~ ERROR inclusive range syntax is experimental
for _ in ...10 {} //~ ERROR inclusive range syntax is experimental
for _ in 1..=10 {} //~ ERROR inclusive range syntax is experimental
for _ in ..=10 {} //~ ERROR inclusive range syntax is experimental
f(); // not allowed in cfg'ed functions

View file

@ -0,0 +1,20 @@
// Copyright 2017 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.
// Test old and new syntax for inclusive range patterns.
fn main() {
assert!(match 42 { 0 ... 100 => true, _ => false });
assert!(match 42 { 0 ..= 100 => true, _ => false });
assert!(match 'x' { 'a' ... 'z' => true, _ => false });
assert!(match 'x' { 'a' ..= 'z' => true, _ => false });
}

View file

@ -17,18 +17,18 @@ use std::ops::{RangeInclusive, RangeToInclusive};
fn foo() -> isize { 42 }
// Test that range syntax works in return statements
fn return_range_to() -> RangeToInclusive<i32> { return ...1; }
fn return_range_to() -> RangeToInclusive<i32> { return ..=1; }
pub fn main() {
let mut count = 0;
for i in 0_usize...10 {
for i in 0_usize..=10 {
assert!(i >= 0 && i <= 10);
count += i;
}
assert_eq!(count, 55);
let mut count = 0;
let mut range = 0_usize...10;
let mut range = 0_usize..=10;
for i in range {
assert!(i >= 0 && i <= 10);
count += i;
@ -36,53 +36,53 @@ pub fn main() {
assert_eq!(count, 55);
let mut count = 0;
for i in (0_usize...10).step_by(2) {
for i in (0_usize..=10).step_by(2) {
assert!(i >= 0 && i <= 10 && i % 2 == 0);
count += i;
}
assert_eq!(count, 30);
let _ = 0_usize...4+4-3;
let _ = 0...foo();
let _ = 0_usize..=4+4-3;
let _ = 0..=foo();
let _ = { &42...&100 }; // references to literals are OK
let _ = ...42_usize;
let _ = { &42..=&100 }; // references to literals are OK
let _ = ..=42_usize;
// Test we can use two different types with a common supertype.
let x = &42;
{
let y = 42;
let _ = x...&y;
let _ = x..=&y;
}
// test collection indexing
let vec = (0...10).collect::<Vec<_>>();
let vec = (0..=10).collect::<Vec<_>>();
let slice: &[_] = &*vec;
let string = String::from("hello world");
let stir = "hello world";
assert_eq!(&vec[3...6], &[3, 4, 5, 6]);
assert_eq!(&vec[ ...6], &[0, 1, 2, 3, 4, 5, 6]);
assert_eq!(&vec[3..=6], &[3, 4, 5, 6]);
assert_eq!(&vec[ ..=6], &[0, 1, 2, 3, 4, 5, 6]);
assert_eq!(&slice[3...6], &[3, 4, 5, 6]);
assert_eq!(&slice[ ...6], &[0, 1, 2, 3, 4, 5, 6]);
assert_eq!(&slice[3..=6], &[3, 4, 5, 6]);
assert_eq!(&slice[ ..=6], &[0, 1, 2, 3, 4, 5, 6]);
assert_eq!(&string[3...6], "lo w");
assert_eq!(&string[ ...6], "hello w");
assert_eq!(&string[3..=6], "lo w");
assert_eq!(&string[ ..=6], "hello w");
assert_eq!(&stir[3...6], "lo w");
assert_eq!(&stir[ ...6], "hello w");
assert_eq!(&stir[3..=6], "lo w");
assert_eq!(&stir[ ..=6], "hello w");
// test the size hints and emptying
let mut long = 0...255u8;
let mut short = 42...42u8;
let mut long = 0..=255u8;
let mut short = 42..=42u8;
assert_eq!(long.size_hint(), (256, Some(256)));
assert_eq!(short.size_hint(), (1, Some(1)));
long.next();
short.next();
assert_eq!(long.size_hint(), (255, Some(255)));
assert_eq!(short.size_hint(), (0, Some(0)));
assert_eq!(short, 1...0);
assert_eq!(short, 1..=0);
assert_eq!(long.len(), 255);
assert_eq!(short.len(), 0);
@ -94,31 +94,31 @@ pub fn main() {
assert_eq!(long.next(), Some(1));
assert_eq!(long.next(), Some(2));
assert_eq!(long.next_back(), Some(252));
for i in 3...251 {
for i in 3..=251 {
assert_eq!(long.next(), Some(i));
}
assert_eq!(long, 1...0);
assert_eq!(long, 1..=0);
// check underflow
let mut narrow = 1...0;
let mut narrow = 1..=0;
assert_eq!(narrow.next_back(), None);
assert_eq!(narrow, 1...0);
let mut zero = 0u8...0;
assert_eq!(narrow, 1..=0);
let mut zero = 0u8..=0;
assert_eq!(zero.next_back(), Some(0));
assert_eq!(zero.next_back(), None);
assert_eq!(zero, 1...0);
let mut high = 255u8...255;
assert_eq!(zero, 1..=0);
let mut high = 255u8..=255;
assert_eq!(high.next_back(), Some(255));
assert_eq!(high.next_back(), None);
assert_eq!(high, 1...0);
assert_eq!(high, 1..=0);
// what happens if you have a nonsense range?
let mut nonsense = 10...5;
let mut nonsense = 10..=5;
assert_eq!(nonsense.next(), None);
assert_eq!(nonsense, 10...5);
assert_eq!(nonsense, 10..=5);
// output
assert_eq!(format!("{:?}", 0...10), "0...10");
assert_eq!(format!("{:?}", ...10), "...10");
assert_eq!(format!("{:?}", long), "1...0");
assert_eq!(format!("{:?}", 0..=10), "0..=10");
assert_eq!(format!("{:?}", ..=10), "..=10");
assert_eq!(format!("{:?}", long), "1..=0");
}

View file

@ -14,7 +14,7 @@
fn main() {
let mut count = 0;
for i in 0_usize...10 {
for i in 0_usize..=10 {
assert!(i >= 0 && i <= 10);
count += i;
}