Skip to content

Commit c115c51

Browse files
committed
improve span of erroneous empty macro invocation
The ideas is to use the span of the complete macro invocation if the span of a macro error is `DUMMY_SP`. fixes #7970
1 parent 11deb08 commit c115c51

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

src/libsyntax/ext/tt/macro_parser.rs

-16
Original file line numberDiff line numberDiff line change
@@ -249,22 +249,6 @@ pub enum ParseResult<T> {
249249
pub type NamedParseResult = ParseResult<HashMap<Ident, Rc<NamedMatch>>>;
250250
pub type PositionalParseResult = ParseResult<Vec<Rc<NamedMatch>>>;
251251

252-
pub fn parse_or_else(sess: &ParseSess,
253-
cfg: ast::CrateConfig,
254-
rdr: TtReader,
255-
ms: Vec<TokenTree> )
256-
-> HashMap<Ident, Rc<NamedMatch>> {
257-
match parse(sess, cfg, rdr, &ms[..]) {
258-
Success(m) => m,
259-
Failure(sp, str) => {
260-
panic!(sess.span_diagnostic.span_fatal(sp, &str[..]))
261-
}
262-
Error(sp, str) => {
263-
panic!(sess.span_diagnostic.span_fatal(sp, &str[..]))
264-
}
265-
}
266-
}
267-
268252
/// Perform a token equality check, ignoring syntax context (that is, an
269253
/// unhygienic comparison)
270254
pub fn token_name_eq(t1 : &Token, t2 : &Token) -> bool {

src/libsyntax/ext/tt/macro_rules.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ext::base::{ExtCtxt, MacResult, SyntaxExtension};
1414
use ext::base::{NormalTT, TTMacroExpander};
1515
use ext::tt::macro_parser::{Success, Error, Failure};
1616
use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal};
17-
use ext::tt::macro_parser::{parse, parse_or_else};
17+
use ext::tt::macro_parser::parse;
1818
use parse::lexer::new_tt_reader;
1919
use parse::parser::Parser;
2020
use parse::token::{self, special_idents, gensym_ident, NtTT, Token};
@@ -211,12 +211,23 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
211211
best_fail_spot = sp;
212212
best_fail_msg = (*msg).clone();
213213
},
214-
Error(sp, ref msg) => panic!(cx.span_fatal(sp, &msg[..]))
214+
Error(mut spp, ref msg) => {
215+
if spp == DUMMY_SP {
216+
spp = sp;
217+
}
218+
219+
panic!(cx.span_fatal(spp, &msg[..]))
220+
}
215221
}
216222
}
217223
_ => cx.bug("non-matcher found in parsed lhses")
218224
}
219225
}
226+
227+
if best_fail_spot == DUMMY_SP {
228+
best_fail_spot = sp;
229+
}
230+
220231
panic!(cx.span_fatal(best_fail_spot, &best_fail_msg[..]));
221232
}
222233

@@ -266,10 +277,20 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt,
266277
None,
267278
None,
268279
def.body.clone());
269-
let argument_map = parse_or_else(cx.parse_sess(),
270-
cx.cfg(),
271-
arg_reader,
272-
argument_gram);
280+
281+
let argument_map = match parse(cx.parse_sess(),
282+
cx.cfg(),
283+
arg_reader,
284+
&argument_gram) {
285+
Success(m) => m,
286+
Failure(mut sp, str) | Error(mut sp, str) => {
287+
if sp == DUMMY_SP {
288+
sp = def.span;
289+
}
290+
291+
panic!(cx.parse_sess().span_diagnostic.span_fatal(sp, &str[..]));
292+
}
293+
};
273294

274295
// Extract the arguments:
275296
let lhses = match **argument_map.get(&lhs_nm).unwrap() {

src/test/compile-fail/issue-7970a.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2015 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+
fn main() {
12+
println!();
13+
//~^ ERROR unexpected end of macro invocation
14+
}

src/test/compile-fail/issue-7970b.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2015 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+
fn main() {}
12+
13+
macro_rules! test {}
14+
//~^ ERROR unexpected end of macro invocation

0 commit comments

Comments
 (0)