Skip to content

Commit 5b1bd32

Browse files
committed
Auto merge of #33749 - jseyfried:fix_call_site_span, r=nrc
Fix macro call site spans Fix macro call site spans. r? @nrc
2 parents 97e3a24 + e77a219 commit 5b1bd32

File tree

7 files changed

+31
-34
lines changed

7 files changed

+31
-34
lines changed

src/libsyntax/ext/base.rs

-16
Original file line numberDiff line numberDiff line change
@@ -602,22 +602,6 @@ impl<'a> ExtCtxt<'a> {
602602
}
603603
pub fn backtrace(&self) -> ExpnId { self.backtrace }
604604

605-
/// Original span that caused the current exapnsion to happen.
606-
pub fn original_span(&self) -> Span {
607-
let mut expn_id = self.backtrace;
608-
let mut call_site = None;
609-
loop {
610-
match self.codemap().with_expn_info(expn_id, |ei| ei.map(|ei| ei.call_site)) {
611-
None => break,
612-
Some(cs) => {
613-
call_site = Some(cs);
614-
expn_id = cs.expn_id;
615-
}
616-
}
617-
}
618-
call_site.expect("missing expansion backtrace")
619-
}
620-
621605
/// Returns span for the macro which originally caused the current expansion to happen.
622606
///
623607
/// Stops backtracing at include! boundary.

src/libsyntax/ext/expand.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ fn expand_mac_invoc<T>(mac: ast::Mac, ident: Option<Ident>, attrs: Vec<ast::Attr
215215
return None;
216216
};
217217

218+
let call_site = fld.new_span(call_site);
218219
let ident = ident.unwrap_or(keywords::Invalid.ident());
219220
match *extension {
220221
NormalTT(ref expandfun, exp_span, allow_internal_unstable) => {
@@ -234,14 +235,8 @@ fn expand_mac_invoc<T>(mac: ast::Mac, ident: Option<Ident>, attrs: Vec<ast::Attr
234235
},
235236
});
236237

237-
// The span that we pass to the expanders we want to
238-
// be the root of the call stack. That's the most
239-
// relevant span and it's the actual invocation of
240-
// the macro.
241-
let mac_span = fld.cx.original_span();
242-
243238
let marked_tts = mark_tts(&tts[..], mark);
244-
Some(expandfun.expand(fld.cx, mac_span, &marked_tts))
239+
Some(expandfun.expand(fld.cx, call_site, &marked_tts))
245240
}
246241

247242
IdentTT(ref expander, tt_span, allow_internal_unstable) => {
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2016 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+
// error-pattern: requires at least a format string argument
12+
// error-pattern: bad-format-args.rs:19:5: 19:15 note: in this expansion
13+
14+
// error-pattern: expected token: `,`
15+
// error-pattern: bad-format-args.rs:20:5: 20:19 note: in this expansion
16+
// error-pattern: bad-format-args.rs:21:5: 21:22 note: in this expansion
17+
18+
fn main() {
19+
format!();
20+
format!("" 1);
21+
format!("", 1 1);
22+
}

src/test/compile-fail/ifmt-bad-arg.rs

-4
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,4 @@ fn main() {
5050

5151
format!("foo } bar"); //~ ERROR: unmatched `}` found
5252
format!("foo }"); //~ ERROR: unmatched `}` found
53-
54-
format!(); //~ ERROR: requires at least a format string argument
55-
format!("" 1); //~ ERROR: expected token: `,`
56-
format!("", 1 1); //~ ERROR: expected token: `,`
5753
}

src/test/compile-fail/macro-backtrace-println.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ macro_rules! myprint {
2121
}
2222

2323
macro_rules! myprintln {
24-
($fmt:expr) => (myprint!(concat!($fmt, "\n"))); //~ NOTE in this expansion of myprint!
25-
//~^ NOTE in this expansion of concat!
24+
($fmt:expr) => (myprint!(concat!($fmt, "\n"))); //~ ERROR invalid reference to argument `0`
25+
//~| NOTE in this expansion of concat!
26+
//~| NOTE in this expansion of myprint!
2627
}
2728

2829
fn main() {
29-
myprintln!("{}"); //~ ERROR invalid reference to argument `0`
30-
//~^ NOTE in this expansion of
30+
myprintln!("{}"); //~ NOTE in this expansion of
3131
}

src/test/compile-fail/macros-nonfatal-errors.rs

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ fn main() {
3232

3333
foo::blah!(); //~ ERROR
3434

35-
format!(); //~ ERROR
3635
format!(invalid); //~ ERROR
3736

3837
include!(invalid); //~ ERROR

src/test/compile-fail/trace_macros-gate.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ fn main() {
2323
// of the below being caught.
2424

2525
macro_rules! expando {
26-
($x: ident) => { trace_macros!($x) }
26+
($x: ident) => { trace_macros!($x) } //~ ERROR `trace_macros` is not stable
2727
}
2828

29-
expando!(true); //~ ERROR `trace_macros` is not stable
29+
expando!(true); //~ NOTE in this expansion
30+
//~^ NOTE in this expansion
3031
}

0 commit comments

Comments
 (0)