Skip to content

Commit 78ec6fe

Browse files
committed
Obliterate the callee_id hack
Exprs that could be applications of overloaded operators (expr_unary, expr_binary, expr_index) relied on the previous node ID being "reserved" to carry extra typechecking info. This was incredibly error-prone. Fixed it; now all exprs have two node IDs (which will be wasted in some cases; future work could make this an option instead if the extra int field ends up being a performance problem). Closes rust-lang#2804
1 parent fec8059 commit 78ec6fe

21 files changed

+148
-57
lines changed

src/fuzzer/fuzzer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn find_rust_files(&files: ~[str], path: str) {
3434

3535
fn common_exprs() -> ~[ast::expr] {
3636
fn dse(e: ast::expr_) -> ast::expr {
37-
{ id: 0, node: e, span: ast_util::dummy_sp() }
37+
{ id: 0, callee_id: -1, node: e, span: ast_util::dummy_sp() }
3838
}
3939

4040
fn dsl(l: ast::lit_) -> ast::lit {

src/libsyntax/ast.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ enum blk_check_mode { default_blk, unchecked_blk, unsafe_blk, }
288288
enum expr_check_mode { claimed_expr, checked_expr, }
289289

290290
#[auto_serialize]
291-
type expr = {id: node_id, node: expr_, span: span};
291+
type expr = {id: node_id, callee_id: node_id, node: expr_, span: span};
292+
// Extra node ID is only used for index, assign_op, unary, binary
292293

293294
#[auto_serialize]
294295
enum alt_mode { alt_check, alt_exhaustive, }

src/libsyntax/ast_util.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,6 @@ pure fn unguarded_pat(a: arm) -> option<~[@pat]> {
272272
if is_unguarded(a) { some(/* FIXME (#2543) */ copy a.pats) } else { none }
273273
}
274274

275-
// Provides an extra node_id to hang callee information on, in case the
276-
// operator is deferred to a user-supplied method. The parser is responsible
277-
// for reserving this id.
278-
fn op_expr_callee_id(e: @expr) -> node_id { e.id - 1 }
279-
280275
pure fn class_item_ident(ci: @class_member) -> ident {
281276
alt ci.node {
282277
instance_var(i,_,_,_,_) { /* FIXME (#2543) */ copy i }
@@ -455,14 +450,8 @@ fn id_visitor(vfn: fn@(node_id)) -> visit::vt<()> {
455450
},
456451

457452
visit_expr: fn@(e: @expr) {
453+
vfn(e.callee_id);
458454
vfn(e.id);
459-
alt e.node {
460-
expr_index(*) | expr_assign_op(*) |
461-
expr_unary(*) | expr_binary(*) {
462-
vfn(ast_util::op_expr_callee_id(e));
463-
}
464-
_ { /* fallthrough */ }
465-
}
466455
},
467456

468457
visit_ty: fn@(t: @ty) {

src/libsyntax/ext/auto_serialize.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ impl helpers for ext_ctxt {
172172
}
173173

174174
fn expr(span: span, node: ast::expr_) -> @ast::expr {
175-
@{id: self.next_id(), node: node, span: span}
175+
@{id: self.next_id(), callee_id: self.next_id(),
176+
node: node, span: span}
176177
}
177178

178179
fn var_ref(span: span, name: ast::ident) -> @ast::expr {

src/libsyntax/ext/build.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import base::ext_ctxt;
33

44
fn mk_expr(cx: ext_ctxt, sp: codemap::span, expr: ast::expr_) ->
55
@ast::expr {
6-
ret @{id: cx.next_id(), node: expr, span: sp};
6+
ret @{id: cx.next_id(), callee_id: cx.next_id(),
7+
node: expr, span: sp};
78
}
89

910
fn mk_lit(cx: ext_ctxt, sp: span, lit: ast::lit_) -> @ast::expr {
1011
let sp_lit = @{node: lit, span: sp};
11-
ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp};
12+
mk_expr(cx, sp, ast::expr_lit(sp_lit))
1213
}
1314
fn mk_str(cx: ext_ctxt, sp: span, s: str) -> @ast::expr {
1415
let lit = ast::lit_str(@s);
@@ -62,7 +63,7 @@ fn mk_call(cx: ext_ctxt, sp: span, fn_path: ~[ast::ident],
6263
fn mk_base_vec_e(cx: ext_ctxt, sp: span, exprs: ~[@ast::expr]) ->
6364
@ast::expr {
6465
let vecexpr = ast::expr_vec(exprs, ast::m_imm);
65-
ret @{id: cx.next_id(), node: vecexpr, span: sp};
66+
mk_expr(cx, sp, vecexpr)
6667
}
6768
fn mk_vstore_e(cx: ext_ctxt, sp: span, expr: @ast::expr, vst: ast::vstore) ->
6869
@ast::expr {

src/libsyntax/ext/concat_idents.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
99
}
1010

1111
ret @{id: cx.next_id(),
12+
callee_id: cx.next_id(),
1213
node: ast::expr_path(@{span: sp, global: false, idents: ~[@res],
1314
rp: none, types: ~[]}),
1415
span: sp};

src/libsyntax/ext/log_syntax.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
1111
);
1212

1313
//trivial expression
14-
ret @{id: cx.next_id(), node: ast::expr_rec(~[], option::none),
15-
span: sp};
14+
ret @{id: cx.next_id(), callee_id: cx.next_id(),
15+
node: ast::expr_rec(~[], option::none), span: sp};
1616
}

src/libsyntax/ext/simplext.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import base::*;
77
import fold::*;
88
import ast_util::respan;
99
import ast::{ident, path, ty, blk_, expr, expr_path,
10-
expr_vec, expr_mac, mac_invoc, node_id};
10+
expr_vec, expr_mac, mac_invoc, node_id, expr_index};
1111

1212
export add_new_extension;
1313

src/libsyntax/fold.rs

+1
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ impl of ast_fold for ast_fold_precursor {
688688
fn fold_expr(&&x: @expr) -> @expr {
689689
let (n, s) = self.fold_expr(x.node, x.span, self as ast_fold);
690690
ret @{id: self.new_id(x.id),
691+
callee_id: self.new_id(x.callee_id),
691692
node: n,
692693
span: self.new_span(s)};
693694
}

src/libsyntax/parse/parser.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import print::pprust::expr_to_str;
2+
13
import result::result;
24
import either::{either, left, right};
35
import std::map::{hashmap, str_hash};
@@ -758,11 +760,13 @@ class parser {
758760
}
759761

760762
fn mk_expr(lo: uint, hi: uint, +node: expr_) -> @expr {
761-
ret @{id: self.get_id(), node: node, span: mk_sp(lo, hi)};
763+
ret @{id: self.get_id(), callee_id: self.get_id(),
764+
node: node, span: mk_sp(lo, hi)};
762765
}
763766

764767
fn mk_mac_expr(lo: uint, hi: uint, m: mac_) -> @expr {
765768
ret @{id: self.get_id(),
769+
callee_id: self.get_id(),
766770
node: expr_mac({node: m, span: mk_sp(lo, hi)}),
767771
span: mk_sp(lo, hi)};
768772
}
@@ -772,7 +776,8 @@ class parser {
772776
let lv_lit = @{node: lit_uint(i as u64, ty_u32),
773777
span: span};
774778

775-
ret @{id: self.get_id(), node: expr_lit(lv_lit), span: span};
779+
ret @{id: self.get_id(), callee_id: self.get_id(),
780+
node: expr_lit(lv_lit), span: span};
776781
}
777782

778783
fn mk_pexpr(lo: uint, hi: uint, node: expr_) -> pexpr {
@@ -1112,7 +1117,6 @@ class parser {
11121117
let ix = self.parse_expr();
11131118
hi = ix.span.hi;
11141119
self.expect(token::RBRACKET);
1115-
self.get_id(); // see ast_util::op_expr_callee_id
11161120
e = self.mk_pexpr(lo, hi, expr_index(self.to_expr(e), ix));
11171121
}
11181122

src/rustc/front/test.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,11 @@ fn mk_test_desc_vec(cx: test_ctxt) -> @ast::expr {
283283
}
284284

285285
let inner_expr = @{id: cx.sess.next_node_id(),
286+
callee_id: cx.sess.next_node_id(),
286287
node: ast::expr_vec(descs, ast::m_imm),
287288
span: dummy_sp()};
288289
ret @{id: cx.sess.next_node_id(),
290+
callee_id: cx.sess.next_node_id(),
289291
node: ast::expr_vstore(inner_expr, ast::vstore_uniq),
290292
span: dummy_sp()};
291293
}
@@ -300,6 +302,7 @@ fn mk_test_desc_rec(cx: test_ctxt, test: test) -> @ast::expr {
300302
nospan(ast::lit_str(@ast_util::path_name_i(path)));
301303
let name_expr: ast::expr =
302304
{id: cx.sess.next_node_id(),
305+
callee_id: cx.sess.next_node_id(),
303306
node: ast::expr_lit(@name_lit),
304307
span: span};
305308

@@ -310,6 +313,7 @@ fn mk_test_desc_rec(cx: test_ctxt, test: test) -> @ast::expr {
310313

311314
let fn_expr: ast::expr =
312315
{id: cx.sess.next_node_id(),
316+
callee_id: cx.sess.next_node_id(),
313317
node: ast::expr_path(fn_path),
314318
span: span};
315319

@@ -322,6 +326,7 @@ fn mk_test_desc_rec(cx: test_ctxt, test: test) -> @ast::expr {
322326

323327
let ignore_expr: ast::expr =
324328
{id: cx.sess.next_node_id(),
329+
callee_id: cx.sess.next_node_id(),
325330
node: ast::expr_lit(@ignore_lit),
326331
span: span};
327332

@@ -332,6 +337,7 @@ fn mk_test_desc_rec(cx: test_ctxt, test: test) -> @ast::expr {
332337

333338
let fail_expr: ast::expr =
334339
{id: cx.sess.next_node_id(),
340+
callee_id: cx.sess.next_node_id(),
335341
node: ast::expr_lit(@fail_lit),
336342
span: span};
337343

@@ -342,7 +348,8 @@ fn mk_test_desc_rec(cx: test_ctxt, test: test) -> @ast::expr {
342348
ast::expr_rec(~[name_field, fn_field, ignore_field, fail_field],
343349
option::none);
344350
let desc_rec: ast::expr =
345-
{id: cx.sess.next_node_id(), node: desc_rec_, span: span};
351+
{id: cx.sess.next_node_id(), callee_id: cx.sess.next_node_id(),
352+
node: desc_rec_, span: span};
346353
ret @desc_rec;
347354
}
348355

@@ -354,6 +361,7 @@ fn mk_test_wrapper(cx: test_ctxt,
354361
span: span) -> @ast::expr {
355362
let call_expr: ast::expr = {
356363
id: cx.sess.next_node_id(),
364+
callee_id: cx.sess.next_node_id(),
357365
node: ast::expr_call(@fn_path_expr, ~[], false),
358366
span: span
359367
};
@@ -379,6 +387,7 @@ fn mk_test_wrapper(cx: test_ctxt,
379387

380388
let wrapper_expr: ast::expr = {
381389
id: cx.sess.next_node_id(),
390+
callee_id: cx.sess.next_node_id(),
382391
node: ast::expr_fn(ast::proto_bare, wrapper_decl,
383392
wrapper_body, @~[]),
384393
span: span
@@ -444,37 +453,40 @@ fn mk_test_main_call(cx: test_ctxt) -> @ast::expr {
444453
let args_path_expr_: ast::expr_ = ast::expr_path(args_path);
445454

446455
let args_path_expr: ast::expr =
447-
{id: cx.sess.next_node_id(), node: args_path_expr_, span: dummy_sp()};
456+
{id: cx.sess.next_node_id(), callee_id: cx.sess.next_node_id(),
457+
node: args_path_expr_, span: dummy_sp()};
448458

449459
// Call __test::test to generate the vector of test_descs
450460
let test_path = path_node(~[@"tests"]);
451461

452462
let test_path_expr_: ast::expr_ = ast::expr_path(test_path);
453463

454464
let test_path_expr: ast::expr =
455-
{id: cx.sess.next_node_id(), node: test_path_expr_, span: dummy_sp()};
465+
{id: cx.sess.next_node_id(), callee_id: cx.sess.next_node_id(),
466+
node: test_path_expr_, span: dummy_sp()};
456467

457468
let test_call_expr_ = ast::expr_call(@test_path_expr, ~[], false);
458469

459470
let test_call_expr: ast::expr =
460-
{id: cx.sess.next_node_id(), node: test_call_expr_, span: dummy_sp()};
471+
{id: cx.sess.next_node_id(), callee_id: cx.sess.next_node_id(),
472+
node: test_call_expr_, span: dummy_sp()};
461473

462474
// Call std::test::test_main
463475
let test_main_path = path_node(mk_path(cx, ~[@"test", @"test_main"]));
464476

465477
let test_main_path_expr_: ast::expr_ = ast::expr_path(test_main_path);
466478

467479
let test_main_path_expr: ast::expr =
468-
{id: cx.sess.next_node_id(), node: test_main_path_expr_,
469-
span: dummy_sp()};
480+
{id: cx.sess.next_node_id(), callee_id: cx.sess.next_node_id(),
481+
node: test_main_path_expr_, span: dummy_sp()};
470482

471483
let test_main_call_expr_: ast::expr_ =
472484
ast::expr_call(@test_main_path_expr,
473485
~[@args_path_expr, @test_call_expr], false);
474486

475487
let test_main_call_expr: ast::expr =
476-
{id: cx.sess.next_node_id(), node: test_main_call_expr_,
477-
span: dummy_sp()};
488+
{id: cx.sess.next_node_id(), callee_id: cx.sess.next_node_id(),
489+
node: test_main_call_expr_, span: dummy_sp()};
478490

479491
ret @test_main_call_expr;
480492
}

src/rustc/middle/borrowck.rs

-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ import std::list::{list, cons, nil};
159159
import result::{result, ok, err, extensions};
160160
import syntax::print::pprust;
161161
import util::common::indenter;
162-
import ast_util::op_expr_callee_id;
163162
import ty::to_str;
164163
import driver::session::session;
165164
import dvec::{dvec, extensions};

src/rustc/middle/borrowck/check_loans.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -616,15 +616,15 @@ fn check_loans_in_expr(expr: @ast::expr,
616616
if self.bccx.method_map.contains_key(expr.id) {
617617
self.check_call(expr,
618618
none,
619-
ast_util::op_expr_callee_id(expr),
619+
expr.callee_id,
620620
expr.span,
621621
~[rval]);
622622
}
623623
ast::expr_unary(*) | ast::expr_index(*)
624624
if self.bccx.method_map.contains_key(expr.id) {
625625
self.check_call(expr,
626626
none,
627-
ast_util::op_expr_callee_id(expr),
627+
expr.callee_id,
628628
expr.span,
629629
~[]);
630630
}

src/rustc/middle/lint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ fn check_item_path_statement(cx: ty::ctxt, it: @ast::item) {
405405
visit_stmt: fn@(s: @ast::stmt) {
406406
alt s.node {
407407
ast::stmt_semi(@{id: id,
408+
callee_id: _,
408409
node: ast::expr_path(@path),
409410
span: _}, _) {
410411
cx.sess.span_lint(

src/rustc/middle/trans/base.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -1475,12 +1475,12 @@ fn trans_unary(bcx: block, op: ast::unop, e: @ast::expr,
14751475
// Check for user-defined method call
14761476
alt bcx.ccx().maps.method_map.find(un_expr.id) {
14771477
some(mentry) {
1478-
let callee_id = ast_util::op_expr_callee_id(un_expr);
1479-
let fty = node_id_type(bcx, callee_id);
1478+
let fty = node_id_type(bcx, un_expr.callee_id);
14801479
ret trans_call_inner(
14811480
bcx, un_expr.info(), fty,
14821481
expr_ty(bcx, un_expr),
1483-
|bcx| impl::trans_method_callee(bcx, callee_id, e, mentry),
1482+
|bcx| impl::trans_method_callee(bcx, un_expr.callee_id, e,
1483+
mentry),
14841484
arg_exprs(~[]), dest);
14851485
}
14861486
_ {}
@@ -1703,10 +1703,9 @@ fn trans_assign_op(bcx: block, ex: @ast::expr, op: ast::binop,
17031703
alt bcx.ccx().maps.method_map.find(ex.id) {
17041704
some(origin) {
17051705
let bcx = lhs_res.bcx;
1706-
let callee_id = ast_util::op_expr_callee_id(ex);
17071706
#debug["user-defined method callee_id: %s",
1708-
ast_map::node_id_to_str(bcx.tcx().items, callee_id)];
1709-
let fty = node_id_type(bcx, callee_id);
1707+
ast_map::node_id_to_str(bcx.tcx().items, ex.callee_id)];
1708+
let fty = node_id_type(bcx, ex.callee_id);
17101709

17111710
let dty = expr_ty(bcx, dst);
17121711
let target = alloc_ty(bcx, dty);
@@ -1717,7 +1716,7 @@ fn trans_assign_op(bcx: block, ex: @ast::expr, op: ast::binop,
17171716
|bcx| {
17181717
// FIXME (#2528): provide the already-computed address, not
17191718
// the expr.
1720-
impl::trans_method_callee(bcx, callee_id, dst, origin)
1719+
impl::trans_method_callee(bcx, ex.callee_id, dst, origin)
17211720
},
17221721
arg_exprs(~[src]), save_in(target));
17231722

@@ -1851,13 +1850,12 @@ fn trans_binary(bcx: block, op: ast::binop, lhs: @ast::expr,
18511850
// User-defined operators
18521851
alt bcx.ccx().maps.method_map.find(ex.id) {
18531852
some(origin) {
1854-
let callee_id = ast_util::op_expr_callee_id(ex);
1855-
let fty = node_id_type(bcx, callee_id);
1853+
let fty = node_id_type(bcx, ex.callee_id);
18561854
ret trans_call_inner(
18571855
bcx, ex.info(), fty,
18581856
expr_ty(bcx, ex),
18591857
|bcx| {
1860-
impl::trans_method_callee(bcx, callee_id, lhs, origin)
1858+
impl::trans_method_callee(bcx, ex.callee_id, lhs, origin)
18611859
},
18621860
arg_exprs(~[rhs]), dest);
18631861
}
@@ -3597,12 +3595,12 @@ fn trans_expr(bcx: block, e: @ast::expr, dest: dest) -> block {
35973595
// If it is here, it's not an lval, so this is a user-defined
35983596
// index op
35993597
let origin = bcx.ccx().maps.method_map.get(e.id);
3600-
let callee_id = ast_util::op_expr_callee_id(e);
3601-
let fty = node_id_type(bcx, callee_id);
3598+
let fty = node_id_type(bcx, e.callee_id);
36023599
ret trans_call_inner(
36033600
bcx, e.info(), fty,
36043601
expr_ty(bcx, e),
3605-
|bcx| impl::trans_method_callee(bcx, callee_id, base, origin),
3602+
|bcx| impl::trans_method_callee(bcx, e.callee_id, base,
3603+
origin),
36063604
arg_exprs(~[idx]), dest);
36073605
}
36083606

0 commit comments

Comments
 (0)