about summary refs log tree commit diff
path: root/src/comp/syntax
diff options
context:
space:
mode:
authorPaul Stansifer <paul.stansifer@gmail.com>2011-08-15 13:33:12 -0700
committerPaul Stansifer <paul.stansifer@gmail.com>2011-08-15 15:35:28 -0700
commitebb16e6a25da9694dabe19ecea24baeb7007ba1f (patch)
tree180e6854766f09a97f0ef887eada5bad88403327 /src/comp/syntax
parentc48036c0b7d10606dc8c63b87351fb394cf7d4a6 (diff)
downloadrust-ebb16e6a25da9694dabe19ecea24baeb7007ba1f.tar.gz
rust-ebb16e6a25da9694dabe19ecea24baeb7007ba1f.zip
Use span stacks to track macro expansion for less troublesome error messages.
Diffstat (limited to 'src/comp/syntax')
-rw-r--r--src/comp/syntax/ext/base.rs24
-rw-r--r--src/comp/syntax/ext/simplext.rs7
-rw-r--r--src/comp/syntax/fold.rs50
-rw-r--r--src/comp/syntax/parse/parser.rs3
4 files changed, 55 insertions, 29 deletions
diff --git a/src/comp/syntax/ext/base.rs b/src/comp/syntax/ext/base.rs
index 62c480ad96b..9ad3d8d1b2e 100644
--- a/src/comp/syntax/ext/base.rs
+++ b/src/comp/syntax/ext/base.rs
@@ -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 {
diff --git a/src/comp/syntax/ext/simplext.rs b/src/comp/syntax/ext/simplext.rs
index 2a0e7859b10..b57812cb1d2 100644
--- a/src/comp/syntax/ext/simplext.rs
+++ b/src/comp/syntax/ext/simplext.rs
@@ -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
diff --git a/src/comp/syntax/fold.rs b/src/comp/syntax/fold.rs
index 180a518105f..7e5bbf41493 100644
--- a/src/comp/syntax/fold.rs
+++ b/src/comp/syntax/fold.rs
@@ -44,7 +44,8 @@ type ast_fold_precursor =
      fold_path: fn(&path_, ast_fold) -> path_ ,
      fold_local: fn(&local_, ast_fold) -> local_ ,
      map_exprs: fn(fn(&@expr) -> @expr , [@expr]) -> [@expr],
-     new_id: fn(node_id) -> node_id};
+     new_id: fn(node_id) -> node_id,
+     new_span: fn(&span) -> span};
 
 type a_f =
     {fold_crate: fn(&crate) -> crate ,
@@ -70,7 +71,8 @@ type a_f =
      fold_path: fn(&path) -> path ,
      fold_local: fn(&@local) -> @local ,
      map_exprs: fn(fn(&@expr) -> @expr , [@expr]) -> [@expr],
-     new_id: fn(node_id) -> node_id};
+     new_id: fn(node_id) -> node_id,
+     new_span: fn(&span) -> span};
 
 
 //fn nf_dummy[T](&T node) -> T { fail; }
@@ -514,6 +516,8 @@ fn noop_map_exprs(f: fn(&@expr) -> @expr , es: [@expr]) -> [@expr] {
 
 fn noop_id(i: node_id) -> node_id { ret i; }
 
+fn noop_span(sp: &span) -> span { ret sp; }
+
 
 fn default_ast_fold() -> @ast_fold_precursor {
     ret @{fold_crate: noop_fold_crate,
@@ -539,7 +543,8 @@ fn default_ast_fold() -> @ast_fold_precursor {
           fold_path: noop_fold_path,
           fold_local: noop_fold_local,
           map_exprs: noop_map_exprs,
-          new_id: noop_id};
+          new_id: noop_id,
+          new_span: noop_span};
 }
 
 fn dummy_out(a: ast_fold) {
@@ -567,7 +572,8 @@ fn dummy_out(a: ast_fold) {
          fold_path: nf_path_dummy,
          fold_local: nf_local_dummy,
          map_exprs: noop_map_exprs,
-         new_id: noop_id};
+         new_id: noop_id,
+         new_span: noop_span};
 }
 
 
@@ -596,19 +602,22 @@ fn make_fold(afp: &ast_fold_precursor) -> ast_fold {
                   fold_path: nf_path_dummy,
                   fold_local: nf_local_dummy,
                   map_exprs: noop_map_exprs,
-                  new_id: noop_id};
+                  new_id: noop_id,
+                  new_span: noop_span};
 
     /* naturally, a macro to write these would be nice */
     fn f_crate(afp: &ast_fold_precursor, f: ast_fold, c: &crate) -> crate {
-        ret {node: afp.fold_crate(c.node, f), span: c.span};
+        ret {node: afp.fold_crate(c.node, f), span: afp.new_span(c.span)};
     }
     fn f_crate_directive(afp: &ast_fold_precursor, f: ast_fold,
                          c: &@crate_directive) -> @crate_directive {
-        ret @{node: afp.fold_crate_directive(c.node, f), span: c.span};
+        ret @{node: afp.fold_crate_directive(c.node, f),
+              span: afp.new_span(c.span)};
     }
     fn f_view_item(afp: &ast_fold_precursor, f: ast_fold, x: &@view_item) ->
        @view_item {
-        ret @{node: afp.fold_view_item(x.node, f), span: x.span};
+        ret @{node: afp.fold_view_item(x.node, f),
+              span: afp.new_span(x.span)};
     }
     fn f_native_item(afp: &ast_fold_precursor, f: ast_fold, x: &@native_item)
        -> @native_item {
@@ -623,34 +632,34 @@ fn make_fold(afp: &ast_fold_precursor) -> ast_fold {
     }
     fn f_method(afp: &ast_fold_precursor, f: ast_fold, x: &@method) ->
        @method {
-        ret @{node: afp.fold_method(x.node, f), span: x.span};
+        ret @{node: afp.fold_method(x.node, f), span: afp.new_span(x.span)};
     }
     fn f_block(afp: &ast_fold_precursor, f: ast_fold, x: &blk) -> blk {
-        ret {node: afp.fold_block(x.node, f), span: x.span};
+        ret {node: afp.fold_block(x.node, f), span: afp.new_span(x.span)};
     }
     fn f_stmt(afp: &ast_fold_precursor, f: ast_fold, x: &@stmt) -> @stmt {
-        ret @{node: afp.fold_stmt(x.node, f), span: x.span};
+        ret @{node: afp.fold_stmt(x.node, f), span: afp.new_span(x.span)};
     }
     fn f_arm(afp: &ast_fold_precursor, f: ast_fold, x: &arm) -> arm {
         ret afp.fold_arm(x, f);
     }
     fn f_pat(afp: &ast_fold_precursor, f: ast_fold, x: &@pat) -> @pat {
         ret @{id: afp.new_id(x.id),
-              node: afp.fold_pat(x.node, f), span: x.span};
+              node: afp.fold_pat(x.node, f), span: afp.new_span(x.span)};
     }
     fn f_decl(afp: &ast_fold_precursor, f: ast_fold, x: &@decl) -> @decl {
-        ret @{node: afp.fold_decl(x.node, f), span: x.span};
+        ret @{node: afp.fold_decl(x.node, f), span: afp.new_span(x.span)};
     }
     fn f_expr(afp: &ast_fold_precursor, f: ast_fold, x: &@expr) -> @expr {
         ret @{id: afp.new_id(x.id),
-              node: afp.fold_expr(x.node, f), span: x.span};
+              node: afp.fold_expr(x.node, f), span: afp.new_span(x.span)};
     }
     fn f_ty(afp: &ast_fold_precursor, f: ast_fold, x: &@ty) -> @ty {
-        ret @{node: afp.fold_ty(x.node, f), span: x.span};
+        ret @{node: afp.fold_ty(x.node, f), span: afp.new_span(x.span)};
     }
     fn f_constr(afp: &ast_fold_precursor, f: ast_fold, x: &@ast::constr) ->
        @ast::constr {
-        ret @{node: afp.fold_constr(x.node, f), span: x.span};
+        ret @{node: afp.fold_constr(x.node, f), span: afp.new_span(x.span)};
     }
     fn f_fn(afp: &ast_fold_precursor, f: ast_fold, x: &_fn) -> _fn {
         ret afp.fold_fn(x, f);
@@ -664,16 +673,16 @@ fn make_fold(afp: &ast_fold_precursor) -> ast_fold {
     }
     fn f_variant(afp: &ast_fold_precursor, f: ast_fold, x: &variant) ->
        variant {
-        ret {node: afp.fold_variant(x.node, f), span: x.span};
+        ret {node: afp.fold_variant(x.node, f), span: afp.new_span(x.span)};
     }
     fn f_ident(afp: &ast_fold_precursor, f: ast_fold, x: &ident) -> ident {
         ret afp.fold_ident(x, f);
     }
     fn f_path(afp: &ast_fold_precursor, f: ast_fold, x: &path) -> path {
-        ret {node: afp.fold_path(x.node, f), span: x.span};
+        ret {node: afp.fold_path(x.node, f), span: afp.new_span(x.span)};
     }
     fn f_local(afp: &ast_fold_precursor, f: ast_fold, x: &@local) -> @local {
-        ret @{node: afp.fold_local(x.node, f), span: x.span};
+        ret @{node: afp.fold_local(x.node, f), span: afp.new_span(x.span)};
     }
 
     *result =
@@ -700,7 +709,8 @@ fn make_fold(afp: &ast_fold_precursor) -> ast_fold {
          fold_path: bind f_path(afp, result, _),
          fold_local: bind f_local(afp, result, _),
          map_exprs: afp.map_exprs,
-         new_id: afp.new_id};
+         new_id: afp.new_id,
+         new_span: afp.new_span};
     ret result;
 }
 
diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs
index f8badc33b6f..19f058d7ee2 100644
--- a/src/comp/syntax/parse/parser.rs
+++ b/src/comp/syntax/parse/parser.rs
@@ -1478,7 +1478,8 @@ fn parse_pat(p: &parser) -> @ast::pat {
         if p.peek() == token::RPAREN {
             hi = p.get_hi_pos();
             p.bump();
-            pat = ast::pat_lit(@{node: ast::lit_nil, span: {lo: lo, hi: hi}});
+            pat = ast::pat_lit(@{node: ast::lit_nil,
+                                 span: ast::mk_sp(lo,hi)});
         } else {
             let fields = ~[parse_pat(p)];
             while p.peek() == token::COMMA {