Skip to content

Commit 8db52a5

Browse files
committed
auto merge of #9756 : catamorphism/rust/issue-2354, r=alexcrichton
r? anybody 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
2 parents e293026 + 77d9ac3 commit 8db52a5

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ pub fn Parser(sess: @mut ParseSess,
309309
quote_depth: @mut 0,
310310
obsolete_set: @mut HashSet::new(),
311311
mod_path_stack: @mut ~[],
312+
open_braces: @mut ~[]
312313
}
313314
}
314315

@@ -337,6 +338,8 @@ pub struct Parser {
337338
obsolete_set: @mut HashSet<ObsoleteSyntax>,
338339
/// Used to determine the path to externally loaded source files
339340
mod_path_stack: @mut ~[@str],
341+
/// Stack of spans of open delimiters. Used for error message.
342+
open_braces: @mut ~[Span]
340343
}
341344

342345
#[unsafe_destructor]
@@ -2024,12 +2027,18 @@ impl Parser {
20242027

20252028
match *self.token {
20262029
token::EOF => {
2027-
self.fatal("file ended with unbalanced delimiters");
2030+
for sp in self.open_braces.iter() {
2031+
self.span_note(*sp, "Did you mean to close this delimiter?");
2032+
}
2033+
// There shouldn't really be a span, but it's easier for the test runner
2034+
// if we give it one
2035+
self.fatal("This file contains an un-closed delimiter ");
20282036
}
20292037
token::LPAREN | token::LBRACE | token::LBRACKET => {
20302038
let close_delim = token::flip_delimiter(&*self.token);
20312039

20322040
// Parse the open delimiter.
2041+
(*self.open_braces).push(*self.span);
20332042
let mut result = ~[parse_any_tt_tok(self)];
20342043

20352044
let trees =
@@ -2040,6 +2049,7 @@ impl Parser {
20402049

20412050
// Parse the close delimiter.
20422051
result.push(parse_any_tt_tok(self));
2052+
self.open_braces.pop();
20432053

20442054
tt_delim(@mut result)
20452055
}

src/test/compile-fail/issue-2354-1.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2013 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+
static foo: int = 2; } //~ ERROR incorrect close delimiter:
12+

src/test/compile-fail/issue-2354.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// xfail-test
12-
/*
13-
Ideally, the error about the missing close brace in foo would be reported
14-
near the corresponding open brace. But currently it's reported at the end.
15-
xfailed for now (see Issue #2354)
16-
*/
17-
fn foo() { //~ ERROR this open brace is not closed
11+
fn foo() { //~ NOTE Did you mean to close this delimiter?
1812
match Some(x) {
1913
Some(y) { fail!(); }
2014
None { fail!(); }
@@ -25,4 +19,4 @@ fn bar() {
2519
while (i < 1000) {}
2620
}
2721

28-
fn main() {}
22+
fn main() {} //~ ERROR This file contains an un-closed delimiter

0 commit comments

Comments
 (0)