Skip to content

Commit 49efab8

Browse files
committed
rustc: Fix enum variant privacy across crates
The code in resolve erroneously assumed that private enums weren't visited, so the logic was adjusted to check to see if the enum definition itself was public. Closes #11680
1 parent cf6857b commit 49efab8

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

src/librustc/middle/resolve.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1620,18 +1620,20 @@ impl<'a> Resolver<'a> {
16201620

16211621
match def {
16221622
DefMod(_) | DefForeignMod(_) => {}
1623-
DefVariant(_, variant_id, is_struct) => {
1623+
DefVariant(enum_did, variant_id, is_struct) => {
16241624
debug!("(building reduced graph for external crate) building \
16251625
variant {}",
16261626
final_ident);
1627-
// We assume the parent is visible, or else we wouldn't have seen
1628-
// it. Also variants are public-by-default if the parent was also
1629-
// public.
1627+
// If this variant is public, then it was publicly reexported,
1628+
// otherwise we need to inherit the visibility of the enum
1629+
// definition.
1630+
let is_exported = is_public ||
1631+
self.external_exports.contains(&enum_did);
16301632
if is_struct {
1631-
child_name_bindings.define_type(def, DUMMY_SP, true);
1633+
child_name_bindings.define_type(def, DUMMY_SP, is_exported);
16321634
self.structs.insert(variant_id);
16331635
} else {
1634-
child_name_bindings.define_value(def, DUMMY_SP, true);
1636+
child_name_bindings.define_value(def, DUMMY_SP, is_exported);
16351637
}
16361638
}
16371639
DefFn(..) | DefStaticMethod(..) | DefStatic(..) => {

src/test/auxiliary/issue-11680.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
enum Foo {
12+
Bar(int)
13+
}
14+
15+
pub mod test {
16+
enum Foo {
17+
Bar(int)
18+
}
19+
}

src/test/compile-fail/issue-11680.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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:issue-11680.rs
12+
13+
extern crate other = "issue-11680";
14+
15+
fn main() {
16+
let _b = other::Bar(1);
17+
//~^ ERROR: variant `Bar` is private
18+
19+
let _b = other::test::Bar(1);
20+
//~^ ERROR: variant `Bar` is private
21+
}

0 commit comments

Comments
 (0)