Skip to content

Commit 1a20f8e

Browse files
committed
rustc: Be more careful about spans in 'unexpected token' errors
Closes #2017
1 parent 7a34ac5 commit 1a20f8e

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/rustc/syntax/parse/parser.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,13 @@ fn bad_expr_word_table() -> hashmap<str, ()> {
156156
words
157157
}
158158

159-
fn unexpected(p: parser, t: token::token) -> ! {
160-
let s: str = "unexpected token: '" + token::to_str(p.reader, t) +
161-
"'";
162-
p.fatal(s);
159+
fn unexpected_last(p: parser, t: token::token) -> ! {
160+
p.span_fatal(p.last_span,
161+
"unexpected token: '" + token::to_str(p.reader, t) + "'");
162+
}
163+
164+
fn unexpected(p: parser) -> ! {
165+
p.fatal("unexpected token: '" + token::to_str(p.reader, p.token) + "'");
163166
}
164167

165168
fn expect(p: parser, t: token::token) {
@@ -483,7 +486,7 @@ fn parse_ty(p: parser, colons_before_params: bool) -> @ast::ty {
483486
let elems =
484487
parse_seq(token::LBRACE, token::RBRACE, seq_sep_opt(token::COMMA),
485488
parse_ty_field, p);
486-
if vec::len(elems.node) == 0u { unexpected(p, token::RBRACE); }
489+
if vec::len(elems.node) == 0u { unexpected_last(p, token::RBRACE); }
487490
let hi = elems.span.hi;
488491

489492
let t = ast::ty_rec(elems.node);
@@ -669,7 +672,7 @@ fn lit_from_token(p: parser, tok: token::token) -> ast::lit_ {
669672
token::LIT_FLOAT(s, ft) { ast::lit_float(p.get_str(s), ft) }
670673
token::LIT_STR(s) { ast::lit_str(p.get_str(s)) }
671674
token::LPAREN { expect(p, token::RPAREN); ast::lit_nil }
672-
_ { unexpected(p, tok); }
675+
_ { unexpected_last(p, tok); }
673676
}
674677
}
675678

@@ -1065,7 +1068,7 @@ fn parse_dot_or_call_expr_with(p: parser, e0: pexpr) -> pexpr {
10651068
p.get_str(i),
10661069
tys));
10671070
}
1068-
t { unexpected(p, t); }
1071+
_ { unexpected(p); }
10691072
}
10701073
cont;
10711074
}
@@ -2200,7 +2203,7 @@ fn parse_fn_purity(p: parser) -> ast::purity {
22002203
if eat_word(p, "fn") { ast::impure_fn }
22012204
else if eat_word(p, "pure") { expect_word(p, "fn"); ast::pure_fn }
22022205
else if eat_word(p, "unsafe") { expect_word(p, "fn"); ast::unsafe_fn }
2203-
else { unexpected(p, p.token); }
2206+
else { unexpected(p); }
22042207
}
22052208

22062209
fn parse_native_item(p: parser, attrs: [ast::attribute]) ->
@@ -2738,7 +2741,7 @@ fn parse_crate_directive(p: parser, first_outer_attr: [ast::attribute]) ->
27382741
ret spanned(lo, hi,
27392742
ast::cdir_dir_mod(id, cdirs, mod_attrs));
27402743
}
2741-
t { unexpected(p, t); }
2744+
_ { unexpected(p); }
27422745
}
27432746
} else if is_view_item(p) {
27442747
let vi = parse_view_item(p);

0 commit comments

Comments
 (0)