Skip to content

Commit 6d39be4

Browse files
committed
Add backtraces for syntax problems.
1 parent 5428d4e commit 6d39be4

File tree

4 files changed

+61
-65
lines changed

4 files changed

+61
-65
lines changed

src/comp/syntax/ext/base.rs

+33-37
Original file line numberDiff line numberDiff line change
@@ -32,41 +32,46 @@ fn syntax_expander_table() -> hashmap[str, syntax_extension] {
3232
ret syntax_expanders;
3333
}
3434

35-
type span_msg_fn = fn(span, str) -> ! ;
36-
type msg_fn = fn(str) -> ! ;
35+
obj ext_ctxt(sess: @session, crate_file_name_hack: str,
36+
mutable backtrace: span[]) {
37+
fn crate_file_name() -> str { ret crate_file_name_hack; }
3738

38-
type next_id_fn = fn() -> ast::node_id ;
39-
40-
41-
// Provides a limited set of services necessary for syntax extensions
42-
// to do their thing
43-
type ext_ctxt =
44-
{crate_file_name_hack: str,
45-
span_fatal: span_msg_fn,
46-
span_unimpl: span_msg_fn,
47-
span_bug: span_msg_fn,
48-
bug: msg_fn,
49-
next_id: next_id_fn};
39+
fn print_backtrace() {
40+
for sp: span in backtrace {
41+
sess.span_note(sp, "(while expanding this)")
42+
}
43+
}
44+
45+
fn bt_push(sp: span) { backtrace += ~[sp]; }
46+
fn bt_pop() { ivec::pop(backtrace); }
5047

51-
fn mk_ctxt(sess: &session) -> ext_ctxt {
52-
fn ext_span_fatal_(sess: &session, sp: span, msg: str) -> ! {
48+
fn span_fatal(sp: span, msg: str) -> ! {
49+
self.print_backtrace();
50+
sess.span_fatal(sp, msg);
51+
}
52+
fn span_err(sp: span, msg: str) {
53+
self.print_backtrace();
5354
sess.span_err(sp, msg);
54-
fail;
5555
}
56-
let ext_span_fatal = bind ext_span_fatal_(sess, _, _);
57-
fn ext_span_unimpl_(sess: &session, sp: span, msg: str) -> ! {
58-
sess.span_err(sp, "unimplemented " + msg);
59-
fail;
56+
fn span_unimpl(sp:span, msg: str) -> ! {
57+
self.print_backtrace();
58+
sess.span_unimpl(sp, msg);
6059
}
61-
let ext_span_bug = bind ext_span_bug_(sess, _, _);
62-
fn ext_span_bug_(sess: &session, sp: span, msg: str) -> ! {
63-
sess.span_bug(sp, msg);
60+
fn span_bug(sp:span, msg: str) -> ! {
61+
self.print_backtrace();
62+
sess.span_bug(sp, msg);
63+
}
64+
fn bug(msg: str) -> ! {
65+
self.print_backtrace();
66+
sess.bug(msg);
67+
}
68+
fn next_id() -> ast::node_id {
69+
ret sess.next_node_id();
6470
}
65-
let ext_span_unimpl = bind ext_span_unimpl_(sess, _, _);
66-
fn ext_bug_(sess: &session, msg: str) -> ! { sess.bug(msg); }
67-
let ext_bug = bind ext_bug_(sess, _);
6871

72+
}
6973

74+
fn mk_ctxt(sess: &session) -> ext_ctxt {
7075
// FIXME: Some extensions work by building ASTs with paths to functions
7176
// they need to call at runtime. As those functions live in the std crate,
7277
// the paths are prefixed with "std::". Unfortunately, these paths can't
@@ -76,16 +81,7 @@ fn mk_ctxt(sess: &session) -> ext_ctxt {
7681
// super-ugly and needs a better solution.
7782
let crate_file_name_hack = sess.get_codemap().files.(0).name;
7883

79-
fn ext_next_id_(sess: &session) -> ast::node_id {
80-
ret sess.next_node_id(); // temporary, until bind works better
81-
}
82-
let ext_next_id = bind ext_next_id_(sess);
83-
ret {crate_file_name_hack: crate_file_name_hack,
84-
span_fatal: ext_span_fatal,
85-
span_unimpl: ext_span_unimpl,
86-
span_bug: ext_span_bug,
87-
bug: ext_bug,
88-
next_id: ext_next_id};
84+
ret ext_ctxt(@sess, crate_file_name_hack, ~[]);
8985
}
9086

9187
fn expr_to_str(cx: &ext_ctxt, expr: @ast::expr, error: str) -> str {

src/comp/syntax/ext/expand.rs

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,63 @@
1-
2-
import codemap::emit_error;
31
import driver::session;
4-
import syntax::ast::crate;
5-
import syntax::ast::expr_;
6-
import syntax::ast::expr_mac;
7-
import syntax::ast::mac_invoc;
8-
import syntax::fold::*;
92

103
import std::option::none;
114
import std::option::some;
125

136
import std::map::hashmap;
147
import std::ivec;
158

16-
fn expand_expr(exts: &hashmap[str, base::syntax_extension],
17-
sess: &session::session, e: &expr_, fld: ast_fold,
18-
orig: &fn(&ast::expr_, ast_fold) -> expr_ ) -> expr_ {
9+
import syntax::ast::crate;
10+
import syntax::ast::expr_;
11+
import syntax::ast::expr_mac;
12+
import syntax::ast::mac_invoc;
13+
import syntax::fold::*;
14+
import syntax::ext::base::*;
15+
16+
17+
fn expand_expr(exts: &hashmap[str, syntax_extension], cx: &ext_ctxt,
18+
e: &expr_, fld: ast_fold,
19+
orig: &fn(&expr_, ast_fold) -> expr_ ) -> expr_ {
1920
ret alt e {
2021
expr_mac(mac) {
2122
alt mac.node {
2223
mac_invoc(pth, args, body) {
2324
assert (ivec::len(pth.node.idents) > 0u);
2425
let extname = pth.node.idents.(0);
25-
let ext_cx = base::mk_ctxt(sess);
2626
alt exts.find(extname) {
2727
none. {
28-
emit_error(some(pth.span),
29-
"unknown syntax expander: '" + extname + "'",
30-
sess.get_codemap());
31-
fail
28+
cx.span_fatal(pth.span,
29+
#fmt["macro undefined: '%s'", extname])
3230
}
33-
some(base::normal(ext)) {
31+
some(normal(ext)) {
32+
let expanded = ext(cx, pth.span, args, body);
3433

34+
cx.bt_push(mac.span);
3535
//keep going, outside-in
36-
fld.fold_expr(ext(ext_cx, pth.span, args, body)).node
36+
let fully_expanded = fld.fold_expr(expanded).node;
37+
cx.bt_pop();
38+
39+
fully_expanded
3740
}
38-
some(base::macro_defining(ext)) {
39-
let named_extension = ext(ext_cx, pth.span, args, body);
41+
some(macro_defining(ext)) {
42+
let named_extension = ext(cx, pth.span, args, body);
4043
exts.insert(named_extension.ident, named_extension.ext);
4144
ast::expr_rec(~[], none)
4245
}
4346
}
4447
}
45-
_ {
46-
emit_error(some(mac.span), "naked syntactic bit",
47-
sess.get_codemap());
48-
fail
49-
}
48+
_ { cx.span_bug(mac.span, "naked syntactic bit") }
5049
}
5150
}
5251
_ { orig(e, fld) }
5352
};
5453
}
5554

5655
fn expand_crate(sess: &session::session, c: &@crate) -> @crate {
57-
let exts = ext::base::syntax_expander_table();
56+
let exts = syntax_expander_table();
5857
let afp = default_ast_fold();
58+
let cx: ext_ctxt = mk_ctxt(sess);
5959
let f_pre =
60-
{fold_expr: bind expand_expr(exts, sess, _, _, afp.fold_expr)
60+
{fold_expr: bind expand_expr(exts, cx, _, _, afp.fold_expr)
6161
with *afp};
6262
let f = make_fold(f_pre);
6363
let res = @f.fold_crate(*c);

src/comp/syntax/ext/fmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn pieces_to_expr(cx: &ext_ctxt, sp: span, pieces: &[piece],
9898
}
9999
fn make_path_vec(cx: &ext_ctxt, ident: str) -> [str] {
100100
fn compiling_std(cx: &ext_ctxt) -> bool {
101-
ret str::find(cx.crate_file_name_hack, "std.rc") >= 0;
101+
ret str::find(cx.crate_file_name(), "std.rc") >= 0;
102102
}
103103
if compiling_std(cx) {
104104
ret ~["extfmt", "rt", ident];
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
// error-pattern:unknown syntax expander
1+
// error-pattern:macro undefined
22
fn main() { #iamnotanextensionthatexists(""); }

0 commit comments

Comments
 (0)