Auto merge of #79956 - camelid:variant-field-vis, r=petrochenkov
Resolve enum field visibility correctly Fixes #79593. 🎉 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.
This commit is contained in:
commit
d149b6579f
5 changed files with 75 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -403,6 +403,7 @@ enum PathResult<'a> {
|
|||
},
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ModuleKind {
|
||||
/// An anonymous module; e.g., just a block.
|
||||
///
|
||||
|
|
|
@ -1248,7 +1248,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
if no_accessible_remaining_fields {
|
||||
self.report_no_accessible_fields(adt_ty, span);
|
||||
} else {
|
||||
self.report_missing_field(adt_ty, span, remaining_fields);
|
||||
self.report_missing_fields(adt_ty, span, remaining_fields);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1279,7 +1279,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
///
|
||||
/// error: aborting due to previous error
|
||||
/// ```
|
||||
fn report_missing_field(
|
||||
fn report_missing_fields(
|
||||
&self,
|
||||
adt_ty: Ty<'tcx>,
|
||||
span: Span,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue