Auto merge of #78077 - petrochenkov:qvis, r=davidtwco

Calculate visibilities once in resolve

Then use them through a query based on resolver outputs.

Item visibilities were previously calculated in three places - initially in `rustc_resolve`, then in `rustc_privacy` during type privacy checkin, and then in `rustc_metadata` during metadata encoding.
The visibility logic is not entirely trivial, especially for things like constructors or enum variants, and all of it was duplicated.

This PR deduplicates all the visibility calculations, visibilities are determined once during early name resolution and then stored in `ResolverOutputs` and are later available through `tcx` as a query `tcx.visibility(def_id)`.
(This query existed previously, but only worked for other crates.)

Some special cases (e.g. visibilities for closure types, which are needed for type privacy checking) are not processed in resolve, but deferred and performed directly in the query instead.
This commit is contained in:
bors 2020-10-21 20:23:26 +00:00
commit 1eaadebb3d
26 changed files with 323 additions and 409 deletions

View File

@ -2381,15 +2381,6 @@ impl VisibilityKind<'_> {
VisibilityKind::Crate(..) | VisibilityKind::Restricted { .. } => true, VisibilityKind::Crate(..) | VisibilityKind::Restricted { .. } => true,
} }
} }
pub fn descr(&self) -> &'static str {
match *self {
VisibilityKind::Public => "public",
VisibilityKind::Inherited => "private",
VisibilityKind::Crate(..) => "crate-visible",
VisibilityKind::Restricted { .. } => "restricted",
}
}
} }
#[derive(Debug, HashStable_Generic)] #[derive(Debug, HashStable_Generic)]

View File

@ -28,7 +28,6 @@ use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
use rustc_serialize::{opaque, Encodable, Encoder}; use rustc_serialize::{opaque, Encodable, Encoder};
use rustc_session::config::CrateType; use rustc_session::config::CrateType;
use rustc_span::hygiene::{ExpnDataEncodeMode, HygieneEncodeContext}; use rustc_span::hygiene::{ExpnDataEncodeMode, HygieneEncodeContext};
use rustc_span::source_map::Spanned;
use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::{self, ExternalSource, FileName, SourceFile, Span, SyntaxContext}; use rustc_span::{self, ExternalSource, FileName, SourceFile, Span, SyntaxContext};
use rustc_target::abi::VariantIdx; use rustc_target::abi::VariantIdx;
@ -436,8 +435,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
fn encode_info_for_items(&mut self) { fn encode_info_for_items(&mut self) {
let krate = self.tcx.hir().krate(); let krate = self.tcx.hir().krate();
let vis = Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Public }; self.encode_info_for_mod(hir::CRATE_HIR_ID, &krate.item.module, &krate.item.attrs);
self.encode_info_for_mod(hir::CRATE_HIR_ID, &krate.item.module, &krate.item.attrs, &vis);
// Proc-macro crates only export proc-macro items, which are looked // Proc-macro crates only export proc-macro items, which are looked
// up using `proc_macro_data` // up using `proc_macro_data`
@ -739,12 +737,8 @@ impl EncodeContext<'a, 'tcx> {
is_non_exhaustive: variant.is_field_list_non_exhaustive(), is_non_exhaustive: variant.is_field_list_non_exhaustive(),
}; };
let enum_id = tcx.hir().local_def_id_to_hir_id(def.did.expect_local());
let enum_vis = &tcx.hir().expect_item(enum_id).vis;
record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data)));
record!(self.tables.visibility[def_id] <- record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
ty::Visibility::from_hir(enum_vis, enum_id, self.tcx));
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
record!(self.tables.attributes[def_id] <- &self.tcx.get_attrs(def_id)[..]); record!(self.tables.attributes[def_id] <- &self.tcx.get_attrs(def_id)[..]);
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
@ -785,17 +779,8 @@ impl EncodeContext<'a, 'tcx> {
is_non_exhaustive: variant.is_field_list_non_exhaustive(), is_non_exhaustive: variant.is_field_list_non_exhaustive(),
}; };
// Variant constructors have the same visibility as the parent enums, unless marked as
// non-exhaustive, in which case they are lowered to `pub(crate)`.
let enum_id = tcx.hir().local_def_id_to_hir_id(def.did.expect_local());
let enum_vis = &tcx.hir().expect_item(enum_id).vis;
let mut ctor_vis = ty::Visibility::from_hir(enum_vis, enum_id, tcx);
if variant.is_field_list_non_exhaustive() && ctor_vis == ty::Visibility::Public {
ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
}
record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data)));
record!(self.tables.visibility[def_id] <- ctor_vis); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
self.encode_stability(def_id); self.encode_stability(def_id);
self.encode_deprecation(def_id); self.encode_deprecation(def_id);
@ -811,13 +796,7 @@ impl EncodeContext<'a, 'tcx> {
self.encode_promoted_mir(def_id.expect_local()); self.encode_promoted_mir(def_id.expect_local());
} }
fn encode_info_for_mod( fn encode_info_for_mod(&mut self, id: hir::HirId, md: &hir::Mod<'_>, attrs: &[ast::Attribute]) {
&mut self,
id: hir::HirId,
md: &hir::Mod<'_>,
attrs: &[ast::Attribute],
vis: &hir::Visibility<'_>,
) {
let tcx = self.tcx; let tcx = self.tcx;
let local_def_id = tcx.hir().local_def_id(id); let local_def_id = tcx.hir().local_def_id(id);
let def_id = local_def_id.to_def_id(); let def_id = local_def_id.to_def_id();
@ -850,7 +829,7 @@ impl EncodeContext<'a, 'tcx> {
}; };
record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data))); record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data)));
record!(self.tables.visibility[def_id] <- ty::Visibility::from_hir(vis, id, self.tcx)); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
record!(self.tables.attributes[def_id] <- attrs); record!(self.tables.attributes[def_id] <- attrs);
if self.is_proc_macro { if self.is_proc_macro {
@ -881,7 +860,7 @@ impl EncodeContext<'a, 'tcx> {
let variant_data = tcx.hir().expect_variant_data(variant_id); let variant_data = tcx.hir().expect_variant_data(variant_id);
record!(self.tables.kind[def_id] <- EntryKind::Field); record!(self.tables.kind[def_id] <- EntryKind::Field);
record!(self.tables.visibility[def_id] <- field.vis); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
record!(self.tables.attributes[def_id] <- variant_data.fields()[field_index].attrs); record!(self.tables.attributes[def_id] <- variant_data.fields()[field_index].attrs);
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
@ -906,25 +885,8 @@ impl EncodeContext<'a, 'tcx> {
is_non_exhaustive: variant.is_field_list_non_exhaustive(), is_non_exhaustive: variant.is_field_list_non_exhaustive(),
}; };
let struct_id = tcx.hir().local_def_id_to_hir_id(adt_def.did.expect_local());
let struct_vis = &tcx.hir().expect_item(struct_id).vis;
let mut ctor_vis = ty::Visibility::from_hir(struct_vis, struct_id, tcx);
for field in &variant.fields {
if ctor_vis.is_at_least(field.vis, tcx) {
ctor_vis = field.vis;
}
}
// If the structure is marked as non_exhaustive then lower the visibility
// to within the crate.
if adt_def.non_enum_variant().is_field_list_non_exhaustive()
&& ctor_vis == ty::Visibility::Public
{
ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
}
record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data), adt_def.repr)); record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data), adt_def.repr));
record!(self.tables.visibility[def_id] <- ctor_vis); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
self.encode_stability(def_id); self.encode_stability(def_id);
@ -1030,7 +992,7 @@ impl EncodeContext<'a, 'tcx> {
EntryKind::AssocType(container) EntryKind::AssocType(container)
} }
}); });
record!(self.tables.visibility[def_id] <- trait_item.vis); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
record!(self.tables.span[def_id] <- ast_item.span); record!(self.tables.span[def_id] <- ast_item.span);
record!(self.tables.attributes[def_id] <- ast_item.attrs); record!(self.tables.attributes[def_id] <- ast_item.attrs);
self.encode_ident_span(def_id, ast_item.ident); self.encode_ident_span(def_id, ast_item.ident);
@ -1112,7 +1074,7 @@ impl EncodeContext<'a, 'tcx> {
} }
ty::AssocKind::Type => EntryKind::AssocType(container) ty::AssocKind::Type => EntryKind::AssocType(container)
}); });
record!(self.tables.visibility[def_id] <- impl_item.vis); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
record!(self.tables.span[def_id] <- ast_item.span); record!(self.tables.span[def_id] <- ast_item.span);
record!(self.tables.attributes[def_id] <- ast_item.attrs); record!(self.tables.attributes[def_id] <- ast_item.attrs);
self.encode_ident_span(def_id, impl_item.ident); self.encode_ident_span(def_id, impl_item.ident);
@ -1261,7 +1223,7 @@ impl EncodeContext<'a, 'tcx> {
EntryKind::Fn(self.lazy(data)) EntryKind::Fn(self.lazy(data))
} }
hir::ItemKind::Mod(ref m) => { hir::ItemKind::Mod(ref m) => {
return self.encode_info_for_mod(item.hir_id, m, &item.attrs, &item.vis); return self.encode_info_for_mod(item.hir_id, m, &item.attrs);
} }
hir::ItemKind::ForeignMod(_) => EntryKind::ForeignMod, hir::ItemKind::ForeignMod(_) => EntryKind::ForeignMod,
hir::ItemKind::GlobalAsm(..) => EntryKind::GlobalAsm, hir::ItemKind::GlobalAsm(..) => EntryKind::GlobalAsm,
@ -1352,8 +1314,7 @@ impl EncodeContext<'a, 'tcx> {
hir::ItemKind::ExternCrate(_) | hir::ItemKind::ExternCrate(_) |
hir::ItemKind::Use(..) => bug!("cannot encode info for item {:?}", item), hir::ItemKind::Use(..) => bug!("cannot encode info for item {:?}", item),
}); });
record!(self.tables.visibility[def_id] <- record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
ty::Visibility::from_hir(&item.vis, item.hir_id, tcx));
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
record!(self.tables.attributes[def_id] <- item.attrs); record!(self.tables.attributes[def_id] <- item.attrs);
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
@ -1470,7 +1431,7 @@ impl EncodeContext<'a, 'tcx> {
fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef<'_>) { fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef<'_>) {
let def_id = self.tcx.hir().local_def_id(macro_def.hir_id).to_def_id(); let def_id = self.tcx.hir().local_def_id(macro_def.hir_id).to_def_id();
record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone()))); record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone())));
record!(self.tables.visibility[def_id] <- ty::Visibility::Public); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
record!(self.tables.span[def_id] <- macro_def.span); record!(self.tables.span[def_id] <- macro_def.span);
record!(self.tables.attributes[def_id] <- macro_def.attrs); record!(self.tables.attributes[def_id] <- macro_def.attrs);
self.encode_ident_span(def_id, macro_def.ident); self.encode_ident_span(def_id, macro_def.ident);
@ -1480,7 +1441,6 @@ impl EncodeContext<'a, 'tcx> {
fn encode_info_for_generic_param(&mut self, def_id: DefId, kind: EntryKind, encode_type: bool) { fn encode_info_for_generic_param(&mut self, def_id: DefId, kind: EntryKind, encode_type: bool) {
record!(self.tables.kind[def_id] <- kind); record!(self.tables.kind[def_id] <- kind);
record!(self.tables.visibility[def_id] <- ty::Visibility::Public);
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
if encode_type { if encode_type {
self.encode_item_type(def_id); self.encode_item_type(def_id);
@ -1505,7 +1465,6 @@ impl EncodeContext<'a, 'tcx> {
_ => bug!("closure that is neither generator nor closure"), _ => bug!("closure that is neither generator nor closure"),
}); });
record!(self.tables.visibility[def_id.to_def_id()] <- ty::Visibility::Public);
record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id)); record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id));
record!(self.tables.attributes[def_id.to_def_id()] <- &self.tcx.get_attrs(def_id.to_def_id())[..]); record!(self.tables.attributes[def_id.to_def_id()] <- &self.tcx.get_attrs(def_id.to_def_id())[..]);
self.encode_item_type(def_id.to_def_id()); self.encode_item_type(def_id.to_def_id());
@ -1525,7 +1484,6 @@ impl EncodeContext<'a, 'tcx> {
let qualifs = self.tcx.mir_const_qualif(def_id); let qualifs = self.tcx.mir_const_qualif(def_id);
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(qualifs, const_data)); record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(qualifs, const_data));
record!(self.tables.visibility[def_id.to_def_id()] <- ty::Visibility::Public);
record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id)); record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id));
self.encode_item_type(def_id.to_def_id()); self.encode_item_type(def_id.to_def_id());
self.encode_generics(def_id.to_def_id()); self.encode_generics(def_id.to_def_id());
@ -1762,8 +1720,7 @@ impl EncodeContext<'a, 'tcx> {
hir::ForeignItemKind::Static(_, hir::Mutability::Not) => EntryKind::ForeignImmStatic, hir::ForeignItemKind::Static(_, hir::Mutability::Not) => EntryKind::ForeignImmStatic,
hir::ForeignItemKind::Type => EntryKind::ForeignType, hir::ForeignItemKind::Type => EntryKind::ForeignType,
}); });
record!(self.tables.visibility[def_id] <- record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
ty::Visibility::from_hir(&nitem.vis, nitem.hir_id, self.tcx));
record!(self.tables.span[def_id] <- nitem.span); record!(self.tables.span[def_id] <- nitem.span);
record!(self.tables.attributes[def_id] <- nitem.attrs); record!(self.tables.attributes[def_id] <- nitem.attrs);
self.encode_ident_span(def_id, nitem.ident); self.encode_ident_span(def_id, nitem.ident);

View File

@ -256,24 +256,12 @@ pub enum EvalResult {
} }
// See issue #38412. // See issue #38412.
fn skip_stability_check_due_to_privacy(tcx: TyCtxt<'_>, mut def_id: DefId) -> bool { fn skip_stability_check_due_to_privacy(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
// Check if `def_id` is a trait method. if tcx.def_kind(def_id) == DefKind::TyParam {
match tcx.def_kind(def_id) { // Have no visibility, considered public for the purpose of this check.
DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => { return false;
if let ty::TraitContainer(trait_def_id) = tcx.associated_item(def_id).container {
// Trait methods do not declare visibility (even
// for visibility info in cstore). Use containing
// trait instead, so methods of `pub` traits are
// themselves considered `pub`.
def_id = trait_def_id;
} }
} match tcx.visibility(def_id) {
_ => {}
}
let visibility = tcx.visibility(def_id);
match visibility {
// Must check stability for `pub` items. // Must check stability for `pub` items.
ty::Visibility::Public => false, ty::Visibility::Public => false,

View File

@ -1267,6 +1267,7 @@ rustc_queries! {
TypeChecking { TypeChecking {
query visibility(def_id: DefId) -> ty::Visibility { query visibility(def_id: DefId) -> ty::Visibility {
eval_always
desc { |tcx| "computing visibility of `{}`", tcx.def_path_str(def_id) } desc { |tcx| "computing visibility of `{}`", tcx.def_path_str(def_id) }
} }
} }

View File

@ -22,7 +22,7 @@ use crate::ty::{
ExistentialPredicate, FloatVar, FloatVid, GenericParamDefKind, InferConst, InferTy, IntVar, ExistentialPredicate, FloatVar, FloatVid, GenericParamDefKind, InferConst, InferTy, IntVar,
IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind, IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind,
ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar,
TyVid, TypeAndMut, TyVid, TypeAndMut, Visibility,
}; };
use rustc_ast as ast; use rustc_ast as ast;
use rustc_ast::expand::allocator::AllocatorKind; use rustc_ast::expand::allocator::AllocatorKind;
@ -911,6 +911,9 @@ pub struct GlobalCtxt<'tcx> {
/// Common consts, pre-interned for your convenience. /// Common consts, pre-interned for your convenience.
pub consts: CommonConsts<'tcx>, pub consts: CommonConsts<'tcx>,
/// Visibilities produced by resolver.
pub visibilities: FxHashMap<LocalDefId, Visibility>,
/// Resolutions of `extern crate` items produced by resolver. /// Resolutions of `extern crate` items produced by resolver.
extern_crate_map: FxHashMap<LocalDefId, CrateNum>, extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
@ -1124,6 +1127,7 @@ impl<'tcx> TyCtxt<'tcx> {
types: common_types, types: common_types,
lifetimes: common_lifetimes, lifetimes: common_lifetimes,
consts: common_consts, consts: common_consts,
visibilities: resolutions.visibilities,
extern_crate_map: resolutions.extern_crate_map, extern_crate_map: resolutions.extern_crate_map,
trait_map, trait_map,
export_map: resolutions.export_map, export_map: resolutions.export_map,

View File

@ -125,6 +125,7 @@ mod sty;
pub struct ResolverOutputs { pub struct ResolverOutputs {
pub definitions: rustc_hir::definitions::Definitions, pub definitions: rustc_hir::definitions::Definitions,
pub cstore: Box<CrateStoreDyn>, pub cstore: Box<CrateStoreDyn>,
pub visibilities: FxHashMap<LocalDefId, Visibility>,
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>, pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>, pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>, pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,

View File

@ -1,6 +1,7 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(in_band_lifetimes)] #![feature(in_band_lifetimes)]
#![feature(nll)] #![feature(nll)]
#![feature(or_patterns)]
#![recursion_limit = "256"] #![recursion_limit = "256"]
use rustc_attr as attr; use rustc_attr as attr;
@ -14,13 +15,14 @@ use rustc_hir::{AssocItemKind, HirIdSet, Node, PatKind};
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::hir::map::Map; use rustc_middle::hir::map::Map;
use rustc_middle::middle::privacy::{AccessLevel, AccessLevels}; use rustc_middle::middle::privacy::{AccessLevel, AccessLevels};
use rustc_middle::span_bug;
use rustc_middle::ty::fold::TypeVisitor; use rustc_middle::ty::fold::TypeVisitor;
use rustc_middle::ty::query::Providers; use rustc_middle::ty::query::Providers;
use rustc_middle::ty::subst::InternalSubsts; use rustc_middle::ty::subst::InternalSubsts;
use rustc_middle::ty::{self, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeFoldable}; use rustc_middle::ty::{self, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeFoldable};
use rustc_session::lint; use rustc_session::lint;
use rustc_span::hygiene::Transparency; use rustc_span::hygiene::Transparency;
use rustc_span::symbol::{kw, sym, Ident}; use rustc_span::symbol::{kw, Ident};
use rustc_span::Span; use rustc_span::Span;
use std::marker::PhantomData; use std::marker::PhantomData;
@ -233,125 +235,6 @@ where
} }
} }
fn def_id_visibility<'tcx>(
tcx: TyCtxt<'tcx>,
def_id: DefId,
) -> (ty::Visibility, Span, &'static str) {
match def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) {
Some(hir_id) => {
let vis = match tcx.hir().get(hir_id) {
Node::Item(item) => &item.vis,
Node::ForeignItem(foreign_item) => &foreign_item.vis,
Node::MacroDef(macro_def) => {
if tcx.sess.contains_name(&macro_def.attrs, sym::macro_export) {
return (ty::Visibility::Public, macro_def.span, "public");
} else {
&macro_def.vis
}
}
Node::TraitItem(..) | Node::Variant(..) => {
return def_id_visibility(tcx, tcx.hir().get_parent_did(hir_id).to_def_id());
}
Node::ImplItem(impl_item) => {
match tcx.hir().get(tcx.hir().get_parent_item(hir_id)) {
Node::Item(item) => match &item.kind {
hir::ItemKind::Impl { of_trait: None, .. } => &impl_item.vis,
hir::ItemKind::Impl { of_trait: Some(trait_ref), .. } => {
return def_id_visibility(tcx, trait_ref.path.res.def_id());
}
kind => bug!("unexpected item kind: {:?}", kind),
},
node => bug!("unexpected node kind: {:?}", node),
}
}
Node::Ctor(vdata) => {
let parent_hir_id = tcx.hir().get_parent_node(hir_id);
match tcx.hir().get(parent_hir_id) {
Node::Variant(..) => {
let parent_did = tcx.hir().local_def_id(parent_hir_id);
let (mut ctor_vis, mut span, mut descr) =
def_id_visibility(tcx, parent_did.to_def_id());
let adt_def = tcx.adt_def(tcx.hir().get_parent_did(hir_id).to_def_id());
let ctor_did = tcx.hir().local_def_id(vdata.ctor_hir_id().unwrap());
let variant = adt_def.variant_with_ctor_id(ctor_did.to_def_id());
if variant.is_field_list_non_exhaustive()
&& ctor_vis == ty::Visibility::Public
{
ctor_vis =
ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
let attrs = tcx.get_attrs(variant.def_id);
span = tcx
.sess
.find_by_name(&attrs, sym::non_exhaustive)
.unwrap()
.span;
descr = "crate-visible";
}
return (ctor_vis, span, descr);
}
Node::Item(..) => {
let item = match tcx.hir().get(parent_hir_id) {
Node::Item(item) => item,
node => bug!("unexpected node kind: {:?}", node),
};
let (mut ctor_vis, mut span, mut descr) = (
ty::Visibility::from_hir(&item.vis, parent_hir_id, tcx),
item.vis.span,
item.vis.node.descr(),
);
for field in vdata.fields() {
let field_vis = ty::Visibility::from_hir(&field.vis, hir_id, tcx);
if ctor_vis.is_at_least(field_vis, tcx) {
ctor_vis = field_vis;
span = field.vis.span;
descr = field.vis.node.descr();
}
}
// If the structure is marked as non_exhaustive then lower the
// visibility to within the crate.
if ctor_vis == ty::Visibility::Public {
let adt_def =
tcx.adt_def(tcx.hir().get_parent_did(hir_id).to_def_id());
if adt_def.non_enum_variant().is_field_list_non_exhaustive() {
ctor_vis =
ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
span = tcx
.sess
.find_by_name(&item.attrs, sym::non_exhaustive)
.unwrap()
.span;
descr = "crate-visible";
}
}
return (ctor_vis, span, descr);
}
node => bug!("unexpected node kind: {:?}", node),
}
}
Node::Expr(expr) => {
return (
ty::Visibility::Restricted(tcx.parent_module(expr.hir_id).to_def_id()),
expr.span,
"private",
);
}
node => bug!("unexpected node kind: {:?}", node),
};
(ty::Visibility::from_hir(vis, hir_id, tcx), vis.span, vis.node.descr())
}
None => {
let vis = tcx.visibility(def_id);
let descr = if vis == ty::Visibility::Public { "public" } else { "private" };
(vis, tcx.def_span(def_id), descr)
}
}
}
fn min(vis1: ty::Visibility, vis2: ty::Visibility, tcx: TyCtxt<'_>) -> ty::Visibility { fn min(vis1: ty::Visibility, vis2: ty::Visibility, tcx: TyCtxt<'_>) -> ty::Visibility {
if vis1.is_at_least(vis2, tcx) { vis2 } else { vis1 } if vis1.is_at_least(vis2, tcx) { vis2 } else { vis1 }
} }
@ -424,7 +307,7 @@ trait VisibilityLike: Sized {
impl VisibilityLike for ty::Visibility { impl VisibilityLike for ty::Visibility {
const MAX: Self = ty::Visibility::Public; const MAX: Self = ty::Visibility::Public;
fn new_min(find: &FindMin<'_, '_, Self>, def_id: DefId) -> Self { fn new_min(find: &FindMin<'_, '_, Self>, def_id: DefId) -> Self {
min(def_id_visibility(find.tcx, def_id).0, find.min, find.tcx) min(find.tcx.visibility(def_id), find.min, find.tcx)
} }
} }
impl VisibilityLike for Option<AccessLevel> { impl VisibilityLike for Option<AccessLevel> {
@ -534,17 +417,16 @@ impl EmbargoVisitor<'tcx> {
let hir_id = item_id.id; let hir_id = item_id.id;
let item_def_id = self.tcx.hir().local_def_id(hir_id); let item_def_id = self.tcx.hir().local_def_id(hir_id);
let def_kind = self.tcx.def_kind(item_def_id); let def_kind = self.tcx.def_kind(item_def_id);
let item = self.tcx.hir().expect_item(hir_id); let vis = self.tcx.visibility(item_def_id);
let vis = ty::Visibility::from_hir(&item.vis, hir_id, self.tcx);
self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod); self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod);
} }
if let Some(exports) = self.tcx.module_exports(module_def_id) { if let Some(exports) = self.tcx.module_exports(module_def_id) {
for export in exports { for export in exports {
if export.vis.is_accessible_from(defining_mod, self.tcx) { if export.vis.is_accessible_from(defining_mod, self.tcx) {
if let Res::Def(def_kind, def_id) = export.res { if let Res::Def(def_kind, def_id) = export.res {
let vis = def_id_visibility(self.tcx, def_id).0;
if let Some(def_id) = def_id.as_local() { if let Some(def_id) = def_id.as_local() {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
let vis = self.tcx.visibility(def_id.to_def_id());
self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod); self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod);
} }
} }
@ -596,7 +478,7 @@ impl EmbargoVisitor<'tcx> {
{ {
for field in struct_def.fields() { for field in struct_def.fields() {
let field_vis = let field_vis =
ty::Visibility::from_hir(&field.vis, field.hir_id, self.tcx); self.tcx.visibility(self.tcx.hir().local_def_id(field.hir_id));
if field_vis.is_accessible_from(module, self.tcx) { if field_vis.is_accessible_from(module, self.tcx) {
self.reach(field.hir_id, level).ty(); self.reach(field.hir_id, level).ty();
} }
@ -1015,11 +897,10 @@ impl DefIdVisitor<'tcx> for ReachEverythingInTheInterfaceVisitor<'_, 'tcx> {
} }
fn visit_def_id(&mut self, def_id: DefId, _kind: &str, _descr: &dyn fmt::Display) -> bool { fn visit_def_id(&mut self, def_id: DefId, _kind: &str, _descr: &dyn fmt::Display) -> bool {
if let Some(def_id) = def_id.as_local() { if let Some(def_id) = def_id.as_local() {
let hir_id = self.ev.tcx.hir().local_def_id_to_hir_id(def_id); if let (ty::Visibility::Public, _) | (_, Some(AccessLevel::ReachableFromImplTrait)) =
if let ((ty::Visibility::Public, ..), _) (self.tcx().visibility(def_id.to_def_id()), self.access_level)
| (_, Some(AccessLevel::ReachableFromImplTrait)) =
(def_id_visibility(self.tcx(), def_id.to_def_id()), self.access_level)
{ {
let hir_id = self.ev.tcx.hir().local_def_id_to_hir_id(def_id);
self.ev.update(hir_id, self.access_level); self.ev.update(hir_id, self.access_level);
} }
} }
@ -1184,9 +1065,7 @@ impl<'tcx> TypePrivacyVisitor<'tcx> {
} }
fn item_is_accessible(&self, did: DefId) -> bool { fn item_is_accessible(&self, did: DefId) -> bool {
def_id_visibility(self.tcx, did) self.tcx.visibility(did).is_accessible_from(self.current_item.to_def_id(), self.tcx)
.0
.is_accessible_from(self.current_item.to_def_id(), self.tcx)
} }
// Take node-id of an expression or pattern and check its type for privacy. // Take node-id of an expression or pattern and check its type for privacy.
@ -1840,8 +1719,21 @@ impl SearchInterfaceForPrivateItemsVisitor<'tcx> {
None => return false, None => return false,
}; };
let (vis, vis_span, vis_descr) = def_id_visibility(self.tcx, def_id); let vis = self.tcx.visibility(def_id);
if !vis.is_at_least(self.required_visibility, self.tcx) { if !vis.is_at_least(self.required_visibility, self.tcx) {
let vis_descr = match vis {
ty::Visibility::Public => "public",
ty::Visibility::Invisible => "private",
ty::Visibility::Restricted(vis_def_id) => {
if vis_def_id == self.tcx.parent_module(hir_id).to_def_id() {
"private"
} else if vis_def_id.is_top_level_module() {
"crate-private"
} else {
"restricted"
}
}
};
let make_msg = || format!("{} {} `{}` in public interface", vis_descr, kind, descr); let make_msg = || format!("{} {} `{}` in public interface", vis_descr, kind, descr);
if self.has_pub_restricted || self.has_old_errors || self.in_assoc_ty { if self.has_pub_restricted || self.has_old_errors || self.in_assoc_ty {
let mut err = if kind == "trait" { let mut err = if kind == "trait" {
@ -1849,6 +1741,8 @@ impl SearchInterfaceForPrivateItemsVisitor<'tcx> {
} else { } else {
struct_span_err!(self.tcx.sess, self.span, E0446, "{}", make_msg()) struct_span_err!(self.tcx.sess, self.span, E0446, "{}", make_msg())
}; };
let vis_span =
self.tcx.sess.source_map().guess_head_span(self.tcx.def_span(def_id));
err.span_label(self.span, format!("can't leak {} {}", vis_descr, kind)); err.span_label(self.span, format!("can't leak {} {}", vis_descr, kind));
err.span_label(vis_span, format!("`{}` declared as {}", descr, vis_descr)); err.span_label(vis_span, format!("`{}` declared as {}", descr, vis_descr));
err.emit(); err.emit();
@ -1965,7 +1859,7 @@ impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx>
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
let tcx = self.tcx; let tcx = self.tcx;
let item_visibility = ty::Visibility::from_hir(&item.vis, item.hir_id, tcx); let item_visibility = tcx.visibility(tcx.hir().local_def_id(item.hir_id).to_def_id());
match item.kind { match item.kind {
// Crates are always public. // Crates are always public.
@ -2019,7 +1913,7 @@ impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx>
// Subitems of foreign modules have their own publicity. // Subitems of foreign modules have their own publicity.
hir::ItemKind::ForeignMod(ref foreign_mod) => { hir::ItemKind::ForeignMod(ref foreign_mod) => {
for foreign_item in foreign_mod.items { for foreign_item in foreign_mod.items {
let vis = ty::Visibility::from_hir(&foreign_item.vis, item.hir_id, tcx); let vis = tcx.visibility(tcx.hir().local_def_id(foreign_item.hir_id));
self.check(foreign_item.hir_id, vis).generics().predicates().ty(); self.check(foreign_item.hir_id, vis).generics().predicates().ty();
} }
} }
@ -2028,7 +1922,7 @@ impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx>
self.check(item.hir_id, item_visibility).generics().predicates(); self.check(item.hir_id, item_visibility).generics().predicates();
for field in struct_def.fields() { for field in struct_def.fields() {
let field_visibility = ty::Visibility::from_hir(&field.vis, item.hir_id, tcx); let field_visibility = tcx.visibility(tcx.hir().local_def_id(field.hir_id));
self.check(field.hir_id, min(item_visibility, field_visibility, tcx)).ty(); self.check(field.hir_id, min(item_visibility, field_visibility, tcx)).ty();
} }
} }
@ -2040,10 +1934,9 @@ impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx>
let impl_vis = ty::Visibility::of_impl(item.hir_id, tcx, &Default::default()); let impl_vis = ty::Visibility::of_impl(item.hir_id, tcx, &Default::default());
self.check(item.hir_id, impl_vis).generics().predicates(); self.check(item.hir_id, impl_vis).generics().predicates();
for impl_item_ref in items { for impl_item_ref in items {
let impl_item = tcx.hir().impl_item(impl_item_ref.id);
let impl_item_vis = if of_trait.is_none() { let impl_item_vis = if of_trait.is_none() {
min( min(
ty::Visibility::from_hir(&impl_item.vis, item.hir_id, tcx), tcx.visibility(tcx.hir().local_def_id(impl_item_ref.id.hir_id)),
impl_vis, impl_vis,
tcx, tcx,
) )
@ -2064,6 +1957,7 @@ impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx>
pub fn provide(providers: &mut Providers) { pub fn provide(providers: &mut Providers) {
*providers = Providers { *providers = Providers {
visibility,
privacy_access_levels, privacy_access_levels,
check_private_in_public, check_private_in_public,
check_mod_privacy, check_mod_privacy,
@ -2071,6 +1965,55 @@ pub fn provide(providers: &mut Providers) {
}; };
} }
fn visibility(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Visibility {
let def_id = def_id.expect_local();
match tcx.visibilities.get(&def_id) {
Some(vis) => *vis,
None => {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
match tcx.hir().get(hir_id) {
// Unique types created for closures participate in type privacy checking.
// They have visibilities inherited from the module they are defined in.
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => {
ty::Visibility::Restricted(tcx.parent_module(hir_id).to_def_id())
}
// - AST lowering may clone `use` items and the clones don't
// get their entries in the resolver's visibility table.
// - AST lowering also creates opaque type items with inherited visibilies.
// Visibility on them should have no effect, but to avoid the visibility
// query failing on some items, we provide it for opaque types as well.
Node::Item(hir::Item {
vis,
kind: hir::ItemKind::Use(..) | hir::ItemKind::OpaqueTy(..),
..
}) => ty::Visibility::from_hir(vis, hir_id, tcx),
// Visibilities of trait impl items are inherited from their traits
// and are not filled in resolve.
Node::ImplItem(impl_item) => {
match tcx.hir().get(tcx.hir().get_parent_item(hir_id)) {
Node::Item(hir::Item {
kind: hir::ItemKind::Impl { of_trait: Some(tr), .. },
..
}) => tr.path.res.opt_def_id().map_or_else(
|| {
tcx.sess.delay_span_bug(tr.path.span, "trait without a def-id");
ty::Visibility::Public
},
|def_id| tcx.visibility(def_id),
),
_ => span_bug!(impl_item.span, "the parent is not a trait impl"),
}
}
_ => span_bug!(
tcx.def_span(def_id),
"visibility table unexpectedly missing a def-id: {:?}",
def_id,
),
}
}
}
}
fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: LocalDefId) { fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
// Check privacy of names not checked in previous compilation stages. // Check privacy of names not checked in previous compilation stages.
let mut visitor = NamePrivacyVisitor { tcx, maybe_typeck_results: None, current_item: None }; let mut visitor = NamePrivacyVisitor { tcx, maybe_typeck_results: None, current_item: None };

View File

@ -613,12 +613,21 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
/// Constructs the reduced graph for one item. /// Constructs the reduced graph for one item.
fn build_reduced_graph_for_item(&mut self, item: &'b Item) { fn build_reduced_graph_for_item(&mut self, item: &'b Item) {
if matches!(item.kind, ItemKind::Mod(..)) && item.ident.name == kw::Invalid {
// Fake crate root item from expand.
return;
}
let parent_scope = &self.parent_scope; let parent_scope = &self.parent_scope;
let parent = parent_scope.module; let parent = parent_scope.module;
let expansion = parent_scope.expansion; let expansion = parent_scope.expansion;
let ident = item.ident; let ident = item.ident;
let sp = item.span; let sp = item.span;
let vis = self.resolve_visibility(&item.vis); let vis = self.resolve_visibility(&item.vis);
let local_def_id = self.r.local_def_id(item.id);
let def_id = local_def_id.to_def_id();
self.r.visibilities.insert(local_def_id, vis);
match item.kind { match item.kind {
ItemKind::Use(ref use_tree) => { ItemKind::Use(ref use_tree) => {
@ -651,10 +660,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
} else if orig_name == Some(kw::SelfLower) { } else if orig_name == Some(kw::SelfLower) {
self.r.graph_root self.r.graph_root
} else { } else {
let def_id = self.r.local_def_id(item.id); let crate_id = self.r.crate_loader.process_extern_crate(
let crate_id = item,
self.r.crate_loader.process_extern_crate(item, &self.r.definitions, def_id); &self.r.definitions,
self.r.extern_crate_map.insert(def_id, crate_id); local_def_id,
);
self.r.extern_crate_map.insert(local_def_id, crate_id);
self.r.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX }) self.r.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX })
}; };
@ -705,25 +716,16 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.r.define(parent, ident, TypeNS, imported_binding); self.r.define(parent, ident, TypeNS, imported_binding);
} }
ItemKind::Mod(..) if ident.name == kw::Invalid => {} // Crate root
ItemKind::Mod(..) => { ItemKind::Mod(..) => {
let def_id = self.r.local_def_id(item.id); let module_kind = ModuleKind::Def(DefKind::Mod, def_id, ident.name);
let module_kind = ModuleKind::Def(DefKind::Mod, def_id.to_def_id(), ident.name);
let module = self.r.arenas.alloc_module(ModuleData { let module = self.r.arenas.alloc_module(ModuleData {
no_implicit_prelude: parent.no_implicit_prelude || { no_implicit_prelude: parent.no_implicit_prelude || {
self.r.session.contains_name(&item.attrs, sym::no_implicit_prelude) self.r.session.contains_name(&item.attrs, sym::no_implicit_prelude)
}, },
..ModuleData::new( ..ModuleData::new(Some(parent), module_kind, def_id, expansion, item.span)
Some(parent),
module_kind,
def_id.to_def_id(),
expansion,
item.span,
)
}); });
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion)); self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
self.r.module_map.insert(def_id, module); self.r.module_map.insert(local_def_id, module);
// Descend into the module. // Descend into the module.
self.parent_scope.module = module; self.parent_scope.module = module;
@ -731,15 +733,15 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
// These items live in the value namespace. // These items live in the value namespace.
ItemKind::Static(..) => { ItemKind::Static(..) => {
let res = Res::Def(DefKind::Static, self.r.local_def_id(item.id).to_def_id()); let res = Res::Def(DefKind::Static, def_id);
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion)); self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
} }
ItemKind::Const(..) => { ItemKind::Const(..) => {
let res = Res::Def(DefKind::Const, self.r.local_def_id(item.id).to_def_id()); let res = Res::Def(DefKind::Const, def_id);
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion)); self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
} }
ItemKind::Fn(..) => { ItemKind::Fn(..) => {
let res = Res::Def(DefKind::Fn, self.r.local_def_id(item.id).to_def_id()); let res = Res::Def(DefKind::Fn, def_id);
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion)); self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
// Functions introducing procedural macros reserve a slot // Functions introducing procedural macros reserve a slot
@ -749,13 +751,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
// These items live in the type namespace. // These items live in the type namespace.
ItemKind::TyAlias(..) => { ItemKind::TyAlias(..) => {
let res = Res::Def(DefKind::TyAlias, self.r.local_def_id(item.id).to_def_id()); let res = Res::Def(DefKind::TyAlias, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion)); self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
} }
ItemKind::Enum(_, _) => { ItemKind::Enum(_, _) => {
let def_id = self.r.local_def_id(item.id).to_def_id();
self.r.variant_vis.insert(def_id, vis);
let module_kind = ModuleKind::Def(DefKind::Enum, def_id, ident.name); let module_kind = ModuleKind::Def(DefKind::Enum, def_id, ident.name);
let module = self.r.new_module( let module = self.r.new_module(
parent, parent,
@ -769,14 +769,13 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
} }
ItemKind::TraitAlias(..) => { ItemKind::TraitAlias(..) => {
let res = Res::Def(DefKind::TraitAlias, self.r.local_def_id(item.id).to_def_id()); let res = Res::Def(DefKind::TraitAlias, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion)); self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
} }
// These items live in both the type and value namespaces. // These items live in both the type and value namespaces.
ItemKind::Struct(ref vdata, _) => { ItemKind::Struct(ref vdata, _) => {
// Define a name in the type namespace. // Define a name in the type namespace.
let def_id = self.r.local_def_id(item.id).to_def_id();
let res = Res::Def(DefKind::Struct, def_id); let res = Res::Def(DefKind::Struct, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion)); self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
@ -810,17 +809,19 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
} }
ret_fields.push(field_vis); ret_fields.push(field_vis);
} }
let ctor_def_id = self.r.local_def_id(ctor_node_id);
let ctor_res = Res::Def( let ctor_res = Res::Def(
DefKind::Ctor(CtorOf::Struct, CtorKind::from_ast(vdata)), DefKind::Ctor(CtorOf::Struct, CtorKind::from_ast(vdata)),
self.r.local_def_id(ctor_node_id).to_def_id(), ctor_def_id.to_def_id(),
); );
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion)); self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
self.r.visibilities.insert(ctor_def_id, ctor_vis);
self.r.struct_constructors.insert(def_id, (ctor_res, ctor_vis, ret_fields)); self.r.struct_constructors.insert(def_id, (ctor_res, ctor_vis, ret_fields));
} }
} }
ItemKind::Union(ref vdata, _) => { ItemKind::Union(ref vdata, _) => {
let def_id = self.r.local_def_id(item.id).to_def_id();
let res = Res::Def(DefKind::Union, def_id); let res = Res::Def(DefKind::Union, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion)); self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
@ -829,8 +830,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
} }
ItemKind::Trait(..) => { ItemKind::Trait(..) => {
let def_id = self.r.local_def_id(item.id).to_def_id();
// Add all the items within to a new module. // Add all the items within to a new module.
let module_kind = ModuleKind::Def(DefKind::Trait, def_id, ident.name); let module_kind = ModuleKind::Def(DefKind::Trait, def_id, ident.name);
let module = self.r.new_module( let module = self.r.new_module(
@ -845,6 +844,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
} }
// These items do not add names to modules. // These items do not add names to modules.
ItemKind::Impl { of_trait: Some(..), .. } => {
self.r.trait_impl_items.insert(local_def_id);
}
ItemKind::Impl { .. } | ItemKind::ForeignMod(..) | ItemKind::GlobalAsm(..) => {} ItemKind::Impl { .. } | ItemKind::ForeignMod(..) | ItemKind::GlobalAsm(..) => {}
ItemKind::MacroDef(..) | ItemKind::MacCall(_) => unreachable!(), ItemKind::MacroDef(..) | ItemKind::MacCall(_) => unreachable!(),
@ -853,22 +855,20 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
/// Constructs the reduced graph for one foreign item. /// Constructs the reduced graph for one foreign item.
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) { fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) {
let (res, ns) = match item.kind { let local_def_id = self.r.local_def_id(item.id);
ForeignItemKind::Fn(..) => { let def_id = local_def_id.to_def_id();
(Res::Def(DefKind::Fn, self.r.local_def_id(item.id).to_def_id()), ValueNS) let (def_kind, ns) = match item.kind {
} ForeignItemKind::Fn(..) => (DefKind::Fn, ValueNS),
ForeignItemKind::Static(..) => { ForeignItemKind::Static(..) => (DefKind::Static, ValueNS),
(Res::Def(DefKind::Static, self.r.local_def_id(item.id).to_def_id()), ValueNS) ForeignItemKind::TyAlias(..) => (DefKind::ForeignTy, TypeNS),
}
ForeignItemKind::TyAlias(..) => {
(Res::Def(DefKind::ForeignTy, self.r.local_def_id(item.id).to_def_id()), TypeNS)
}
ForeignItemKind::MacCall(_) => unreachable!(), ForeignItemKind::MacCall(_) => unreachable!(),
}; };
let parent = self.parent_scope.module; let parent = self.parent_scope.module;
let expansion = self.parent_scope.expansion; let expansion = self.parent_scope.expansion;
let vis = self.resolve_visibility(&item.vis); let vis = self.resolve_visibility(&item.vis);
let res = Res::Def(def_kind, def_id);
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion)); self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
self.r.visibilities.insert(local_def_id, vis);
} }
fn build_reduced_graph_for_block(&mut self, block: &Block) { fn build_reduced_graph_for_block(&mut self, block: &Block) {
@ -1205,6 +1205,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.r.check_reserved_macro_name(ident, res); self.r.check_reserved_macro_name(ident, res);
self.insert_unused_macro(ident, def_id, item.id, span); self.insert_unused_macro(ident, def_id, item.id, span);
} }
self.r.visibilities.insert(def_id, vis);
MacroRulesScope::Binding(self.r.arenas.alloc_macro_rules_binding(MacroRulesBinding { MacroRulesScope::Binding(self.r.arenas.alloc_macro_rules_binding(MacroRulesBinding {
parent_macro_rules_scope: parent_scope.macro_rules, parent_macro_rules_scope: parent_scope.macro_rules,
binding, binding,
@ -1224,6 +1225,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.insert_unused_macro(ident, def_id, item.id, span); self.insert_unused_macro(ident, def_id, item.id, span);
} }
self.r.define(module, ident, MacroNS, (res, vis, span, expansion)); self.r.define(module, ident, MacroNS, (res, vis, span, expansion));
self.r.visibilities.insert(def_id, vis);
self.parent_scope.macro_rules self.parent_scope.macro_rules
} }
} }
@ -1297,36 +1299,64 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
} }
fn visit_assoc_item(&mut self, item: &'b AssocItem, ctxt: AssocCtxt) { fn visit_assoc_item(&mut self, item: &'b AssocItem, ctxt: AssocCtxt) {
let parent = self.parent_scope.module;
if let AssocItemKind::MacCall(_) = item.kind { if let AssocItemKind::MacCall(_) = item.kind {
self.visit_invoc(item.id); self.visit_invoc(item.id);
return; return;
} }
if let AssocCtxt::Impl = ctxt { let local_def_id = self.r.local_def_id(item.id);
self.resolve_visibility(&item.vis); let def_id = local_def_id.to_def_id();
visit::walk_assoc_item(self, item, ctxt); let vis = match ctxt {
return; AssocCtxt::Trait => {
} let (def_kind, ns) = match item.kind {
AssocItemKind::Const(..) => (DefKind::AssocConst, ValueNS),
// Add the item to the trait info.
let item_def_id = self.r.local_def_id(item.id).to_def_id();
let (res, ns) = match item.kind {
AssocItemKind::Const(..) => (Res::Def(DefKind::AssocConst, item_def_id), ValueNS),
AssocItemKind::Fn(_, ref sig, _, _) => { AssocItemKind::Fn(_, ref sig, _, _) => {
if sig.decl.has_self() { if sig.decl.has_self() {
self.r.has_self.insert(item_def_id); self.r.has_self.insert(def_id);
} }
(Res::Def(DefKind::AssocFn, item_def_id), ValueNS) (DefKind::AssocFn, ValueNS)
} }
AssocItemKind::TyAlias(..) => (Res::Def(DefKind::AssocTy, item_def_id), TypeNS), AssocItemKind::TyAlias(..) => (DefKind::AssocTy, TypeNS),
AssocItemKind::MacCall(_) => bug!(), // handled above AssocItemKind::MacCall(_) => bug!(), // handled above
}; };
let vis = ty::Visibility::Public; let parent = self.parent_scope.module;
let expansion = self.parent_scope.expansion; let expansion = self.parent_scope.expansion;
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion)); let res = Res::Def(def_kind, def_id);
// Trait item visibility is inherited from its trait when not specified explicitly.
let vis = match &item.vis.kind {
ast::VisibilityKind::Inherited => {
self.r.visibilities[&parent.def_id().unwrap().expect_local()]
}
_ => self.resolve_visibility(&item.vis),
};
// FIXME: For historical reasons the binding visibility is set to public,
// use actual visibility here instead, using enum variants as an example.
let vis_hack = ty::Visibility::Public;
self.r.define(parent, item.ident, ns, (res, vis_hack, item.span, expansion));
Some(vis)
}
AssocCtxt::Impl => {
// Trait impl item visibility is inherited from its trait when not specified
// explicitly. In that case we cannot determine it here in early resolve,
// so we leave a hole in the visibility table to be filled later.
// Inherent impl item visibility is never inherited from other items.
if matches!(item.vis.kind, ast::VisibilityKind::Inherited)
&& self
.r
.trait_impl_items
.contains(&ty::DefIdTree::parent(&*self.r, def_id).unwrap().expect_local())
{
None
} else {
Some(self.resolve_visibility(&item.vis))
}
}
};
if let Some(vis) = vis {
self.r.visibilities.insert(local_def_id, vis);
}
visit::walk_assoc_item(self, item, ctxt); visit::walk_assoc_item(self, item, ctxt);
} }
@ -1394,7 +1424,8 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
if sf.is_placeholder { if sf.is_placeholder {
self.visit_invoc(sf.id); self.visit_invoc(sf.id);
} else { } else {
self.resolve_visibility(&sf.vis); let vis = self.resolve_visibility(&sf.vis);
self.r.visibilities.insert(self.r.local_def_id(sf.id), vis);
visit::walk_struct_field(self, sf); visit::walk_struct_field(self, sf);
} }
} }
@ -1408,22 +1439,30 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
} }
let parent = self.parent_scope.module; let parent = self.parent_scope.module;
let vis = self.r.variant_vis[&parent.def_id().expect("enum without def-id")]; let vis = match variant.vis.kind {
// Variant visibility is inherited from its enum when not specified explicitly.
ast::VisibilityKind::Inherited => {
self.r.visibilities[&parent.def_id().unwrap().expect_local()]
}
_ => self.resolve_visibility(&variant.vis),
};
let expn_id = self.parent_scope.expansion; let expn_id = self.parent_scope.expansion;
let ident = variant.ident; let ident = variant.ident;
// Define a name in the type namespace. // Define a name in the type namespace.
let def_id = self.r.local_def_id(variant.id).to_def_id(); let def_id = self.r.local_def_id(variant.id);
let res = Res::Def(DefKind::Variant, def_id); let res = Res::Def(DefKind::Variant, def_id.to_def_id());
self.r.define(parent, ident, TypeNS, (res, vis, variant.span, expn_id)); self.r.define(parent, ident, TypeNS, (res, vis, variant.span, expn_id));
self.r.visibilities.insert(def_id, vis);
// If the variant is marked as non_exhaustive then lower the visibility to within the // If the variant is marked as non_exhaustive then lower the visibility to within the crate.
// crate. let ctor_vis = if vis == ty::Visibility::Public
let mut ctor_vis = vis; && self.r.session.contains_name(&variant.attrs, sym::non_exhaustive)
let has_non_exhaustive = self.r.session.contains_name(&variant.attrs, sym::non_exhaustive); {
if has_non_exhaustive && vis == ty::Visibility::Public { ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))
ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX)); } else {
} vis
};
// Define a constructor name in the value namespace. // Define a constructor name in the value namespace.
// Braced variants, unlike structs, generate unusable names in // Braced variants, unlike structs, generate unusable names in
@ -1431,12 +1470,15 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
// It's ok to use the variant's id as a ctor id since an // It's ok to use the variant's id as a ctor id since an
// error will be reported on any use of such resolution anyway. // error will be reported on any use of such resolution anyway.
let ctor_node_id = variant.data.ctor_id().unwrap_or(variant.id); let ctor_node_id = variant.data.ctor_id().unwrap_or(variant.id);
let ctor_def_id = self.r.local_def_id(ctor_node_id).to_def_id(); let ctor_def_id = self.r.local_def_id(ctor_node_id);
let ctor_kind = CtorKind::from_ast(&variant.data); let ctor_kind = CtorKind::from_ast(&variant.data);
let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id); let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id.to_def_id());
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, variant.span, expn_id)); self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, variant.span, expn_id));
if ctor_def_id != def_id {
self.r.visibilities.insert(ctor_def_id, ctor_vis);
}
// Record field names for error reporting. // Record field names for error reporting.
self.insert_field_names_local(ctor_def_id, &variant.data); self.insert_field_names_local(ctor_def_id.to_def_id(), &variant.data);
visit::walk_variant(self, variant); visit::walk_variant(self, variant);
} }

View File

@ -76,6 +76,7 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
let def_data = match &i.kind { let def_data = match &i.kind {
ItemKind::Impl { .. } => DefPathData::Impl, ItemKind::Impl { .. } => DefPathData::Impl,
ItemKind::Mod(..) if i.ident.name == kw::Invalid => { ItemKind::Mod(..) if i.ident.name == kw::Invalid => {
// Fake crate root item from expand.
return visit::walk_item(self, i); return visit::walk_item(self, i);
} }
ItemKind::Mod(..) ItemKind::Mod(..)

View File

@ -944,7 +944,8 @@ pub struct Resolver<'a> {
/// Maps glob imports to the names of items actually imported. /// Maps glob imports to the names of items actually imported.
glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>, glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
/// Visibilities in "lowered" form, for all entities that have them.
visibilities: FxHashMap<LocalDefId, ty::Visibility>,
used_imports: FxHashSet<(NodeId, Namespace)>, used_imports: FxHashSet<(NodeId, Namespace)>,
maybe_unused_trait_imports: FxHashSet<LocalDefId>, maybe_unused_trait_imports: FxHashSet<LocalDefId>,
maybe_unused_extern_crates: Vec<(LocalDefId, Span)>, maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
@ -1008,10 +1009,6 @@ pub struct Resolver<'a> {
/// Features enabled for this crate. /// Features enabled for this crate.
active_features: FxHashSet<Symbol>, active_features: FxHashSet<Symbol>,
/// Stores enum visibilities to properly build a reduced graph
/// when visiting the correspondent variants.
variant_vis: DefIdMap<ty::Visibility>,
lint_buffer: LintBuffer, lint_buffer: LintBuffer,
next_node_id: NodeId, next_node_id: NodeId,
@ -1028,6 +1025,9 @@ pub struct Resolver<'a> {
invocation_parents: FxHashMap<ExpnId, LocalDefId>, invocation_parents: FxHashMap<ExpnId, LocalDefId>,
next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>, next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>,
/// Some way to know that we are in a *trait* impl in `visit_assoc_item`.
/// FIXME: Replace with a more general AST map (together with some other fields).
trait_impl_items: FxHashSet<LocalDefId>,
} }
/// Nothing really interesting here; it just provides memory for the rest of the crate. /// Nothing really interesting here; it just provides memory for the rest of the crate.
@ -1195,7 +1195,8 @@ impl<'a> Resolver<'a> {
metadata_loader: &'a MetadataLoaderDyn, metadata_loader: &'a MetadataLoaderDyn,
arenas: &'a ResolverArenas<'a>, arenas: &'a ResolverArenas<'a>,
) -> Resolver<'a> { ) -> Resolver<'a> {
let root_def_id = DefId::local(CRATE_DEF_INDEX); let root_local_def_id = LocalDefId { local_def_index: CRATE_DEF_INDEX };
let root_def_id = root_local_def_id.to_def_id();
let root_module_kind = ModuleKind::Def(DefKind::Mod, root_def_id, kw::Invalid); let root_module_kind = ModuleKind::Def(DefKind::Mod, root_def_id, kw::Invalid);
let graph_root = arenas.alloc_module(ModuleData { let graph_root = arenas.alloc_module(ModuleData {
no_implicit_prelude: session.contains_name(&krate.attrs, sym::no_implicit_prelude), no_implicit_prelude: session.contains_name(&krate.attrs, sym::no_implicit_prelude),
@ -1213,11 +1214,14 @@ impl<'a> Resolver<'a> {
) )
}); });
let mut module_map = FxHashMap::default(); let mut module_map = FxHashMap::default();
module_map.insert(LocalDefId { local_def_index: CRATE_DEF_INDEX }, graph_root); module_map.insert(root_local_def_id, graph_root);
let definitions = Definitions::new(crate_name, session.local_crate_disambiguator()); let definitions = Definitions::new(crate_name, session.local_crate_disambiguator());
let root = definitions.get_root_def(); let root = definitions.get_root_def();
let mut visibilities = FxHashMap::default();
visibilities.insert(root_local_def_id, ty::Visibility::Public);
let mut def_id_to_span = IndexVec::default(); let mut def_id_to_span = IndexVec::default();
assert_eq!(def_id_to_span.push(rustc_span::DUMMY_SP), root); assert_eq!(def_id_to_span.push(rustc_span::DUMMY_SP), root);
let mut def_id_to_node_id = IndexVec::default(); let mut def_id_to_node_id = IndexVec::default();
@ -1290,7 +1294,7 @@ impl<'a> Resolver<'a> {
ast_transform_scopes: FxHashMap::default(), ast_transform_scopes: FxHashMap::default(),
glob_map: Default::default(), glob_map: Default::default(),
visibilities,
used_imports: FxHashSet::default(), used_imports: FxHashSet::default(),
maybe_unused_trait_imports: Default::default(), maybe_unused_trait_imports: Default::default(),
maybe_unused_extern_crates: Vec::new(), maybe_unused_extern_crates: Vec::new(),
@ -1339,7 +1343,6 @@ impl<'a> Resolver<'a> {
.map(|(feat, ..)| *feat) .map(|(feat, ..)| *feat)
.chain(features.declared_lang_features.iter().map(|(feat, ..)| *feat)) .chain(features.declared_lang_features.iter().map(|(feat, ..)| *feat))
.collect(), .collect(),
variant_vis: Default::default(),
lint_buffer: LintBuffer::default(), lint_buffer: LintBuffer::default(),
next_node_id: NodeId::from_u32(1), next_node_id: NodeId::from_u32(1),
def_id_to_span, def_id_to_span,
@ -1348,6 +1351,7 @@ impl<'a> Resolver<'a> {
placeholder_field_indices: Default::default(), placeholder_field_indices: Default::default(),
invocation_parents, invocation_parents,
next_disambiguator: Default::default(), next_disambiguator: Default::default(),
trait_impl_items: Default::default(),
} }
} }
@ -1371,6 +1375,7 @@ impl<'a> Resolver<'a> {
pub fn into_outputs(self) -> ResolverOutputs { pub fn into_outputs(self) -> ResolverOutputs {
let definitions = self.definitions; let definitions = self.definitions;
let visibilities = self.visibilities;
let extern_crate_map = self.extern_crate_map; let extern_crate_map = self.extern_crate_map;
let export_map = self.export_map; let export_map = self.export_map;
let maybe_unused_trait_imports = self.maybe_unused_trait_imports; let maybe_unused_trait_imports = self.maybe_unused_trait_imports;
@ -1379,6 +1384,7 @@ impl<'a> Resolver<'a> {
ResolverOutputs { ResolverOutputs {
definitions: definitions, definitions: definitions,
cstore: Box::new(self.crate_loader.into_cstore()), cstore: Box::new(self.crate_loader.into_cstore()),
visibilities,
extern_crate_map, extern_crate_map,
export_map, export_map,
glob_map, glob_map,
@ -1396,6 +1402,7 @@ impl<'a> Resolver<'a> {
ResolverOutputs { ResolverOutputs {
definitions: self.definitions.clone(), definitions: self.definitions.clone(),
cstore: Box::new(self.cstore().clone()), cstore: Box::new(self.cstore().clone()),
visibilities: self.visibilities.clone(),
extern_crate_map: self.extern_crate_map.clone(), extern_crate_map: self.extern_crate_map.clone(),
export_map: self.export_map.clone(), export_map: self.export_map.clone(),
glob_map: self.glob_map.clone(), glob_map: self.glob_map.clone(),

View File

@ -80,7 +80,6 @@ fn sized_constraint_for_ty<'tcx>(
fn associated_item_from_trait_item_ref( fn associated_item_from_trait_item_ref(
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
parent_def_id: LocalDefId, parent_def_id: LocalDefId,
parent_vis: &hir::Visibility<'_>,
trait_item_ref: &hir::TraitItemRef, trait_item_ref: &hir::TraitItemRef,
) -> ty::AssocItem { ) -> ty::AssocItem {
let def_id = tcx.hir().local_def_id(trait_item_ref.id.hir_id); let def_id = tcx.hir().local_def_id(trait_item_ref.id.hir_id);
@ -93,8 +92,7 @@ fn associated_item_from_trait_item_ref(
ty::AssocItem { ty::AssocItem {
ident: trait_item_ref.ident, ident: trait_item_ref.ident,
kind, kind,
// Visibility of trait items is inherited from their traits. vis: tcx.visibility(def_id),
vis: ty::Visibility::from_hir(parent_vis, trait_item_ref.id.hir_id, tcx),
defaultness: trait_item_ref.defaultness, defaultness: trait_item_ref.defaultness,
def_id: def_id.to_def_id(), def_id: def_id.to_def_id(),
container: ty::TraitContainer(parent_def_id.to_def_id()), container: ty::TraitContainer(parent_def_id.to_def_id()),
@ -117,8 +115,7 @@ fn associated_item_from_impl_item_ref(
ty::AssocItem { ty::AssocItem {
ident: impl_item_ref.ident, ident: impl_item_ref.ident,
kind, kind,
// Visibility of trait impl items doesn't matter. vis: tcx.visibility(def_id),
vis: ty::Visibility::from_hir(&impl_item_ref.vis, impl_item_ref.id.hir_id, tcx),
defaultness: impl_item_ref.defaultness, defaultness: impl_item_ref.defaultness,
def_id: def_id.to_def_id(), def_id: def_id.to_def_id(),
container: ty::ImplContainer(parent_def_id.to_def_id()), container: ty::ImplContainer(parent_def_id.to_def_id()),
@ -143,12 +140,8 @@ fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem {
hir::ItemKind::Trait(.., ref trait_item_refs) => { hir::ItemKind::Trait(.., ref trait_item_refs) => {
if let Some(trait_item_ref) = trait_item_refs.iter().find(|i| i.id.hir_id == id) { if let Some(trait_item_ref) = trait_item_refs.iter().find(|i| i.id.hir_id == id) {
let assoc_item = associated_item_from_trait_item_ref( let assoc_item =
tcx, associated_item_from_trait_item_ref(tcx, parent_def_id, trait_item_ref);
parent_def_id,
&parent_item.vis,
trait_item_ref,
);
debug_assert_eq!(assoc_item.def_id, def_id); debug_assert_eq!(assoc_item.def_id, def_id);
return assoc_item; return assoc_item;
} }

View File

@ -851,7 +851,6 @@ fn convert_variant(
parent_did: LocalDefId, parent_did: LocalDefId,
) -> ty::VariantDef { ) -> ty::VariantDef {
let mut seen_fields: FxHashMap<Ident, Span> = Default::default(); let mut seen_fields: FxHashMap<Ident, Span> = Default::default();
let hir_id = tcx.hir().local_def_id_to_hir_id(variant_did.unwrap_or(parent_did));
let fields = def let fields = def
.fields() .fields()
.iter() .iter()
@ -868,11 +867,7 @@ fn convert_variant(
seen_fields.insert(f.ident.normalize_to_macros_2_0(), f.span); seen_fields.insert(f.ident.normalize_to_macros_2_0(), f.span);
} }
ty::FieldDef { ty::FieldDef { did: fid.to_def_id(), ident: f.ident, vis: tcx.visibility(fid) }
did: fid.to_def_id(),
ident: f.ident,
vis: ty::Visibility::from_hir(&f.vis, hir_id, tcx),
}
}) })
.collect(); .collect();
let recovered = match def { let recovered = match def {

View File

@ -1,18 +1,27 @@
error[E0445]: private trait `Foo` in public interface error[E0445]: private trait `Foo` in public interface
--> $DIR/E0445.rs:5:1 --> $DIR/E0445.rs:5:1
| |
LL | trait Foo {
| --------- `Foo` declared as private
...
LL | pub trait Bar : Foo {} LL | pub trait Bar : Foo {}
| ^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait | ^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
error[E0445]: private trait `Foo` in public interface error[E0445]: private trait `Foo` in public interface
--> $DIR/E0445.rs:7:1 --> $DIR/E0445.rs:7:1
| |
LL | trait Foo {
| --------- `Foo` declared as private
...
LL | pub struct Bar2<T: Foo>(pub T); LL | pub struct Bar2<T: Foo>(pub T);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
error[E0445]: private trait `Foo` in public interface error[E0445]: private trait `Foo` in public interface
--> $DIR/E0445.rs:9:1 --> $DIR/E0445.rs:9:1
| |
LL | trait Foo {
| --------- `Foo` declared as private
...
LL | pub fn foo<T: Foo> (t: T) {} LL | pub fn foo<T: Foo> (t: T) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait

View File

@ -2,7 +2,7 @@ error[E0446]: private type `Bar` in public interface
--> $DIR/E0446.rs:4:5 --> $DIR/E0446.rs:4:5
| |
LL | struct Bar(u32); LL | struct Bar(u32);
| - `Bar` declared as private | ---------------- `Bar` declared as private
LL | LL |
LL | pub fn bar() -> Bar { LL | pub fn bar() -> Bar {
| ^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^ can't leak private type

View File

@ -2,7 +2,7 @@ error[E0445]: private trait `Private<<Self as Public>::P, <Self as Public>::R>`
--> $DIR/issue-18389.rs:7:1 --> $DIR/issue-18389.rs:7:1
| |
LL | trait Private<P, R> { LL | trait Private<P, R> {
| - `Private<<Self as Public>::P, <Self as Public>::R>` declared as private | ------------------- `Private<<Self as Public>::P, <Self as Public>::R>` declared as private
... ...
LL | / pub trait Public: Private< LL | / pub trait Public: Private<
LL | | LL | |

View File

@ -12,7 +12,7 @@ error[E0446]: private type `m2::Priv` in public interface
--> $DIR/issue-30079.rs:18:9 --> $DIR/issue-30079.rs:18:9
| |
LL | struct Priv; LL | struct Priv;
| - `m2::Priv` declared as private | ------------ `m2::Priv` declared as private
LL | impl ::std::ops::Deref for ::SemiPriv { LL | impl ::std::ops::Deref for ::SemiPriv {
LL | type Target = Priv; LL | type Target = Priv;
| ^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -21,7 +21,7 @@ error[E0446]: private type `m3::Priv` in public interface
--> $DIR/issue-30079.rs:35:9 --> $DIR/issue-30079.rs:35:9
| |
LL | struct Priv; LL | struct Priv;
| - `m3::Priv` declared as private | ------------ `m3::Priv` declared as private
LL | impl ::SemiPrivTrait for () { LL | impl ::SemiPrivTrait for () {
LL | type Assoc = Priv; LL | type Assoc = Priv;
| ^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^ can't leak private type

View File

@ -2,7 +2,7 @@ error[E0446]: private type `Priv` in public interface
--> $DIR/private-in-public-assoc-ty.rs:17:9 --> $DIR/private-in-public-assoc-ty.rs:17:9
| |
LL | struct Priv; LL | struct Priv;
| - `Priv` declared as private | ------------ `Priv` declared as private
... ...
LL | type A = Priv; LL | type A = Priv;
| ^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^ can't leak private type
@ -39,7 +39,7 @@ error[E0446]: private type `Priv` in public interface
--> $DIR/private-in-public-assoc-ty.rs:34:9 --> $DIR/private-in-public-assoc-ty.rs:34:9
| |
LL | struct Priv; LL | struct Priv;
| - `Priv` declared as private | ------------ `Priv` declared as private
... ...
LL | type Alias4 = Priv; LL | type Alias4 = Priv;
| ^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -48,7 +48,7 @@ error[E0446]: private type `Priv` in public interface
--> $DIR/private-in-public-assoc-ty.rs:41:9 --> $DIR/private-in-public-assoc-ty.rs:41:9
| |
LL | struct Priv; LL | struct Priv;
| - `Priv` declared as private | ------------ `Priv` declared as private
... ...
LL | type Alias1 = Priv; LL | type Alias1 = Priv;
| ^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -57,7 +57,7 @@ error[E0445]: private trait `PrivTr` in public interface
--> $DIR/private-in-public-assoc-ty.rs:44:9 --> $DIR/private-in-public-assoc-ty.rs:44:9
| |
LL | trait PrivTr {} LL | trait PrivTr {}
| - `PrivTr` declared as private | ------------ `PrivTr` declared as private
... ...
LL | type Exist = impl PrivTr; LL | type Exist = impl PrivTr;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait

View File

@ -2,7 +2,7 @@ error[E0446]: private type `m1::Priv` in public interface
--> $DIR/private-in-public-lint.rs:6:9 --> $DIR/private-in-public-lint.rs:6:9
| |
LL | struct Priv; LL | struct Priv;
| - `m1::Priv` declared as private | ------------ `m1::Priv` declared as private
... ...
LL | pub fn f() -> Priv {Priv} LL | pub fn f() -> Priv {Priv}
| ^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^ can't leak private type
@ -11,7 +11,7 @@ error[E0446]: private type `m2::Priv` in public interface
--> $DIR/private-in-public-lint.rs:15:9 --> $DIR/private-in-public-lint.rs:15:9
| |
LL | struct Priv; LL | struct Priv;
| - `m2::Priv` declared as private | ------------ `m2::Priv` declared as private
... ...
LL | pub fn f() -> Priv {Priv} LL | pub fn f() -> Priv {Priv}
| ^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^ can't leak private type

View File

@ -43,7 +43,7 @@ error[E0446]: private type `types::Priv` in public interface
--> $DIR/private-in-public-warn.rs:26:9 --> $DIR/private-in-public-warn.rs:26:9
| |
LL | struct Priv; LL | struct Priv;
| - `types::Priv` declared as private | ------------ `types::Priv` declared as private
... ...
LL | type Alias = Priv; LL | type Alias = Priv;
| ^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^ can't leak private type
@ -97,7 +97,7 @@ error[E0446]: private type `types::Priv` in public interface
--> $DIR/private-in-public-warn.rs:41:9 --> $DIR/private-in-public-warn.rs:41:9
| |
LL | struct Priv; LL | struct Priv;
| - `types::Priv` declared as private | ------------ `types::Priv` declared as private
... ...
LL | type Alias = Priv; LL | type Alias = Priv;
| ^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^ can't leak private type
@ -250,7 +250,7 @@ error[E0446]: private type `impls::Priv` in public interface
--> $DIR/private-in-public-warn.rs:135:9 --> $DIR/private-in-public-warn.rs:135:9
| |
LL | struct Priv; LL | struct Priv;
| - `impls::Priv` declared as private | ------------ `impls::Priv` declared as private
... ...
LL | type Alias = Priv; LL | type Alias = Priv;
| ^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^ can't leak private type
@ -268,7 +268,7 @@ error[E0446]: private type `aliases_pub::Priv` in public interface
--> $DIR/private-in-public-warn.rs:210:9 --> $DIR/private-in-public-warn.rs:210:9
| |
LL | struct Priv; LL | struct Priv;
| - `aliases_pub::Priv` declared as private | ------------ `aliases_pub::Priv` declared as private
... ...
LL | type Check = Priv; LL | type Check = Priv;
| ^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^ can't leak private type
@ -277,7 +277,7 @@ error[E0446]: private type `aliases_pub::Priv` in public interface
--> $DIR/private-in-public-warn.rs:213:9 --> $DIR/private-in-public-warn.rs:213:9
| |
LL | struct Priv; LL | struct Priv;
| - `aliases_pub::Priv` declared as private | ------------ `aliases_pub::Priv` declared as private
... ...
LL | type Check = Priv; LL | type Check = Priv;
| ^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^ can't leak private type
@ -286,7 +286,7 @@ error[E0446]: private type `aliases_pub::Priv` in public interface
--> $DIR/private-in-public-warn.rs:216:9 --> $DIR/private-in-public-warn.rs:216:9
| |
LL | struct Priv; LL | struct Priv;
| - `aliases_pub::Priv` declared as private | ------------ `aliases_pub::Priv` declared as private
... ...
LL | type Check = Priv; LL | type Check = Priv;
| ^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^ can't leak private type
@ -295,7 +295,7 @@ error[E0446]: private type `aliases_pub::Priv` in public interface
--> $DIR/private-in-public-warn.rs:219:9 --> $DIR/private-in-public-warn.rs:219:9
| |
LL | struct Priv; LL | struct Priv;
| - `aliases_pub::Priv` declared as private | ------------ `aliases_pub::Priv` declared as private
... ...
LL | type Check = Priv; LL | type Check = Priv;
| ^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^ can't leak private type

View File

@ -2,7 +2,7 @@ error[E0446]: private type `types::Priv` in public interface
--> $DIR/private-in-public.rs:13:5 --> $DIR/private-in-public.rs:13:5
| |
LL | struct Priv; LL | struct Priv;
| - `types::Priv` declared as private | ------------ `types::Priv` declared as private
... ...
LL | pub const C: Priv = Priv; LL | pub const C: Priv = Priv;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -11,7 +11,7 @@ error[E0446]: private type `types::Priv` in public interface
--> $DIR/private-in-public.rs:14:5 --> $DIR/private-in-public.rs:14:5
| |
LL | struct Priv; LL | struct Priv;
| - `types::Priv` declared as private | ------------ `types::Priv` declared as private
... ...
LL | pub static S: Priv = Priv; LL | pub static S: Priv = Priv;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -20,7 +20,7 @@ error[E0446]: private type `types::Priv` in public interface
--> $DIR/private-in-public.rs:15:5 --> $DIR/private-in-public.rs:15:5
| |
LL | struct Priv; LL | struct Priv;
| - `types::Priv` declared as private | ------------ `types::Priv` declared as private
... ...
LL | pub fn f1(arg: Priv) {} LL | pub fn f1(arg: Priv) {}
| ^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -29,7 +29,7 @@ error[E0446]: private type `types::Priv` in public interface
--> $DIR/private-in-public.rs:16:5 --> $DIR/private-in-public.rs:16:5
| |
LL | struct Priv; LL | struct Priv;
| - `types::Priv` declared as private | ------------ `types::Priv` declared as private
... ...
LL | pub fn f2() -> Priv { panic!() } LL | pub fn f2() -> Priv { panic!() }
| ^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -38,7 +38,7 @@ error[E0446]: private type `types::Priv` in public interface
--> $DIR/private-in-public.rs:17:19 --> $DIR/private-in-public.rs:17:19
| |
LL | struct Priv; LL | struct Priv;
| - `types::Priv` declared as private | ------------ `types::Priv` declared as private
... ...
LL | pub struct S1(pub Priv); LL | pub struct S1(pub Priv);
| ^^^^^^^^ can't leak private type | ^^^^^^^^ can't leak private type
@ -47,7 +47,7 @@ error[E0446]: private type `types::Priv` in public interface
--> $DIR/private-in-public.rs:18:21 --> $DIR/private-in-public.rs:18:21
| |
LL | struct Priv; LL | struct Priv;
| - `types::Priv` declared as private | ------------ `types::Priv` declared as private
... ...
LL | pub struct S2 { pub field: Priv } LL | pub struct S2 { pub field: Priv }
| ^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^ can't leak private type
@ -56,7 +56,7 @@ error[E0446]: private type `types::Priv` in public interface
--> $DIR/private-in-public.rs:20:9 --> $DIR/private-in-public.rs:20:9
| |
LL | struct Priv; LL | struct Priv;
| - `types::Priv` declared as private | ------------ `types::Priv` declared as private
... ...
LL | pub const C: Priv = Priv; LL | pub const C: Priv = Priv;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -65,7 +65,7 @@ error[E0446]: private type `types::Priv` in public interface
--> $DIR/private-in-public.rs:21:9 --> $DIR/private-in-public.rs:21:9
| |
LL | struct Priv; LL | struct Priv;
| - `types::Priv` declared as private | ------------ `types::Priv` declared as private
... ...
LL | pub fn f1(arg: Priv) {} LL | pub fn f1(arg: Priv) {}
| ^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -74,7 +74,7 @@ error[E0446]: private type `types::Priv` in public interface
--> $DIR/private-in-public.rs:22:9 --> $DIR/private-in-public.rs:22:9
| |
LL | struct Priv; LL | struct Priv;
| - `types::Priv` declared as private | ------------ `types::Priv` declared as private
... ...
LL | pub fn f2() -> Priv { panic!() } LL | pub fn f2() -> Priv { panic!() }
| ^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -83,7 +83,7 @@ error[E0445]: private trait `traits::PrivTr` in public interface
--> $DIR/private-in-public.rs:31:5 --> $DIR/private-in-public.rs:31:5
| |
LL | trait PrivTr {} LL | trait PrivTr {}
| - `traits::PrivTr` declared as private | ------------ `traits::PrivTr` declared as private
... ...
LL | pub enum E<T: PrivTr> { V(T) } LL | pub enum E<T: PrivTr> { V(T) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
@ -92,7 +92,7 @@ error[E0445]: private trait `traits::PrivTr` in public interface
--> $DIR/private-in-public.rs:32:5 --> $DIR/private-in-public.rs:32:5
| |
LL | trait PrivTr {} LL | trait PrivTr {}
| - `traits::PrivTr` declared as private | ------------ `traits::PrivTr` declared as private
... ...
LL | pub fn f<T: PrivTr>(arg: T) {} LL | pub fn f<T: PrivTr>(arg: T) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
@ -101,7 +101,7 @@ error[E0445]: private trait `traits::PrivTr` in public interface
--> $DIR/private-in-public.rs:33:5 --> $DIR/private-in-public.rs:33:5
| |
LL | trait PrivTr {} LL | trait PrivTr {}
| - `traits::PrivTr` declared as private | ------------ `traits::PrivTr` declared as private
... ...
LL | pub struct S1<T: PrivTr>(T); LL | pub struct S1<T: PrivTr>(T);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
@ -110,7 +110,7 @@ error[E0445]: private trait `traits::PrivTr` in public interface
--> $DIR/private-in-public.rs:34:5 --> $DIR/private-in-public.rs:34:5
| |
LL | trait PrivTr {} LL | trait PrivTr {}
| - `traits::PrivTr` declared as private | ------------ `traits::PrivTr` declared as private
... ...
LL | / impl<T: PrivTr> Pub<T> { LL | / impl<T: PrivTr> Pub<T> {
LL | | pub fn f<U: PrivTr>(arg: U) {} LL | | pub fn f<U: PrivTr>(arg: U) {}
@ -121,7 +121,7 @@ error[E0445]: private trait `traits::PrivTr` in public interface
--> $DIR/private-in-public.rs:35:9 --> $DIR/private-in-public.rs:35:9
| |
LL | trait PrivTr {} LL | trait PrivTr {}
| - `traits::PrivTr` declared as private | ------------ `traits::PrivTr` declared as private
... ...
LL | pub fn f<U: PrivTr>(arg: U) {} LL | pub fn f<U: PrivTr>(arg: U) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
@ -130,7 +130,7 @@ error[E0445]: private trait `traits_where::PrivTr` in public interface
--> $DIR/private-in-public.rs:44:5 --> $DIR/private-in-public.rs:44:5
| |
LL | trait PrivTr {} LL | trait PrivTr {}
| - `traits_where::PrivTr` declared as private | ------------ `traits_where::PrivTr` declared as private
... ...
LL | pub enum E<T> where T: PrivTr { V(T) } LL | pub enum E<T> where T: PrivTr { V(T) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
@ -139,7 +139,7 @@ error[E0445]: private trait `traits_where::PrivTr` in public interface
--> $DIR/private-in-public.rs:46:5 --> $DIR/private-in-public.rs:46:5
| |
LL | trait PrivTr {} LL | trait PrivTr {}
| - `traits_where::PrivTr` declared as private | ------------ `traits_where::PrivTr` declared as private
... ...
LL | pub fn f<T>(arg: T) where T: PrivTr {} LL | pub fn f<T>(arg: T) where T: PrivTr {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
@ -148,7 +148,7 @@ error[E0445]: private trait `traits_where::PrivTr` in public interface
--> $DIR/private-in-public.rs:48:5 --> $DIR/private-in-public.rs:48:5
| |
LL | trait PrivTr {} LL | trait PrivTr {}
| - `traits_where::PrivTr` declared as private | ------------ `traits_where::PrivTr` declared as private
... ...
LL | pub struct S1<T>(T) where T: PrivTr; LL | pub struct S1<T>(T) where T: PrivTr;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
@ -157,7 +157,7 @@ error[E0445]: private trait `traits_where::PrivTr` in public interface
--> $DIR/private-in-public.rs:50:5 --> $DIR/private-in-public.rs:50:5
| |
LL | trait PrivTr {} LL | trait PrivTr {}
| - `traits_where::PrivTr` declared as private | ------------ `traits_where::PrivTr` declared as private
... ...
LL | / impl<T> Pub<T> where T: PrivTr { LL | / impl<T> Pub<T> where T: PrivTr {
LL | | LL | |
@ -170,7 +170,7 @@ error[E0445]: private trait `traits_where::PrivTr` in public interface
--> $DIR/private-in-public.rs:52:9 --> $DIR/private-in-public.rs:52:9
| |
LL | trait PrivTr {} LL | trait PrivTr {}
| - `traits_where::PrivTr` declared as private | ------------ `traits_where::PrivTr` declared as private
... ...
LL | pub fn f<U>(arg: U) where U: PrivTr {} LL | pub fn f<U>(arg: U) where U: PrivTr {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
@ -179,7 +179,7 @@ error[E0446]: private type `generics::Priv` in public interface
--> $DIR/private-in-public.rs:63:5 --> $DIR/private-in-public.rs:63:5
| |
LL | struct Priv<T = u8>(T); LL | struct Priv<T = u8>(T);
| - `generics::Priv` declared as private | ----------------------- `generics::Priv` declared as private
... ...
LL | pub fn f1(arg: [Priv; 1]) {} LL | pub fn f1(arg: [Priv; 1]) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -188,7 +188,7 @@ error[E0446]: private type `generics::Priv` in public interface
--> $DIR/private-in-public.rs:64:5 --> $DIR/private-in-public.rs:64:5
| |
LL | struct Priv<T = u8>(T); LL | struct Priv<T = u8>(T);
| - `generics::Priv` declared as private | ----------------------- `generics::Priv` declared as private
... ...
LL | pub fn f2(arg: Pub<Priv>) {} LL | pub fn f2(arg: Pub<Priv>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -197,7 +197,7 @@ error[E0446]: private type `generics::Priv<generics::Pub>` in public interface
--> $DIR/private-in-public.rs:65:5 --> $DIR/private-in-public.rs:65:5
| |
LL | struct Priv<T = u8>(T); LL | struct Priv<T = u8>(T);
| - `generics::Priv<generics::Pub>` declared as private | ----------------------- `generics::Priv<generics::Pub>` declared as private
... ...
LL | pub fn f3(arg: Priv<Pub>) {} LL | pub fn f3(arg: Priv<Pub>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -206,7 +206,7 @@ error[E0446]: private type `impls::Priv` in public interface
--> $DIR/private-in-public.rs:80:9 --> $DIR/private-in-public.rs:80:9
| |
LL | struct Priv; LL | struct Priv;
| - `impls::Priv` declared as private | ------------ `impls::Priv` declared as private
... ...
LL | pub fn f(arg: Priv) {} LL | pub fn f(arg: Priv) {}
| ^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -215,7 +215,7 @@ error[E0445]: private trait `aliases_pub::PrivTr` in public interface
--> $DIR/private-in-public.rs:104:5 --> $DIR/private-in-public.rs:104:5
| |
LL | trait PrivTr { LL | trait PrivTr {
| - `aliases_pub::PrivTr` declared as private | ------------ `aliases_pub::PrivTr` declared as private
... ...
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {} LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
@ -224,7 +224,7 @@ error[E0446]: private type `aliases_pub::Priv` in public interface
--> $DIR/private-in-public.rs:104:5 --> $DIR/private-in-public.rs:104:5
| |
LL | struct Priv; LL | struct Priv;
| - `aliases_pub::Priv` declared as private | ------------ `aliases_pub::Priv` declared as private
... ...
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {} LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -233,7 +233,7 @@ error[E0446]: private type `aliases_pub::Priv` in public interface
--> $DIR/private-in-public.rs:109:9 --> $DIR/private-in-public.rs:109:9
| |
LL | struct Priv; LL | struct Priv;
| - `aliases_pub::Priv` declared as private | ------------ `aliases_pub::Priv` declared as private
... ...
LL | pub fn f(arg: Priv) {} LL | pub fn f(arg: Priv) {}
| ^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -242,7 +242,7 @@ error[E0446]: private type `Priv1` in public interface
--> $DIR/private-in-public.rs:131:5 --> $DIR/private-in-public.rs:131:5
| |
LL | struct Priv1; LL | struct Priv1;
| - `Priv1` declared as private | ------------- `Priv1` declared as private
... ...
LL | pub fn f1(arg: PrivUseAlias) {} LL | pub fn f1(arg: PrivUseAlias) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -251,7 +251,7 @@ error[E0446]: private type `Priv2` in public interface
--> $DIR/private-in-public.rs:132:5 --> $DIR/private-in-public.rs:132:5
| |
LL | struct Priv2; LL | struct Priv2;
| - `Priv2` declared as private | ------------- `Priv2` declared as private
... ...
LL | pub fn f2(arg: PrivAlias) {} LL | pub fn f2(arg: PrivAlias) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -260,7 +260,7 @@ error[E0445]: private trait `aliases_priv::PrivTr` in public interface
--> $DIR/private-in-public.rs:133:5 --> $DIR/private-in-public.rs:133:5
| |
LL | trait PrivTr { LL | trait PrivTr {
| - `aliases_priv::PrivTr` declared as private | ------------ `aliases_priv::PrivTr` declared as private
... ...
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {} LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
@ -269,7 +269,7 @@ error[E0446]: private type `aliases_priv::Priv` in public interface
--> $DIR/private-in-public.rs:133:5 --> $DIR/private-in-public.rs:133:5
| |
LL | struct Priv; LL | struct Priv;
| - `aliases_priv::Priv` declared as private | ------------ `aliases_priv::Priv` declared as private
... ...
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {} LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -278,7 +278,7 @@ error[E0446]: private type `aliases_params::Priv` in public interface
--> $DIR/private-in-public.rs:143:5 --> $DIR/private-in-public.rs:143:5
| |
LL | struct Priv; LL | struct Priv;
| - `aliases_params::Priv` declared as private | ------------ `aliases_params::Priv` declared as private
... ...
LL | pub fn f2(arg: PrivAliasGeneric) {} LL | pub fn f2(arg: PrivAliasGeneric) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -287,7 +287,7 @@ error[E0446]: private type `aliases_params::Priv` in public interface
--> $DIR/private-in-public.rs:145:5 --> $DIR/private-in-public.rs:145:5
| |
LL | struct Priv; LL | struct Priv;
| - `aliases_params::Priv` declared as private | ------------ `aliases_params::Priv` declared as private
... ...
LL | pub fn f3(arg: Result<u8>) {} LL | pub fn f3(arg: Result<u8>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type

View File

@ -2,7 +2,7 @@ error[E0446]: private type `Priv` in public interface
--> $DIR/private-inferred-type.rs:61:36 --> $DIR/private-inferred-type.rs:61:36
| |
LL | struct Priv; LL | struct Priv;
| - `Priv` declared as private | ------------ `Priv` declared as private
... ...
LL | impl TraitWithAssocTy for u8 { type AssocTy = Priv; } LL | impl TraitWithAssocTy for u8 { type AssocTy = Priv; }
| ^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -11,7 +11,7 @@ error[E0446]: private type `S2` in public interface
--> $DIR/private-inferred-type.rs:83:9 --> $DIR/private-inferred-type.rs:83:9
| |
LL | struct S2; LL | struct S2;
| - `S2` declared as private | ---------- `S2` declared as private
... ...
LL | type Target = S2Alias; LL | type Target = S2Alias;
| ^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^ can't leak private type

View File

@ -2,7 +2,7 @@ error[E0446]: private type `Priv` in public interface
--> $DIR/private-in-public.rs:8:9 --> $DIR/private-in-public.rs:8:9
| |
LL | struct Priv; LL | struct Priv;
| - `Priv` declared as private | ------------ `Priv` declared as private
... ...
LL | pub(crate) fn g(_: Priv) {} LL | pub(crate) fn g(_: Priv) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
@ -11,7 +11,7 @@ error[E0446]: private type `Priv` in public interface
--> $DIR/private-in-public.rs:9:9 --> $DIR/private-in-public.rs:9:9
| |
LL | struct Priv; LL | struct Priv;
| - `Priv` declared as private | ------------ `Priv` declared as private
... ...
LL | crate fn h(_: Priv) {} LL | crate fn h(_: Priv) {}
| ^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^ can't leak private type

View File

@ -3,8 +3,7 @@
#[macro_use] #[macro_use]
extern crate issue_50493; extern crate issue_50493;
#[derive(Derive)] //~ ERROR field `field` of struct `Restricted` is private #[derive(Derive)]
//~| ERROR field `field` of struct `Restricted` is private
struct Restricted { struct Restricted {
pub(in restricted) field: usize, //~ visibilities can only be restricted to ancestor modules pub(in restricted) field: usize, //~ visibilities can only be restricted to ancestor modules
} }

View File

@ -1,26 +1,9 @@
error[E0742]: visibilities can only be restricted to ancestor modules error[E0742]: visibilities can only be restricted to ancestor modules
--> $DIR/issue-50493.rs:9:12 --> $DIR/issue-50493.rs:8:12
| |
LL | pub(in restricted) field: usize, LL | pub(in restricted) field: usize,
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0616]: field `field` of struct `Restricted` is private error: aborting due to previous error
--> $DIR/issue-50493.rs:6:10
|
LL | #[derive(Derive)]
| ^^^^^^ private field
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0616]: field `field` of struct `Restricted` is private For more information about this error, try `rustc --explain E0742`.
--> $DIR/issue-50493.rs:6:10
|
LL | #[derive(Derive)]
| ^^^^^^ private field
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0616, E0742.
For more information about an error, try `rustc --explain E0616`.

View File

@ -1,11 +1,11 @@
#![allow(non_camel_case_types)] // genus is always capitalized #![allow(non_camel_case_types)] // genus is always capitalized
pub(crate) struct Snail; pub(crate) struct Snail;
//~^ NOTE `Snail` declared as crate-visible //~^ NOTE `Snail` declared as private
mod sea { mod sea {
pub(super) struct Turtle; pub(super) struct Turtle;
//~^ NOTE `Turtle` declared as restricted //~^ NOTE `Turtle` declared as crate-private
} }
struct Tortoise; struct Tortoise;
@ -16,11 +16,11 @@ pub struct Shell<T> {
} }
pub type Helix_pomatia = Shell<Snail>; pub type Helix_pomatia = Shell<Snail>;
//~^ ERROR crate-visible type `Snail` in public interface //~^ ERROR private type `Snail` in public interface
//~| NOTE can't leak crate-visible type //~| NOTE can't leak private type
pub type Dermochelys_coriacea = Shell<sea::Turtle>; pub type Dermochelys_coriacea = Shell<sea::Turtle>;
//~^ ERROR restricted type `Turtle` in public interface //~^ ERROR crate-private type `Turtle` in public interface
//~| NOTE can't leak restricted type //~| NOTE can't leak crate-private type
pub type Testudo_graeca = Shell<Tortoise>; pub type Testudo_graeca = Shell<Tortoise>;
//~^ ERROR private type `Tortoise` in public interface //~^ ERROR private type `Tortoise` in public interface
//~| NOTE can't leak private type //~| NOTE can't leak private type

View File

@ -1,26 +1,26 @@
error[E0446]: crate-visible type `Snail` in public interface error[E0446]: private type `Snail` in public interface
--> $DIR/issue-33174-restricted-type-in-public-interface.rs:18:1 --> $DIR/issue-33174-restricted-type-in-public-interface.rs:18:1
| |
LL | pub(crate) struct Snail; LL | pub(crate) struct Snail;
| ---------- `Snail` declared as crate-visible | ------------------------ `Snail` declared as private
... ...
LL | pub type Helix_pomatia = Shell<Snail>; LL | pub type Helix_pomatia = Shell<Snail>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak crate-visible type | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
error[E0446]: restricted type `Turtle` in public interface error[E0446]: crate-private type `Turtle` in public interface
--> $DIR/issue-33174-restricted-type-in-public-interface.rs:21:1 --> $DIR/issue-33174-restricted-type-in-public-interface.rs:21:1
| |
LL | pub(super) struct Turtle; LL | pub(super) struct Turtle;
| ---------- `Turtle` declared as restricted | ------------------------- `Turtle` declared as crate-private
... ...
LL | pub type Dermochelys_coriacea = Shell<sea::Turtle>; LL | pub type Dermochelys_coriacea = Shell<sea::Turtle>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak restricted type | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak crate-private type
error[E0446]: private type `Tortoise` in public interface error[E0446]: private type `Tortoise` in public interface
--> $DIR/issue-33174-restricted-type-in-public-interface.rs:24:1 --> $DIR/issue-33174-restricted-type-in-public-interface.rs:24:1
| |
LL | struct Tortoise; LL | struct Tortoise;
| - `Tortoise` declared as private | ---------------- `Tortoise` declared as private
... ...
LL | pub type Testudo_graeca = Shell<Tortoise>; LL | pub type Testudo_graeca = Shell<Tortoise>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type