Skip to content

Commit 8e74842

Browse files
committed
resolve: Remove visibility hacks for enum variants and trait items
Special treatment like this was necessary before `pub(restricted)` had been implemented and only two visibilities existed - `pub` and non-`pub`. Now it's no longer necessary and the desired behavior follows from `pub(restricted)`-style visibilities naturally assigned to enum variants and trait items.
1 parent b4b6b62 commit 8e74842

8 files changed

+80
-137
lines changed

Diff for: compiler/rustc_resolve/src/build_reduced_graph.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1398,10 +1398,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
13981398
let parent = self.parent_scope.module;
13991399
let expansion = self.parent_scope.expansion;
14001400
let res = Res::Def(def_kind, def_id);
1401-
// FIXME: For historical reasons the binding visibility is set to public,
1402-
// use actual visibility here instead, using enum variants as an example.
1403-
let vis_hack = ty::Visibility::Public;
1404-
self.r.define(parent, item.ident, ns, (res, vis_hack, item.span, expansion));
1401+
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
14051402
}
14061403

14071404
visit::walk_assoc_item(self, item, ctxt);

Diff for: compiler/rustc_resolve/src/imports.rs

+7-74
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ use rustc_errors::{pluralize, struct_span_err, Applicability};
1818
use rustc_hir::def::{self, PartialRes};
1919
use rustc_hir::def_id::DefId;
2020
use rustc_middle::hir::exports::Export;
21+
use rustc_middle::span_bug;
2122
use rustc_middle::ty;
22-
use rustc_middle::{bug, span_bug};
2323
use rustc_session::lint::builtin::{PUB_USE_OF_PRIVATE_EXTERN_CRATE, UNUSED_IMPORTS};
2424
use rustc_session::lint::BuiltinLintDiagnostics;
25-
use rustc_session::DiagnosticMessageId;
2625
use rustc_span::hygiene::ExpnId;
2726
use rustc_span::lev_distance::find_best_match_for_name;
2827
use rustc_span::symbol::{kw, Ident, Symbol};
@@ -456,13 +455,13 @@ impl<'a> Resolver<'a> {
456455
binding: &'a NameBinding<'a>,
457456
import: &'a Import<'a>,
458457
) -> &'a NameBinding<'a> {
459-
let vis = if binding.pseudo_vis().is_at_least(import.vis.get(), self) ||
458+
let vis = if binding.vis.is_at_least(import.vis.get(), self) ||
460459
// cf. `PUB_USE_OF_PRIVATE_EXTERN_CRATE`
461460
!import.is_glob() && binding.is_extern_crate()
462461
{
463462
import.vis.get()
464463
} else {
465-
binding.pseudo_vis()
464+
binding.vis
466465
};
467466

468467
if let ImportKind::Glob { ref max_vis, .. } = import.kind {
@@ -1178,7 +1177,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
11781177
self.r.per_ns(|this, ns| {
11791178
if let Ok(binding) = source_bindings[ns].get() {
11801179
let vis = import.vis.get();
1181-
if !binding.pseudo_vis().is_at_least(vis, &*this) {
1180+
if !binding.vis.is_at_least(vis, &*this) {
11821181
reexport_error = Some((ns, binding));
11831182
} else {
11841183
any_successful_reexport = true;
@@ -1362,7 +1361,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
13621361
Some(None) => import.parent_scope.module,
13631362
None => continue,
13641363
};
1365-
if self.r.is_accessible_from(binding.pseudo_vis(), scope) {
1364+
if self.r.is_accessible_from(binding.vis, scope) {
13661365
let imported_binding = self.r.import(binding, import);
13671366
let _ = self.r.try_define(import.parent_scope.module, key, imported_binding);
13681367
}
@@ -1380,9 +1379,8 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
13801379

13811380
let mut reexports = Vec::new();
13821381

1383-
module.for_each_child(self.r, |this, ident, ns, binding| {
1384-
// Filter away ambiguous imports and anything that has def-site
1385-
// hygiene.
1382+
module.for_each_child(self.r, |this, ident, _, binding| {
1383+
// Filter away ambiguous imports and anything that has def-site hygiene.
13861384
// FIXME: Implement actual cross-crate hygiene.
13871385
let is_good_import =
13881386
binding.is_import() && !binding.is_ambiguity() && !ident.span.from_expansion();
@@ -1392,71 +1390,6 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
13921390
reexports.push(Export { ident, res, span: binding.span, vis: binding.vis });
13931391
}
13941392
}
1395-
1396-
if let NameBindingKind::Import { binding: orig_binding, import, .. } = binding.kind {
1397-
if ns == TypeNS
1398-
&& orig_binding.is_variant()
1399-
&& !orig_binding.vis.is_at_least(binding.vis, &*this)
1400-
{
1401-
let msg = match import.kind {
1402-
ImportKind::Single { .. } => {
1403-
format!("variant `{}` is private and cannot be re-exported", ident)
1404-
}
1405-
ImportKind::Glob { .. } => {
1406-
let msg = "enum is private and its variants \
1407-
cannot be re-exported"
1408-
.to_owned();
1409-
let error_id = (
1410-
DiagnosticMessageId::ErrorId(0), // no code?!
1411-
Some(binding.span),
1412-
msg.clone(),
1413-
);
1414-
let fresh =
1415-
this.session.one_time_diagnostics.borrow_mut().insert(error_id);
1416-
if !fresh {
1417-
return;
1418-
}
1419-
msg
1420-
}
1421-
ref s => bug!("unexpected import kind {:?}", s),
1422-
};
1423-
let mut err = this.session.struct_span_err(binding.span, &msg);
1424-
1425-
let imported_module = match import.imported_module.get() {
1426-
Some(ModuleOrUniformRoot::Module(module)) => module,
1427-
_ => bug!("module should exist"),
1428-
};
1429-
let parent_module = imported_module.parent.expect("parent should exist");
1430-
let resolutions = this.resolutions(parent_module).borrow();
1431-
let enum_path_segment_index = import.module_path.len() - 1;
1432-
let enum_ident = import.module_path[enum_path_segment_index].ident;
1433-
1434-
let key = this.new_key(enum_ident, TypeNS);
1435-
let enum_resolution = resolutions.get(&key).expect("resolution should exist");
1436-
let enum_span =
1437-
enum_resolution.borrow().binding.expect("binding should exist").span;
1438-
let enum_def_span = this.session.source_map().guess_head_span(enum_span);
1439-
let enum_def_snippet = this
1440-
.session
1441-
.source_map()
1442-
.span_to_snippet(enum_def_span)
1443-
.expect("snippet should exist");
1444-
// potentially need to strip extant `crate`/`pub(path)` for suggestion
1445-
let after_vis_index = enum_def_snippet
1446-
.find("enum")
1447-
.expect("`enum` keyword should exist in snippet");
1448-
let suggestion = format!("pub {}", &enum_def_snippet[after_vis_index..]);
1449-
1450-
this.session.diag_span_suggestion_once(
1451-
&mut err,
1452-
DiagnosticMessageId::ErrorId(0),
1453-
enum_def_span,
1454-
"consider making the enum public",
1455-
suggestion,
1456-
);
1457-
err.emit();
1458-
}
1459-
}
14601393
});
14611394

14621395
if !reexports.is_empty() {

Diff for: compiler/rustc_resolve/src/lib.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -750,27 +750,12 @@ impl<'a> NameBinding<'a> {
750750
fn is_possibly_imported_variant(&self) -> bool {
751751
match self.kind {
752752
NameBindingKind::Import { binding, .. } => binding.is_possibly_imported_variant(),
753-
_ => self.is_variant(),
754-
}
755-
}
756-
757-
// We sometimes need to treat variants as `pub` for backwards compatibility.
758-
fn pseudo_vis(&self) -> ty::Visibility {
759-
if self.is_variant() && self.res().def_id().is_local() {
760-
ty::Visibility::Public
761-
} else {
762-
self.vis
763-
}
764-
}
765-
766-
fn is_variant(&self) -> bool {
767-
matches!(
768-
self.kind,
769753
NameBindingKind::Res(
770754
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Variant, ..), _),
771755
_,
772-
)
773-
)
756+
) => true,
757+
NameBindingKind::Res(..) | NameBindingKind::Module(..) => false,
758+
}
774759
}
775760

776761
fn is_extern_crate(&self) -> bool {

Diff for: src/test/ui/privacy/issue-46209-private-enum-variant-reexport.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#![feature(crate_visibility_modifier)]
22

3+
#[deny(unused_imports)]
34
mod rank {
45
pub use self::Professor::*;
5-
//~^ ERROR enum is private and its variants cannot be re-exported
6+
//~^ ERROR glob import doesn't reexport anything
67
pub use self::Lieutenant::{JuniorGrade, Full};
7-
//~^ ERROR variant `JuniorGrade` is private and cannot be re-exported
8-
//~| ERROR variant `Full` is private and cannot be re-exported
8+
//~^ ERROR `JuniorGrade` is private, and cannot be re-exported
9+
//~| ERROR `Full` is private, and cannot be re-exported
910
pub use self::PettyOfficer::*;
10-
//~^ ERROR enum is private and its variants cannot be re-exported
11+
//~^ ERROR glob import doesn't reexport anything
1112
pub use self::Crewman::*;
12-
//~^ ERROR enum is private and its variants cannot be re-exported
13+
//~^ ERROR glob import doesn't reexport anything
1314

1415
enum Professor {
1516
Adjunct,
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,51 @@
1-
error: variant `JuniorGrade` is private and cannot be re-exported
2-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:32
1+
error[E0364]: `JuniorGrade` is private, and cannot be re-exported
2+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:7:32
3+
|
4+
LL | pub use self::Lieutenant::{JuniorGrade, Full};
5+
| ^^^^^^^^^^^
6+
|
7+
note: consider marking `JuniorGrade` as `pub` in the imported module
8+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:7:32
39
|
410
LL | pub use self::Lieutenant::{JuniorGrade, Full};
511
| ^^^^^^^^^^^
6-
...
7-
LL | enum Lieutenant {
8-
| --------------- help: consider making the enum public: `pub enum Lieutenant`
912

10-
error: variant `Full` is private and cannot be re-exported
11-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:45
13+
error[E0364]: `Full` is private, and cannot be re-exported
14+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:7:45
15+
|
16+
LL | pub use self::Lieutenant::{JuniorGrade, Full};
17+
| ^^^^
18+
|
19+
note: consider marking `Full` as `pub` in the imported module
20+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:7:45
1221
|
1322
LL | pub use self::Lieutenant::{JuniorGrade, Full};
1423
| ^^^^
1524

16-
error: enum is private and its variants cannot be re-exported
17-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:4:13
25+
error: glob import doesn't reexport anything because no candidate is public enough
26+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5:13
1827
|
1928
LL | pub use self::Professor::*;
2029
| ^^^^^^^^^^^^^^^^^^
21-
...
22-
LL | enum Professor {
23-
| -------------- help: consider making the enum public: `pub enum Professor`
30+
|
31+
note: the lint level is defined here
32+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:3:8
33+
|
34+
LL | #[deny(unused_imports)]
35+
| ^^^^^^^^^^^^^^
2436

25-
error: enum is private and its variants cannot be re-exported
26-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:9:13
37+
error: glob import doesn't reexport anything because no candidate is public enough
38+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13
2739
|
2840
LL | pub use self::PettyOfficer::*;
2941
| ^^^^^^^^^^^^^^^^^^^^^
30-
...
31-
LL | pub(in rank) enum PettyOfficer {
32-
| ------------------------------ help: consider making the enum public: `pub enum PettyOfficer`
3342

34-
error: enum is private and its variants cannot be re-exported
35-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:11:13
43+
error: glob import doesn't reexport anything because no candidate is public enough
44+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:12:13
3645
|
3746
LL | pub use self::Crewman::*;
3847
| ^^^^^^^^^^^^^^^^
39-
...
40-
LL | crate enum Crewman {
41-
| ------------------ help: consider making the enum public: `pub enum Crewman`
4248

4349
error: aborting due to 5 previous errors
4450

51+
For more information about this error, try `rustc --explain E0364`.

Diff for: src/test/ui/privacy/private-variant-reexport.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
mod m1 {
2-
pub use ::E::V; //~ ERROR variant `V` is private and cannot be re-exported
2+
pub use ::E::V; //~ ERROR `V` is private, and cannot be re-exported
33
}
44

55
mod m2 {
6-
pub use ::E::{V}; //~ ERROR variant `V` is private and cannot be re-exported
6+
pub use ::E::{V}; //~ ERROR `V` is private, and cannot be re-exported
77
}
88

99
mod m3 {
10-
pub use ::E::V::{self}; //~ ERROR variant `V` is private and cannot be re-exported
10+
pub use ::E::V::{self}; //~ ERROR `V` is private, and cannot be re-exported
1111
}
1212

13+
#[deny(unused_imports)]
1314
mod m4 {
14-
pub use ::E::*; //~ ERROR enum is private and its variants cannot be re-exported
15+
pub use ::E::*; //~ ERROR glob import doesn't reexport anything
1516
}
1617

1718
enum E { V }

Diff for: src/test/ui/privacy/private-variant-reexport.stderr

+28-9
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,48 @@
1-
error: variant `V` is private and cannot be re-exported
1+
error[E0364]: `V` is private, and cannot be re-exported
2+
--> $DIR/private-variant-reexport.rs:2:13
3+
|
4+
LL | pub use ::E::V;
5+
| ^^^^^^
6+
|
7+
note: consider marking `V` as `pub` in the imported module
28
--> $DIR/private-variant-reexport.rs:2:13
39
|
410
LL | pub use ::E::V;
511
| ^^^^^^
6-
...
7-
LL | enum E { V }
8-
| ------ help: consider making the enum public: `pub enum E`
912

10-
error: variant `V` is private and cannot be re-exported
13+
error[E0364]: `V` is private, and cannot be re-exported
14+
--> $DIR/private-variant-reexport.rs:6:19
15+
|
16+
LL | pub use ::E::{V};
17+
| ^
18+
|
19+
note: consider marking `V` as `pub` in the imported module
1120
--> $DIR/private-variant-reexport.rs:6:19
1221
|
1322
LL | pub use ::E::{V};
1423
| ^
1524

16-
error: variant `V` is private and cannot be re-exported
25+
error[E0365]: `V` is private, and cannot be re-exported
1726
--> $DIR/private-variant-reexport.rs:10:22
1827
|
1928
LL | pub use ::E::V::{self};
20-
| ^^^^
29+
| ^^^^ re-export of private `V`
30+
|
31+
= note: consider declaring type or module `V` with `pub`
2132

22-
error: enum is private and its variants cannot be re-exported
23-
--> $DIR/private-variant-reexport.rs:14:13
33+
error: glob import doesn't reexport anything because no candidate is public enough
34+
--> $DIR/private-variant-reexport.rs:15:13
2435
|
2536
LL | pub use ::E::*;
2637
| ^^^^^^
38+
|
39+
note: the lint level is defined here
40+
--> $DIR/private-variant-reexport.rs:13:8
41+
|
42+
LL | #[deny(unused_imports)]
43+
| ^^^^^^^^^^^^^^
2744

2845
error: aborting due to 4 previous errors
2946

47+
Some errors have detailed explanations: E0364, E0365.
48+
For more information about an error, try `rustc --explain E0364`.

Diff for: src/test/ui/variants/variant-namespacing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// aux-build:variant-namespacing.rs
22

3-
enum E {
3+
pub enum E {
44
Struct { a: u8 },
55
Tuple(u8),
66
Unit,

0 commit comments

Comments
 (0)