Skip to content

Commit b1423be

Browse files
committed
Move the ids of pat AST nodes into their struct
Just like it was done with items and exprs. Simplifies some code.
1 parent b669430 commit b1423be

File tree

11 files changed

+68
-79
lines changed

11 files changed

+68
-79
lines changed

src/comp/front/ast.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,15 @@ type block = spanned[block_];
110110

111111
type block_ = rec(vec[@stmt] stmts, option::t[@expr] expr, node_id id);
112112

113-
type pat = spanned[pat_];
113+
type pat = rec(node_id id,
114+
pat_ node,
115+
span span);
114116

115117
tag pat_ {
116-
pat_wild(node_id);
117-
pat_bind(ident, node_id);
118-
pat_lit(@lit, node_id);
119-
pat_tag(path, vec[@pat], node_id);
118+
pat_wild;
119+
pat_bind(ident);
120+
pat_lit(@lit);
121+
pat_tag(path, vec[@pat]);
120122
}
121123

122124
tag mutability { mut; imm; maybe_mut; }

src/comp/front/fold.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,12 @@ fn noop_fold_arm(&arm a, ast_fold fld) -> arm {
252252

253253
fn noop_fold_pat(&pat_ p, ast_fold fld) -> pat_ {
254254
ret alt (p) {
255-
case (pat_wild(_)) { p }
256-
case (pat_bind(?id, ?d)) { pat_bind(fld.fold_ident(id), d)}
257-
case (pat_lit(_, _)) { p }
258-
case (pat_tag(?pth, ?pats, ?nid)) {
259-
pat_tag(fld.fold_path(pth), map(fld.fold_pat, pats), nid)
260-
}
255+
case (pat_wild) { p }
256+
case (pat_bind(?ident)) { pat_bind(fld.fold_ident(ident))}
257+
case (pat_lit(_)) { p }
258+
case (pat_tag(?pth, ?pats)) {
259+
pat_tag(fld.fold_path(pth), map(fld.fold_pat, pats))
260+
}
261261
};
262262
}
263263

@@ -616,7 +616,7 @@ fn make_fold(&ast_fold_precursor afp) -> ast_fold {
616616
ret afp.fold_arm(x, f);
617617
}
618618
fn f_pat(&ast_fold_precursor afp, ast_fold f, &@pat x) -> @pat {
619-
ret @rec(node=afp.fold_pat(x.node, f), span=x.span);
619+
ret @rec(id=x.id, node=afp.fold_pat(x.node, f), span=x.span);
620620
}
621621
fn f_decl(&ast_fold_precursor afp, ast_fold f, &@decl x) -> @decl {
622622
ret @rec(node=afp.fold_decl(x.node, f), span=x.span);

src/comp/front/parser.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,16 +1396,15 @@ fn parse_pat(&parser p) -> @ast::pat {
13961396
alt (p.peek()) {
13971397
case (token::UNDERSCORE) {
13981398
p.bump();
1399-
pat = ast::pat_wild(p.get_id());
1399+
pat = ast::pat_wild;
14001400
}
14011401
case (token::QUES) {
14021402
p.bump();
14031403
alt (p.peek()) {
14041404
case (token::IDENT(?id, _)) {
14051405
hi = p.get_hi_pos();
14061406
p.bump();
1407-
pat =
1408-
ast::pat_bind(p.get_str(id), p.get_id());
1407+
pat = ast::pat_bind(p.get_str(id));
14091408
}
14101409
case (?tok) {
14111410
p.fatal("expected identifier after '?' in pattern but " +
@@ -1418,7 +1417,7 @@ fn parse_pat(&parser p) -> @ast::pat {
14181417
if (!is_ident(tok) || is_word(p, "true") || is_word(p, "false")) {
14191418
auto lit = parse_lit(p);
14201419
hi = lit.span.hi;
1421-
pat = ast::pat_lit(@lit, p.get_id());
1420+
pat = ast::pat_lit(@lit);
14221421
} else {
14231422
auto tag_path = parse_path_and_ty_param_substs(p);
14241423
hi = tag_path.span.hi;
@@ -1434,11 +1433,11 @@ fn parse_pat(&parser p) -> @ast::pat {
14341433
}
14351434
case (_) { args = []; }
14361435
}
1437-
pat = ast::pat_tag(tag_path, args, p.get_id());
1436+
pat = ast::pat_tag(tag_path, args);
14381437
}
14391438
}
14401439
}
1441-
ret @spanned(lo, hi, pat);
1440+
ret @rec(id=p.get_id(), node=pat, span=rec(lo=lo, hi=hi));
14421441
}
14431442

14441443
fn parse_local_full(&option::t[@ast::ty] tyopt, &parser p)

src/comp/middle/alias.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ fn arm_defnums(&ast::arm arm) -> vec[node_id] {
291291
auto dnums = [];
292292
fn walk_pat(&mutable vec[node_id] found, &@ast::pat p) {
293293
alt (p.node) {
294-
case (ast::pat_bind(_, ?id)) { vec::push(found, id); }
295-
case (ast::pat_tag(_, ?children, _)) {
294+
case (ast::pat_bind(_)) { vec::push(found, p.id); }
295+
case (ast::pat_tag(_, ?children)) {
296296
for (@ast::pat child in children) { walk_pat(found, child); }
297297
}
298298
case (_) { }

src/comp/middle/resolve.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,14 @@ fn resolve_names(&@env e, &@ast::crate c) {
301301
}
302302
fn walk_pat(&env e, &scopes sc, &@ast::pat pat) {
303303
alt (pat.node) {
304-
case (ast::pat_tag(?p, ?children, ?id)) {
304+
case (ast::pat_tag(?p, ?children)) {
305305
auto fnd =
306306
lookup_path_strict(e, sc, p.span, p.node.idents,
307307
ns_value);
308308
if (option::is_some(fnd)) {
309309
alt (option::get(fnd)) {
310310
case (ast::def_variant(?did, ?vid)) {
311-
e.def_map.insert(id, option::get(fnd));
311+
e.def_map.insert(pat.id, option::get(fnd));
312312
for (@ast::pat child in children) {
313313
walk_pat(e, sc, child);
314314
}
@@ -694,14 +694,14 @@ fn lookup_in_ty_params(&ident name, &vec[ast::ty_param] ty_params) ->
694694

695695
fn lookup_in_pat(&ident name, &ast::pat pat) -> option::t[def] {
696696
alt (pat.node) {
697-
case (ast::pat_bind(?p_name, ?id)) {
697+
case (ast::pat_bind(?p_name)) {
698698
if (str::eq(p_name, name)) {
699-
ret some(ast::def_binding(local_def(id)));
699+
ret some(ast::def_binding(local_def(pat.id)));
700700
}
701701
}
702-
case (ast::pat_wild(_)) { }
703-
case (ast::pat_lit(_, _)) { }
704-
case (ast::pat_tag(_, ?pats, _)) {
702+
case (ast::pat_wild) { }
703+
case (ast::pat_lit(_)) { }
704+
case (ast::pat_tag(_, ?pats)) {
705705
for (@ast::pat p in pats) {
706706
auto found = lookup_in_pat(name, *p);
707707
if (!option::is_none(found)) { ret found; }
@@ -1248,8 +1248,8 @@ fn check_arm(@env e, &ast::arm a, &() x, &vt[()] v) {
12481248
visit::visit_arm(a, x, v);
12491249
fn walk_pat(checker ch, &@ast::pat p) {
12501250
alt (p.node) {
1251-
case (ast::pat_bind(?name, _)) { add_name(ch, p.span, name); }
1252-
case (ast::pat_tag(_, ?children, _)) {
1251+
case (ast::pat_bind(?name)) { add_name(ch, p.span, name); }
1252+
case (ast::pat_tag(_, ?children)) {
12531253
for (@ast::pat child in children) { walk_pat(ch, child); }
12541254
}
12551255
case (_) { }

src/comp/middle/trans.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4629,19 +4629,19 @@ fn trans_do_while(&@block_ctxt cx, &ast::block body, &@ast::expr cond) ->
46294629
fn trans_pat_match(&@block_ctxt cx, &@ast::pat pat, ValueRef llval,
46304630
&@block_ctxt next_cx) -> result {
46314631
alt (pat.node) {
4632-
case (ast::pat_wild(_)) { ret rslt(cx, llval); }
4633-
case (ast::pat_bind(_, _)) { ret rslt(cx, llval); }
4634-
case (ast::pat_lit(?lt, ?id)) {
4635-
auto lllit = trans_lit(cx.fcx.lcx.ccx, *lt, id);
4636-
auto lltype = ty::node_id_to_type(cx.fcx.lcx.ccx.tcx, id);
4632+
case (ast::pat_wild) { ret rslt(cx, llval); }
4633+
case (ast::pat_bind(_)) { ret rslt(cx, llval); }
4634+
case (ast::pat_lit(?lt)) {
4635+
auto lllit = trans_lit(cx.fcx.lcx.ccx, *lt, pat.id);
4636+
auto lltype = ty::node_id_to_type(cx.fcx.lcx.ccx.tcx, pat.id);
46374637
auto lleq = trans_compare(cx, ast::eq, lltype, llval, lllit);
46384638
auto matched_cx = new_sub_block_ctxt(lleq.bcx, "matched_cx");
46394639
lleq.bcx.build.CondBr(lleq.val, matched_cx.llbb, next_cx.llbb);
46404640
ret rslt(matched_cx, llval);
46414641
}
4642-
case (ast::pat_tag(?ident, ?subpats, ?id)) {
4642+
case (ast::pat_tag(?ident, ?subpats)) {
46434643
auto vdef =
4644-
ast::variant_def_ids(cx.fcx.lcx.ccx.tcx.def_map.get(id));
4644+
ast::variant_def_ids(cx.fcx.lcx.ccx.tcx.def_map.get(pat.id));
46454645
auto variants = ty::tag_variants(cx.fcx.lcx.ccx.tcx, vdef._0);
46464646
auto matched_cx = new_sub_block_ctxt(cx, "matched_cx");
46474647
auto llblobptr = llval;
@@ -4675,7 +4675,7 @@ fn trans_pat_match(&@block_ctxt cx, &@ast::pat pat, ValueRef llval,
46754675
}
46764676
}
46774677
auto ty_params = ty::node_id_to_type_params
4678-
(cx.fcx.lcx.ccx.tcx, id);
4678+
(cx.fcx.lcx.ccx.tcx, pat.id);
46794679
if (vec::len(subpats) > 0u) {
46804680
auto i = 0;
46814681
for (@ast::pat subpat in subpats) {
@@ -4702,37 +4702,37 @@ fn trans_pat_match(&@block_ctxt cx, &@ast::pat pat, ValueRef llval,
47024702
fn trans_pat_binding(&@block_ctxt cx, &@ast::pat pat, ValueRef llval,
47034703
bool bind_alias) -> result {
47044704
alt (pat.node) {
4705-
case (ast::pat_wild(_)) { ret rslt(cx, llval); }
4706-
case (ast::pat_lit(_, _)) { ret rslt(cx, llval); }
4707-
case (ast::pat_bind(?name, ?id)) {
4705+
case (ast::pat_wild) { ret rslt(cx, llval); }
4706+
case (ast::pat_lit(_)) { ret rslt(cx, llval); }
4707+
case (ast::pat_bind(?name)) {
47084708
if (bind_alias) {
4709-
cx.fcx.lllocals.insert(id, llval);
4709+
cx.fcx.lllocals.insert(pat.id, llval);
47104710
ret rslt(cx, llval);
47114711
} else {
4712-
auto t = node_id_type(cx.fcx.lcx.ccx, id);
4712+
auto t = node_id_type(cx.fcx.lcx.ccx, pat.id);
47134713
auto rslt = alloc_ty(cx, t);
47144714
auto dst = rslt.val;
47154715
auto bcx = rslt.bcx;
47164716
maybe_name_value(cx.fcx.lcx.ccx, dst, name);
4717-
bcx.fcx.lllocals.insert(id, dst);
4717+
bcx.fcx.lllocals.insert(pat.id, dst);
47184718
bcx.cleanups += [clean(bind drop_slot(_, dst, t))];
47194719
ret copy_val(bcx, INIT, dst, llval, t);
47204720
}
47214721
}
4722-
case (ast::pat_tag(_, ?subpats, ?id)) {
4722+
case (ast::pat_tag(_, ?subpats)) {
47234723
if (vec::len[@ast::pat](subpats) == 0u) { ret rslt(cx, llval); }
47244724
// Get the appropriate variant for this tag.
47254725

47264726
auto vdef =
4727-
ast::variant_def_ids(cx.fcx.lcx.ccx.tcx.def_map.get(id));
4727+
ast::variant_def_ids(cx.fcx.lcx.ccx.tcx.def_map.get(pat.id));
47284728
auto llblobptr = llval;
47294729
if (vec::len(ty::tag_variants(cx.fcx.lcx.ccx.tcx, vdef._0))!=1u) {
47304730
auto lltagptr = cx.build.PointerCast
47314731
(llval, T_opaque_tag_ptr(cx.fcx.lcx.ccx.tn));
47324732
llblobptr = cx.build.GEP(lltagptr, [C_int(0), C_int(1)]);
47334733
}
47344734
auto ty_param_substs =
4735-
ty::node_id_to_type_params(cx.fcx.lcx.ccx.tcx, id);
4735+
ty::node_id_to_type_params(cx.fcx.lcx.ccx.tcx, pat.id);
47364736
auto this_cx = cx;
47374737
auto i = 0;
47384738
for (@ast::pat subpat in subpats) {

src/comp/middle/ty.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ export mo_val;
9999
export mo_alias;
100100
export mt;
101101
export node_type_table;
102-
export pat_node_id;
103102
export pat_ty;
104103
export cname;
105104
export rename;
@@ -1850,7 +1849,7 @@ fn block_ty(&ctxt cx, &ast::block b) -> t {
18501849
// Returns the type of a pattern as a monotype. Like @expr_ty, this function
18511850
// doesn't provide type parameter substitutions.
18521851
fn pat_ty(&ctxt cx, &@ast::pat pat) -> t {
1853-
ret node_id_to_monotype(cx, pat_node_id(pat));
1852+
ret node_id_to_monotype(cx, pat.id);
18541853
}
18551854

18561855

@@ -1888,16 +1887,6 @@ fn stmt_node_id(&@ast::stmt s) -> ast::node_id {
18881887
}
18891888
}
18901889

1891-
fn pat_node_id(&@ast::pat p) -> ast::node_id {
1892-
alt (p.node) {
1893-
case (ast::pat_wild(?id)) { ret id; }
1894-
case (ast::pat_bind(_, ?id)) { ret id; }
1895-
case (ast::pat_lit(_, ?id)) { ret id; }
1896-
case (ast::pat_tag(_, _, ?id)) { ret id; }
1897-
}
1898-
}
1899-
1900-
19011890
// Expression utilities
19021891
fn field_num(&session::session sess, &span sp, &ast::ident id) -> uint {
19031892
let uint accum = 0u;

src/comp/middle/typeck.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ mod writeback {
10561056
resolve_type_vars_for_node(fcx, b.span, b.node.id);
10571057
}
10581058
fn visit_pat_pre(@fn_ctxt fcx, &@ast::pat p) {
1059-
resolve_type_vars_for_node(fcx, p.span, ty::pat_node_id(p));
1059+
resolve_type_vars_for_node(fcx, p.span, p.id);
10601060
}
10611061
fn visit_local_pre(@fn_ctxt fcx, &@ast::local l) {
10621062
auto var_id = lookup_local(fcx, l.span, l.node.id);
@@ -1196,9 +1196,9 @@ fn gather_locals(&@crate_ctxt ccx, &ast::fn_decl decl, &ast::block body,
11961196
hashmap[ast::node_id, ast::ident] local_names,
11971197
@mutable int nvi, &@ast::pat p) {
11981198
alt (p.node) {
1199-
case (ast::pat_bind(?ident, ?id)) {
1199+
case (ast::pat_bind(?ident)) {
12001200
assign(ccx.tcx, vb, locals, local_names, nvi,
1201-
id, ident, none[ty::t]);
1201+
p.id, ident, none[ty::t]);
12021202
}
12031203
case (_) {/* no-op */ }
12041204
}
@@ -1249,24 +1249,23 @@ fn check_lit(@crate_ctxt ccx, &@ast::lit lit) -> ty::t {
12491249
// their types immediately.
12501250
fn check_pat(&@fn_ctxt fcx, &@ast::pat pat, ty::t expected) {
12511251
alt (pat.node) {
1252-
case (ast::pat_wild(?id)) {
1253-
write::ty_only_fixup(fcx, id, expected);
1252+
case (ast::pat_wild) {
1253+
write::ty_only_fixup(fcx, pat.id, expected);
12541254
}
1255-
case (ast::pat_lit(?lt, ?id)) {
1255+
case (ast::pat_lit(?lt)) {
12561256
auto typ = check_lit(fcx.ccx, lt);
12571257
typ = demand::simple(fcx, pat.span, expected, typ);
1258-
write::ty_only_fixup(fcx, id, typ);
1258+
write::ty_only_fixup(fcx, pat.id, typ);
12591259
}
1260-
case (ast::pat_bind(?name, ?id)) {
1261-
auto vid = lookup_local(fcx, pat.span, id);
1260+
case (ast::pat_bind(?name)) {
1261+
auto vid = lookup_local(fcx, pat.span, pat.id);
12621262
auto typ = ty::mk_var(fcx.ccx.tcx, vid);
12631263
typ = demand::simple(fcx, pat.span, expected, typ);
1264-
write::ty_only_fixup(fcx, id, typ);
1264+
write::ty_only_fixup(fcx, pat.id, typ);
12651265
}
1266-
case (ast::pat_tag(?path, ?subpats, ?id)) {
1266+
case (ast::pat_tag(?path, ?subpats)) {
12671267
// Typecheck the path.
1268-
1269-
auto v_def = lookup_def(fcx, path.span, id);
1268+
auto v_def = lookup_def(fcx, path.span, pat.id);
12701269
auto v_def_ids = ast::variant_def_ids(v_def);
12711270
auto tag_tpt = ty::lookup_item_type(fcx.ccx.tcx, v_def_ids._0);
12721271
auto path_tpot = instantiate_path(fcx, path, tag_tpt, pat.span);
@@ -1341,7 +1340,7 @@ fn check_pat(&@fn_ctxt fcx, &@ast::pat pat, ty::t expected) {
13411340
""
13421341
} else { "s" }));
13431342
}
1344-
write::ty_fixup(fcx, id, path_tpot);
1343+
write::ty_fixup(fcx, pat.id, path_tpot);
13451344
}
13461345
}
13471346
}

src/comp/middle/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ fn visit_constr[E](&@constr c, &E e, &vt[E] v) {
190190

191191
fn visit_pat[E](&@pat p, &E e, &vt[E] v) {
192192
alt (p.node) {
193-
case (pat_tag(?path, ?children, _)) {
193+
case (pat_tag(?path, ?children)) {
194194
for (@ty tp in path.node.types) { vt(v).visit_ty(tp, e, v); }
195195
for (@pat child in children) { vt(v).visit_pat(child, e, v); }
196196
}

src/comp/middle/walk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ fn walk_ty(&ast_visitor v, @ast::ty t) {
185185
fn walk_pat(&ast_visitor v, &@ast::pat p) {
186186
v.visit_pat_pre(p);
187187
alt (p.node) {
188-
case (ast::pat_tag(?path, ?children, _)) {
188+
case (ast::pat_tag(?path, ?children)) {
189189
for (@ast::ty tp in path.node.types) { walk_ty(v, tp); }
190190
for (@ast::pat child in children) { walk_pat(v, child); }
191191
}

src/comp/pretty/pprust.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,10 +1016,10 @@ fn print_path(&ps s, &ast::path path) {
10161016
fn print_pat(&ps s, &@ast::pat pat) {
10171017
maybe_print_comment(s, pat.span.lo);
10181018
alt (pat.node) {
1019-
case (ast::pat_wild(_)) { word(s.s, "_"); }
1020-
case (ast::pat_bind(?id, _)) { word(s.s, "?" + id); }
1021-
case (ast::pat_lit(?lit, _)) { print_literal(s, lit); }
1022-
case (ast::pat_tag(?path, ?args, _)) {
1019+
case (ast::pat_wild) { word(s.s, "_"); }
1020+
case (ast::pat_bind(?id)) { word(s.s, "?" + id); }
1021+
case (ast::pat_lit(?lit)) { print_literal(s, lit); }
1022+
case (ast::pat_tag(?path, ?args)) {
10231023
print_path(s, path);
10241024
if (vec::len(args) > 0u) {
10251025
popen(s);
@@ -1033,7 +1033,7 @@ fn print_pat(&ps s, &@ast::pat pat) {
10331033
alt (s.mode) {
10341034
case (mo_identified) {
10351035
space(s.s);
1036-
synth_comment(s, int::to_str(ty::pat_node_id(pat), 10u));
1036+
synth_comment(s, int::to_str(pat.id, 10u));
10371037
}
10381038
case (_) {/* no-op */ }
10391039
}

0 commit comments

Comments
 (0)