Skip to content

Commit b4b6b62

Browse files
committed
resolve: Cleanup visibility resolution in enums and traits
1 parent 07194ff commit b4b6b62

File tree

1 file changed

+42
-63
lines changed

1 file changed

+42
-63
lines changed

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

+42-63
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,16 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
258258
Ok(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX)))
259259
}
260260
ast::VisibilityKind::Inherited => {
261-
if matches!(self.parent_scope.module.kind, ModuleKind::Def(DefKind::Enum, _, _)) {
262-
// Any inherited visibility resolved directly inside an enum
263-
// (e.g. variants or fields) inherits from the visibility of the enum.
264-
let parent_enum = self.parent_scope.module.def_id().unwrap().expect_local();
265-
Ok(self.r.visibilities[&parent_enum])
266-
} else {
267-
// If it's not in an enum, its visibility is restricted to the `mod` item
268-
// that it's defined in.
269-
Ok(ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod))
270-
}
261+
Ok(match self.parent_scope.module.kind {
262+
// Any inherited visibility resolved directly inside an enum or trait
263+
// (i.e. variants, fields, and trait items) inherits from the visibility
264+
// of the enum or trait.
265+
ModuleKind::Def(DefKind::Enum | DefKind::Trait, def_id, _) => {
266+
self.r.visibilities[&def_id.expect_local()]
267+
}
268+
// Otherwise, the visibility is restricted to the nearest parent `mod` item.
269+
_ => ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod),
270+
})
271271
}
272272
ast::VisibilityKind::Restricted { ref path, id, .. } => {
273273
// For visibilities we are not ready to provide correct implementation of "uniform
@@ -1365,58 +1365,43 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
13651365
return;
13661366
}
13671367

1368+
let vis = self.resolve_visibility(&item.vis);
13681369
let local_def_id = self.r.local_def_id(item.id);
13691370
let def_id = local_def_id.to_def_id();
1370-
let vis = match ctxt {
1371-
AssocCtxt::Trait => {
1372-
let (def_kind, ns) = match item.kind {
1373-
AssocItemKind::Const(..) => (DefKind::AssocConst, ValueNS),
1374-
AssocItemKind::Fn(box FnKind(_, ref sig, _, _)) => {
1375-
if sig.decl.has_self() {
1376-
self.r.has_self.insert(def_id);
1377-
}
1378-
(DefKind::AssocFn, ValueNS)
1379-
}
1380-
AssocItemKind::TyAlias(..) => (DefKind::AssocTy, TypeNS),
1381-
AssocItemKind::MacCall(_) => bug!(), // handled above
1382-
};
13831371

1384-
let parent = self.parent_scope.module;
1385-
let expansion = self.parent_scope.expansion;
1386-
let res = Res::Def(def_kind, def_id);
1387-
// Trait item visibility is inherited from its trait when not specified explicitly.
1388-
let vis = match &item.vis.kind {
1389-
ast::VisibilityKind::Inherited => {
1390-
self.r.visibilities[&parent.def_id().unwrap().expect_local()]
1372+
if !(ctxt == AssocCtxt::Impl
1373+
&& matches!(item.vis.kind, ast::VisibilityKind::Inherited)
1374+
&& self
1375+
.r
1376+
.trait_impl_items
1377+
.contains(&ty::DefIdTree::parent(&*self.r, def_id).unwrap().expect_local()))
1378+
{
1379+
// Trait impl item visibility is inherited from its trait when not specified
1380+
// explicitly. In that case we cannot determine it here in early resolve,
1381+
// so we leave a hole in the visibility table to be filled later.
1382+
self.r.visibilities.insert(local_def_id, vis);
1383+
}
1384+
1385+
if ctxt == AssocCtxt::Trait {
1386+
let (def_kind, ns) = match item.kind {
1387+
AssocItemKind::Const(..) => (DefKind::AssocConst, ValueNS),
1388+
AssocItemKind::Fn(box FnKind(_, ref sig, _, _)) => {
1389+
if sig.decl.has_self() {
1390+
self.r.has_self.insert(def_id);
13911391
}
1392-
_ => self.resolve_visibility(&item.vis),
1393-
};
1394-
// FIXME: For historical reasons the binding visibility is set to public,
1395-
// use actual visibility here instead, using enum variants as an example.
1396-
let vis_hack = ty::Visibility::Public;
1397-
self.r.define(parent, item.ident, ns, (res, vis_hack, item.span, expansion));
1398-
Some(vis)
1399-
}
1400-
AssocCtxt::Impl => {
1401-
// Trait impl item visibility is inherited from its trait when not specified
1402-
// explicitly. In that case we cannot determine it here in early resolve,
1403-
// so we leave a hole in the visibility table to be filled later.
1404-
// Inherent impl item visibility is never inherited from other items.
1405-
if matches!(item.vis.kind, ast::VisibilityKind::Inherited)
1406-
&& self
1407-
.r
1408-
.trait_impl_items
1409-
.contains(&ty::DefIdTree::parent(&*self.r, def_id).unwrap().expect_local())
1410-
{
1411-
None
1412-
} else {
1413-
Some(self.resolve_visibility(&item.vis))
1392+
(DefKind::AssocFn, ValueNS)
14141393
}
1415-
}
1416-
};
1394+
AssocItemKind::TyAlias(..) => (DefKind::AssocTy, TypeNS),
1395+
AssocItemKind::MacCall(_) => bug!(), // handled above
1396+
};
14171397

1418-
if let Some(vis) = vis {
1419-
self.r.visibilities.insert(local_def_id, vis);
1398+
let parent = self.parent_scope.module;
1399+
let expansion = self.parent_scope.expansion;
1400+
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));
14201405
}
14211406

14221407
visit::walk_assoc_item(self, item, ctxt);
@@ -1490,19 +1475,13 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
14901475
}
14911476

14921477
let parent = self.parent_scope.module;
1493-
let vis = match variant.vis.kind {
1494-
// Variant visibility is inherited from its enum when not specified explicitly.
1495-
ast::VisibilityKind::Inherited => {
1496-
self.r.visibilities[&parent.def_id().unwrap().expect_local()]
1497-
}
1498-
_ => self.resolve_visibility(&variant.vis),
1499-
};
15001478
let expn_id = self.parent_scope.expansion;
15011479
let ident = variant.ident;
15021480

15031481
// Define a name in the type namespace.
15041482
let def_id = self.r.local_def_id(variant.id);
15051483
let res = Res::Def(DefKind::Variant, def_id.to_def_id());
1484+
let vis = self.resolve_visibility(&variant.vis);
15061485
self.r.define(parent, ident, TypeNS, (res, vis, variant.span, expn_id));
15071486
self.r.visibilities.insert(def_id, vis);
15081487

0 commit comments

Comments
 (0)