Skip to content

Commit a417518

Browse files
committed
structured suggestion and rewording for ... expression syntax error
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".
1 parent 4650361 commit a417518

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

src/libsyntax/parse/parser.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -4800,12 +4800,14 @@ impl<'a> Parser<'a> {
48004800

48014801
fn err_dotdotdot_syntax(&self, span: Span) {
48024802
self.diagnostic().struct_span_err(span, {
4803-
"`...` syntax cannot be used in expressions"
4804-
}).help({
4805-
"Use `..` if you need an exclusive range (a < b)"
4806-
}).help({
4807-
"or `..=` if you need an inclusive range (a <= b)"
4808-
}).emit();
4803+
"unexpected token: `...`"
4804+
}).span_suggestion_with_applicability(
4805+
span, "use `..` for an exclusive range", "..".to_owned(),
4806+
Applicability::MaybeIncorrect
4807+
).span_suggestion_with_applicability(
4808+
span, "or `..=` for an inclusive range", "..=".to_owned(),
4809+
Applicability::MaybeIncorrect
4810+
).emit();
48094811
}
48104812

48114813
// Parse bounds of a type parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`.

src/test/parse-fail/range_inclusive_dotdotdot.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,21 @@
1515
use std::ops::RangeToInclusive;
1616

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

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

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

32-
for _ in 0...1 {} //~ERROR `...` syntax cannot be used in expressions
33-
//~^HELP Use `..` if you need an exclusive range (a < b)
34-
//~^^HELP or `..=` if you need an inclusive range (a <= b)
32+
for _ in 0...1 {} //~ERROR unexpected token: `...`
33+
//~^HELP use `..` for an exclusive range
34+
//~^^HELP or `..=` for an inclusive range
3535
}
36-
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let _redemptive = 1...21;
13+
//~^ ERROR unexpected token
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: unexpected token: `...`
2+
--> $DIR/dotdotdot-expr.rs:12:24
3+
|
4+
LL | let _redemptive = 1...21;
5+
| ^^^
6+
help: use `..` for an exclusive range
7+
|
8+
LL | let _redemptive = 1..21;
9+
| ^^
10+
help: or `..=` for an inclusive range
11+
|
12+
LL | let _redemptive = 1..=21;
13+
| ^^^
14+
15+
error: aborting due to previous error
16+

0 commit comments

Comments
 (0)