Use span stacks to track macro expansion for less troublesome error messages.

This commit is contained in:
Paul Stansifer 2011-08-15 13:33:12 -07:00
parent c48036c0b7
commit ebb16e6a25
8 changed files with 63 additions and 37 deletions

View file

@ -35,19 +35,29 @@ fn syntax_expander_table() -> hashmap[str, syntax_extension] {
}
obj ext_ctxt(sess: @session, crate_file_name_hack: str,
mutable backtrace: span[]) {
mutable backtrace: codemap::opt_span) {
fn crate_file_name() -> str { ret crate_file_name_hack; }
fn session() -> @session { ret sess; }
fn print_backtrace() {
for sp: span in backtrace {
sess.span_note(sp, "(while expanding this)")
}
}
fn bt_push(sp: span) { backtrace += ~[sp]; }
fn bt_pop() { ivec::pop(backtrace); }
fn backtrace() -> codemap::opt_span { ret backtrace; }
fn bt_push(sp: span) {
backtrace = codemap::os_some(@{lo: sp.lo, hi: sp.hi,
expanded_from: backtrace});
}
fn bt_pop() {
alt backtrace {
codemap::os_some(@{expanded_from: pre, _}) {
let tmp = pre;
backtrace = tmp;
}
_ { self.bug("tried to pop without a push"); }
}
}
fn span_fatal(sp: span, msg: str) -> ! {
self.print_backtrace();
@ -85,7 +95,7 @@ fn mk_ctxt(sess: &session) -> ext_ctxt {
// super-ugly and needs a better solution.
let crate_file_name_hack = sess.get_codemap().files.(0).name;
ret ext_ctxt(@sess, crate_file_name_hack, ~[]);
ret ext_ctxt(@sess, crate_file_name_hack, codemap::os_none);
}
fn expr_to_str(cx: &ext_ctxt, expr: @ast::expr, error: str) -> str {

View file

@ -205,6 +205,10 @@ fn use_selectors_to_bind(b: &binders, e: @expr) -> option::t[bindings] {
fn transcribe(cx: &ext_ctxt, b: &bindings, body: @expr) -> @expr {
let idx_path: @mutable [uint] = @mutable ~[];
fn new_id(old: node_id, cx: &ext_ctxt) -> node_id { ret cx.next_id(); }
fn new_span(cx: &ext_ctxt, sp: &span) -> span {
/* this discards information in the case of macro-defining macros */
ret {lo: sp.lo, hi: sp.hi, expanded_from: cx.backtrace()};
}
let afp = default_ast_fold();
let f_pre =
{fold_ident: bind transcribe_ident(cx, b, idx_path, _, _),
@ -215,7 +219,8 @@ fn transcribe(cx: &ext_ctxt, b: &bindings, body: @expr) -> @expr {
fold_block:
bind transcribe_block(cx, b, idx_path, _, _, afp.fold_block),
map_exprs: bind transcribe_exprs(cx, b, idx_path, _, _),
new_id: bind new_id(_, cx) with *afp};
new_id: bind new_id(_, cx),
new_span: bind new_span(cx, _) with *afp};
let f = make_fold(f_pre);
let result = f.fold_expr(body);
dummy_out(f); //temporary: kill circular reference