Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First stage of implementation of namespaced enums #18481

Merged
merged 1 commit into from
Nov 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/librustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use metadata::common::*;
use metadata::cstore;
use metadata::decoder;
use middle::def;
use middle::lang_items;
use middle::resolve;
use middle::ty;
Expand Down Expand Up @@ -114,6 +115,12 @@ pub fn maybe_get_item_ast<'tcx>(tcx: &ty::ctxt<'tcx>, def: ast::DefId,
decoder::maybe_get_item_ast(&*cdata, tcx, def.node, decode_inlined_item)
}

pub fn get_enum_variant_defs(cstore: &cstore::CStore, enum_id: ast::DefId)
-> Vec<(def::Def, ast::Name, ast::Visibility)> {
let cdata = cstore.get_crate_data(enum_id.krate);
decoder::get_enum_variant_defs(&*cstore.intr, &*cdata, enum_id.node)
}

pub fn get_enum_variants(tcx: &ty::ctxt, def: ast::DefId)
-> Vec<Rc<ty::VariantInfo>> {
let cstore = &tcx.sess.cstore;
Expand Down
18 changes: 18 additions & 0 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,24 @@ pub fn maybe_get_item_ast<'tcx>(cdata: Cmd, tcx: &ty::ctxt<'tcx>, id: ast::NodeI
}
}

pub fn get_enum_variant_defs(intr: &IdentInterner,
cdata: Cmd,
id: ast::NodeId)
-> Vec<(def::Def, ast::Name, ast::Visibility)> {
let data = cdata.data();
let items = reader::get_doc(rbml::Doc::new(data), tag_items);
let item = find_item(id, items);
enum_variant_ids(item, cdata).iter().map(|did| {
let item = find_item(did.node, items);
let name = item_name(intr, item);
let visibility = item_visibility(item);
match item_to_def_like(item, *did, cdata.cnum) {
DlDef(def @ def::DefVariant(..)) => (def, name, visibility),
_ => unreachable!()
}
}).collect()
}

pub fn get_enum_variants(intr: Rc<IdentInterner>, cdata: Cmd, id: ast::NodeId,
tcx: &ty::ctxt) -> Vec<Rc<ty::VariantInfo>> {
let data = cdata.data();
Expand Down
296 changes: 210 additions & 86 deletions src/librustc/middle/resolve.rs

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions src/test/auxiliary/namespaced_enum_emulate_flat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(globs, struct_variant)]

pub use Foo::*;

pub enum Foo {
A,
B(int),
C { a: int },
}

impl Foo {
pub fn foo() {}
}

pub mod nest {
pub use self::Bar::*;

pub enum Bar {
D,
E(int),
F { a: int },
}

impl Bar {
pub fn foo() {}
}
}


22 changes: 22 additions & 0 deletions src/test/auxiliary/namespaced_enums.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(struct_variant)]

pub enum Foo {
A,
B(int),
C { a: int },
}

impl Foo {
pub fn foo() {}
pub fn bar(&self) {}
}

8 changes: 7 additions & 1 deletion src/test/auxiliary/use_from_trait_xc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub use self::sub::Bar;
pub use self::sub::{Bar, Baz};

pub trait Trait {
fn foo();
Expand All @@ -26,4 +26,10 @@ mod sub {
impl Bar {
pub fn new() {}
}

pub enum Baz {}

impl Baz {
pub fn new() {}
}
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/enum-and-module-in-same-scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod Foo {
}

enum Foo { //~ ERROR duplicate definition of type or module `Foo`
X
X //~ ERROR duplicate definition of value `X`
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:namespaced_enums.rs
#![feature(struct_variant, globs)]

extern crate namespaced_enums;

mod m {
pub use namespaced_enums::Foo::*;
}

pub fn main() {
use namespaced_enums::Foo::*;

foo(); //~ ERROR unresolved name `foo`
m::foo(); //~ ERROR unresolved name `m::foo`
bar(); //~ ERROR unresolved name `bar`
m::bar(); //~ ERROR unresolved name `m::bar`
}

36 changes: 36 additions & 0 deletions src/test/compile-fail/namespaced-enum-glob-import-no-impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(struct_variant, globs)]

mod m2 {
pub enum Foo {
A,
B(int),
C { a: int },
}

impl Foo {
pub fn foo() {}
pub fn bar(&self) {}
}
}

mod m {
pub use m2::Foo::*;
}

pub fn main() {
use m2::Foo::*;

foo(); //~ ERROR unresolved name `foo`
m::foo(); //~ ERROR unresolved name `m::foo`
bar(); //~ ERROR unresolved name `bar`
m::bar(); //~ ERROR unresolved name `m::bar`
}
11 changes: 7 additions & 4 deletions src/test/compile-fail/use-from-trait-xc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
extern crate use_from_trait_xc;

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

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

use use_from_trait_xc::Bar::new;
//~^ ERROR unresolved import `use_from_trait_xc::Bar::new`. Cannot import from a trait or type
use use_from_trait_xc::Bar::new as bnew;
//~^ ERROR `bnew` is not directly importable

use use_from_trait_xc::Baz::new as baznew;
//~^ ERROR `baznew` is not directly importable

fn main() {}
4 changes: 2 additions & 2 deletions src/test/compile-fail/use-from-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
// except according to those terms.

use Trait::foo;
//~^ ERROR unresolved import `Trait::foo`. Cannot import from a trait or type implementation
//~^ ERROR `foo` is not directly importable
use Foo::new;
//~^ ERROR unresolved import `Foo::new`. Cannot import from a trait or type implementation
//~^ ERROR `new` is not directly importable

pub trait Trait {
fn foo();
Expand Down
32 changes: 32 additions & 0 deletions src/test/run-pass/namespaced-enum-emulate-flat-xc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:namespaced_enum_emulate_flat.rs
#![feature(struct_variant)]

extern crate namespaced_enum_emulate_flat;

use namespaced_enum_emulate_flat::{Foo, A, B, C};
use namespaced_enum_emulate_flat::nest::{Bar, D, E, F};

fn _f(f: Foo) {
match f {
A | B(_) | C { .. } => {}
}
}

fn _f2(f: Bar) {
match f {
D | E(_) | F { .. } => {}
}
}

pub fn main() {}

51 changes: 51 additions & 0 deletions src/test/run-pass/namespaced-enum-emulate-flat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(globs, struct_variant)]

pub use Foo::*;
use nest::{Bar, D, E, F};

pub enum Foo {
A,
B(int),
C { a: int },
}

impl Foo {
pub fn foo() {}
}

fn _f(f: Foo) {
match f {
A | B(_) | C { .. } => {}
}
}

mod nest {
pub use self::Bar::*;

pub enum Bar {
D,
E(int),
F { a: int },
}

impl Bar {
pub fn foo() {}
}
}

fn _f2(f: Bar) {
match f {
D | E(_) | F { .. } => {}
}
}

fn main() {}
34 changes: 34 additions & 0 deletions src/test/run-pass/namespaced-enum-glob-import-xcrate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:namespaced_enums.rs
#![feature(globs, struct_variant)]

extern crate namespaced_enums;

fn _f(f: namespaced_enums::Foo) {
use namespaced_enums::Foo::*;

match f {
A | B(_) | C { .. } => {}
}
}

mod m {
pub use namespaced_enums::Foo::*;
}

fn _f2(f: namespaced_enums::Foo) {
match f {
m::A | m::B(_) | m::C { .. } => {}
}
}

pub fn main() {}
42 changes: 42 additions & 0 deletions src/test/run-pass/namespaced-enum-glob-import.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(globs, struct_variant)]

mod m2 {
pub enum Foo {
A,
B(int),
C { a: int },
}

impl Foo {
pub fn foo() {}
}
}

mod m {
pub use m2::Foo::*;
}

fn _f(f: m2::Foo) {
use m2::Foo::*;

match f {
A | B(_) | C { .. } => {}
}
}

fn _f2(f: m2::Foo) {
match f {
m::A | m::B(_) | m::C { .. } => {}
}
}

pub fn main() {}
Loading