From bed669cba6c20e3bdbe01e87ce4566b60d7bc232 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Mon, 7 Oct 2013 15:43:42 -0400 Subject: [PATCH 1/2] syntax: Display spans for open delimiters when a file ends prematurely It's more helpful to list the span of each open delimiter seen so far than to print out an error with the span of the last position in the file. Closes #2354 --- src/libsyntax/parse/parser.rs | 12 +++++++++++- src/test/compile-fail/issue-2354-1.rs | 12 ++++++++++++ src/test/compile-fail/issue-2354.rs | 10 ++-------- 3 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 src/test/compile-fail/issue-2354-1.rs diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index cad19543608..e9f55c62b6c 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -308,6 +308,7 @@ pub fn Parser(sess: @mut ParseSess, quote_depth: @mut 0, obsolete_set: @mut HashSet::new(), mod_path_stack: @mut ~[], + open_braces: @mut ~[] } } @@ -336,6 +337,8 @@ pub struct Parser { obsolete_set: @mut HashSet, /// Used to determine the path to externally loaded source files mod_path_stack: @mut ~[@str], + /// Stack of spans of open delimiters. Used for error message. + open_braces: @mut ~[@Span] } #[unsafe_destructor] @@ -2022,12 +2025,18 @@ impl Parser { match *self.token { token::EOF => { - self.fatal("file ended with unbalanced delimiters"); + for sp in self.open_braces.iter() { + self.span_note(**sp, "Did you mean to close this delimiter?"); + } + // There shouldn't really be a span, but it's easier for the test runner + // if we give it one + self.fatal("This file contains an un-closed delimiter "); } token::LPAREN | token::LBRACE | token::LBRACKET => { let close_delim = token::flip_delimiter(&*self.token); // Parse the open delimiter. + (*self.open_braces).push(@*self.span); let mut result = ~[parse_any_tt_tok(self)]; let trees = @@ -2038,6 +2047,7 @@ impl Parser { // Parse the close delimiter. result.push(parse_any_tt_tok(self)); + self.open_braces.pop(); tt_delim(@mut result) } diff --git a/src/test/compile-fail/issue-2354-1.rs b/src/test/compile-fail/issue-2354-1.rs new file mode 100644 index 00000000000..67b5c6becc7 --- /dev/null +++ b/src/test/compile-fail/issue-2354-1.rs @@ -0,0 +1,12 @@ +// Copyright 2013 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. + +static foo: int = 2; } //~ ERROR incorrect close delimiter: + diff --git a/src/test/compile-fail/issue-2354.rs b/src/test/compile-fail/issue-2354.rs index 409e1c1f040..9761bd3317c 100644 --- a/src/test/compile-fail/issue-2354.rs +++ b/src/test/compile-fail/issue-2354.rs @@ -8,13 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// xfail-test -/* - Ideally, the error about the missing close brace in foo would be reported - near the corresponding open brace. But currently it's reported at the end. - xfailed for now (see Issue #2354) - */ -fn foo() { //~ ERROR this open brace is not closed +fn foo() { //~ NOTE Did you mean to close this delimiter? match Some(x) { Some(y) { fail!(); } None { fail!(); } @@ -25,4 +19,4 @@ fn bar() { while (i < 1000) {} } -fn main() {} +fn main() {} //~ ERROR This file contains an un-closed delimiter From 77d9ac37fc866743b91c0da2951d42c459bb74c5 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Mon, 7 Oct 2013 18:37:36 -0700 Subject: [PATCH 2/2] syntax: Remove unnecessary @ --- src/libsyntax/parse/parser.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index e9f55c62b6c..4d3c762338d 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -338,7 +338,7 @@ pub struct Parser { /// Used to determine the path to externally loaded source files mod_path_stack: @mut ~[@str], /// Stack of spans of open delimiters. Used for error message. - open_braces: @mut ~[@Span] + open_braces: @mut ~[Span] } #[unsafe_destructor] @@ -2026,7 +2026,7 @@ impl Parser { match *self.token { token::EOF => { for sp in self.open_braces.iter() { - self.span_note(**sp, "Did you mean to close this delimiter?"); + self.span_note(*sp, "Did you mean to close this delimiter?"); } // There shouldn't really be a span, but it's easier for the test runner // if we give it one @@ -2036,7 +2036,7 @@ impl Parser { let close_delim = token::flip_delimiter(&*self.token); // Parse the open delimiter. - (*self.open_braces).push(@*self.span); + (*self.open_braces).push(*self.span); let mut result = ~[parse_any_tt_tok(self)]; let trees =