From 180d6b55ca19c63347664f0622b6ccc37fb101f5 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Wed, 23 Mar 2016 09:24:54 +1300 Subject: [PATCH] Tests --- src/libsyntax/parse/parser.rs | 7 +++- .../issue-10636-2.rs | 8 ++--- src/test/compile-fail/issue-31804.rs | 16 +++++++++ .../compile-fail/token-error-correct-2.rs | 17 ++++++++++ .../compile-fail/token-error-correct-3.rs | 33 +++++++++++++++++++ src/test/compile-fail/token-error-correct.rs | 20 +++++++++++ src/test/parse-fail/issue-2354-1.rs | 2 +- .../macro-mismatched-delim-paren-brace.rs | 2 +- 8 files changed, 97 insertions(+), 8 deletions(-) rename src/test/{parse-fail => compile-fail}/issue-10636-2.rs (75%) create mode 100644 src/test/compile-fail/issue-31804.rs create mode 100644 src/test/compile-fail/token-error-correct-2.rs create mode 100644 src/test/compile-fail/token-error-correct-3.rs create mode 100644 src/test/compile-fail/token-error-correct.rs diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 3010c040914df..29ef105eccb72 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2841,7 +2841,12 @@ impl<'a> Parser<'a> { maybe_whole!(deref self, NtTT); match self.token { token::CloseDelim(_) => { - panic!("should have been caught above"); + // An unexpected closing delimiter (i.e., there is no + // matching opening delimiter). + let token_str = self.this_token_to_string(); + let err = self.diagnostic().struct_span_err(self.span, + &format!("unexpected close delimiter: `{}`", token_str)); + Err(err) }, /* we ought to allow different depths of unquotation */ token::Dollar | token::SubstNt(..) if self.quote_depth > 0 => { diff --git a/src/test/parse-fail/issue-10636-2.rs b/src/test/compile-fail/issue-10636-2.rs similarity index 75% rename from src/test/parse-fail/issue-10636-2.rs rename to src/test/compile-fail/issue-10636-2.rs index 41a3b06e6556e..eaccaf3cdbd27 100644 --- a/src/test/parse-fail/issue-10636-2.rs +++ b/src/test/compile-fail/issue-10636-2.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// Copyright 2013-2016 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -11,11 +11,9 @@ // FIXME(31528) we emit a bunch of silly errors here due to continuing past the // first one. This would be easy-ish to address by better recovery in tokenisation. -// compile-flags: -Z parse-only - -pub fn trace_option(option: Option) { //~ HELP did you mean to close this delimiter? +pub fn trace_option(option: Option) { option.map(|some| 42; //~ NOTE: unclosed delimiter //~^ ERROR: expected one of + //~^^ ERROR: mismatched types } //~ ERROR: incorrect close delimiter //~^ ERROR: expected one of -//~ ERROR: this file contains an un-closed delimiter diff --git a/src/test/compile-fail/issue-31804.rs b/src/test/compile-fail/issue-31804.rs new file mode 100644 index 0000000000000..b6a04bee85d4f --- /dev/null +++ b/src/test/compile-fail/issue-31804.rs @@ -0,0 +1,16 @@ +// Copyright 2016 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. + +// Test that error recovery in the parser to an EOF does not give an infinite +// spew of errors. + +fn main() { + let +} //~ ERROR unexpected token: `}` diff --git a/src/test/compile-fail/token-error-correct-2.rs b/src/test/compile-fail/token-error-correct-2.rs new file mode 100644 index 0000000000000..ab429ab878073 --- /dev/null +++ b/src/test/compile-fail/token-error-correct-2.rs @@ -0,0 +1,17 @@ +// Copyright 2016 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. + +// Test that we do some basic error correcton in the tokeniser (and don't ICE). + +fn main() { + if foo { //~ NOTE: unclosed delimiter + //~^ ERROR: unresolved name `foo` + ) //~ ERROR: incorrect close delimiter: `)` +} diff --git a/src/test/compile-fail/token-error-correct-3.rs b/src/test/compile-fail/token-error-correct-3.rs new file mode 100644 index 0000000000000..fe8c9f690139f --- /dev/null +++ b/src/test/compile-fail/token-error-correct-3.rs @@ -0,0 +1,33 @@ +// Copyright 2016 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. + +// Test that we do some basic error correcton in the tokeniser (and don't spew +// too many bogus errors). + +pub mod raw { + use std::{io, fs}; + use std::path::Path; + + pub fn ensure_dir_exists, F: FnOnce(&Path)>(path: P, + callback: F) + -> io::Result { + if !is_directory(path.as_ref()) { //~ ERROR: unresolved name `is_directory` + callback(path.as_ref(); //~ NOTE: unclosed delimiter + //~^ ERROR: expected one of + fs::create_dir_all(path.as_ref()).map(|()| true) //~ ERROR: expected one of + } else { //~ ERROR: incorrect close delimiter: `}` + Ok(false); + } + + panic!(); + } +} + +fn main() {} diff --git a/src/test/compile-fail/token-error-correct.rs b/src/test/compile-fail/token-error-correct.rs new file mode 100644 index 0000000000000..6c54acd7bdbf6 --- /dev/null +++ b/src/test/compile-fail/token-error-correct.rs @@ -0,0 +1,20 @@ +// Copyright 2016 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. + +// Test that we do some basic error correcton in the tokeniser. + +fn main() { + foo(bar(; //~ NOTE: unclosed delimiter + //~^ NOTE: unclosed delimiter + //~^^ ERROR: unexpected token: `;` + //~^^^ ERROR: unresolved name `bar` + //~^^^^ ERROR: unresolved name `foo` +} //~ ERROR: incorrect close delimiter: `}` +//~^ ERROR: incorrect close delimiter: `}` diff --git a/src/test/parse-fail/issue-2354-1.rs b/src/test/parse-fail/issue-2354-1.rs index e65f95780bb96..f24c544073578 100644 --- a/src/test/parse-fail/issue-2354-1.rs +++ b/src/test/parse-fail/issue-2354-1.rs @@ -10,4 +10,4 @@ // compile-flags: -Z parse-only -static foo: isize = 2; } //~ ERROR incorrect close delimiter: +static foo: isize = 2; } //~ ERROR unexpected close delimiter: diff --git a/src/test/parse-fail/macro-mismatched-delim-paren-brace.rs b/src/test/parse-fail/macro-mismatched-delim-paren-brace.rs index 84094ab6ca89c..cbc0ed0ccdb84 100644 --- a/src/test/parse-fail/macro-mismatched-delim-paren-brace.rs +++ b/src/test/parse-fail/macro-mismatched-delim-paren-brace.rs @@ -14,4 +14,4 @@ fn main() { foo! ( bar, "baz", 1, 2.0 } //~ ERROR incorrect close delimiter -} +} //~ ERROR unexpected close delimiter: `}`