Skip to content

Commit

Permalink
structured suggestion and rewording for ... expression syntax error
Browse files Browse the repository at this point in the history
Now that `..=` inclusive ranges are stabilized, people probably
shouldn't be using `...` even in patterns, even if it's still legal
there (see #51043). To avoid drawing attention to `...` being a real
thing, let's reword this message to just say "unexpected token" rather
"cannot be used in expressions".
  • Loading branch information
zackmdavis committed Jun 24, 2018
1 parent 4650361 commit a417518
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
14 changes: 8 additions & 6 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -4800,12 +4800,14 @@ impl<'a> Parser<'a> {

fn err_dotdotdot_syntax(&self, span: Span) {
self.diagnostic().struct_span_err(span, {
"`...` syntax cannot be used in expressions"
}).help({
"Use `..` if you need an exclusive range (a < b)"
}).help({
"or `..=` if you need an inclusive range (a <= b)"
}).emit();
"unexpected token: `...`"
}).span_suggestion_with_applicability(
span, "use `..` for an exclusive range", "..".to_owned(),
Applicability::MaybeIncorrect
).span_suggestion_with_applicability(
span, "or `..=` for an inclusive range", "..=".to_owned(),
Applicability::MaybeIncorrect
).emit();
}

// Parse bounds of a type parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`.
Expand Down
25 changes: 12 additions & 13 deletions src/test/parse-fail/range_inclusive_dotdotdot.rs
Expand Up @@ -15,22 +15,21 @@
use std::ops::RangeToInclusive;

fn return_range_to() -> RangeToInclusive<i32> {
return ...1; //~ERROR `...` syntax cannot be used in expressions
//~^HELP Use `..` if you need an exclusive range (a < b)
//~^^HELP or `..=` if you need an inclusive range (a <= b)
return ...1; //~ERROR unexpected token: `...`
//~^HELP use `..` for an exclusive range
//~^^HELP or `..=` for an inclusive range
}

pub fn main() {
let x = ...0; //~ERROR `...` syntax cannot be used in expressions
//~^HELP Use `..` if you need an exclusive range (a < b)
//~^^HELP or `..=` if you need an inclusive range (a <= b)
let x = ...0; //~ERROR unexpected token: `...`
//~^HELP use `..` for an exclusive range
//~^^HELP or `..=` for an inclusive range

let x = 5...5; //~ERROR `...` syntax cannot be used in expressions
//~^HELP Use `..` if you need an exclusive range (a < b)
//~^^HELP or `..=` if you need an inclusive range (a <= b)
let x = 5...5; //~ERROR unexpected token: `...`
//~^HELP use `..` for an exclusive range
//~^^HELP or `..=` for an inclusive range

for _ in 0...1 {} //~ERROR `...` syntax cannot be used in expressions
//~^HELP Use `..` if you need an exclusive range (a < b)
//~^^HELP or `..=` if you need an inclusive range (a <= b)
for _ in 0...1 {} //~ERROR unexpected token: `...`
//~^HELP use `..` for an exclusive range
//~^^HELP or `..=` for an inclusive range
}

14 changes: 14 additions & 0 deletions src/test/ui/suggestions/dotdotdot-expr.rs
@@ -0,0 +1,14 @@
// 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.

fn main() {
let _redemptive = 1...21;
//~^ ERROR unexpected token
}
16 changes: 16 additions & 0 deletions src/test/ui/suggestions/dotdotdot-expr.stderr
@@ -0,0 +1,16 @@
error: unexpected token: `...`
--> $DIR/dotdotdot-expr.rs:12:24
|
LL | let _redemptive = 1...21;
| ^^^
help: use `..` for an exclusive range
|
LL | let _redemptive = 1..21;
| ^^
help: or `..=` for an inclusive range
|
LL | let _redemptive = 1..=21;
| ^^^

error: aborting due to previous error

0 comments on commit a417518

Please sign in to comment.