Skip to content

syntax: Display spans for open delimiters when a file ends prematurely #9756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ~[]
}
}

Expand Down Expand Up @@ -336,6 +337,8 @@ pub struct Parser {
obsolete_set: @mut HashSet<ObsoleteSyntax>,
/// 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]
Expand Down Expand Up @@ -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 =
Expand All @@ -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)
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/compile-fail/issue-2354-1.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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:

10 changes: 2 additions & 8 deletions src/test/compile-fail/issue-2354.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(); }
Expand All @@ -25,4 +19,4 @@ fn bar() {
while (i < 1000) {}
}

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