Skip to content

Commit dd9693f

Browse files
committed
Reserve node_id 0 for the crate top-level module
And define a const to refer to it.
1 parent dbfa1b5 commit dd9693f

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/comp/middle/resolve.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ export def_map, ext_map, exp_map, impl_map, iscopes;
2727
// locates all names (in expressions, types, and alt patterns) and resolves
2828
// them, storing the resulting def in the AST nodes.
2929

30-
const crate_mod: int = -1;
31-
3230
tag scope {
3331
scope_crate;
3432
scope_item(@ast::item);
@@ -176,7 +174,7 @@ fn map_crate(e: @env, c: @ast::crate) {
176174
visit::visit_crate(*c, cons(scope_crate, @nil), visit::mk_vt(v_map_mod));
177175

178176
// Register the top-level mod
179-
e.mod_map.insert(crate_mod,
177+
e.mod_map.insert(ast::crate_node_id,
180178
@{m: some(c.node.module),
181179
index: index_mod(c.node.module),
182180
mutable glob_imports: [],
@@ -260,7 +258,7 @@ fn map_crate(e: @env, c: @ast::crate) {
260258
e.block_map.insert(b.node.id, globs);
261259
}
262260
scope_crate. {
263-
e.mod_map.get(crate_mod).glob_imports += [glob];
261+
e.mod_map.get(ast::crate_node_id).glob_imports += [glob];
264262
}
265263
}
266264
}
@@ -534,7 +532,8 @@ fn resolve_import(e: env, defid: ast::def_id, name: ast::ident,
534532
lst(id, b.node.view_items)
535533
}
536534
cons(scope_crate., _) {
537-
lst(id, option::get(e.mod_map.get(crate_mod).m).view_items)
535+
lst(id,
536+
option::get(e.mod_map.get(ast::crate_node_id).m).view_items)
538537
}
539538
}
540539
}
@@ -639,7 +638,7 @@ fn unresolved_err(e: env, cx: ctxt, sp: span, name: ident, kind: str) {
639638
let did = def_id_of_def(def);
640639
if did.crate == ast::local_crate {
641640
path = e.mod_map.get(did.node).path + path;
642-
} else if did.node != -1 {
641+
} else if did.node != ast::crate_node_id {
643642
let paths = e.ext_map.get(did);
644643
if vec::len(paths) > 0u {
645644
path = str::connect(paths, "::") + "::" + path;
@@ -744,7 +743,8 @@ fn lookup_in_scope(e: env, sc: scopes, sp: span, name: ident, ns: namespace)
744743
option::t<def> {
745744
alt s {
746745
scope_crate. {
747-
ret lookup_in_local_mod(e, crate_mod, sp, name, ns, inside);
746+
ret lookup_in_local_mod(e, ast::crate_node_id, sp,
747+
name, ns, inside);
748748
}
749749
scope_item(it) {
750750
alt it.node {
@@ -1057,7 +1057,9 @@ fn lookup_in_mod(e: env, m: def, sp: span, name: ident, ns: namespace,
10571057
let cached = e.ext_cache.find({did: defid, ident: name, ns: ns});
10581058
if !is_none(cached) { ret cached; }
10591059
let path = [name];
1060-
if defid.node != crate_mod { path = e.ext_map.get(defid) + path; }
1060+
if defid.node != ast::crate_node_id {
1061+
path = e.ext_map.get(defid) + path;
1062+
}
10611063
let fnd = lookup_external(e, defid.crate, path, ns);
10621064
if !is_none(fnd) {
10631065
e.ext_cache.insert({did: defid, ident: name, ns: ns},
@@ -1079,7 +1081,7 @@ fn found_view_item(e: env, vi: @ast::view_item) -> option::t<def> {
10791081
alt vi.node {
10801082
ast::view_item_use(_, _, id) {
10811083
let cnum = cstore::get_use_stmt_cnum(e.cstore, id);
1082-
ret some(ast::def_mod({crate: cnum, node: crate_mod}));
1084+
ret some(ast::def_mod({crate: cnum, node: ast::crate_node_id}));
10831085
}
10841086
}
10851087
}

src/comp/syntax/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type node_id = int;
2222
type def_id = {crate: crate_num, node: node_id};
2323

2424
const local_crate: crate_num = 0;
25+
const crate_node_id: node_id = 0;
2526

2627
type ty_param = {ident: ident, kind: kind};
2728

0 commit comments

Comments
 (0)