2015-08-09 14:15:05 -07:00
|
|
|
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
2017-04-16 21:19:24 +03:00
|
|
|
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
|
|
|
|
html_root_url = "https://doc.rust-lang.org/nightly/")]
|
2015-01-15 10:47:17 -08:00
|
|
|
|
2018-09-26 14:26:46 -07:00
|
|
|
#![feature(nll)]
|
2015-01-30 12:26:44 -08:00
|
|
|
#![feature(rustc_diagnostic_macros)]
|
2017-05-08 14:36:44 -07:00
|
|
|
|
2018-03-03 06:22:19 +01:00
|
|
|
#![recursion_limit="256"]
|
|
|
|
|
2017-04-22 22:46:34 +03:00
|
|
|
#[macro_use] extern crate rustc;
|
2015-01-15 10:47:17 -08:00
|
|
|
#[macro_use] extern crate syntax;
|
2018-12-15 08:01:57 -05:00
|
|
|
#[macro_use] extern crate log;
|
2017-09-16 16:45:49 +03:00
|
|
|
extern crate rustc_typeck;
|
2016-06-21 18:08:13 -04:00
|
|
|
extern crate syntax_pos;
|
2018-02-27 17:11:14 +01:00
|
|
|
extern crate rustc_data_structures;
|
2015-01-15 10:47:17 -08:00
|
|
|
|
2019-01-16 01:47:49 +03:00
|
|
|
use rustc::hir::{self, Node, PatKind, AssociatedItemKind};
|
2017-03-23 14:18:25 -04:00
|
|
|
use rustc::hir::def::Def;
|
2017-04-22 22:46:34 +03:00
|
|
|
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, CrateNum, DefId};
|
2016-11-28 14:00:26 -05:00
|
|
|
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
|
2016-11-12 12:24:17 +02:00
|
|
|
use rustc::hir::itemlikevisit::DeepVisitor;
|
2015-11-26 20:56:20 +03:00
|
|
|
use rustc::lint;
|
2015-11-19 14:16:35 +03:00
|
|
|
use rustc::middle::privacy::{AccessLevel, AccessLevels};
|
2018-12-16 16:18:45 +03:00
|
|
|
use rustc::ty::{self, TyCtxt, Ty, TraitRef, TypeFoldable, GenericParamDefKind};
|
2016-11-28 05:09:28 +02:00
|
|
|
use rustc::ty::fold::TypeVisitor;
|
2018-06-08 19:14:03 +02:00
|
|
|
use rustc::ty::query::{Providers, queries};
|
2018-12-31 03:02:40 +03:00
|
|
|
use rustc::ty::subst::Substs;
|
2016-03-16 02:50:34 +00:00
|
|
|
use rustc::util::nodemap::NodeSet;
|
2018-11-27 02:59:49 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
|
|
|
use rustc_data_structures::sync::Lrc;
|
2018-06-08 19:14:03 +02:00
|
|
|
use syntax::ast::{self, DUMMY_NODE_ID, Ident};
|
2018-12-16 16:18:45 +03:00
|
|
|
use syntax::attr;
|
2017-03-25 01:46:38 +00:00
|
|
|
use syntax::symbol::keywords;
|
2017-04-24 17:23:36 +03:00
|
|
|
use syntax_pos::Span;
|
2015-01-15 10:47:17 -08:00
|
|
|
|
2018-12-31 03:02:40 +03:00
|
|
|
use std::{cmp, fmt, mem};
|
2018-12-16 16:18:45 +03:00
|
|
|
use std::marker::PhantomData;
|
2015-01-15 10:47:17 -08:00
|
|
|
|
2017-08-19 03:09:55 +03:00
|
|
|
mod diagnostics;
|
2015-01-15 10:47:17 -08:00
|
|
|
|
2018-12-16 16:18:45 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Generic infrastructure used to implement specific visitors below.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/// Implemented to visit all `DefId`s in a type.
|
|
|
|
/// Visiting `DefId`s is useful because visibilities and reachabilities are attached to them.
|
2018-12-31 03:02:40 +03:00
|
|
|
/// The idea is to visit "all components of a type", as documented in
|
|
|
|
/// https://github.com/rust-lang/rfcs/blob/master/text/2145-type-privacy.md#how-to-determine-visibility-of-a-type
|
|
|
|
/// Default type visitor (`TypeVisitor`) does most of the job, but it has some shortcomings.
|
|
|
|
/// First, it doesn't have overridable `fn visit_trait_ref`, so we have to catch trait def-ids
|
|
|
|
/// manually. Second, it doesn't visit some type components like signatures of fn types, or traits
|
2019-01-13 00:41:11 +03:00
|
|
|
/// in `impl Trait`, see individual comments in `DefIdVisitorSkeleton::visit_ty`.
|
2018-12-16 16:18:45 +03:00
|
|
|
trait DefIdVisitor<'a, 'tcx: 'a> {
|
|
|
|
fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx>;
|
2019-01-05 23:46:04 +03:00
|
|
|
fn shallow(&self) -> bool { false }
|
|
|
|
fn skip_assoc_tys(&self) -> bool { false }
|
2018-12-16 16:18:45 +03:00
|
|
|
fn visit_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool;
|
|
|
|
|
|
|
|
/// Not overridden, but used to actually visit types and traits.
|
|
|
|
fn skeleton(&mut self) -> DefIdVisitorSkeleton<'_, 'a, 'tcx, Self> {
|
|
|
|
DefIdVisitorSkeleton {
|
|
|
|
def_id_visitor: self,
|
|
|
|
visited_opaque_tys: Default::default(),
|
|
|
|
dummy: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn visit(&mut self, ty_fragment: impl TypeFoldable<'tcx>) -> bool {
|
|
|
|
ty_fragment.visit_with(&mut self.skeleton())
|
|
|
|
}
|
|
|
|
fn visit_trait(&mut self, trait_ref: TraitRef<'tcx>) -> bool {
|
|
|
|
self.skeleton().visit_trait(trait_ref)
|
|
|
|
}
|
|
|
|
fn visit_predicates(&mut self, predicates: Lrc<ty::GenericPredicates<'tcx>>) -> bool {
|
|
|
|
self.skeleton().visit_predicates(predicates)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DefIdVisitorSkeleton<'v, 'a, 'tcx, V>
|
|
|
|
where V: DefIdVisitor<'a, 'tcx> + ?Sized
|
|
|
|
{
|
|
|
|
def_id_visitor: &'v mut V,
|
|
|
|
visited_opaque_tys: FxHashSet<DefId>,
|
|
|
|
dummy: PhantomData<TyCtxt<'a, 'tcx, 'tcx>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx, V> DefIdVisitorSkeleton<'_, 'a, 'tcx, V>
|
|
|
|
where V: DefIdVisitor<'a, 'tcx> + ?Sized
|
|
|
|
{
|
|
|
|
fn visit_trait(&mut self, trait_ref: TraitRef<'tcx>) -> bool {
|
2018-12-31 03:02:40 +03:00
|
|
|
let TraitRef { def_id, substs } = trait_ref;
|
2019-01-04 21:30:54 +03:00
|
|
|
self.def_id_visitor.visit_def_id(def_id, "trait", &trait_ref) ||
|
2019-01-05 23:46:04 +03:00
|
|
|
(!self.def_id_visitor.shallow() && substs.visit_with(self))
|
2018-12-16 16:18:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_predicates(&mut self, predicates: Lrc<ty::GenericPredicates<'tcx>>) -> bool {
|
2018-12-31 03:02:40 +03:00
|
|
|
let ty::GenericPredicates { parent: _, predicates } = &*predicates;
|
|
|
|
for (predicate, _span) in predicates {
|
|
|
|
match predicate {
|
2018-12-16 16:18:45 +03:00
|
|
|
ty::Predicate::Trait(poly_predicate) => {
|
2018-12-31 03:02:40 +03:00
|
|
|
let ty::TraitPredicate { trait_ref } = *poly_predicate.skip_binder();
|
|
|
|
if self.visit_trait(trait_ref) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-16 16:18:45 +03:00
|
|
|
}
|
|
|
|
ty::Predicate::Projection(poly_predicate) => {
|
2018-12-31 03:02:40 +03:00
|
|
|
let ty::ProjectionPredicate { projection_ty, ty } =
|
|
|
|
*poly_predicate.skip_binder();
|
|
|
|
if ty.visit_with(self) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if self.visit_trait(projection_ty.trait_ref(self.def_id_visitor.tcx())) {
|
2018-12-16 16:18:45 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::Predicate::TypeOutlives(poly_predicate) => {
|
2018-12-31 03:02:40 +03:00
|
|
|
let ty::OutlivesPredicate(ty, _region) = *poly_predicate.skip_binder();
|
|
|
|
if ty.visit_with(self) {
|
2018-12-16 16:18:45 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 03:02:40 +03:00
|
|
|
ty::Predicate::RegionOutlives(..) => {},
|
2018-12-16 16:18:45 +03:00
|
|
|
_ => bug!("unexpected predicate: {:?}", predicate),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx, V> TypeVisitor<'tcx> for DefIdVisitorSkeleton<'_, 'a, 'tcx, V>
|
|
|
|
where V: DefIdVisitor<'a, 'tcx> + ?Sized
|
|
|
|
{
|
|
|
|
fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
|
|
|
|
let tcx = self.def_id_visitor.tcx();
|
2018-12-31 03:02:40 +03:00
|
|
|
// Substs are not visited here because they are visited below in `super_visit_with`.
|
2018-12-16 16:18:45 +03:00
|
|
|
match ty.sty {
|
|
|
|
ty::Adt(&ty::AdtDef { did: def_id, .. }, ..) |
|
|
|
|
ty::Foreign(def_id) |
|
|
|
|
ty::FnDef(def_id, ..) |
|
|
|
|
ty::Closure(def_id, ..) |
|
|
|
|
ty::Generator(def_id, ..) => {
|
|
|
|
if self.def_id_visitor.visit_def_id(def_id, "type", ty) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-01-05 23:46:04 +03:00
|
|
|
if self.def_id_visitor.shallow() {
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-31 03:02:40 +03:00
|
|
|
// Default type visitor doesn't visit signatures of fn types.
|
|
|
|
// Something like `fn() -> Priv {my_func}` is considered a private type even if
|
|
|
|
// `my_func` is public, so we need to visit signatures.
|
2018-12-16 16:18:45 +03:00
|
|
|
if let ty::FnDef(..) = ty.sty {
|
|
|
|
if tcx.fn_sig(def_id).visit_with(self) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 03:02:40 +03:00
|
|
|
// Inherent static methods don't have self type in substs.
|
|
|
|
// Something like `fn() {my_method}` type of the method
|
|
|
|
// `impl Pub<Priv> { pub fn my_method() {} }` is considered a private type,
|
|
|
|
// so we need to visit the self type additionally.
|
2018-12-16 16:18:45 +03:00
|
|
|
if let Some(assoc_item) = tcx.opt_associated_item(def_id) {
|
|
|
|
if let ty::ImplContainer(impl_def_id) = assoc_item.container {
|
|
|
|
if tcx.type_of(impl_def_id).visit_with(self) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::Projection(proj) | ty::UnnormalizedProjection(proj) => {
|
2019-01-05 23:46:04 +03:00
|
|
|
if self.def_id_visitor.skip_assoc_tys() {
|
2018-12-16 16:18:45 +03:00
|
|
|
// Visitors searching for minimal visibility/reachability want to
|
|
|
|
// conservatively approximate associated types like `<Type as Trait>::Alias`
|
|
|
|
// as visible/reachable even if both `Type` and `Trait` are private.
|
|
|
|
// Ideally, associated types should be substituted in the same way as
|
|
|
|
// free type aliases, but this isn't done yet.
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-04 21:30:54 +03:00
|
|
|
// This will also visit substs if necessary, so we don't need to recurse.
|
2018-12-16 16:18:45 +03:00
|
|
|
return self.visit_trait(proj.trait_ref(tcx));
|
|
|
|
}
|
|
|
|
ty::Dynamic(predicates, ..) => {
|
2019-01-05 23:46:04 +03:00
|
|
|
// All traits in the list are considered the "primary" part of the type
|
|
|
|
// and are visited by shallow visitors.
|
2018-12-16 16:18:45 +03:00
|
|
|
for predicate in *predicates.skip_binder() {
|
2018-12-31 03:02:40 +03:00
|
|
|
let trait_ref = match *predicate {
|
|
|
|
ty::ExistentialPredicate::Trait(trait_ref) => trait_ref,
|
|
|
|
ty::ExistentialPredicate::Projection(proj) => proj.trait_ref(tcx),
|
|
|
|
ty::ExistentialPredicate::AutoTrait(def_id) =>
|
|
|
|
ty::ExistentialTraitRef { def_id, substs: Substs::empty() },
|
2018-12-16 16:18:45 +03:00
|
|
|
};
|
2018-12-31 03:02:40 +03:00
|
|
|
let ty::ExistentialTraitRef { def_id, substs: _ } = trait_ref;
|
|
|
|
if self.def_id_visitor.visit_def_id(def_id, "trait", &trait_ref) {
|
2018-12-16 16:18:45 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::Opaque(def_id, ..) => {
|
|
|
|
// Skip repeated `Opaque`s to avoid infinite recursion.
|
|
|
|
if self.visited_opaque_tys.insert(def_id) {
|
2019-01-05 23:46:04 +03:00
|
|
|
// The intent is to treat `impl Trait1 + Trait2` identically to
|
|
|
|
// `dyn Trait1 + Trait2`. Therefore we ignore def-id of the opaque type itself
|
|
|
|
// (it either has no visibility, or its visibility is insignificant, like
|
|
|
|
// visibilities of type aliases) and recurse into predicates instead to go
|
|
|
|
// through the trait list (default type visitor doesn't visit those traits).
|
|
|
|
// All traits in the list are considered the "primary" part of the type
|
|
|
|
// and are visited by shallow visitors.
|
2018-12-16 16:18:45 +03:00
|
|
|
if self.visit_predicates(tcx.predicates_of(def_id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// These types don't have their own def-ids (but may have subcomponents
|
|
|
|
// with def-ids that should be visited recursively).
|
|
|
|
ty::Bool | ty::Char | ty::Int(..) | ty::Uint(..) |
|
|
|
|
ty::Float(..) | ty::Str | ty::Never |
|
|
|
|
ty::Array(..) | ty::Slice(..) | ty::Tuple(..) |
|
|
|
|
ty::RawPtr(..) | ty::Ref(..) | ty::FnPtr(..) |
|
|
|
|
ty::Param(..) | ty::Error | ty::GeneratorWitness(..) => {}
|
|
|
|
ty::Bound(..) | ty::Placeholder(..) | ty::Infer(..) =>
|
|
|
|
bug!("unexpected type: {:?}", ty),
|
|
|
|
}
|
|
|
|
|
2019-01-05 23:46:04 +03:00
|
|
|
!self.def_id_visitor.shallow() && ty.super_visit_with(self)
|
2018-12-16 16:18:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn def_id_visibility<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
|
|
|
|
-> (ty::Visibility, Span, &'static str) {
|
|
|
|
match tcx.hir().as_local_node_id(def_id) {
|
|
|
|
Some(node_id) => {
|
|
|
|
let vis = match tcx.hir().get(node_id) {
|
|
|
|
Node::Item(item) => &item.vis,
|
|
|
|
Node::ForeignItem(foreign_item) => &foreign_item.vis,
|
|
|
|
Node::TraitItem(..) | Node::Variant(..) => {
|
|
|
|
return def_id_visibility(tcx, tcx.hir().get_parent_did(node_id));
|
|
|
|
}
|
|
|
|
Node::ImplItem(impl_item) => {
|
|
|
|
match tcx.hir().get(tcx.hir().get_parent(node_id)) {
|
|
|
|
Node::Item(item) => match &item.node {
|
|
|
|
hir::ItemKind::Impl(.., None, _, _) => &impl_item.vis,
|
|
|
|
hir::ItemKind::Impl(.., Some(trait_ref), _, _)
|
|
|
|
=> return def_id_visibility(tcx, trait_ref.path.def.def_id()),
|
|
|
|
kind => bug!("unexpected item kind: {:?}", kind),
|
|
|
|
}
|
|
|
|
node => bug!("unexpected node kind: {:?}", node),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Node::StructCtor(vdata) => {
|
|
|
|
let struct_node_id = tcx.hir().get_parent(node_id);
|
|
|
|
let item = match tcx.hir().get(struct_node_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, struct_node_id, tcx),
|
|
|
|
item.vis.span, item.vis.node.descr());
|
|
|
|
for field in vdata.fields() {
|
|
|
|
let field_vis = ty::Visibility::from_hir(&field.vis, node_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(node_id));
|
|
|
|
if adt_def.non_enum_variant().is_field_list_non_exhaustive() {
|
|
|
|
ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
|
|
|
|
span = attr::find_by_name(&item.attrs, "non_exhaustive").unwrap().span;
|
|
|
|
descr = "crate-visible";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ctor_vis, span, descr);
|
|
|
|
}
|
|
|
|
Node::Expr(expr) => {
|
|
|
|
return (ty::Visibility::Restricted(tcx.hir().get_module_parent(expr.id)),
|
|
|
|
expr.span, "private")
|
|
|
|
}
|
|
|
|
node => bug!("unexpected node kind: {:?}", node)
|
|
|
|
};
|
|
|
|
(ty::Visibility::from_hir(vis, node_id, tcx), vis.span, vis.node.descr())
|
|
|
|
}
|
2018-12-31 03:02:40 +03:00
|
|
|
None => {
|
|
|
|
let vis = tcx.visibility(def_id);
|
|
|
|
let descr = if vis == ty::Visibility::Public { "public" } else { "private" };
|
|
|
|
(vis, tcx.def_span(def_id), descr)
|
|
|
|
}
|
2018-12-16 16:18:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the correct `TypeckTables` for the given `item_id` (or an empty table if
|
|
|
|
// there is no `TypeckTables` for the item).
|
2018-12-31 03:02:40 +03:00
|
|
|
fn item_tables<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
node_id: ast::NodeId,
|
|
|
|
empty_tables: &'a ty::TypeckTables<'tcx>)
|
|
|
|
-> &'a ty::TypeckTables<'tcx> {
|
|
|
|
let def_id = tcx.hir().local_def_id(node_id);
|
|
|
|
if tcx.has_typeck_tables(def_id) { tcx.typeck_tables_of(def_id) } else { empty_tables }
|
2018-12-16 16:18:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn min<'a, 'tcx>(vis1: ty::Visibility, vis2: ty::Visibility, tcx: TyCtxt<'a, 'tcx, 'tcx>)
|
|
|
|
-> ty::Visibility {
|
|
|
|
if vis1.is_at_least(vis2, tcx) { vis2 } else { vis1 }
|
|
|
|
}
|
|
|
|
|
2017-03-17 20:38:32 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Visitor used to determine if pub(restricted) is used anywhere in the crate.
|
|
|
|
///
|
|
|
|
/// This is done so that `private_in_public` warnings can be turned into hard errors
|
|
|
|
/// in crates that have been updated to use pub(restricted).
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
struct PubRestrictedVisitor<'a, 'tcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
has_pub_restricted: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for PubRestrictedVisitor<'a, 'tcx> {
|
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2018-12-04 13:45:36 +01:00
|
|
|
NestedVisitorMap::All(&self.tcx.hir())
|
2017-03-17 20:38:32 -07:00
|
|
|
}
|
|
|
|
fn visit_vis(&mut self, vis: &'tcx hir::Visibility) {
|
2018-06-30 20:34:18 -07:00
|
|
|
self.has_pub_restricted = self.has_pub_restricted || vis.node.is_pub_restricted();
|
2017-03-17 20:38:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 16:18:45 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Visitor used to determine impl visibility and reachability.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-12-31 03:02:40 +03:00
|
|
|
struct FindMin<'a, 'tcx, VL: VisibilityLike> {
|
2018-12-16 16:18:45 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
access_levels: &'a AccessLevels,
|
2018-12-31 03:02:40 +03:00
|
|
|
min: VL,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx, VL: VisibilityLike> DefIdVisitor<'a, 'tcx> for FindMin<'a, 'tcx, VL> {
|
|
|
|
fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> { self.tcx }
|
2019-01-05 23:46:04 +03:00
|
|
|
fn shallow(&self) -> bool { VL::SHALLOW }
|
|
|
|
fn skip_assoc_tys(&self) -> bool { true }
|
2018-12-31 03:02:40 +03:00
|
|
|
fn visit_def_id(&mut self, def_id: DefId, _kind: &str, _descr: &dyn fmt::Display) -> bool {
|
|
|
|
self.min = VL::new_min(self, def_id);
|
|
|
|
false
|
|
|
|
}
|
2018-12-16 16:18:45 +03:00
|
|
|
}
|
|
|
|
|
2018-12-31 03:02:40 +03:00
|
|
|
trait VisibilityLike: Sized {
|
2018-12-16 16:18:45 +03:00
|
|
|
const MAX: Self;
|
2019-01-05 23:46:04 +03:00
|
|
|
const SHALLOW: bool = false;
|
2018-12-16 16:18:45 +03:00
|
|
|
fn new_min<'a, 'tcx>(find: &FindMin<'a, 'tcx, Self>, def_id: DefId) -> Self;
|
2018-12-31 03:02:40 +03:00
|
|
|
|
2019-01-05 23:46:04 +03:00
|
|
|
// Returns an over-approximation (`skip_assoc_tys` = true) of visibility due to
|
2018-12-31 03:02:40 +03:00
|
|
|
// associated types for which we can't determine visibility precisely.
|
|
|
|
fn of_impl<'a, 'tcx>(node_id: ast::NodeId, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
access_levels: &'a AccessLevels) -> Self {
|
|
|
|
let mut find = FindMin { tcx, access_levels, min: Self::MAX };
|
|
|
|
let def_id = tcx.hir().local_def_id(node_id);
|
|
|
|
find.visit(tcx.type_of(def_id));
|
|
|
|
if let Some(trait_ref) = tcx.impl_trait_ref(def_id) {
|
|
|
|
find.visit_trait(trait_ref);
|
|
|
|
}
|
|
|
|
find.min
|
|
|
|
}
|
2018-12-16 16:18:45 +03:00
|
|
|
}
|
2018-12-31 03:02:40 +03:00
|
|
|
impl VisibilityLike for ty::Visibility {
|
2018-12-16 16:18:45 +03:00
|
|
|
const MAX: Self = ty::Visibility::Public;
|
|
|
|
fn new_min<'a, 'tcx>(find: &FindMin<'a, 'tcx, Self>, def_id: DefId) -> Self {
|
|
|
|
min(def_id_visibility(find.tcx, def_id).0, find.min, find.tcx)
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 03:02:40 +03:00
|
|
|
impl VisibilityLike for Option<AccessLevel> {
|
2018-12-16 16:18:45 +03:00
|
|
|
const MAX: Self = Some(AccessLevel::Public);
|
2019-01-04 21:30:54 +03:00
|
|
|
// Type inference is very smart sometimes.
|
|
|
|
// It can make an impl reachable even some components of its type or trait are unreachable.
|
|
|
|
// E.g. methods of `impl ReachableTrait<UnreachableTy> for ReachableTy<UnreachableTy> { ... }`
|
|
|
|
// can be usable from other crates (#57264). So we skip substs when calculating reachability
|
2019-01-05 23:46:04 +03:00
|
|
|
// and consider an impl reachable if its "shallow" type and trait are reachable.
|
|
|
|
//
|
|
|
|
// The assumption we make here is that type-inference won't let you use an impl without knowing
|
|
|
|
// both "shallow" version of its self type and "shallow" version of its trait if it exists
|
|
|
|
// (which require reaching the `DefId`s in them).
|
|
|
|
const SHALLOW: bool = true;
|
2018-12-16 16:18:45 +03:00
|
|
|
fn new_min<'a, 'tcx>(find: &FindMin<'a, 'tcx, Self>, def_id: DefId) -> Self {
|
|
|
|
cmp::min(if let Some(node_id) = find.tcx.hir().as_local_node_id(def_id) {
|
|
|
|
find.access_levels.map.get(&node_id).cloned()
|
|
|
|
} else {
|
|
|
|
Self::MAX
|
|
|
|
}, find.min)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 10:47:17 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// The embargo visitor, used to determine the exports of the ast
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct EmbargoVisitor<'a, 'tcx: 'a> {
|
2016-05-03 05:23:22 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2015-01-15 10:47:17 -08:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Accessibility levels for reachable nodes.
|
2015-11-19 14:16:35 +03:00
|
|
|
access_levels: AccessLevels,
|
2018-11-27 02:59:49 +00:00
|
|
|
// Previous accessibility level; `None` means unreachable.
|
2015-11-19 14:16:35 +03:00
|
|
|
prev_level: Option<AccessLevel>,
|
2018-11-27 02:59:49 +00:00
|
|
|
// Has something changed in the level map?
|
2015-11-19 14:16:35 +03:00
|
|
|
changed: bool,
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
|
2016-02-14 00:04:17 +03:00
|
|
|
struct ReachEverythingInTheInterfaceVisitor<'b, 'a: 'b, 'tcx: 'a> {
|
2018-08-21 00:11:59 +01:00
|
|
|
access_level: Option<AccessLevel>,
|
2016-11-28 05:09:28 +02:00
|
|
|
item_def_id: DefId,
|
2016-02-14 00:04:17 +03:00
|
|
|
ev: &'b mut EmbargoVisitor<'a, 'tcx>,
|
|
|
|
}
|
|
|
|
|
2015-01-15 10:47:17 -08:00
|
|
|
impl<'a, 'tcx> EmbargoVisitor<'a, 'tcx> {
|
2015-11-19 14:16:35 +03:00
|
|
|
fn get(&self, id: ast::NodeId) -> Option<AccessLevel> {
|
|
|
|
self.access_levels.map.get(&id).cloned()
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Updates node level and returns the updated level.
|
2015-11-19 14:16:35 +03:00
|
|
|
fn update(&mut self, id: ast::NodeId, level: Option<AccessLevel>) -> Option<AccessLevel> {
|
|
|
|
let old_level = self.get(id);
|
2018-11-27 02:59:49 +00:00
|
|
|
// Accessibility levels can only grow.
|
2015-11-19 14:16:35 +03:00
|
|
|
if level > old_level {
|
|
|
|
self.access_levels.map.insert(id, level.unwrap());
|
|
|
|
self.changed = true;
|
|
|
|
level
|
|
|
|
} else {
|
|
|
|
old_level
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
2015-10-25 04:42:31 +03:00
|
|
|
}
|
2016-02-14 00:04:17 +03:00
|
|
|
|
2018-12-16 16:18:45 +03:00
|
|
|
fn reach(&mut self, item_id: ast::NodeId, access_level: Option<AccessLevel>)
|
|
|
|
-> ReachEverythingInTheInterfaceVisitor<'_, 'a, 'tcx> {
|
2016-11-28 05:09:28 +02:00
|
|
|
ReachEverythingInTheInterfaceVisitor {
|
2018-12-16 16:18:45 +03:00
|
|
|
access_level: cmp::min(access_level, Some(AccessLevel::Reachable)),
|
2018-12-04 13:45:36 +01:00
|
|
|
item_def_id: self.tcx.hir().local_def_id(item_id),
|
2016-11-28 05:09:28 +02:00
|
|
|
ev: self,
|
|
|
|
}
|
2016-02-14 00:04:17 +03:00
|
|
|
}
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
|
2016-11-09 16:45:26 -05:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
|
2015-11-17 18:56:13 -05:00
|
|
|
/// We want to visit items in the context of their containing
|
|
|
|
/// module and so forth, so supply a crate for doing a deep walk.
|
2016-11-28 14:00:26 -05:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2018-12-04 13:45:36 +01:00
|
|
|
NestedVisitorMap::All(&self.tcx.hir())
|
2015-11-17 18:56:13 -05:00
|
|
|
}
|
2015-11-19 14:16:35 +03:00
|
|
|
|
2016-11-09 16:45:26 -05:00
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item) {
|
2015-11-19 14:16:35 +03:00
|
|
|
let inherited_item_level = match item.node {
|
2018-12-16 16:18:45 +03:00
|
|
|
hir::ItemKind::Impl(..) =>
|
2018-12-31 03:02:40 +03:00
|
|
|
Option::<AccessLevel>::of_impl(item.id, self.tcx, &self.access_levels),
|
2018-11-27 02:59:49 +00:00
|
|
|
// Foreign modules inherit level from parents.
|
2018-12-16 16:18:45 +03:00
|
|
|
hir::ItemKind::ForeignMod(..) => self.prev_level,
|
2018-11-27 02:59:49 +00:00
|
|
|
// Other `pub` items inherit levels from parents.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Const(..) | hir::ItemKind::Enum(..) | hir::ItemKind::ExternCrate(..) |
|
|
|
|
hir::ItemKind::GlobalAsm(..) | hir::ItemKind::Fn(..) | hir::ItemKind::Mod(..) |
|
|
|
|
hir::ItemKind::Static(..) | hir::ItemKind::Struct(..) |
|
|
|
|
hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..) |
|
|
|
|
hir::ItemKind::Existential(..) |
|
|
|
|
hir::ItemKind::Ty(..) | hir::ItemKind::Union(..) | hir::ItemKind::Use(..) => {
|
2018-06-30 20:34:18 -07:00
|
|
|
if item.vis.node.is_pub() { self.prev_level } else { None }
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
2015-11-19 14:16:35 +03:00
|
|
|
};
|
2015-01-15 10:47:17 -08:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Update level of the item itself.
|
2015-11-19 14:16:35 +03:00
|
|
|
let item_level = self.update(item.id, inherited_item_level);
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Update levels of nested things.
|
2015-01-15 10:47:17 -08:00
|
|
|
match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Enum(ref def, _) => {
|
2015-01-31 12:20:46 -05:00
|
|
|
for variant in &def.variants {
|
2015-11-19 14:16:35 +03:00
|
|
|
let variant_level = self.update(variant.node.data.id(), item_level);
|
2015-10-25 04:42:31 +03:00
|
|
|
for field in variant.node.data.fields() {
|
2016-02-27 11:34:29 +03:00
|
|
|
self.update(field.id, variant_level);
|
2015-10-25 04:42:31 +03:00
|
|
|
}
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
}
|
2018-12-16 16:18:45 +03:00
|
|
|
hir::ItemKind::Impl(.., ref trait_ref, _, ref impl_item_refs) => {
|
2016-11-10 09:47:00 -05:00
|
|
|
for impl_item_ref in impl_item_refs {
|
2018-12-16 16:18:45 +03:00
|
|
|
if trait_ref.is_some() || impl_item_ref.vis.node.is_pub() {
|
2016-12-04 04:21:06 +02:00
|
|
|
self.update(impl_item_ref.id.node_id, item_level);
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Trait(.., ref trait_item_refs) => {
|
2016-12-04 04:21:06 +02:00
|
|
|
for trait_item_ref in trait_item_refs {
|
|
|
|
self.update(trait_item_ref.id.node_id, item_level);
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Struct(ref def, _) | hir::ItemKind::Union(ref def, _) => {
|
2015-10-08 23:45:46 +03:00
|
|
|
if !def.is_struct() {
|
2015-11-19 14:16:35 +03:00
|
|
|
self.update(def.id(), item_level);
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
2015-10-08 23:45:46 +03:00
|
|
|
for field in def.fields() {
|
2018-06-30 20:34:18 -07:00
|
|
|
if field.vis.node.is_pub() {
|
2016-02-27 11:34:29 +03:00
|
|
|
self.update(field.id, item_level);
|
2015-02-25 22:37:12 +11:00
|
|
|
}
|
|
|
|
}
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::ForeignMod(ref foreign_mod) => {
|
2015-11-19 14:16:35 +03:00
|
|
|
for foreign_item in &foreign_mod.items {
|
2018-06-30 20:34:18 -07:00
|
|
|
if foreign_item.vis.node.is_pub() {
|
2015-11-19 14:16:35 +03:00
|
|
|
self.update(foreign_item.id, item_level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-16 16:18:45 +03:00
|
|
|
hir::ItemKind::Existential(..) |
|
2018-07-12 08:20:31 +08:00
|
|
|
hir::ItemKind::Use(..) |
|
|
|
|
hir::ItemKind::Static(..) |
|
|
|
|
hir::ItemKind::Const(..) |
|
|
|
|
hir::ItemKind::GlobalAsm(..) |
|
|
|
|
hir::ItemKind::Ty(..) |
|
|
|
|
hir::ItemKind::Mod(..) |
|
|
|
|
hir::ItemKind::TraitAlias(..) |
|
|
|
|
hir::ItemKind::Fn(..) |
|
|
|
|
hir::ItemKind::ExternCrate(..) => {}
|
2016-02-14 00:04:17 +03:00
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Mark all items in interfaces of reachable items as reachable.
|
2016-02-14 00:04:17 +03:00
|
|
|
match item.node {
|
2018-11-27 02:59:49 +00:00
|
|
|
// The interface is empty.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::ExternCrate(..) => {}
|
2018-11-27 02:59:49 +00:00
|
|
|
// All nested items are checked by `visit_item`.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Mod(..) => {}
|
2018-11-27 02:59:49 +00:00
|
|
|
// Re-exports are handled in `visit_mod`.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Use(..) => {}
|
2018-11-27 02:59:49 +00:00
|
|
|
// The interface is empty.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::GlobalAsm(..) => {}
|
2018-12-16 16:18:45 +03:00
|
|
|
hir::ItemKind::Existential(..) => {
|
|
|
|
// FIXME: This is some serious pessimization intended to workaround deficiencies
|
|
|
|
// in the reachability pass (`middle/reachable.rs`). Types are marked as link-time
|
|
|
|
// reachable if they are returned via `impl Trait`, even from private functions.
|
|
|
|
let exist_level = cmp::max(item_level, Some(AccessLevel::ReachableFromImplTrait));
|
|
|
|
self.reach(item.id, exist_level).generics().predicates().ty();
|
2018-06-29 10:58:17 +02:00
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
// Visit everything.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Const(..) | hir::ItemKind::Static(..) |
|
|
|
|
hir::ItemKind::Fn(..) | hir::ItemKind::Ty(..) => {
|
2016-11-28 05:09:28 +02:00
|
|
|
if item_level.is_some() {
|
2018-12-16 16:18:45 +03:00
|
|
|
self.reach(item.id, item_level).generics().predicates().ty();
|
2016-11-28 05:09:28 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Trait(.., ref trait_item_refs) => {
|
2016-11-28 05:09:28 +02:00
|
|
|
if item_level.is_some() {
|
2018-12-16 16:18:45 +03:00
|
|
|
self.reach(item.id, item_level).generics().predicates();
|
2016-11-28 05:09:28 +02:00
|
|
|
|
2016-12-04 04:21:06 +02:00
|
|
|
for trait_item_ref in trait_item_refs {
|
2018-12-16 16:18:45 +03:00
|
|
|
let mut reach = self.reach(trait_item_ref.id.node_id, item_level);
|
2016-11-28 05:09:28 +02:00
|
|
|
reach.generics().predicates();
|
|
|
|
|
2019-01-16 01:47:49 +03:00
|
|
|
if trait_item_ref.kind == AssociatedItemKind::Type &&
|
2016-12-04 04:21:06 +02:00
|
|
|
!trait_item_ref.defaultness.has_value() {
|
2016-11-28 05:09:28 +02:00
|
|
|
// No type to visit.
|
|
|
|
} else {
|
2017-04-24 15:20:46 +03:00
|
|
|
reach.ty();
|
2016-11-28 05:09:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::TraitAlias(..) => {
|
2017-10-02 12:28:16 +00:00
|
|
|
if item_level.is_some() {
|
2018-12-16 16:18:45 +03:00
|
|
|
self.reach(item.id, item_level).generics().predicates();
|
2017-10-02 12:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
// Visit everything except for private impl items.
|
2018-12-16 16:18:45 +03:00
|
|
|
hir::ItemKind::Impl(.., ref impl_item_refs) => {
|
2016-02-14 00:04:17 +03:00
|
|
|
if item_level.is_some() {
|
2019-01-05 21:56:49 +03:00
|
|
|
self.reach(item.id, item_level).generics().predicates().ty().trait_ref();
|
2016-11-28 05:09:28 +02:00
|
|
|
|
2016-12-04 04:21:06 +02:00
|
|
|
for impl_item_ref in impl_item_refs {
|
2018-12-16 16:18:45 +03:00
|
|
|
let impl_item_level = self.get(impl_item_ref.id.node_id);
|
|
|
|
if impl_item_level.is_some() {
|
|
|
|
self.reach(impl_item_ref.id.node_id, impl_item_level)
|
|
|
|
.generics().predicates().ty();
|
2016-11-28 05:09:28 +02:00
|
|
|
}
|
|
|
|
}
|
2016-02-14 00:04:17 +03:00
|
|
|
}
|
|
|
|
}
|
2016-11-28 05:09:28 +02:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Visit everything, but enum variants have their own levels.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Enum(ref def, _) => {
|
2016-02-14 00:04:17 +03:00
|
|
|
if item_level.is_some() {
|
2018-12-16 16:18:45 +03:00
|
|
|
self.reach(item.id, item_level).generics().predicates();
|
2016-02-14 00:04:17 +03:00
|
|
|
}
|
|
|
|
for variant in &def.variants {
|
2018-12-16 16:18:45 +03:00
|
|
|
let variant_level = self.get(variant.node.data.id());
|
|
|
|
if variant_level.is_some() {
|
2016-02-14 00:04:17 +03:00
|
|
|
for field in variant.node.data.fields() {
|
2018-12-16 16:18:45 +03:00
|
|
|
self.reach(field.id, variant_level).ty();
|
2016-02-14 00:04:17 +03:00
|
|
|
}
|
|
|
|
// Corner case: if the variant is reachable, but its
|
|
|
|
// enum is not, make the enum reachable as well.
|
2018-12-16 16:18:45 +03:00
|
|
|
self.update(item.id, variant_level);
|
2016-02-14 00:04:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
// Visit everything, but foreign items have their own levels.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::ForeignMod(ref foreign_mod) => {
|
2016-02-14 00:04:17 +03:00
|
|
|
for foreign_item in &foreign_mod.items {
|
2018-12-16 16:18:45 +03:00
|
|
|
let foreign_item_level = self.get(foreign_item.id);
|
|
|
|
if foreign_item_level.is_some() {
|
|
|
|
self.reach(foreign_item.id, foreign_item_level)
|
|
|
|
.generics().predicates().ty();
|
2016-02-14 00:04:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
// Visit everything except for private fields.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Struct(ref struct_def, _) |
|
|
|
|
hir::ItemKind::Union(ref struct_def, _) => {
|
2016-02-14 00:04:17 +03:00
|
|
|
if item_level.is_some() {
|
2018-12-16 16:18:45 +03:00
|
|
|
self.reach(item.id, item_level).generics().predicates();
|
2016-02-14 00:04:17 +03:00
|
|
|
for field in struct_def.fields() {
|
2018-12-16 16:18:45 +03:00
|
|
|
let field_level = self.get(field.id);
|
|
|
|
if field_level.is_some() {
|
|
|
|
self.reach(field.id, field_level).ty();
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 03:02:40 +03:00
|
|
|
let orig_level = mem::replace(&mut self.prev_level, item_level);
|
2015-11-17 18:56:13 -05:00
|
|
|
intravisit::walk_item(self, item);
|
2015-11-19 14:16:35 +03:00
|
|
|
self.prev_level = orig_level;
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
|
2016-11-09 16:45:26 -05:00
|
|
|
fn visit_block(&mut self, b: &'tcx hir::Block) {
|
2015-11-19 14:16:35 +03:00
|
|
|
// Blocks can have public items, for example impls, but they always
|
|
|
|
// start as completely private regardless of publicity of a function,
|
2018-11-27 02:59:49 +00:00
|
|
|
// constant, type, field, etc., in which this block resides.
|
2018-12-31 03:02:40 +03:00
|
|
|
let orig_level = mem::replace(&mut self.prev_level, None);
|
2015-11-17 18:56:13 -05:00
|
|
|
intravisit::walk_block(self, b);
|
2015-11-19 14:16:35 +03:00
|
|
|
self.prev_level = orig_level;
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
|
2016-11-09 16:45:26 -05:00
|
|
|
fn visit_mod(&mut self, m: &'tcx hir::Mod, _sp: Span, id: ast::NodeId) {
|
2015-01-15 10:47:17 -08:00
|
|
|
// This code is here instead of in visit_item so that the
|
|
|
|
// crate module gets processed as well.
|
2015-11-19 14:16:35 +03:00
|
|
|
if self.prev_level.is_some() {
|
2018-12-04 13:45:36 +01:00
|
|
|
let def_id = self.tcx.hir().local_def_id(id);
|
2017-09-08 13:51:57 -07:00
|
|
|
if let Some(exports) = self.tcx.module_exports(def_id) {
|
2017-08-29 11:10:22 -07:00
|
|
|
for export in exports.iter() {
|
2018-11-11 20:28:56 +03:00
|
|
|
if export.vis == ty::Visibility::Public {
|
|
|
|
if let Some(def_id) = export.def.opt_def_id() {
|
2018-12-04 13:45:36 +01:00
|
|
|
if let Some(node_id) = self.tcx.hir().as_local_node_id(def_id) {
|
2018-11-11 20:28:56 +03:00
|
|
|
self.update(node_id, Some(AccessLevel::Exported));
|
|
|
|
}
|
2017-11-29 11:20:49 -08:00
|
|
|
}
|
2016-01-12 04:55:21 +00:00
|
|
|
}
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 14:16:35 +03:00
|
|
|
|
2016-07-28 05:58:45 -04:00
|
|
|
intravisit::walk_mod(self, m, id);
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
2015-10-25 04:42:31 +03:00
|
|
|
|
2016-11-09 16:45:26 -05:00
|
|
|
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef) {
|
2017-03-25 01:46:38 +00:00
|
|
|
if md.legacy {
|
|
|
|
self.update(md.id, Some(AccessLevel::Public));
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-04 13:45:36 +01:00
|
|
|
let module_did = ty::DefIdTree::parent(
|
|
|
|
self.tcx,
|
|
|
|
self.tcx.hir().local_def_id(md.id)
|
|
|
|
).unwrap();
|
|
|
|
let mut module_id = self.tcx.hir().as_local_node_id(module_did).unwrap();
|
2018-06-30 20:34:18 -07:00
|
|
|
let level = if md.vis.node.is_pub() { self.get(module_id) } else { None };
|
2017-03-25 01:46:38 +00:00
|
|
|
let level = self.update(md.id, level);
|
|
|
|
if level.is_none() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let module = if module_id == ast::CRATE_NODE_ID {
|
2018-12-04 13:45:36 +01:00
|
|
|
&self.tcx.hir().krate().module
|
|
|
|
} else if let hir::ItemKind::Mod(ref module) =
|
|
|
|
self.tcx.hir().expect_item(module_id).node {
|
2017-03-25 01:46:38 +00:00
|
|
|
module
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
for id in &module.item_ids {
|
|
|
|
self.update(id.id, level);
|
|
|
|
}
|
2018-12-04 13:45:36 +01:00
|
|
|
let def_id = self.tcx.hir().local_def_id(module_id);
|
2017-11-29 11:20:49 -08:00
|
|
|
if let Some(exports) = self.tcx.module_exports(def_id) {
|
|
|
|
for export in exports.iter() {
|
2018-12-04 13:45:36 +01:00
|
|
|
if let Some(node_id) = self.tcx.hir().as_local_node_id(export.def.def_id()) {
|
2017-11-29 11:20:49 -08:00
|
|
|
self.update(node_id, level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 01:46:38 +00:00
|
|
|
if module_id == ast::CRATE_NODE_ID {
|
|
|
|
break
|
|
|
|
}
|
2018-12-04 13:45:36 +01:00
|
|
|
module_id = self.tcx.hir().get_parent_node(module_id);
|
2017-03-25 01:46:38 +00:00
|
|
|
}
|
2015-10-25 04:42:31 +03:00
|
|
|
}
|
2016-02-14 00:04:17 +03:00
|
|
|
}
|
|
|
|
|
2018-12-16 16:18:45 +03:00
|
|
|
impl<'a, 'tcx> ReachEverythingInTheInterfaceVisitor<'_, 'a, 'tcx> {
|
2016-11-28 05:09:28 +02:00
|
|
|
fn generics(&mut self) -> &mut Self {
|
2018-05-10 23:02:41 +01:00
|
|
|
for param in &self.ev.tcx.generics_of(self.item_def_id).params {
|
2018-04-19 00:40:22 +01:00
|
|
|
match param.kind {
|
2018-05-16 13:03:04 +03:00
|
|
|
GenericParamDefKind::Type { has_default, .. } => {
|
|
|
|
if has_default {
|
2018-12-16 16:18:45 +03:00
|
|
|
self.visit(self.ev.tcx.type_of(param.def_id));
|
2018-04-13 23:12:14 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-10 23:46:57 +01:00
|
|
|
GenericParamDefKind::Lifetime => {}
|
2017-01-25 22:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-28 05:09:28 +02:00
|
|
|
self
|
2016-11-04 18:20:15 -04:00
|
|
|
}
|
|
|
|
|
2016-11-28 05:09:28 +02:00
|
|
|
fn predicates(&mut self) -> &mut Self {
|
2018-12-16 16:18:45 +03:00
|
|
|
self.visit_predicates(self.ev.tcx.predicates_of(self.item_def_id));
|
2016-11-28 05:09:28 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-04-24 15:20:46 +03:00
|
|
|
fn ty(&mut self) -> &mut Self {
|
2018-12-16 16:18:45 +03:00
|
|
|
self.visit(self.ev.tcx.type_of(self.item_def_id));
|
2016-11-28 05:09:28 +02:00
|
|
|
self
|
|
|
|
}
|
2019-01-05 21:56:49 +03:00
|
|
|
|
|
|
|
fn trait_ref(&mut self) -> &mut Self {
|
|
|
|
if let Some(trait_ref) = self.ev.tcx.impl_trait_ref(self.item_def_id) {
|
|
|
|
self.visit_trait(trait_ref);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
2016-11-28 05:09:28 +02:00
|
|
|
}
|
|
|
|
|
2018-12-16 16:18:45 +03:00
|
|
|
impl<'a, 'tcx> DefIdVisitor<'a, 'tcx> for ReachEverythingInTheInterfaceVisitor<'_, 'a, 'tcx> {
|
|
|
|
fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> { self.ev.tcx }
|
|
|
|
fn visit_def_id(&mut self, def_id: DefId, _kind: &str, _descr: &dyn fmt::Display) -> bool {
|
|
|
|
if let Some(node_id) = self.ev.tcx.hir().as_local_node_id(def_id) {
|
|
|
|
self.ev.update(node_id, self.access_level);
|
2016-02-14 00:04:17 +03:00
|
|
|
}
|
2018-12-16 16:18:45 +03:00
|
|
|
false
|
2016-02-14 00:04:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-16 21:19:24 +03:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Name privacy visitor, checks privacy and reports violations.
|
|
|
|
/// Most of name privacy checks are performed during the main resolution phase,
|
|
|
|
/// or later in type checking when field accesses and associated items are resolved.
|
|
|
|
/// This pass performs remaining checks for fields in struct expressions and patterns.
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
2015-01-15 10:47:17 -08:00
|
|
|
|
2017-04-16 21:19:24 +03:00
|
|
|
struct NamePrivacyVisitor<'a, 'tcx: 'a> {
|
2016-05-03 05:23:22 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-01-25 16:24:00 -05:00
|
|
|
tables: &'a ty::TypeckTables<'tcx>,
|
2017-03-25 01:46:38 +00:00
|
|
|
current_item: ast::NodeId,
|
2017-08-04 09:49:40 +02:00
|
|
|
empty_tables: &'a ty::TypeckTables<'tcx>,
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
|
2017-04-16 21:19:24 +03:00
|
|
|
impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
|
2018-02-08 15:48:16 -08:00
|
|
|
// Checks that a field in a struct constructor (expression or pattern) is accessible.
|
|
|
|
fn check_field(&mut self,
|
2018-11-27 02:59:49 +00:00
|
|
|
use_ctxt: Span, // syntax context of the field name at the use site
|
|
|
|
span: Span, // span of the field pattern, e.g., `x: 0`
|
|
|
|
def: &'tcx ty::AdtDef, // definition of the struct or enum
|
|
|
|
field: &'tcx ty::FieldDef) { // definition of the field
|
2018-05-26 02:50:15 +03:00
|
|
|
let ident = Ident::new(keywords::Invalid.name(), use_ctxt);
|
2017-03-25 01:46:38 +00:00
|
|
|
let def_id = self.tcx.adjust_ident(ident, def.did, self.current_item).1;
|
|
|
|
if !def.is_enum() && !field.vis.is_accessible_from(def_id, self.tcx) {
|
2016-08-22 19:56:14 +03:00
|
|
|
struct_span_err!(self.tcx.sess, span, E0451, "field `{}` of {} `{}` is private",
|
2018-05-26 02:50:15 +03:00
|
|
|
field.ident, def.variant_descr(), self.tcx.item_path_str(def.did))
|
|
|
|
.span_label(span, format!("field `{}` is private", field.ident))
|
2016-08-27 23:28:38 +03:00
|
|
|
.emit();
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-16 21:19:24 +03:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for NamePrivacyVisitor<'a, 'tcx> {
|
2015-11-17 18:56:13 -05:00
|
|
|
/// We want to visit items in the context of their containing
|
|
|
|
/// module and so forth, so supply a crate for doing a deep walk.
|
2016-11-28 14:00:26 -05:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2018-12-04 13:45:36 +01:00
|
|
|
NestedVisitorMap::All(&self.tcx.hir())
|
2015-11-17 18:56:13 -05:00
|
|
|
}
|
|
|
|
|
2018-06-08 19:14:03 +02:00
|
|
|
fn visit_mod(&mut self, _m: &'tcx hir::Mod, _s: Span, _n: ast::NodeId) {
|
2019-01-15 00:54:56 +01:00
|
|
|
// Don't visit nested modules, since we run a separate visitor walk
|
|
|
|
// for each module in `privacy_access_levels`
|
2018-06-08 19:14:03 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 21:54:24 +02:00
|
|
|
fn visit_nested_body(&mut self, body: hir::BodyId) {
|
2018-12-31 03:02:40 +03:00
|
|
|
let orig_tables = mem::replace(&mut self.tables, self.tcx.body_tables(body));
|
2018-12-04 13:45:36 +01:00
|
|
|
let body = self.tcx.hir().body(body);
|
2017-01-06 21:54:24 +02:00
|
|
|
self.visit_body(body);
|
2017-04-16 21:19:24 +03:00
|
|
|
self.tables = orig_tables;
|
2017-01-06 21:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 16:45:26 -05:00
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item) {
|
2018-12-31 03:02:40 +03:00
|
|
|
let orig_current_item = mem::replace(&mut self.current_item, item.id);
|
|
|
|
let orig_tables =
|
|
|
|
mem::replace(&mut self.tables, item_tables(self.tcx, item.id, self.empty_tables));
|
2015-11-17 18:56:13 -05:00
|
|
|
intravisit::walk_item(self, item);
|
2017-04-16 21:19:24 +03:00
|
|
|
self.current_item = orig_current_item;
|
2017-08-04 09:49:40 +02:00
|
|
|
self.tables = orig_tables;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem) {
|
2018-12-31 03:02:40 +03:00
|
|
|
let orig_tables =
|
|
|
|
mem::replace(&mut self.tables, item_tables(self.tcx, ti.id, self.empty_tables));
|
2017-08-04 09:49:40 +02:00
|
|
|
intravisit::walk_trait_item(self, ti);
|
|
|
|
self.tables = orig_tables;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem) {
|
2018-12-31 03:02:40 +03:00
|
|
|
let orig_tables =
|
|
|
|
mem::replace(&mut self.tables, item_tables(self.tcx, ii.id, self.empty_tables));
|
2017-08-04 09:49:40 +02:00
|
|
|
intravisit::walk_impl_item(self, ii);
|
|
|
|
self.tables = orig_tables;
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
|
2016-11-09 16:45:26 -05:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
|
2015-01-15 10:47:17 -08:00
|
|
|
match expr.node {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Struct(ref qpath, ref fields, ref base) => {
|
2017-08-04 09:49:40 +02:00
|
|
|
let def = self.tables.qpath_def(qpath, expr.hir_id);
|
2017-01-06 21:54:24 +02:00
|
|
|
let adt = self.tables.expr_ty(expr).ty_adt_def().unwrap();
|
2016-11-25 13:21:19 +02:00
|
|
|
let variant = adt.variant_of_def(def);
|
2017-04-16 21:19:24 +03:00
|
|
|
if let Some(ref base) = *base {
|
|
|
|
// If the expression uses FRU we need to make sure all the unmentioned fields
|
|
|
|
// are checked for privacy (RFC 736). Rather than computing the set of
|
|
|
|
// unmentioned fields, just check them all.
|
2018-04-05 03:20:21 +03:00
|
|
|
for (vf_index, variant_field) in variant.fields.iter().enumerate() {
|
|
|
|
let field = fields.iter().find(|f| {
|
|
|
|
self.tcx.field_index(f.id, self.tables) == vf_index
|
|
|
|
});
|
2018-02-08 15:48:16 -08:00
|
|
|
let (use_ctxt, span) = match field {
|
2018-05-26 02:50:15 +03:00
|
|
|
Some(field) => (field.ident.span, field.span),
|
2018-03-18 02:57:23 +03:00
|
|
|
None => (base.span, base.span),
|
2018-02-08 15:48:16 -08:00
|
|
|
};
|
|
|
|
self.check_field(use_ctxt, span, adt, variant_field);
|
2016-08-26 19:23:42 +03:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-16 21:19:24 +03:00
|
|
|
for field in fields {
|
2018-05-26 02:50:15 +03:00
|
|
|
let use_ctxt = field.ident.span;
|
2018-04-05 03:20:21 +03:00
|
|
|
let index = self.tcx.field_index(field.id, self.tables);
|
|
|
|
self.check_field(use_ctxt, field.span, adt, &variant.fields[index]);
|
2016-08-26 19:23:42 +03:00
|
|
|
}
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2015-11-17 18:56:13 -05:00
|
|
|
intravisit::walk_expr(self, expr);
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
|
2017-04-16 21:19:24 +03:00
|
|
|
fn visit_pat(&mut self, pat: &'tcx hir::Pat) {
|
|
|
|
match pat.node {
|
2016-11-25 13:21:19 +02:00
|
|
|
PatKind::Struct(ref qpath, ref fields, _) => {
|
2017-08-04 09:49:40 +02:00
|
|
|
let def = self.tables.qpath_def(qpath, pat.hir_id);
|
2017-04-16 21:19:24 +03:00
|
|
|
let adt = self.tables.pat_ty(pat).ty_adt_def().unwrap();
|
2016-11-25 13:21:19 +02:00
|
|
|
let variant = adt.variant_of_def(def);
|
2015-08-02 22:52:50 +03:00
|
|
|
for field in fields {
|
2018-05-26 02:50:15 +03:00
|
|
|
let use_ctxt = field.node.ident.span;
|
2018-04-05 03:20:21 +03:00
|
|
|
let index = self.tcx.field_index(field.node.id, self.tables);
|
|
|
|
self.check_field(use_ctxt, field.span, adt, &variant.fields[index]);
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2017-04-16 21:19:24 +03:00
|
|
|
intravisit::walk_pat(self, pat);
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-22 22:46:34 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Type privacy visitor, checks types for privacy and reports violations.
|
|
|
|
/// Both explicitly written types and inferred types of expressions and patters are checked.
|
|
|
|
/// Checks are performed on "semantic" types regardless of names and their hygiene.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct TypePrivacyVisitor<'a, 'tcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
tables: &'a ty::TypeckTables<'tcx>,
|
|
|
|
current_item: DefId,
|
2017-11-18 18:49:37 +03:00
|
|
|
in_body: bool,
|
2017-04-22 22:46:34 +03:00
|
|
|
span: Span,
|
2017-08-04 09:49:40 +02:00
|
|
|
empty_tables: &'a ty::TypeckTables<'tcx>,
|
2017-04-22 22:46:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> TypePrivacyVisitor<'a, 'tcx> {
|
|
|
|
fn item_is_accessible(&self, did: DefId) -> bool {
|
2018-12-16 16:18:45 +03:00
|
|
|
def_id_visibility(self.tcx, did).0.is_accessible_from(self.current_item, self.tcx)
|
2017-04-22 22:46:34 +03:00
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Take node-id of an expression or pattern and check its type for privacy.
|
2017-08-07 14:43:43 +02:00
|
|
|
fn check_expr_pat_type(&mut self, id: hir::HirId, span: Span) -> bool {
|
2017-06-03 17:51:32 +03:00
|
|
|
self.span = span;
|
2018-12-16 16:18:45 +03:00
|
|
|
if self.visit(self.tables.node_id_to_type(id)) || self.visit(self.tables.node_substs(id)) {
|
2017-06-03 17:51:32 +03:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-10 16:10:04 +02:00
|
|
|
if let Some(adjustments) = self.tables.adjustments().get(id) {
|
2017-06-03 17:51:32 +03:00
|
|
|
for adjustment in adjustments {
|
2018-12-16 16:18:45 +03:00
|
|
|
if self.visit(adjustment.target) {
|
2017-06-03 17:51:32 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 22:46:34 +03:00
|
|
|
}
|
2017-06-03 17:51:32 +03:00
|
|
|
false
|
2017-04-22 22:46:34 +03:00
|
|
|
}
|
2017-11-18 18:49:37 +03:00
|
|
|
|
2018-12-16 16:18:45 +03:00
|
|
|
fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
|
|
|
|
let is_error = !self.item_is_accessible(def_id);
|
|
|
|
if is_error {
|
|
|
|
self.tcx.sess.span_err(self.span, &format!("{} `{}` is private", kind, descr));
|
2017-11-18 18:49:37 +03:00
|
|
|
}
|
2018-12-16 16:18:45 +03:00
|
|
|
is_error
|
2017-11-18 18:49:37 +03:00
|
|
|
}
|
2017-04-22 22:46:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
|
|
|
|
/// We want to visit items in the context of their containing
|
|
|
|
/// module and so forth, so supply a crate for doing a deep walk.
|
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2018-12-04 13:45:36 +01:00
|
|
|
NestedVisitorMap::All(&self.tcx.hir())
|
2017-04-22 22:46:34 +03:00
|
|
|
}
|
|
|
|
|
2018-06-08 19:14:03 +02:00
|
|
|
fn visit_mod(&mut self, _m: &'tcx hir::Mod, _s: Span, _n: ast::NodeId) {
|
2019-01-15 00:54:56 +01:00
|
|
|
// Don't visit nested modules, since we run a separate visitor walk
|
|
|
|
// for each module in `privacy_access_levels`
|
2018-06-08 19:14:03 +02:00
|
|
|
}
|
|
|
|
|
2017-04-22 22:46:34 +03:00
|
|
|
fn visit_nested_body(&mut self, body: hir::BodyId) {
|
2018-12-31 03:02:40 +03:00
|
|
|
let orig_tables = mem::replace(&mut self.tables, self.tcx.body_tables(body));
|
|
|
|
let orig_in_body = mem::replace(&mut self.in_body, true);
|
2018-12-04 13:45:36 +01:00
|
|
|
let body = self.tcx.hir().body(body);
|
2017-04-22 22:46:34 +03:00
|
|
|
self.visit_body(body);
|
|
|
|
self.tables = orig_tables;
|
2017-11-18 18:49:37 +03:00
|
|
|
self.in_body = orig_in_body;
|
2017-04-22 22:46:34 +03:00
|
|
|
}
|
|
|
|
|
2017-09-16 16:45:49 +03:00
|
|
|
fn visit_ty(&mut self, hir_ty: &'tcx hir::Ty) {
|
|
|
|
self.span = hir_ty.span;
|
2017-11-18 18:49:37 +03:00
|
|
|
if self.in_body {
|
2017-09-16 16:45:49 +03:00
|
|
|
// Types in bodies.
|
2018-12-16 16:18:45 +03:00
|
|
|
if self.visit(self.tables.node_id_to_type(hir_ty.hir_id)) {
|
2017-09-16 16:45:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Types in signatures.
|
|
|
|
// FIXME: This is very ineffective. Ideally each HIR type should be converted
|
|
|
|
// into a semantic type only once and the result should be cached somehow.
|
2018-12-16 16:18:45 +03:00
|
|
|
if self.visit(rustc_typeck::hir_ty_to_ty(self.tcx, hir_ty)) {
|
2017-09-16 16:45:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
intravisit::walk_ty(self, hir_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_trait_ref(&mut self, trait_ref: &'tcx hir::TraitRef) {
|
2017-11-18 18:49:37 +03:00
|
|
|
self.span = trait_ref.path.span;
|
|
|
|
if !self.in_body {
|
|
|
|
// Avoid calling `hir_trait_to_predicates` in bodies, it will ICE.
|
|
|
|
// The traits' privacy in bodies is already checked as a part of trait object types.
|
|
|
|
let (principal, projections) =
|
|
|
|
rustc_typeck::hir_trait_to_predicates(self.tcx, trait_ref);
|
2018-12-16 16:18:45 +03:00
|
|
|
if self.visit_trait(*principal.skip_binder()) {
|
2017-11-18 18:49:37 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-09-16 20:15:49 +03:00
|
|
|
for (poly_predicate, _) in projections {
|
2017-11-18 18:49:37 +03:00
|
|
|
let tcx = self.tcx;
|
2018-12-16 16:18:45 +03:00
|
|
|
if self.visit(poly_predicate.skip_binder().ty) ||
|
|
|
|
self.visit_trait(poly_predicate.skip_binder().projection_ty.trait_ref(tcx)) {
|
2017-11-18 18:49:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-09-16 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
intravisit::walk_trait_ref(self, trait_ref);
|
|
|
|
}
|
|
|
|
|
2017-04-22 22:46:34 +03:00
|
|
|
// Check types of expressions
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
|
2017-08-07 14:43:43 +02:00
|
|
|
if self.check_expr_pat_type(expr.hir_id, expr.span) {
|
2017-04-22 22:46:34 +03:00
|
|
|
// Do not check nested expressions if the error already happened.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
match expr.node {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Assign(.., ref rhs) | hir::ExprKind::Match(ref rhs, ..) => {
|
2017-04-22 22:46:34 +03:00
|
|
|
// Do not report duplicate errors for `x = y` and `match x { ... }`.
|
2017-08-07 14:43:43 +02:00
|
|
|
if self.check_expr_pat_type(rhs.hir_id, rhs.span) {
|
2017-04-22 22:46:34 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::MethodCall(_, span, _) => {
|
2017-04-22 22:46:34 +03:00
|
|
|
// Method calls have to be checked specially.
|
2017-07-07 15:57:51 +03:00
|
|
|
self.span = span;
|
2018-06-28 22:49:01 +01:00
|
|
|
if let Some(def) = self.tables.type_dependent_defs().get(expr.hir_id) {
|
2018-12-16 16:18:45 +03:00
|
|
|
if self.visit(self.tcx.type_of(def.def_id())) {
|
2018-06-28 22:49:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.tcx.sess.delay_span_bug(expr.span,
|
|
|
|
"no type-dependent def for method call");
|
2017-04-22 22:46:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
intravisit::walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
|
2017-11-18 18:49:37 +03:00
|
|
|
// Prohibit access to associated items with insufficient nominal visibility.
|
2017-11-18 19:15:16 +03:00
|
|
|
//
|
|
|
|
// Additionally, until better reachability analysis for macros 2.0 is available,
|
|
|
|
// we prohibit access to private statics from other crates, this allows to give
|
|
|
|
// more code internal visibility at link time. (Access to private functions
|
2018-02-16 15:56:50 +01:00
|
|
|
// is already prohibited by type privacy for function types.)
|
2018-07-31 10:43:51 -06:00
|
|
|
fn visit_qpath(&mut self, qpath: &'tcx hir::QPath, id: hir::HirId, span: Span) {
|
2017-11-18 18:49:37 +03:00
|
|
|
let def = match *qpath {
|
|
|
|
hir::QPath::Resolved(_, ref path) => match path.def {
|
|
|
|
Def::Method(..) | Def::AssociatedConst(..) |
|
2018-12-16 16:18:45 +03:00
|
|
|
Def::AssociatedTy(..) | Def::AssociatedExistential(..) |
|
|
|
|
Def::Static(..) => Some(path.def),
|
2017-11-18 18:49:37 +03:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
hir::QPath::TypeRelative(..) => {
|
2018-07-31 10:43:51 -06:00
|
|
|
self.tables.type_dependent_defs().get(id).cloned()
|
2017-11-18 18:49:37 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Some(def) = def {
|
|
|
|
let def_id = def.def_id();
|
2017-11-18 19:15:16 +03:00
|
|
|
let is_local_static = if let Def::Static(..) = def { def_id.is_local() } else { false };
|
|
|
|
if !self.item_is_accessible(def_id) && !is_local_static {
|
2017-11-18 18:49:37 +03:00
|
|
|
let name = match *qpath {
|
2018-06-10 17:40:45 +03:00
|
|
|
hir::QPath::Resolved(_, ref path) => path.to_string(),
|
|
|
|
hir::QPath::TypeRelative(_, ref segment) => segment.ident.to_string(),
|
2017-11-18 18:49:37 +03:00
|
|
|
};
|
|
|
|
let msg = format!("{} `{}` is private", def.kind_name(), name);
|
|
|
|
self.tcx.sess.span_err(span, &msg);
|
|
|
|
return;
|
2017-06-03 17:51:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
intravisit::walk_qpath(self, qpath, id, span);
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Check types of patterns.
|
2017-04-22 22:46:34 +03:00
|
|
|
fn visit_pat(&mut self, pattern: &'tcx hir::Pat) {
|
2017-08-07 14:43:43 +02:00
|
|
|
if self.check_expr_pat_type(pattern.hir_id, pattern.span) {
|
2017-04-22 22:46:34 +03:00
|
|
|
// Do not check nested patterns if the error already happened.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
intravisit::walk_pat(self, pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_local(&mut self, local: &'tcx hir::Local) {
|
|
|
|
if let Some(ref init) = local.init {
|
2017-08-07 14:43:43 +02:00
|
|
|
if self.check_expr_pat_type(init.hir_id, init.span) {
|
2017-04-22 22:46:34 +03:00
|
|
|
// Do not report duplicate errors for `let x = y`.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
intravisit::walk_local(self, local);
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Check types in item interfaces.
|
2017-04-22 22:46:34 +03:00
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item) {
|
2018-12-16 16:18:45 +03:00
|
|
|
let orig_current_item =
|
2018-12-31 03:02:40 +03:00
|
|
|
mem::replace(&mut self.current_item, self.tcx.hir().local_def_id(item.id));
|
|
|
|
let orig_in_body = mem::replace(&mut self.in_body, false);
|
|
|
|
let orig_tables =
|
|
|
|
mem::replace(&mut self.tables, item_tables(self.tcx, item.id, self.empty_tables));
|
2017-04-22 22:46:34 +03:00
|
|
|
intravisit::walk_item(self, item);
|
2017-08-04 09:49:40 +02:00
|
|
|
self.tables = orig_tables;
|
2017-11-18 18:49:37 +03:00
|
|
|
self.in_body = orig_in_body;
|
2017-04-22 22:46:34 +03:00
|
|
|
self.current_item = orig_current_item;
|
|
|
|
}
|
2017-08-04 09:49:40 +02:00
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem) {
|
2018-12-31 03:02:40 +03:00
|
|
|
let orig_tables =
|
|
|
|
mem::replace(&mut self.tables, item_tables(self.tcx, ti.id, self.empty_tables));
|
2017-08-04 09:49:40 +02:00
|
|
|
intravisit::walk_trait_item(self, ti);
|
|
|
|
self.tables = orig_tables;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem) {
|
2018-12-31 03:02:40 +03:00
|
|
|
let orig_tables =
|
|
|
|
mem::replace(&mut self.tables, item_tables(self.tcx, ii.id, self.empty_tables));
|
2017-08-04 09:49:40 +02:00
|
|
|
intravisit::walk_impl_item(self, ii);
|
|
|
|
self.tables = orig_tables;
|
|
|
|
}
|
2017-04-22 22:46:34 +03:00
|
|
|
}
|
|
|
|
|
2018-12-16 16:18:45 +03:00
|
|
|
impl<'a, 'tcx> DefIdVisitor<'a, 'tcx> for TypePrivacyVisitor<'a, 'tcx> {
|
|
|
|
fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> { self.tcx }
|
|
|
|
fn visit_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
|
|
|
|
self.check_def_id(def_id, kind, descr)
|
2017-04-22 22:46:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-26 19:26:15 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Obsolete visitors for checking for private items in public interfaces.
|
|
|
|
/// These visitors are supposed to be kept in frozen state and produce an
|
|
|
|
/// "old error node set". For backward compatibility the new visitor reports
|
|
|
|
/// warnings instead of hard errors when the erroneous node is not in this old set.
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx: 'a> {
|
2016-05-03 05:23:22 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2015-11-26 19:26:15 +03:00
|
|
|
access_levels: &'a AccessLevels,
|
|
|
|
in_variant: bool,
|
2018-11-27 02:59:49 +00:00
|
|
|
// Set of errors produced by this obsolete visitor.
|
2015-11-26 19:26:15 +03:00
|
|
|
old_error_set: NodeSet,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ObsoleteCheckTypeForPrivatenessVisitor<'a, 'b: 'a, 'tcx: 'b> {
|
|
|
|
inner: &'a ObsoleteVisiblePrivateTypesVisitor<'b, 'tcx>,
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Whether the type refers to private types.
|
2015-11-26 19:26:15 +03:00
|
|
|
contains_private: bool,
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Whether we've recurred at all (i.e., if we're pointing at the
|
|
|
|
/// first type on which `visit_ty` was called).
|
2015-11-26 19:26:15 +03:00
|
|
|
at_outer_type: bool,
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Whether that first type is a public path.
|
2015-11-26 19:26:15 +03:00
|
|
|
outer_type_is_public_path: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
|
2016-11-25 13:21:19 +02:00
|
|
|
fn path_is_private_type(&self, path: &hir::Path) -> bool {
|
|
|
|
let did = match path.def {
|
2018-08-03 00:43:49 +03:00
|
|
|
Def::PrimTy(..) | Def::SelfTy(..) | Def::Err => return false,
|
2016-06-03 23:15:00 +03:00
|
|
|
def => def.def_id(),
|
2015-11-26 19:26:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// A path can only be private if:
|
|
|
|
// it's in this crate...
|
2018-12-04 13:45:36 +01:00
|
|
|
if let Some(node_id) = self.tcx.hir().as_local_node_id(did) {
|
2015-11-26 19:26:15 +03:00
|
|
|
// .. and it corresponds to a private type in the AST (this returns
|
2018-11-27 02:59:49 +00:00
|
|
|
// `None` for type parameters).
|
2018-12-04 13:45:36 +01:00
|
|
|
match self.tcx.hir().find(node_id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Item(ref item)) => !item.vis.node.is_pub(),
|
2015-11-26 19:26:15 +03:00
|
|
|
Some(_) | None => false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trait_is_public(&self, trait_id: ast::NodeId) -> bool {
|
|
|
|
// FIXME: this would preferably be using `exported_items`, but all
|
2018-11-27 02:59:49 +00:00
|
|
|
// traits are exported currently (see `EmbargoVisitor.exported_trait`).
|
2015-11-26 19:26:15 +03:00
|
|
|
self.access_levels.is_public(trait_id)
|
|
|
|
}
|
|
|
|
|
2018-06-16 11:14:07 +01:00
|
|
|
fn check_generic_bound(&mut self, bound: &hir::GenericBound) {
|
2018-06-14 15:00:21 +01:00
|
|
|
if let hir::GenericBound::Trait(ref trait_ref, _) = *bound {
|
2016-11-25 13:21:19 +02:00
|
|
|
if self.path_is_private_type(&trait_ref.trait_ref.path) {
|
2015-11-26 19:26:15 +03:00
|
|
|
self.old_error_set.insert(trait_ref.trait_ref.ref_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 06:08:11 +00:00
|
|
|
fn item_is_public(&self, id: &ast::NodeId, vis: &hir::Visibility) -> bool {
|
2018-06-30 20:34:18 -07:00
|
|
|
self.access_levels.is_reachable(*id) || vis.node.is_pub()
|
2015-11-26 19:26:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'tcx, 'v> Visitor<'v> for ObsoleteCheckTypeForPrivatenessVisitor<'a, 'b, 'tcx> {
|
2016-11-28 14:00:26 -05:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
|
|
|
|
NestedVisitorMap::None
|
|
|
|
}
|
2016-11-24 20:15:11 +01:00
|
|
|
|
2015-11-26 19:26:15 +03:00
|
|
|
fn visit_ty(&mut self, ty: &hir::Ty) {
|
2018-07-11 22:41:03 +08:00
|
|
|
if let hir::TyKind::Path(hir::QPath::Resolved(_, ref path)) = ty.node {
|
2016-11-25 13:21:19 +02:00
|
|
|
if self.inner.path_is_private_type(path) {
|
2015-11-26 19:26:15 +03:00
|
|
|
self.contains_private = true;
|
2018-11-27 02:59:49 +00:00
|
|
|
// Found what we're looking for, so let's stop working.
|
2015-11-26 19:26:15 +03:00
|
|
|
return
|
2016-11-25 13:21:19 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
if let hir::TyKind::Path(_) = ty.node {
|
2016-11-25 13:21:19 +02:00
|
|
|
if self.at_outer_type {
|
2015-11-26 19:26:15 +03:00
|
|
|
self.outer_type_is_public_path = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.at_outer_type = false;
|
|
|
|
intravisit::walk_ty(self, ty)
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Don't want to recurse into `[, .. expr]`.
|
2015-11-26 19:26:15 +03:00
|
|
|
fn visit_expr(&mut self, _: &hir::Expr) {}
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:45:26 -05:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
|
2015-11-26 19:26:15 +03:00
|
|
|
/// We want to visit items in the context of their containing
|
|
|
|
/// module and so forth, so supply a crate for doing a deep walk.
|
2016-11-28 14:00:26 -05:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2018-12-04 13:45:36 +01:00
|
|
|
NestedVisitorMap::All(&self.tcx.hir())
|
2015-11-26 19:26:15 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 16:45:26 -05:00
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item) {
|
2015-11-26 19:26:15 +03:00
|
|
|
match item.node {
|
2018-11-27 02:59:49 +00:00
|
|
|
// Contents of a private mod can be re-exported, so we need
|
2015-11-26 19:26:15 +03:00
|
|
|
// to check internals.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Mod(_) => {}
|
2015-11-26 19:26:15 +03:00
|
|
|
|
|
|
|
// An `extern {}` doesn't introduce a new privacy
|
|
|
|
// namespace (the contents have their own privacies).
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::ForeignMod(_) => {}
|
2015-11-26 19:26:15 +03:00
|
|
|
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Trait(.., ref bounds, _) => {
|
2015-11-26 19:26:15 +03:00
|
|
|
if !self.trait_is_public(item.id) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for bound in bounds.iter() {
|
2018-06-16 11:14:07 +01:00
|
|
|
self.check_generic_bound(bound)
|
2015-11-26 19:26:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Impls need some special handling to try to offer useful
|
2015-11-26 19:26:15 +03:00
|
|
|
// error messages without (too many) false positives
|
2018-11-27 02:59:49 +00:00
|
|
|
// (i.e., we could just return here to not check them at
|
2015-11-26 19:26:15 +03:00
|
|
|
// all, or some worse estimation of whether an impl is
|
|
|
|
// publicly visible).
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Impl(.., ref g, ref trait_ref, ref self_, ref impl_item_refs) => {
|
2015-11-26 19:26:15 +03:00
|
|
|
// `impl [... for] Private` is never visible.
|
|
|
|
let self_contains_private;
|
2018-11-27 02:59:49 +00:00
|
|
|
// `impl [... for] Public<...>`, but not `impl [... for]
|
|
|
|
// Vec<Public>` or `(Public,)`, etc.
|
2015-11-26 19:26:15 +03:00
|
|
|
let self_is_public_path;
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Check the properties of the `Self` type:
|
2015-11-26 19:26:15 +03:00
|
|
|
{
|
|
|
|
let mut visitor = ObsoleteCheckTypeForPrivatenessVisitor {
|
|
|
|
inner: self,
|
|
|
|
contains_private: false,
|
|
|
|
at_outer_type: true,
|
|
|
|
outer_type_is_public_path: false,
|
|
|
|
};
|
2016-02-09 21:28:53 +01:00
|
|
|
visitor.visit_ty(&self_);
|
2015-11-26 19:26:15 +03:00
|
|
|
self_contains_private = visitor.contains_private;
|
|
|
|
self_is_public_path = visitor.outer_type_is_public_path;
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Miscellaneous info about the impl:
|
2015-11-26 19:26:15 +03:00
|
|
|
|
|
|
|
// `true` iff this is `impl Private for ...`.
|
|
|
|
let not_private_trait =
|
|
|
|
trait_ref.as_ref().map_or(true, // no trait counts as public trait
|
|
|
|
|tr| {
|
2016-11-25 13:21:19 +02:00
|
|
|
let did = tr.path.def.def_id();
|
2015-11-26 19:26:15 +03:00
|
|
|
|
2018-12-04 13:45:36 +01:00
|
|
|
if let Some(node_id) = self.tcx.hir().as_local_node_id(did) {
|
2015-11-26 19:26:15 +03:00
|
|
|
self.trait_is_public(node_id)
|
|
|
|
} else {
|
|
|
|
true // external traits must be public
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// `true` iff this is a trait impl or at least one method is public.
|
|
|
|
//
|
|
|
|
// `impl Public { $( fn ...() {} )* }` is not visible.
|
|
|
|
//
|
|
|
|
// This is required over just using the methods' privacy
|
|
|
|
// directly because we might have `impl<T: Foo<Private>> ...`,
|
|
|
|
// and we shouldn't warn about the generics if all the methods
|
|
|
|
// are private (because `T` won't be visible externally).
|
|
|
|
let trait_or_some_public_method =
|
|
|
|
trait_ref.is_some() ||
|
2016-11-10 09:47:00 -05:00
|
|
|
impl_item_refs.iter()
|
|
|
|
.any(|impl_item_ref| {
|
2018-12-04 13:45:36 +01:00
|
|
|
let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
|
2016-11-04 18:20:15 -04:00
|
|
|
match impl_item.node {
|
|
|
|
hir::ImplItemKind::Const(..) |
|
|
|
|
hir::ImplItemKind::Method(..) => {
|
|
|
|
self.access_levels.is_reachable(impl_item.id)
|
|
|
|
}
|
2018-07-03 19:38:14 +02:00
|
|
|
hir::ImplItemKind::Existential(..) |
|
2016-11-04 18:20:15 -04:00
|
|
|
hir::ImplItemKind::Type(_) => false,
|
|
|
|
}
|
|
|
|
});
|
2015-11-26 19:26:15 +03:00
|
|
|
|
|
|
|
if !self_contains_private &&
|
|
|
|
not_private_trait &&
|
|
|
|
trait_or_some_public_method {
|
|
|
|
|
|
|
|
intravisit::walk_generics(self, g);
|
|
|
|
|
|
|
|
match *trait_ref {
|
|
|
|
None => {
|
2016-11-10 09:47:00 -05:00
|
|
|
for impl_item_ref in impl_item_refs {
|
2015-11-26 19:26:15 +03:00
|
|
|
// This is where we choose whether to walk down
|
|
|
|
// further into the impl to check its items. We
|
|
|
|
// should only walk into public items so that we
|
|
|
|
// don't erroneously report errors for private
|
|
|
|
// types in private items.
|
2018-12-04 13:45:36 +01:00
|
|
|
let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
|
2015-11-26 19:26:15 +03:00
|
|
|
match impl_item.node {
|
|
|
|
hir::ImplItemKind::Const(..) |
|
|
|
|
hir::ImplItemKind::Method(..)
|
2016-03-25 06:08:11 +00:00
|
|
|
if self.item_is_public(&impl_item.id, &impl_item.vis) =>
|
2015-11-26 19:26:15 +03:00
|
|
|
{
|
|
|
|
intravisit::walk_impl_item(self, impl_item)
|
|
|
|
}
|
|
|
|
hir::ImplItemKind::Type(..) => {
|
|
|
|
intravisit::walk_impl_item(self, impl_item)
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ref tr) => {
|
|
|
|
// Any private types in a trait impl fall into three
|
|
|
|
// categories.
|
|
|
|
// 1. mentioned in the trait definition
|
|
|
|
// 2. mentioned in the type params/generics
|
|
|
|
// 3. mentioned in the associated types of the impl
|
|
|
|
//
|
|
|
|
// Those in 1. can only occur if the trait is in
|
|
|
|
// this crate and will've been warned about on the
|
|
|
|
// trait definition (there's no need to warn twice
|
|
|
|
// so we don't check the methods).
|
|
|
|
//
|
|
|
|
// Those in 2. are warned via walk_generics and this
|
|
|
|
// call here.
|
|
|
|
intravisit::walk_path(self, &tr.path);
|
|
|
|
|
|
|
|
// Those in 3. are warned with this call.
|
2016-11-10 09:47:00 -05:00
|
|
|
for impl_item_ref in impl_item_refs {
|
2018-12-04 13:45:36 +01:00
|
|
|
let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
|
2015-11-26 19:26:15 +03:00
|
|
|
if let hir::ImplItemKind::Type(ref ty) = impl_item.node {
|
|
|
|
self.visit_ty(ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if trait_ref.is_none() && self_is_public_path {
|
2018-11-27 02:59:49 +00:00
|
|
|
// `impl Public<Private> { ... }`. Any public static
|
2015-11-26 19:26:15 +03:00
|
|
|
// methods will be visible as `Public::foo`.
|
|
|
|
let mut found_pub_static = false;
|
2016-11-10 09:47:00 -05:00
|
|
|
for impl_item_ref in impl_item_refs {
|
2016-12-04 04:21:06 +02:00
|
|
|
if self.item_is_public(&impl_item_ref.id.node_id, &impl_item_ref.vis) {
|
2018-12-04 13:45:36 +01:00
|
|
|
let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
|
2016-12-04 04:21:06 +02:00
|
|
|
match impl_item_ref.kind {
|
2019-01-16 01:47:49 +03:00
|
|
|
AssociatedItemKind::Const => {
|
2015-11-26 19:26:15 +03:00
|
|
|
found_pub_static = true;
|
|
|
|
intravisit::walk_impl_item(self, impl_item);
|
|
|
|
}
|
2019-01-16 01:47:49 +03:00
|
|
|
AssociatedItemKind::Method { has_self: false } => {
|
2015-11-26 19:26:15 +03:00
|
|
|
found_pub_static = true;
|
|
|
|
intravisit::walk_impl_item(self, impl_item);
|
|
|
|
}
|
2016-12-04 04:21:06 +02:00
|
|
|
_ => {}
|
2015-11-26 19:26:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found_pub_static {
|
|
|
|
intravisit::walk_generics(self, g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// `type ... = ...;` can contain private types, because
|
|
|
|
// we're introducing a new name.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Ty(..) => return,
|
2015-11-26 19:26:15 +03:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Not at all public, so we don't care.
|
2016-03-25 06:08:11 +00:00
|
|
|
_ if !self.item_is_public(&item.id, &item.vis) => {
|
2015-11-26 19:26:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've carefully constructed it so that if we're here, then
|
|
|
|
// any `visit_ty`'s will be called on things that are in
|
2018-11-27 02:59:49 +00:00
|
|
|
// public signatures, i.e., things that we're interested in for
|
2015-11-26 19:26:15 +03:00
|
|
|
// this visitor.
|
|
|
|
intravisit::walk_item(self, item);
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:45:26 -05:00
|
|
|
fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
|
2018-06-23 00:21:35 +01:00
|
|
|
for param in &generics.params {
|
|
|
|
for bound in ¶m.bounds {
|
|
|
|
self.check_generic_bound(bound);
|
2015-11-26 19:26:15 +03:00
|
|
|
}
|
2018-06-23 00:21:35 +01:00
|
|
|
}
|
2015-11-26 19:26:15 +03:00
|
|
|
for predicate in &generics.where_clause.predicates {
|
|
|
|
match predicate {
|
2018-12-16 16:18:45 +03:00
|
|
|
hir::WherePredicate::BoundPredicate(bound_pred) => {
|
2015-11-26 19:26:15 +03:00
|
|
|
for bound in bound_pred.bounds.iter() {
|
2018-06-16 11:14:07 +01:00
|
|
|
self.check_generic_bound(bound)
|
2015-11-26 19:26:15 +03:00
|
|
|
}
|
|
|
|
}
|
2018-12-16 16:18:45 +03:00
|
|
|
hir::WherePredicate::RegionPredicate(_) => {}
|
|
|
|
hir::WherePredicate::EqPredicate(eq_pred) => {
|
2017-01-16 21:32:13 +03:00
|
|
|
self.visit_ty(&eq_pred.rhs_ty);
|
2015-11-26 19:26:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:45:26 -05:00
|
|
|
fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem) {
|
2015-11-26 19:26:15 +03:00
|
|
|
if self.access_levels.is_reachable(item.id) {
|
|
|
|
intravisit::walk_foreign_item(self, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:45:26 -05:00
|
|
|
fn visit_ty(&mut self, t: &'tcx hir::Ty) {
|
2018-07-11 22:41:03 +08:00
|
|
|
if let hir::TyKind::Path(hir::QPath::Resolved(_, ref path)) = t.node {
|
2016-11-25 13:21:19 +02:00
|
|
|
if self.path_is_private_type(path) {
|
2015-11-26 19:26:15 +03:00
|
|
|
self.old_error_set.insert(t.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
intravisit::walk_ty(self, t)
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:45:26 -05:00
|
|
|
fn visit_variant(&mut self,
|
|
|
|
v: &'tcx hir::Variant,
|
|
|
|
g: &'tcx hir::Generics,
|
|
|
|
item_id: ast::NodeId) {
|
2015-11-26 19:26:15 +03:00
|
|
|
if self.access_levels.is_reachable(v.node.data.id()) {
|
|
|
|
self.in_variant = true;
|
|
|
|
intravisit::walk_variant(self, v, g, item_id);
|
|
|
|
self.in_variant = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:45:26 -05:00
|
|
|
fn visit_struct_field(&mut self, s: &'tcx hir::StructField) {
|
2018-06-30 20:34:18 -07:00
|
|
|
if s.vis.node.is_pub() || self.in_variant {
|
2015-11-26 19:26:15 +03:00
|
|
|
intravisit::walk_struct_field(self, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// We don't need to introspect into these at all: an
|
2015-11-26 19:26:15 +03:00
|
|
|
// expression/block context can't possibly contain exported things.
|
|
|
|
// (Making them no-ops stops us from traversing the whole AST without
|
|
|
|
// having to be super careful about our `walk_...` calls above.)
|
2016-11-09 16:45:26 -05:00
|
|
|
fn visit_block(&mut self, _: &'tcx hir::Block) {}
|
|
|
|
fn visit_expr(&mut self, _: &'tcx hir::Expr) {}
|
2015-11-26 19:26:15 +03:00
|
|
|
}
|
|
|
|
|
2015-11-21 17:38:17 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// SearchInterfaceForPrivateItemsVisitor traverses an item's interface and
|
|
|
|
/// finds any private components in it.
|
|
|
|
/// PrivateItemsInPublicInterfacesVisitor ensures there are no private types
|
|
|
|
/// and traits in public interfaces.
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct SearchInterfaceForPrivateItemsVisitor<'a, 'tcx: 'a> {
|
2016-05-03 05:23:22 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2018-12-15 08:01:57 -05:00
|
|
|
item_id: ast::NodeId,
|
2016-11-12 12:24:17 +02:00
|
|
|
item_def_id: DefId,
|
|
|
|
span: Span,
|
2018-11-27 02:59:49 +00:00
|
|
|
/// The visitor checks that each component type is at least this visible.
|
2016-04-01 20:16:31 +00:00
|
|
|
required_visibility: ty::Visibility,
|
2017-03-17 20:38:32 -07:00
|
|
|
has_pub_restricted: bool,
|
2016-11-12 12:24:17 +02:00
|
|
|
has_old_errors: bool,
|
2017-11-18 20:32:24 +03:00
|
|
|
in_assoc_ty: bool,
|
2019-01-30 15:33:50 -05:00
|
|
|
private_crates: FxHashSet<CrateNum>
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
|
2015-11-27 01:48:26 +03:00
|
|
|
impl<'a, 'tcx: 'a> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
|
2016-11-12 12:24:17 +02:00
|
|
|
fn generics(&mut self) -> &mut Self {
|
2018-05-10 23:02:41 +01:00
|
|
|
for param in &self.tcx.generics_of(self.item_def_id).params {
|
2018-04-19 00:40:22 +01:00
|
|
|
match param.kind {
|
2018-05-16 13:03:04 +03:00
|
|
|
GenericParamDefKind::Type { has_default, .. } => {
|
|
|
|
if has_default {
|
2018-12-16 16:18:45 +03:00
|
|
|
self.visit(self.tcx.type_of(param.def_id));
|
2018-04-13 23:12:14 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-10 23:46:57 +01:00
|
|
|
GenericParamDefKind::Lifetime => {}
|
2017-01-25 22:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-12 12:24:17 +02:00
|
|
|
self
|
2016-04-01 20:16:31 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 12:24:17 +02:00
|
|
|
fn predicates(&mut self) -> &mut Self {
|
2018-11-27 02:59:49 +00:00
|
|
|
// N.B., we use `explicit_predicates_of` and not `predicates_of`
|
2018-08-28 00:46:08 -04:00
|
|
|
// because we don't want to report privacy errors due to where
|
|
|
|
// clauses that the compiler inferred. We only want to
|
|
|
|
// consider the ones that the user wrote. This is important
|
|
|
|
// for the inferred outlives rules; see
|
|
|
|
// `src/test/ui/rfc-2093-infer-outlives/privacy.rs`.
|
2018-12-16 16:18:45 +03:00
|
|
|
self.visit_predicates(self.tcx.explicit_predicates_of(self.item_def_id));
|
2016-11-12 12:24:17 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-04-24 15:20:46 +03:00
|
|
|
fn ty(&mut self) -> &mut Self {
|
2018-12-16 16:18:45 +03:00
|
|
|
self.visit(self.tcx.type_of(self.item_def_id));
|
2016-11-12 12:24:17 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-12-16 16:18:45 +03:00
|
|
|
fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
|
2019-01-13 18:07:42 -05:00
|
|
|
if self.leaks_private_dep(def_id) {
|
2019-01-20 22:04:22 -05:00
|
|
|
self.tcx.lint_node(lint::builtin::EXPORTED_PRIVATE_DEPENDENCIES,
|
2019-01-13 18:07:42 -05:00
|
|
|
self.item_id,
|
|
|
|
self.span,
|
|
|
|
&format!("{} `{}` from private dependency '{}' in public \
|
|
|
|
interface", kind, descr,
|
|
|
|
self.tcx.crate_name(def_id.krate)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-16 16:18:45 +03:00
|
|
|
let node_id = match self.tcx.hir().as_local_node_id(def_id) {
|
|
|
|
Some(node_id) => node_id,
|
|
|
|
None => return false,
|
|
|
|
};
|
2017-07-11 10:33:09 -04:00
|
|
|
|
2018-12-16 16:18:45 +03:00
|
|
|
let (vis, vis_span, vis_descr) = def_id_visibility(self.tcx, def_id);
|
|
|
|
if !vis.is_at_least(self.required_visibility, self.tcx) {
|
|
|
|
let msg = format!("{} {} `{}` in public interface", vis_descr, kind, descr);
|
|
|
|
if self.has_pub_restricted || self.has_old_errors || self.in_assoc_ty {
|
|
|
|
let mut err = if kind == "trait" {
|
|
|
|
struct_span_err!(self.tcx.sess, self.span, E0445, "{}", msg)
|
2017-07-11 10:33:09 -04:00
|
|
|
} else {
|
2018-12-16 16:18:45 +03:00
|
|
|
struct_span_err!(self.tcx.sess, self.span, E0446, "{}", msg)
|
|
|
|
};
|
|
|
|
err.span_label(self.span, format!("can't leak {} {}", vis_descr, kind));
|
|
|
|
err.span_label(vis_span, format!("`{}` declared as {}", descr, vis_descr));
|
|
|
|
err.emit();
|
|
|
|
} else {
|
|
|
|
let err_code = if kind == "trait" { "E0445" } else { "E0446" };
|
|
|
|
self.tcx.lint_node(lint::builtin::PRIVATE_IN_PUBLIC, node_id, self.span,
|
|
|
|
&format!("{} (error {})", msg, err_code));
|
2017-07-11 10:33:09 -04:00
|
|
|
}
|
2018-12-15 07:00:15 -05:00
|
|
|
|
2018-12-15 08:01:57 -05:00
|
|
|
}
|
|
|
|
|
2019-01-13 18:07:42 -05:00
|
|
|
false
|
2018-12-15 07:00:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An item is 'leaked' from a private dependency if all
|
|
|
|
/// of the following are true:
|
|
|
|
/// 1. It's contained within a public type
|
|
|
|
/// 2. It does not come from a crate marked as public
|
|
|
|
fn leaks_private_dep(&self, item_id: DefId) -> bool {
|
2019-01-30 15:33:50 -05:00
|
|
|
// Don't do any leak checking if no private crates were specified
|
|
|
|
if self.private_crates.is_empty() {
|
2018-12-15 07:00:15 -05:00
|
|
|
return false
|
|
|
|
}
|
2018-12-15 08:01:57 -05:00
|
|
|
let ret = self.required_visibility == ty::Visibility::Public &&
|
2018-12-15 07:00:15 -05:00
|
|
|
!item_id.is_local() &&
|
2019-01-30 15:33:50 -05:00
|
|
|
self.private_crates.contains(&item_id.krate);
|
2018-12-15 08:01:57 -05:00
|
|
|
|
|
|
|
|
|
|
|
debug!("leaks_private_dep(item_id={:?})={}", item_id, ret);
|
|
|
|
return ret;
|
2017-07-11 10:33:09 -04:00
|
|
|
}
|
2015-11-27 01:48:26 +03:00
|
|
|
}
|
|
|
|
|
2019-01-13 18:07:42 -05:00
|
|
|
impl<'a, 'tcx> DefIdVisitor<'a, 'tcx> for SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
|
|
|
|
fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> { self.tcx }
|
|
|
|
fn visit_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
|
|
|
|
self.check_def_id(def_id, kind, descr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-21 17:38:17 +03:00
|
|
|
struct PrivateItemsInPublicInterfacesVisitor<'a, 'tcx: 'a> {
|
2016-05-03 05:23:22 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-03-17 20:38:32 -07:00
|
|
|
has_pub_restricted: bool,
|
2015-11-26 19:26:15 +03:00
|
|
|
old_error_set: &'a NodeSet,
|
2019-01-30 15:33:50 -05:00
|
|
|
private_crates: FxHashSet<CrateNum>
|
2015-11-21 17:38:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> {
|
2016-11-12 12:24:17 +02:00
|
|
|
fn check(&self, item_id: ast::NodeId, required_visibility: ty::Visibility)
|
|
|
|
-> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
|
|
|
|
let mut has_old_errors = false;
|
|
|
|
|
|
|
|
// Slow path taken only if there any errors in the crate.
|
|
|
|
for &id in self.old_error_set {
|
|
|
|
// Walk up the nodes until we find `item_id` (or we hit a root).
|
|
|
|
let mut id = id;
|
|
|
|
loop {
|
|
|
|
if id == item_id {
|
|
|
|
has_old_errors = true;
|
|
|
|
break;
|
|
|
|
}
|
2018-12-04 13:45:36 +01:00
|
|
|
let parent = self.tcx.hir().get_parent_node(id);
|
2016-11-12 12:24:17 +02:00
|
|
|
if parent == id {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
id = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if has_old_errors {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-11-17 18:56:13 -05:00
|
|
|
|
2016-11-12 12:24:17 +02:00
|
|
|
SearchInterfaceForPrivateItemsVisitor {
|
|
|
|
tcx: self.tcx,
|
2018-12-15 08:01:57 -05:00
|
|
|
item_id,
|
2018-12-04 13:45:36 +01:00
|
|
|
item_def_id: self.tcx.hir().local_def_id(item_id),
|
|
|
|
span: self.tcx.hir().span(item_id),
|
2017-08-06 22:54:09 -07:00
|
|
|
required_visibility,
|
2017-03-17 20:38:32 -07:00
|
|
|
has_pub_restricted: self.has_pub_restricted,
|
2017-08-06 22:54:09 -07:00
|
|
|
has_old_errors,
|
2017-11-18 20:32:24 +03:00
|
|
|
in_assoc_ty: false,
|
2019-01-30 15:33:50 -05:00
|
|
|
private_crates: self.private_crates.clone()
|
2016-11-12 12:24:17 +02:00
|
|
|
}
|
2015-11-21 17:38:17 +03:00
|
|
|
}
|
2019-01-16 01:47:49 +03:00
|
|
|
|
|
|
|
fn check_trait_or_impl_item(&self, node_id: ast::NodeId, assoc_item_kind: AssociatedItemKind,
|
|
|
|
defaultness: hir::Defaultness, vis: ty::Visibility) {
|
|
|
|
let mut check = self.check(node_id, vis);
|
|
|
|
|
|
|
|
let (check_ty, is_assoc_ty) = match assoc_item_kind {
|
|
|
|
AssociatedItemKind::Const | AssociatedItemKind::Method { .. } => (true, false),
|
|
|
|
AssociatedItemKind::Type => (defaultness.has_value(), true),
|
|
|
|
// `ty()` for existential types is the underlying type,
|
|
|
|
// it's not a part of interface, so we skip it.
|
|
|
|
AssociatedItemKind::Existential => (false, true),
|
|
|
|
};
|
|
|
|
check.in_assoc_ty = is_assoc_ty;
|
|
|
|
check.generics().predicates();
|
|
|
|
if check_ty {
|
|
|
|
check.ty();
|
|
|
|
}
|
|
|
|
}
|
2015-11-21 17:38:17 +03:00
|
|
|
}
|
|
|
|
|
2016-11-28 18:10:37 +01:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> {
|
2016-11-28 14:00:26 -05:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2018-12-04 13:45:36 +01:00
|
|
|
NestedVisitorMap::OnlyBodies(&self.tcx.hir())
|
2016-11-28 18:10:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item) {
|
2016-11-12 12:24:17 +02:00
|
|
|
let tcx = self.tcx;
|
|
|
|
let item_visibility = ty::Visibility::from_hir(&item.vis, item.id, tcx);
|
2016-04-01 20:16:31 +00:00
|
|
|
|
2015-01-15 10:47:17 -08:00
|
|
|
match item.node {
|
2018-11-27 02:59:49 +00:00
|
|
|
// Crates are always public.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::ExternCrate(..) => {}
|
2018-11-27 02:59:49 +00:00
|
|
|
// All nested items are checked by `visit_item`.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Mod(..) => {}
|
2018-11-27 02:59:49 +00:00
|
|
|
// Checked in resolve.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Use(..) => {}
|
2018-11-27 02:59:49 +00:00
|
|
|
// No subitems.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::GlobalAsm(..) => {}
|
2018-11-27 02:59:49 +00:00
|
|
|
// Subitems of these items have inherited publicity.
|
2019-01-13 00:41:11 +03:00
|
|
|
hir::ItemKind::Const(..) | hir::ItemKind::Static(..) |
|
|
|
|
hir::ItemKind::Fn(..) | hir::ItemKind::Ty(..) => {
|
2017-04-24 15:20:46 +03:00
|
|
|
self.check(item.id, item_visibility).generics().predicates().ty();
|
2016-11-12 12:24:17 +02:00
|
|
|
}
|
2019-01-13 00:41:11 +03:00
|
|
|
hir::ItemKind::Existential(..) => {
|
|
|
|
// `ty()` for existential types is the underlying type,
|
|
|
|
// it's not a part of interface, so we skip it.
|
|
|
|
self.check(item.id, item_visibility).generics().predicates();
|
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Trait(.., ref trait_item_refs) => {
|
2016-11-12 12:24:17 +02:00
|
|
|
self.check(item.id, item_visibility).generics().predicates();
|
|
|
|
|
2016-12-04 04:21:06 +02:00
|
|
|
for trait_item_ref in trait_item_refs {
|
2019-01-16 01:47:49 +03:00
|
|
|
self.check_trait_or_impl_item(trait_item_ref.id.node_id, trait_item_ref.kind,
|
|
|
|
trait_item_ref.defaultness, item_visibility);
|
2016-11-12 12:24:17 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::TraitAlias(..) => {
|
2017-10-02 12:28:16 +00:00
|
|
|
self.check(item.id, item_visibility).generics().predicates();
|
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Enum(ref def, _) => {
|
2016-11-12 12:24:17 +02:00
|
|
|
self.check(item.id, item_visibility).generics().predicates();
|
|
|
|
|
|
|
|
for variant in &def.variants {
|
|
|
|
for field in variant.node.data.fields() {
|
2017-04-24 15:20:46 +03:00
|
|
|
self.check(field.id, item_visibility).ty();
|
2016-11-12 12:24:17 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
// Subitems of foreign modules have their own publicity.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::ForeignMod(ref foreign_mod) => {
|
2015-11-21 17:38:17 +03:00
|
|
|
for foreign_item in &foreign_mod.items {
|
2016-11-12 12:24:17 +02:00
|
|
|
let vis = ty::Visibility::from_hir(&foreign_item.vis, item.id, tcx);
|
2017-04-24 15:20:46 +03:00
|
|
|
self.check(foreign_item.id, vis).generics().predicates().ty();
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
2015-11-21 17:38:17 +03:00
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
// Subitems of structs and unions have their own publicity.
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Struct(ref struct_def, _) |
|
|
|
|
hir::ItemKind::Union(ref struct_def, _) => {
|
2016-11-12 12:24:17 +02:00
|
|
|
self.check(item.id, item_visibility).generics().predicates();
|
2016-04-01 20:16:31 +00:00
|
|
|
|
|
|
|
for field in struct_def.fields() {
|
2016-11-12 12:24:17 +02:00
|
|
|
let field_visibility = ty::Visibility::from_hir(&field.vis, item.id, tcx);
|
2018-12-16 16:18:45 +03:00
|
|
|
self.check(field.id, min(item_visibility, field_visibility, tcx)).ty();
|
2015-11-21 17:38:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// An inherent impl is public when its type is public
|
2018-11-27 02:59:49 +00:00
|
|
|
// Subitems of inherent impls have their own publicity.
|
2015-11-21 17:38:17 +03:00
|
|
|
// A trait impl is public when both its type and its trait are public
|
2018-11-27 02:59:49 +00:00
|
|
|
// Subitems of trait impls have inherited publicity.
|
2018-12-16 16:18:45 +03:00
|
|
|
hir::ItemKind::Impl(.., ref trait_ref, _, ref impl_item_refs) => {
|
2018-12-31 03:02:40 +03:00
|
|
|
let impl_vis = ty::Visibility::of_impl(item.id, tcx, &Default::default());
|
2018-12-16 16:18:45 +03:00
|
|
|
self.check(item.id, impl_vis).generics().predicates();
|
2016-11-10 09:47:00 -05:00
|
|
|
for impl_item_ref in impl_item_refs {
|
2018-12-16 16:18:45 +03:00
|
|
|
let impl_item = tcx.hir().impl_item(impl_item_ref.id);
|
|
|
|
let impl_item_vis = if trait_ref.is_none() {
|
|
|
|
min(ty::Visibility::from_hir(&impl_item.vis, item.id, tcx), impl_vis, tcx)
|
|
|
|
} else {
|
|
|
|
impl_vis
|
|
|
|
};
|
2019-01-16 01:47:49 +03:00
|
|
|
self.check_trait_or_impl_item(impl_item_ref.id.node_id, impl_item_ref.kind,
|
|
|
|
impl_item_ref.defaultness, impl_item_vis);
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 15:13:29 -04:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
|
|
|
*providers = Providers {
|
|
|
|
privacy_access_levels,
|
2018-06-08 19:14:03 +02:00
|
|
|
check_mod_privacy,
|
2017-03-23 15:13:29 -04:00
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-27 17:11:14 +01:00
|
|
|
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Lrc<AccessLevels> {
|
2017-10-23 19:08:08 -04:00
|
|
|
tcx.privacy_access_levels(LOCAL_CRATE)
|
2017-03-23 15:13:29 -04:00
|
|
|
}
|
|
|
|
|
2018-06-08 19:14:03 +02:00
|
|
|
fn check_mod_privacy<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>, module_def_id: DefId) {
|
2017-08-11 11:56:26 +02:00
|
|
|
let empty_tables = ty::TypeckTables::empty(None);
|
2015-01-15 10:47:17 -08:00
|
|
|
|
2018-12-15 07:00:15 -05:00
|
|
|
|
2017-04-16 21:19:24 +03:00
|
|
|
// Check privacy of names not checked in previous compilation stages.
|
|
|
|
let mut visitor = NamePrivacyVisitor {
|
2017-08-06 22:54:09 -07:00
|
|
|
tcx,
|
2017-08-04 09:49:40 +02:00
|
|
|
tables: &empty_tables,
|
2018-06-08 19:14:03 +02:00
|
|
|
current_item: DUMMY_NODE_ID,
|
2017-08-04 09:49:40 +02:00
|
|
|
empty_tables: &empty_tables,
|
2015-01-15 10:47:17 -08:00
|
|
|
};
|
2018-06-08 19:14:03 +02:00
|
|
|
let (module, span, node_id) = tcx.hir().get_module(module_def_id);
|
|
|
|
intravisit::walk_mod(&mut visitor, module, node_id);
|
2015-01-15 10:47:17 -08:00
|
|
|
|
2017-04-22 22:46:34 +03:00
|
|
|
// Check privacy of explicitly written types and traits as well as
|
|
|
|
// inferred types of expressions and patterns.
|
|
|
|
let mut visitor = TypePrivacyVisitor {
|
2017-08-06 22:54:09 -07:00
|
|
|
tcx,
|
2017-08-04 09:49:40 +02:00
|
|
|
tables: &empty_tables,
|
2018-06-08 19:14:03 +02:00
|
|
|
current_item: module_def_id,
|
2017-11-18 18:49:37 +03:00
|
|
|
in_body: false,
|
2018-06-08 19:14:03 +02:00
|
|
|
span,
|
2017-08-04 09:49:40 +02:00
|
|
|
empty_tables: &empty_tables,
|
2017-04-22 22:46:34 +03:00
|
|
|
};
|
2018-06-08 19:14:03 +02:00
|
|
|
intravisit::walk_mod(&mut visitor, module, node_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn privacy_access_levels<'tcx>(
|
|
|
|
tcx: TyCtxt<'_, 'tcx, 'tcx>,
|
|
|
|
krate: CrateNum,
|
|
|
|
) -> Lrc<AccessLevels> {
|
|
|
|
assert_eq!(krate, LOCAL_CRATE);
|
|
|
|
|
|
|
|
let krate = tcx.hir().krate();
|
|
|
|
|
2019-01-15 00:54:56 +01:00
|
|
|
for &module in krate.modules.keys() {
|
2018-06-08 19:14:03 +02:00
|
|
|
queries::check_mod_privacy::ensure(tcx, tcx.hir().local_def_id(module));
|
|
|
|
}
|
2017-04-22 22:46:34 +03:00
|
|
|
|
2019-01-30 15:33:50 -05:00
|
|
|
let private_crates: FxHashSet<CrateNum> = tcx.sess.opts.extern_private.iter()
|
|
|
|
.flat_map(|c| {
|
2019-01-29 16:16:57 -05:00
|
|
|
tcx.crates().iter().find(|&&krate| &tcx.crate_name(krate) == c).cloned()
|
2019-01-30 15:33:50 -05:00
|
|
|
}).collect();
|
2019-01-20 22:04:22 -05:00
|
|
|
|
|
|
|
|
2015-01-15 10:47:17 -08:00
|
|
|
// Build up a set of all exported items in the AST. This is a set of all
|
|
|
|
// items which are reachable from external crates based on visibility.
|
|
|
|
let mut visitor = EmbargoVisitor {
|
2017-08-06 22:54:09 -07:00
|
|
|
tcx,
|
2015-11-19 14:16:35 +03:00
|
|
|
access_levels: Default::default(),
|
|
|
|
prev_level: Some(AccessLevel::Public),
|
|
|
|
changed: false,
|
2015-01-15 10:47:17 -08:00
|
|
|
};
|
|
|
|
loop {
|
2015-11-17 18:56:13 -05:00
|
|
|
intravisit::walk_crate(&mut visitor, krate);
|
2015-11-19 14:16:35 +03:00
|
|
|
if visitor.changed {
|
|
|
|
visitor.changed = false;
|
|
|
|
} else {
|
2015-01-15 10:47:17 -08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 14:16:35 +03:00
|
|
|
visitor.update(ast::CRATE_NODE_ID, Some(AccessLevel::Public));
|
2015-11-26 19:26:15 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut visitor = ObsoleteVisiblePrivateTypesVisitor {
|
2017-08-06 22:54:09 -07:00
|
|
|
tcx,
|
2015-11-26 19:26:15 +03:00
|
|
|
access_levels: &visitor.access_levels,
|
|
|
|
in_variant: false,
|
2018-07-21 22:15:11 +03:00
|
|
|
old_error_set: Default::default(),
|
2015-11-26 19:26:15 +03:00
|
|
|
};
|
|
|
|
intravisit::walk_crate(&mut visitor, krate);
|
|
|
|
|
2017-03-17 20:38:32 -07:00
|
|
|
|
|
|
|
let has_pub_restricted = {
|
|
|
|
let mut pub_restricted_visitor = PubRestrictedVisitor {
|
2017-08-06 22:54:09 -07:00
|
|
|
tcx,
|
2017-03-17 20:38:32 -07:00
|
|
|
has_pub_restricted: false
|
|
|
|
};
|
|
|
|
intravisit::walk_crate(&mut pub_restricted_visitor, krate);
|
|
|
|
pub_restricted_visitor.has_pub_restricted
|
|
|
|
};
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Check for private types and traits in public interfaces.
|
2015-11-26 19:26:15 +03:00
|
|
|
let mut visitor = PrivateItemsInPublicInterfacesVisitor {
|
2017-08-06 22:54:09 -07:00
|
|
|
tcx,
|
|
|
|
has_pub_restricted,
|
2015-11-26 19:26:15 +03:00
|
|
|
old_error_set: &visitor.old_error_set,
|
2019-01-30 15:33:50 -05:00
|
|
|
private_crates
|
2015-11-26 19:26:15 +03:00
|
|
|
};
|
2016-11-12 12:24:17 +02:00
|
|
|
krate.visit_all_item_likes(&mut DeepVisitor::new(&mut visitor));
|
2015-11-26 19:26:15 +03:00
|
|
|
}
|
|
|
|
|
2018-02-27 17:11:14 +01:00
|
|
|
Lrc::new(visitor.access_levels)
|
2015-01-15 10:47:17 -08:00
|
|
|
}
|
2015-11-22 10:22:25 +05:30
|
|
|
|
|
|
|
__build_diagnostic_array! { librustc_privacy, DIAGNOSTICS }
|