Skip to content

Commit ff6b0aa

Browse files
committed
resolve: Fix variant namespacing
1 parent 340e7eb commit ff6b0aa

File tree

6 files changed

+85
-23
lines changed

6 files changed

+85
-23
lines changed

src/librustc_resolve/build_reduced_graph.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,9 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
510510
self.structs.insert(variant_def_id, Vec::new());
511511
}
512512

513+
// Variants are always treated as importable to allow them to be glob used.
514+
// All variants are defined in both type and value namespaces as future-proofing.
513515
let child = self.add_child(name, parent, ForbidDuplicateTypesAndValues, variant.span);
514-
// variants are always treated as importable to allow them to be glob
515-
// used
516516
child.define_value(Def::Variant(item_id, self.ast_map.local_def_id(variant.node.data.id())),
517517
variant.span,
518518
DefModifiers::PUBLIC | DefModifiers::IMPORTABLE | variant_modifiers);
@@ -618,15 +618,14 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
618618
Def::Variant(_, variant_id) => {
619619
debug!("(building reduced graph for external crate) building variant {}",
620620
final_ident);
621-
// variants are always treated as importable to allow them to be
622-
// glob used
621+
// Variants are always treated as importable to allow them to be glob used.
622+
// All variants are defined in both type and value namespaces as future-proofing.
623623
let modifiers = DefModifiers::PUBLIC | DefModifiers::IMPORTABLE;
624+
child_name_bindings.define_type(def, DUMMY_SP, modifiers);
625+
child_name_bindings.define_value(def, DUMMY_SP, modifiers);
624626
if self.session.cstore.variant_kind(variant_id) == Some(VariantKind::Struct) {
625-
child_name_bindings.define_type(def, DUMMY_SP, modifiers);
626627
// Not adding fields for variants as they are not accessed with a self receiver
627628
self.structs.insert(variant_id, Vec::new());
628-
} else {
629-
child_name_bindings.define_value(def, DUMMY_SP, modifiers);
630629
}
631630
}
632631
Def::Fn(..) |
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2015 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+
pub enum XE {
12+
XStruct { a: u8 },
13+
XTuple(u8),
14+
XUnit,
15+
}

src/test/compile-fail/empty-struct-braces-pat-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ fn main() {
3939
XEmpty1 => () // Not an error, `XEmpty1` is interpreted as a new binding
4040
}
4141
match xe3 {
42-
XE::XEmpty3 => () //~ ERROR no associated item named `XEmpty3` found for type
42+
XE::XEmpty3 => () //~ ERROR `XE::XEmpty3` does not name a tuple variant or a tuple struct
4343
}
4444
}

src/test/compile-fail/empty-struct-braces-pat-3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ fn main() {
3636
E::Empty3(..) => () //~ ERROR `E::Empty3` does not name a tuple variant or a tuple struct
3737
}
3838
match xe3 {
39-
XE::XEmpty3(..) => () //~ ERROR no associated item named `XEmpty3` found for type
39+
XE::XEmpty3(..) => () //~ ERROR `XE::XEmpty3` does not name a tuple variant or a tuple
4040
}
4141
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2015 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:variant-namespacing.rs
12+
13+
extern crate variant_namespacing;
14+
pub use variant_namespacing::XE::*;
15+
//~^ ERROR import `XStruct` conflicts with type in this module
16+
//~| ERROR import `XStruct` conflicts with value in this module
17+
//~| ERROR import `XTuple` conflicts with type in this module
18+
//~| ERROR import `XTuple` conflicts with value in this module
19+
//~| ERROR import `XUnit` conflicts with type in this module
20+
//~| ERROR import `XUnit` conflicts with value in this module
21+
pub use E::*;
22+
//~^ ERROR import `Struct` conflicts with type in this module
23+
//~| ERROR import `Struct` conflicts with value in this module
24+
//~| ERROR import `Tuple` conflicts with type in this module
25+
//~| ERROR import `Tuple` conflicts with value in this module
26+
//~| ERROR import `Unit` conflicts with type in this module
27+
//~| ERROR import `Unit` conflicts with value in this module
28+
29+
enum E {
30+
Struct { a: u8 },
31+
Tuple(u8),
32+
Unit,
33+
}
34+
35+
type Struct = u8;
36+
type Tuple = u8;
37+
type Unit = u8;
38+
type XStruct = u8;
39+
type XTuple = u8;
40+
type XUnit = u8;
41+
42+
const Struct: u8 = 0;
43+
const Tuple: u8 = 0;
44+
const Unit: u8 = 0;
45+
const XStruct: u8 = 0;
46+
const XTuple: u8 = 0;
47+
const XUnit: u8 = 0;
48+
49+
fn main() {}

src/test/run-pass/empty-struct-braces.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ fn xcrate() {
9595
let e2: XEmpty2 = XEmpty2 {};
9696
let e2: XEmpty2 = XEmpty2;
9797
let e3: XE = XE::XEmpty3 {};
98-
// FIXME: Commented out tests are waiting for PR 30882 (fixes for variant namespaces)
99-
// let e4: XE = XE::XEmpty4 {};
98+
let e4: XE = XE::XEmpty4 {};
10099
let e4: XE = XE::XEmpty4;
101100

102101
match e1 {
@@ -109,10 +108,10 @@ fn xcrate() {
109108
XE::XEmpty3 {} => {}
110109
_ => {}
111110
}
112-
// match e4 {
113-
// XE::XEmpty4 {} => {}
114-
// _ => {}
115-
// }
111+
match e4 {
112+
XE::XEmpty4 {} => {}
113+
_ => {}
114+
}
116115

117116
match e1 {
118117
XEmpty1 { .. } => {}
@@ -124,18 +123,18 @@ fn xcrate() {
124123
XE::XEmpty3 { .. } => {}
125124
_ => {}
126125
}
127-
// match e4 {
128-
// XE::XEmpty4 { .. } => {}
129-
// _ => {}
130-
// }
126+
match e4 {
127+
XE::XEmpty4 { .. } => {}
128+
_ => {}
129+
}
131130

132131
match e2 {
133132
XEmpty2 => {}
134133
}
135-
// match e4 {
136-
// XE::XEmpty4 => {}
137-
// _ => {}
138-
// }
134+
match e4 {
135+
XE::XEmpty4 => {}
136+
_ => {}
137+
}
139138

140139
let e11: XEmpty1 = XEmpty1 { ..e1 };
141140
let e22: XEmpty2 = XEmpty2 { ..e2 };

0 commit comments

Comments
 (0)