Skip to content

Commit d7ff7da

Browse files
committed
First stage of enum namespacing changes
1 parent 88b6e93 commit d7ff7da

17 files changed

+584
-94
lines changed

src/librustc/metadata/csearch.rs

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use metadata::common::*;
1616
use metadata::cstore;
1717
use metadata::decoder;
18+
use middle::def;
1819
use middle::lang_items;
1920
use middle::resolve;
2021
use middle::ty;
@@ -114,6 +115,12 @@ pub fn maybe_get_item_ast<'tcx>(tcx: &ty::ctxt<'tcx>, def: ast::DefId,
114115
decoder::maybe_get_item_ast(&*cdata, tcx, def.node, decode_inlined_item)
115116
}
116117

118+
pub fn get_enum_variant_defs(cstore: &cstore::CStore, enum_id: ast::DefId)
119+
-> Vec<(def::Def, ast::Name, ast::Visibility)> {
120+
let cdata = cstore.get_crate_data(enum_id.krate);
121+
decoder::get_enum_variant_defs(&*cstore.intr, &*cdata, enum_id.node)
122+
}
123+
117124
pub fn get_enum_variants(tcx: &ty::ctxt, def: ast::DefId)
118125
-> Vec<Rc<ty::VariantInfo>> {
119126
let cstore = &tcx.sess.cstore;

src/librustc/metadata/decoder.rs

+18
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,24 @@ pub fn maybe_get_item_ast<'tcx>(cdata: Cmd, tcx: &ty::ctxt<'tcx>, id: ast::NodeI
659659
}
660660
}
661661

662+
pub fn get_enum_variant_defs(intr: &IdentInterner,
663+
cdata: Cmd,
664+
id: ast::NodeId)
665+
-> Vec<(def::Def, ast::Name, ast::Visibility)> {
666+
let data = cdata.data();
667+
let items = reader::get_doc(rbml::Doc::new(data), tag_items);
668+
let item = find_item(id, items);
669+
enum_variant_ids(item, cdata).iter().map(|did| {
670+
let item = find_item(did.node, items);
671+
let name = item_name(intr, item);
672+
let visibility = item_visibility(item);
673+
match item_to_def_like(item, *did, cdata.cnum) {
674+
DlDef(def @ def::DefVariant(..)) => (def, name, visibility),
675+
_ => unreachable!()
676+
}
677+
}).collect()
678+
}
679+
662680
pub fn get_enum_variants(intr: Rc<IdentInterner>, cdata: Cmd, id: ast::NodeId,
663681
tcx: &ty::ctxt) -> Vec<Rc<ty::VariantInfo>> {
664682
let data = cdata.data();

src/librustc/middle/resolve.rs

+210-86
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
#![feature(globs, struct_variant)]
11+
12+
pub use Foo::*;
13+
14+
pub enum Foo {
15+
A,
16+
B(int),
17+
C { a: int },
18+
}
19+
20+
impl Foo {
21+
pub fn foo() {}
22+
}
23+
24+
pub mod nest {
25+
pub use self::Bar::*;
26+
27+
pub enum Bar {
28+
D,
29+
E(int),
30+
F { a: int },
31+
}
32+
33+
impl Bar {
34+
pub fn foo() {}
35+
}
36+
}
37+
38+
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
#![feature(struct_variant)]
11+
12+
pub enum Foo {
13+
A,
14+
B(int),
15+
C { a: int },
16+
}
17+
18+
impl Foo {
19+
pub fn foo() {}
20+
pub fn bar(&self) {}
21+
}
22+

src/test/auxiliary/use_from_trait_xc.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
pub use self::sub::Bar;
11+
pub use self::sub::{Bar, Baz};
1212

1313
pub trait Trait {
1414
fn foo();
@@ -26,4 +26,10 @@ mod sub {
2626
impl Bar {
2727
pub fn new() {}
2828
}
29+
30+
pub enum Baz {}
31+
32+
impl Baz {
33+
pub fn new() {}
34+
}
2935
}

src/test/compile-fail/enum-and-module-in-same-scope.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod Foo {
1313
}
1414

1515
enum Foo { //~ ERROR duplicate definition of type or module `Foo`
16-
X
16+
X //~ ERROR duplicate definition of value `X`
1717
}
1818

1919
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:namespaced_enums.rs
12+
#![feature(struct_variant, globs)]
13+
14+
extern crate namespaced_enums;
15+
16+
mod m {
17+
pub use namespaced_enums::Foo::*;
18+
}
19+
20+
pub fn main() {
21+
use namespaced_enums::Foo::*;
22+
23+
foo(); //~ ERROR unresolved name `foo`
24+
m::foo(); //~ ERROR unresolved name `m::foo`
25+
bar(); //~ ERROR unresolved name `bar`
26+
m::bar(); //~ ERROR unresolved name `m::bar`
27+
}
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
#![feature(struct_variant, globs)]
11+
12+
mod m2 {
13+
pub enum Foo {
14+
A,
15+
B(int),
16+
C { a: int },
17+
}
18+
19+
impl Foo {
20+
pub fn foo() {}
21+
pub fn bar(&self) {}
22+
}
23+
}
24+
25+
mod m {
26+
pub use m2::Foo::*;
27+
}
28+
29+
pub fn main() {
30+
use m2::Foo::*;
31+
32+
foo(); //~ ERROR unresolved name `foo`
33+
m::foo(); //~ ERROR unresolved name `m::foo`
34+
bar(); //~ ERROR unresolved name `bar`
35+
m::bar(); //~ ERROR unresolved name `m::bar`
36+
}

src/test/compile-fail/use-from-trait-xc.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
extern crate use_from_trait_xc;
1414

1515
use use_from_trait_xc::Trait::foo;
16-
//~^ ERROR unresolved import `use_from_trait_xc::Trait::foo`. Cannot import from a trait or type imp
16+
//~^ ERROR `foo` is not directly importable
1717

1818
use use_from_trait_xc::Foo::new;
19-
//~^ ERROR unresolved import `use_from_trait_xc::Foo::new`. Cannot import from a trait or type imple
19+
//~^ ERROR `new` is not directly importable
2020

21-
use use_from_trait_xc::Bar::new;
22-
//~^ ERROR unresolved import `use_from_trait_xc::Bar::new`. Cannot import from a trait or type
21+
use use_from_trait_xc::Bar::new as bnew;
22+
//~^ ERROR `bnew` is not directly importable
23+
24+
use use_from_trait_xc::Baz::new as baznew;
25+
//~^ ERROR `baznew` is not directly importable
2326

2427
fn main() {}

src/test/compile-fail/use-from-trait.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
// except according to those terms.
1010

1111
use Trait::foo;
12-
//~^ ERROR unresolved import `Trait::foo`. Cannot import from a trait or type implementation
12+
//~^ ERROR `foo` is not directly importable
1313
use Foo::new;
14-
//~^ ERROR unresolved import `Foo::new`. Cannot import from a trait or type implementation
14+
//~^ ERROR `new` is not directly importable
1515

1616
pub trait Trait {
1717
fn foo();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:namespaced_enum_emulate_flat.rs
12+
#![feature(struct_variant)]
13+
14+
extern crate namespaced_enum_emulate_flat;
15+
16+
use namespaced_enum_emulate_flat::{Foo, A, B, C};
17+
use namespaced_enum_emulate_flat::nest::{Bar, D, E, F};
18+
19+
fn _f(f: Foo) {
20+
match f {
21+
A | B(_) | C { .. } => {}
22+
}
23+
}
24+
25+
fn _f2(f: Bar) {
26+
match f {
27+
D | E(_) | F { .. } => {}
28+
}
29+
}
30+
31+
pub fn main() {}
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
#![feature(globs, struct_variant)]
11+
12+
pub use Foo::*;
13+
use nest::{Bar, D, E, F};
14+
15+
pub enum Foo {
16+
A,
17+
B(int),
18+
C { a: int },
19+
}
20+
21+
impl Foo {
22+
pub fn foo() {}
23+
}
24+
25+
fn _f(f: Foo) {
26+
match f {
27+
A | B(_) | C { .. } => {}
28+
}
29+
}
30+
31+
mod nest {
32+
pub use self::Bar::*;
33+
34+
pub enum Bar {
35+
D,
36+
E(int),
37+
F { a: int },
38+
}
39+
40+
impl Bar {
41+
pub fn foo() {}
42+
}
43+
}
44+
45+
fn _f2(f: Bar) {
46+
match f {
47+
D | E(_) | F { .. } => {}
48+
}
49+
}
50+
51+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:namespaced_enums.rs
12+
#![feature(globs, struct_variant)]
13+
14+
extern crate namespaced_enums;
15+
16+
fn _f(f: namespaced_enums::Foo) {
17+
use namespaced_enums::Foo::*;
18+
19+
match f {
20+
A | B(_) | C { .. } => {}
21+
}
22+
}
23+
24+
mod m {
25+
pub use namespaced_enums::Foo::*;
26+
}
27+
28+
fn _f2(f: namespaced_enums::Foo) {
29+
match f {
30+
m::A | m::B(_) | m::C { .. } => {}
31+
}
32+
}
33+
34+
pub fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
#![feature(globs, struct_variant)]
11+
12+
mod m2 {
13+
pub enum Foo {
14+
A,
15+
B(int),
16+
C { a: int },
17+
}
18+
19+
impl Foo {
20+
pub fn foo() {}
21+
}
22+
}
23+
24+
mod m {
25+
pub use m2::Foo::*;
26+
}
27+
28+
fn _f(f: m2::Foo) {
29+
use m2::Foo::*;
30+
31+
match f {
32+
A | B(_) | C { .. } => {}
33+
}
34+
}
35+
36+
fn _f2(f: m2::Foo) {
37+
match f {
38+
m::A | m::B(_) | m::C { .. } => {}
39+
}
40+
}
41+
42+
pub fn main() {}

0 commit comments

Comments
 (0)