From a417518173bae739d1aef50c6cf1a1e3bd4c4319 Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Sat, 23 Jun 2018 16:49:09 -0700 Subject: [PATCH] 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". --- src/libsyntax/parse/parser.rs | 14 ++++++----- .../parse-fail/range_inclusive_dotdotdot.rs | 25 +++++++++---------- src/test/ui/suggestions/dotdotdot-expr.rs | 14 +++++++++++ src/test/ui/suggestions/dotdotdot-expr.stderr | 16 ++++++++++++ 4 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 src/test/ui/suggestions/dotdotdot-expr.rs create mode 100644 src/test/ui/suggestions/dotdotdot-expr.stderr diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 5970f94b97d..955bdbdcf91 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -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 `+`. diff --git a/src/test/parse-fail/range_inclusive_dotdotdot.rs b/src/test/parse-fail/range_inclusive_dotdotdot.rs index fa6474717d3..34b96a59c2d 100644 --- a/src/test/parse-fail/range_inclusive_dotdotdot.rs +++ b/src/test/parse-fail/range_inclusive_dotdotdot.rs @@ -15,22 +15,21 @@ use std::ops::RangeToInclusive; fn return_range_to() -> RangeToInclusive { - 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 } - diff --git a/src/test/ui/suggestions/dotdotdot-expr.rs b/src/test/ui/suggestions/dotdotdot-expr.rs new file mode 100644 index 00000000000..afb73a526a8 --- /dev/null +++ b/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 or the MIT license +// , 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 +} diff --git a/src/test/ui/suggestions/dotdotdot-expr.stderr b/src/test/ui/suggestions/dotdotdot-expr.stderr new file mode 100644 index 00000000000..3315538f2f7 --- /dev/null +++ b/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 +