Skip to content

Commit 6e7d5e1

Browse files
committed
rustc: Implement "use mod"
1 parent 28b1473 commit 6e7d5e1

File tree

8 files changed

+296
-102
lines changed

8 files changed

+296
-102
lines changed

src/libsyntax/ast.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,15 @@ type path_list_ident_ = {name: ident, id: node_id};
11091109
#[auto_serialize]
11101110
type path_list_ident = spanned<path_list_ident_>;
11111111

1112+
#[auto_serialize]
1113+
enum namespace { module_ns, type_value_ns }
1114+
1115+
impl namespace : cmp::Eq {
1116+
pure fn eq(&&other: namespace) -> bool {
1117+
(self as uint) == (other as uint)
1118+
}
1119+
}
1120+
11121121
#[auto_serialize]
11131122
type view_path = spanned<view_path_>;
11141123

@@ -1120,7 +1129,7 @@ enum view_path_ {
11201129
// or just
11211130
//
11221131
// foo::bar::baz (with 'baz =' implicitly on the left)
1123-
view_path_simple(ident, @path, node_id),
1132+
view_path_simple(ident, @path, namespace, node_id),
11241133

11251134
// foo::bar::*
11261135
view_path_glob(@path, node_id),
@@ -1152,12 +1161,7 @@ enum attr_style { attr_outer, attr_inner, }
11521161

11531162
impl attr_style : cmp::Eq {
11541163
pure fn eq(&&other: attr_style) -> bool {
1155-
match (self, other) {
1156-
(attr_outer, attr_outer) => true,
1157-
(attr_inner, attr_inner) => true,
1158-
(attr_outer, _) => false,
1159-
(attr_inner, _) => false,
1160-
}
1164+
(self as uint) == (other as uint)
11611165
}
11621166
}
11631167

src/libsyntax/ast_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ fn map_view_item(vi: @view_item, cx: ctx, _v: vt) {
293293
match vi.node {
294294
view_item_export(vps) => for vps.each |vp| {
295295
let (id, name) = match vp.node {
296-
view_path_simple(nm, _, id) => {
296+
view_path_simple(nm, _, _, id) => {
297297
(id, /* FIXME (#2543) */ copy nm)
298298
}
299299
view_path_glob(pth, id) | view_path_list(pth, _, id) => {

src/libsyntax/ast_util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ fn is_exported(i: ident, m: _mod) -> bool {
211211
has_explicit_exports = true;
212212
for vps.each |vp| {
213213
match vp.node {
214-
ast::view_path_simple(id, _, _) => {
214+
ast::view_path_simple(id, _, _, _) => {
215215
if id == i { return true; }
216216
match parent_enum {
217217
Some(parent_enum_id) => {
@@ -442,7 +442,7 @@ fn id_visitor(vfn: fn@(node_id)) -> visit::vt<()> {
442442
view_item_import(vps) | view_item_export(vps) => {
443443
do vec::iter(vps) |vp| {
444444
match vp.node {
445-
view_path_simple(_, _, id) => vfn(id),
445+
view_path_simple(_, _, _, id) => vfn(id),
446446
view_path_glob(_, id) => vfn(id),
447447
view_path_list(_, _, id) => vfn(id)
448448
}
@@ -602,7 +602,7 @@ fn walk_pat(pat: @pat, it: fn(@pat)) {
602602

603603
fn view_path_id(p: @view_path) -> node_id {
604604
match p.node {
605-
view_path_simple(_, _, id) | view_path_glob(_, id) |
605+
view_path_simple(_, _, _, id) | view_path_glob(_, id) |
606606
view_path_list(_, _, id) => id
607607
}
608608
}

src/libsyntax/parse/parser.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,25 @@ import ast::{_mod, add, alt_check, alt_exhaustive, arg, arm, attribute,
4040
lit_int_unsuffixed, lit_nil, lit_str, lit_uint, local, m_const,
4141
m_imm, m_mutbl, mac_, mac_aq, mac_ellipsis, mac_invoc,
4242
mac_invoc_tt, mac_var, matcher, match_nonterminal, match_seq,
43-
match_tok, method, mode, mt, mul, mutability, named_field, neg,
44-
noreturn, not, pat, pat_box, pat_enum, pat_ident, pat_lit,
45-
pat_range, pat_rec, pat_struct, pat_tup, pat_uniq, pat_wild,
46-
path, private, proto, proto_bare, proto_block, proto_box,
47-
proto_uniq, provided, public, pure_fn, purity, re_anon, re_named,
48-
region, rem, required, ret_style, return_val, self_ty, shl, shr,
49-
stmt, stmt_decl, stmt_expr, stmt_semi, struct_def, struct_field,
50-
struct_variant_kind, subtract, sty_box, sty_by_ref, sty_region,
51-
sty_static, sty_uniq, sty_value, token_tree, trait_method,
52-
trait_ref, tt_delim, tt_seq, tt_tok, tt_nonterminal, ty, ty_,
53-
ty_bot, ty_box, ty_field, ty_fn, ty_infer, ty_mac, ty_method,
54-
ty_nil, ty_param, ty_param_bound, ty_path, ty_ptr, ty_rec,
55-
ty_rptr, ty_tup, ty_u32, ty_uniq, ty_vec, ty_fixed_length,
56-
tuple_variant_kind, unchecked_blk, uniq, unnamed_field,
57-
unsafe_blk, unsafe_fn, variant, view_item, view_item_,
58-
view_item_export, view_item_import, view_item_use, view_path,
59-
view_path_glob, view_path_list, view_path_simple, visibility,
60-
vstore, vstore_box, vstore_fixed, vstore_slice, vstore_uniq};
43+
match_tok, method, mode, module_ns, mt, mul, mutability,
44+
named_field, neg, noreturn, not, pat, pat_box, pat_enum,
45+
pat_ident, pat_lit, pat_range, pat_rec, pat_struct, pat_tup,
46+
pat_uniq, pat_wild, path, private, proto, proto_bare,
47+
proto_block, proto_box, proto_uniq, provided, public, pure_fn,
48+
purity, re_anon, re_named, region, rem, required, ret_style,
49+
return_val, self_ty, shl, shr, stmt, stmt_decl, stmt_expr,
50+
stmt_semi, struct_def, struct_field, struct_variant_kind,
51+
subtract, sty_box, sty_by_ref, sty_region, sty_static, sty_uniq,
52+
sty_value, token_tree, trait_method, trait_ref, tt_delim, tt_seq,
53+
tt_tok, tt_nonterminal, tuple_variant_kind, ty, ty_, ty_bot,
54+
ty_box, ty_field, ty_fn, ty_infer, ty_mac, ty_method, ty_nil,
55+
ty_param, ty_param_bound, ty_path, ty_ptr, ty_rec, ty_rptr,
56+
ty_tup, ty_u32, ty_uniq, ty_vec, ty_fixed_length, type_value_ns,
57+
unchecked_blk, uniq, unnamed_field, unsafe_blk, unsafe_fn,
58+
variant, view_item, view_item_, view_item_export,
59+
view_item_import, view_item_use, view_path, view_path_glob,
60+
view_path_list, view_path_simple, visibility, vstore, vstore_box,
61+
vstore_fixed, vstore_slice, vstore_uniq};
6162

6263
export file_type;
6364
export parser;
@@ -3336,6 +3337,14 @@ struct parser {
33363337

33373338
fn parse_view_path() -> @view_path {
33383339
let lo = self.span.lo;
3340+
3341+
let namespace;
3342+
if self.eat_keyword(~"mod") {
3343+
namespace = module_ns;
3344+
} else {
3345+
namespace = type_value_ns;
3346+
}
3347+
33393348
let first_ident = self.parse_ident();
33403349
let mut path = ~[first_ident];
33413350
debug!("parsed view_path: %s", *self.id_to_str(first_ident));
@@ -3352,7 +3361,8 @@ struct parser {
33523361
let path = @{span: mk_sp(lo, self.span.hi), global: false,
33533362
idents: path, rp: None, types: ~[]};
33543363
return @spanned(lo, self.span.hi,
3355-
view_path_simple(first_ident, path, self.get_id()));
3364+
view_path_simple(first_ident, path, namespace,
3365+
self.get_id()));
33563366
}
33573367

33583368
token::MOD_SEP => {
@@ -3400,7 +3410,7 @@ struct parser {
34003410
let path = @{span: mk_sp(lo, self.span.hi), global: false,
34013411
idents: path, rp: None, types: ~[]};
34023412
return @spanned(lo, self.span.hi,
3403-
view_path_simple(last, path, self.get_id()));
3413+
view_path_simple(last, path, namespace, self.get_id()));
34043414
}
34053415

34063416
fn parse_view_paths() -> ~[@view_path] {

src/libsyntax/print/pprust.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,11 @@ fn print_item(s: ps, &&item: @ast::item) {
468468
ast::item_foreign_mod(nmod) => {
469469
head(s, ~"extern");
470470
match nmod.sort {
471-
ast::named => {
472-
word_nbsp(s, ~"mod");
473-
print_ident(s, item.ident)
474-
}
475-
ast::anonymous => {}
471+
ast::named => {
472+
word_nbsp(s, ~"mod");
473+
print_ident(s, item.ident);
474+
}
475+
ast::anonymous => {}
476476
}
477477
nbsp(s);
478478
bopen(s);
@@ -1687,7 +1687,10 @@ fn print_meta_item(s: ps, &&item: @ast::meta_item) {
16871687
16881688
fn print_view_path(s: ps, &&vp: @ast::view_path) {
16891689
match vp.node {
1690-
ast::view_path_simple(ident, path, _) => {
1690+
ast::view_path_simple(ident, path, namespace, _) => {
1691+
if namespace == ast::module_ns {
1692+
word_space(s, ~"mod");
1693+
}
16911694
if path.idents[vec::len(path.idents)-1u] != ident {
16921695
print_ident(s, ident);
16931696
space(s.s);

0 commit comments

Comments
 (0)