Skip to content

Commit b803326

Browse files
committed
Change node_ids when expanding, to avoid duplicates.
1 parent f50a582 commit b803326

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/comp/syntax/ext/simplext.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import base::expr_to_str;
1515
import base::expr_to_ident;
1616

1717
import fold::*;
18+
import ast::node_id;
1819
import ast::respan;
1920
import ast::ident;
2021
import ast::path;
@@ -198,6 +199,7 @@ fn use_selectors_to_bind(b: &binders, e: @expr) -> option::t[bindings] {
198199

199200
fn transcribe(cx: &ext_ctxt, b: &bindings, body: @expr) -> @expr {
200201
let idx_path: @mutable [uint] = @mutable ~[];
202+
fn new_id(old: node_id, cx: &ext_ctxt) -> node_id { ret cx.next_id(); }
201203
let afp = default_ast_fold();
202204
let f_pre =
203205
{fold_ident: bind transcribe_ident(cx, b, idx_path, _, _),
@@ -207,7 +209,8 @@ fn transcribe(cx: &ext_ctxt, b: &bindings, body: @expr) -> @expr {
207209
fold_ty: bind transcribe_type(cx, b, idx_path, _, _, afp.fold_ty),
208210
fold_block:
209211
bind transcribe_block(cx, b, idx_path, _, _, afp.fold_block),
210-
map_exprs: bind transcribe_exprs(cx, b, idx_path, _, _) with *afp};
212+
map_exprs: bind transcribe_exprs(cx, b, idx_path, _, _),
213+
new_id: bind new_id(_, cx) with *afp};
211214
let f = make_fold(f_pre);
212215
let result = f.fold_expr(body);
213216
dummy_out(f); //temporary: kill circular reference

src/comp/syntax/fold.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ type ast_fold_precursor =
4343
fold_ident: fn(&ident, ast_fold) -> ident ,
4444
fold_path: fn(&path_, ast_fold) -> path_ ,
4545
fold_local: fn(&local_, ast_fold) -> local_ ,
46-
map_exprs: fn(fn(&@expr) -> @expr , [@expr]) -> [@expr] };
46+
map_exprs: fn(fn(&@expr) -> @expr , [@expr]) -> [@expr],
47+
new_id: fn(node_id) -> node_id};
4748

4849
type a_f =
4950
{fold_crate: fn(&crate) -> crate ,
@@ -68,7 +69,9 @@ type a_f =
6869
fold_ident: fn(&ident) -> ident ,
6970
fold_path: fn(&path) -> path ,
7071
fold_local: fn(&@local) -> @local ,
71-
map_exprs: fn(fn(&@expr) -> @expr , [@expr]) -> [@expr] };
72+
map_exprs: fn(fn(&@expr) -> @expr , [@expr]) -> [@expr],
73+
new_id: fn(node_id) -> node_id};
74+
7275

7376
//fn nf_dummy[T](&T node) -> T { fail; }
7477
fn nf_crate_dummy(c: &crate) -> crate { fail; }
@@ -509,6 +512,8 @@ fn noop_map_exprs(f: fn(&@expr) -> @expr , es: [@expr]) -> [@expr] {
509512
ret ivec::map(f, es);
510513
}
511514

515+
fn noop_id(i: node_id) -> node_id { ret i; }
516+
512517

513518
fn default_ast_fold() -> @ast_fold_precursor {
514519
ret @{fold_crate: noop_fold_crate,
@@ -533,7 +538,8 @@ fn default_ast_fold() -> @ast_fold_precursor {
533538
fold_ident: noop_fold_ident,
534539
fold_path: noop_fold_path,
535540
fold_local: noop_fold_local,
536-
map_exprs: noop_map_exprs};
541+
map_exprs: noop_map_exprs,
542+
new_id: noop_id};
537543
}
538544

539545
fn dummy_out(a: ast_fold) {
@@ -560,7 +566,8 @@ fn dummy_out(a: ast_fold) {
560566
fold_ident: nf_ident_dummy,
561567
fold_path: nf_path_dummy,
562568
fold_local: nf_local_dummy,
563-
map_exprs: noop_map_exprs};
569+
map_exprs: noop_map_exprs,
570+
new_id: noop_id};
564571
}
565572

566573

@@ -588,7 +595,8 @@ fn make_fold(afp: &ast_fold_precursor) -> ast_fold {
588595
fold_ident: nf_ident_dummy,
589596
fold_path: nf_path_dummy,
590597
fold_local: nf_local_dummy,
591-
map_exprs: noop_map_exprs};
598+
map_exprs: noop_map_exprs,
599+
new_id: noop_id};
592600

593601
/* naturally, a macro to write these would be nice */
594602
fn f_crate(afp: &ast_fold_precursor, f: ast_fold, c: &crate) -> crate {
@@ -627,13 +635,15 @@ fn make_fold(afp: &ast_fold_precursor) -> ast_fold {
627635
ret afp.fold_arm(x, f);
628636
}
629637
fn f_pat(afp: &ast_fold_precursor, f: ast_fold, x: &@pat) -> @pat {
630-
ret @{id: x.id, node: afp.fold_pat(x.node, f), span: x.span};
638+
ret @{id: afp.new_id(x.id),
639+
node: afp.fold_pat(x.node, f), span: x.span};
631640
}
632641
fn f_decl(afp: &ast_fold_precursor, f: ast_fold, x: &@decl) -> @decl {
633642
ret @{node: afp.fold_decl(x.node, f), span: x.span};
634643
}
635644
fn f_expr(afp: &ast_fold_precursor, f: ast_fold, x: &@expr) -> @expr {
636-
ret @{id: x.id, node: afp.fold_expr(x.node, f), span: x.span};
645+
ret @{id: afp.new_id(x.id),
646+
node: afp.fold_expr(x.node, f), span: x.span};
637647
}
638648
fn f_ty(afp: &ast_fold_precursor, f: ast_fold, x: &@ty) -> @ty {
639649
ret @{node: afp.fold_ty(x.node, f), span: x.span};
@@ -689,7 +699,8 @@ fn make_fold(afp: &ast_fold_precursor) -> ast_fold {
689699
fold_ident: bind f_ident(afp, result, _),
690700
fold_path: bind f_path(afp, result, _),
691701
fold_local: bind f_local(afp, result, _),
692-
map_exprs: afp.map_exprs};
702+
map_exprs: afp.map_exprs,
703+
new_id: afp.new_id};
693704
ret result;
694705
}
695706

0 commit comments

Comments
 (0)