Skip to content

Commit cc87941

Browse files
committed
Make variant flat reexports work
1 parent b052e23 commit cc87941

File tree

4 files changed

+138
-11
lines changed

4 files changed

+138
-11
lines changed

src/librustc/middle/resolve.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,9 @@ impl Module {
572572
bitflags! {
573573
#[deriving(Show)]
574574
flags DefModifiers: u8 {
575-
const PUBLIC = 0b0000_0001,
576-
const IMPORTABLE = 0b0000_0010,
575+
const PUBLIC = 0b0000_0001,
576+
const IMPORTABLE = 0b0000_0010,
577+
const ENUM_STAGING_HACK = 0b0000_0100,
577578
}
578579
}
579580

@@ -1316,14 +1317,14 @@ impl<'a> Resolver<'a> {
13161317
&**variant,
13171318
local_def(item.id),
13181319
ModuleReducedGraphParent(name_bindings.get_module()),
1319-
is_public);
1320+
modifiers);
13201321

13211322
// Temporary staging hack
13221323
self.build_reduced_graph_for_variant(
13231324
&**variant,
13241325
local_def(item.id),
13251326
parent.clone(),
1326-
is_public);
1327+
modifiers | ENUM_STAGING_HACK);
13271328
}
13281329
parent
13291330
}
@@ -1592,9 +1593,8 @@ impl<'a> Resolver<'a> {
15921593
variant: &Variant,
15931594
item_id: DefId,
15941595
parent: ReducedGraphParent,
1595-
is_public: bool) {
1596+
modifiers: DefModifiers) {
15961597
let name = variant.node.name.name;
1597-
let modifiers = IMPORTABLE | if is_public { PUBLIC } else { DefModifiers::empty() };
15981598
let is_exported = match variant.node.kind {
15991599
TupleVariantKind(_) => false,
16001600
StructVariantKind(_) => {
@@ -1881,7 +1881,11 @@ impl<'a> Resolver<'a> {
18811881
// definition.
18821882
let is_exported = is_public ||
18831883
self.external_exports.contains(&enum_did);
1884-
let modifiers = if is_exported { PUBLIC } else { DefModifiers::empty() } | IMPORTABLE;
1884+
let modifiers = IMPORTABLE | ENUM_STAGING_HACK | if is_exported {
1885+
PUBLIC
1886+
} else {
1887+
DefModifiers::empty()
1888+
};
18851889
if is_struct {
18861890
child_name_bindings.define_type(def, DUMMY_SP, modifiers);
18871891
// Not adding fields for variants as they are not accessed with a self receiver
@@ -3060,8 +3064,8 @@ impl<'a> Resolver<'a> {
30603064
match import_resolution.value_target {
30613065
Some(ref target) if !target.shadowable => {
30623066
match *name_bindings.value_def.borrow() {
3063-
None => {}
3064-
Some(ref value) => {
3067+
// We want to allow the "flat" def of enum variants to be shadowed
3068+
Some(ref value) if !value.modifiers.contains(ENUM_STAGING_HACK) => {
30653069
let msg = format!("import `{}` conflicts with value \
30663070
in this module",
30673071
token::get_name(name).get());
@@ -3075,6 +3079,7 @@ impl<'a> Resolver<'a> {
30753079
}
30763080
}
30773081
}
3082+
_ => {}
30783083
}
30793084
}
30803085
Some(_) | None => {}
@@ -3083,8 +3088,8 @@ impl<'a> Resolver<'a> {
30833088
match import_resolution.type_target {
30843089
Some(ref target) if !target.shadowable => {
30853090
match *name_bindings.type_def.borrow() {
3086-
None => {}
3087-
Some(ref ty) => {
3091+
// We want to allow the "flat" def of enum variants to be shadowed
3092+
Some(ref ty) if !ty.modifiers.contains(ENUM_STAGING_HACK) => {
30883093
match ty.module_def {
30893094
None => {
30903095
let msg = format!("import `{}` conflicts with type in \
@@ -3135,6 +3140,7 @@ impl<'a> Resolver<'a> {
31353140
}
31363141
}
31373142
}
3143+
_ => {}
31383144
}
31393145
}
31403146
Some(_) | None => {}
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+
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() {}

0 commit comments

Comments
 (0)