diff options
| author | Camelid <camelidcamel@gmail.com> | 2020-12-11 19:52:51 -0800 |
|---|---|---|
| committer | Camelid <camelidcamel@gmail.com> | 2020-12-12 14:04:59 -0800 |
| commit | 5ce3f4c16636b261a8ce9ddfbbb30896338b9e37 (patch) | |
| tree | 4a1180a9dc6d4cc71023ded7ade3c256be1f19c8 /compiler/rustc_resolve/src | |
| parent | 2225ee1b62ff089917434aefd9b2bf509cfa087f (diff) | |
| download | rust-5ce3f4c16636b261a8ce9ddfbbb30896338b9e37.tar.gz rust-5ce3f4c16636b261a8ce9ddfbbb30896338b9e37.zip | |
Resolve enum field visibility correctly
Previously, this code treated enum fields' visibility as if they were struct fields. However, that's not correct because the visibility of a struct field with `ast::VisibilityKind::Inherited` is private to the module it's defined in, whereas the visibility of an *enum* field with `ast::VisibilityKind::Inherited` is the visibility of the enum it belongs to.
Diffstat (limited to 'compiler/rustc_resolve/src')
| -rw-r--r-- | compiler/rustc_resolve/src/build_reduced_graph.rs | 11 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/lib.rs | 1 |
2 files changed, 11 insertions, 1 deletions
diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 6d7e4ebc253..06e9969697d 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -258,7 +258,16 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { Ok(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))) } ast::VisibilityKind::Inherited => { - Ok(ty::Visibility::Restricted(parent_scope.module.normal_ancestor_id)) + if matches!(self.parent_scope.module.kind, ModuleKind::Def(DefKind::Enum, _, _)) { + // Any inherited visibility resolved directly inside an enum + // (e.g. variants or fields) inherits from the visibility of the enum. + let parent_enum = self.parent_scope.module.def_id().unwrap().expect_local(); + Ok(self.r.visibilities[&parent_enum]) + } else { + // If it's not in an enum, its visibility is restricted to the `mod` item + // that it's defined in. + Ok(ty::Visibility::Restricted(self.parent_scope.module.normal_ancestor_id)) + } } ast::VisibilityKind::Restricted { ref path, id, .. } => { // For visibilities we are not ready to provide correct implementation of "uniform diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index e8a06265ada..f764fbc3f8d 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -403,6 +403,7 @@ enum PathResult<'a> { }, } +#[derive(Debug)] enum ModuleKind { /// An anonymous module; e.g., just a block. /// |
