Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4626,7 +4626,6 @@ dependencies = [
"rustc_middle",
"rustc_span",
"rustc_ty_walk",
"tracing",
]

[[package]]
Expand Down
13 changes: 12 additions & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use rustc_errors::codes::*;
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, ErrorGuaranteed};
use rustc_hir::attrs::lang_items::LangItem;
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId, LocalDefIdMap};
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalDefIdMap};
use rustc_hir::definitions::PerParentDisambiguatorState;
use rustc_hir::lints::DelayedLint;
use rustc_hir::{
Expand Down Expand Up @@ -182,6 +182,7 @@ struct LoweringContext<'a, 'hir> {
owner: &'a PerOwnerResolverData<'hir>,
item_local_id_counter: hir::ItemLocalId,
trait_map: ItemLocalMap<&'hir [TraitCandidate<'hir>]>,
paths_from_private_deps: SortedMap<hir::ItemLocalId, CrateNum>,

impl_trait_defs: Vec<hir::GenericParam<'hir>>,
impl_trait_bounds: Vec<hir::WherePredicate<'hir>>,
Expand Down Expand Up @@ -254,6 +255,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
relowering_checker: Default::default(),

trait_map: Default::default(),
paths_from_private_deps: SortedMap::default(),
next_node_id: resolver.next_node_id,
node_id_to_def_id: NodeMap::default(),
partial_res_overrides: NodeMap::default(),
Expand Down Expand Up @@ -843,6 +845,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
#[cfg(debug_assertions)]
let current_relowering_checker = mem::take(&mut self.relowering_checker);
let current_trait_map = mem::take(&mut self.trait_map);
let current_paths_from_private_deps = mem::take(&mut self.paths_from_private_deps);
let current_owner = mem::replace(&mut self.current_hir_id_owner, owner_id);
let current_local_counter =
mem::replace(&mut self.item_local_id_counter, hir::ItemLocalId::new(1));
Expand Down Expand Up @@ -878,6 +881,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.relowering_checker = current_relowering_checker;
}
self.trait_map = current_trait_map;
self.paths_from_private_deps = current_paths_from_private_deps;
self.current_hir_id_owner = current_owner;
self.item_local_id_counter = current_local_counter;
self.impl_trait_defs = current_impl_trait_defs;
Expand All @@ -895,6 +899,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let mut bodies = mem::take(&mut self.bodies);
let define_opaque = mem::take(&mut self.define_opaque);
let trait_map = mem::take(&mut self.trait_map);
let paths_from_private_deps = mem::take(&mut self.paths_from_private_deps);
let delayed_lints = Steal::new(mem::take(&mut self.delayed_lints).into_boxed_slice());
let children = mem::take(&mut self.children);

Expand Down Expand Up @@ -926,6 +931,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
parenting.stable_hash(&mut hcx, &mut stable_hasher);
trait_map.stable_hash(&mut hcx, &mut stable_hasher);
children.stable_hash(&mut hcx, &mut stable_hasher);
paths_from_private_deps.stable_hash(&mut hcx, &mut stable_hasher);
stable_hasher.finish()
})
});
Expand All @@ -938,6 +944,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
trait_map,
delayed_lints,
children,
paths_from_private_deps,
})
}

Expand All @@ -964,6 +971,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.trait_map.insert(hir_id.local_id, *traits);
}

if let Some(&krate) = self.owner.paths_from_private_deps.get(&ast_node_id) {
self.paths_from_private_deps.insert(hir_id.local_id, krate);
}

// Check whether the same `NodeId` is lowered more than once.
#[cfg(debug_assertions)]
self.relowering_checker.assert_node_is_not_relowered(ast_node_id, local_id);
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use rustc_target::asm::InlineAsmRegOrRegClass;
use tracing::debug;

use crate::def::{CtorKind, DefKind, MacroKinds, PerNS, Res};
use crate::def_id::{DefId, LocalDefIdMap};
use crate::def_id::{CrateNum, DefId, LocalDefIdMap};
use crate::intravisit::{FnKind, VisitorExt};
use crate::lints::DelayedLints;

Expand Down Expand Up @@ -1388,6 +1388,9 @@ pub struct OwnerInfo<'hir> {
pub trait_map: ItemLocalMap<&'hir [TraitCandidate<'hir>]>,
/// Owners generated as side-effect by lowering.
pub children: UnordMap<LocalDefId, MaybeOwner<'hir>>,
/// Map from each nested owner that is a type imported from a
/// private dependency; generated by resolve.
pub paths_from_private_deps: SortedMap<ItemLocalId, CrateNum>,

/// Lints delayed during ast lowering to be emitted
/// after hir has completely built
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,10 @@ impl CStore {
}
}

fn is_extern_private_dep(&self, externs: &Externs, name: Symbol) -> bool {
externs.get(name.as_str()).map(|e| e.is_private_dep).unwrap_or_default()
}

fn register_crate<'tcx>(
&mut self,
tcx: TyCtxt<'tcx>,
Expand All @@ -593,6 +597,7 @@ impl CStore {
let Library { source, metadata } = lib;
let crate_root = metadata.get_root();
let host_hash = host_lib.as_ref().map(|lib| lib.metadata.get_root().hash());
let extern_private_dep = self.is_extern_private_dep(&tcx.sess.opts.externs, name);
let private_dep = self.is_private_dep(&tcx.sess.opts.externs, name, private_dep);

// Claim this crate number and cache it
Expand Down Expand Up @@ -651,6 +656,7 @@ impl CStore {
dep_kind,
source,
private_dep,
extern_private_dep,
host_hash,
);

Expand Down
21 changes: 18 additions & 3 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ pub(crate) struct CrateMetadata {
/// Used by the 'exported_private_dependencies' lint, and for determining
/// whether to emit suggestions that reference this crate.
private_dep: bool,
/// Whether or not this is explicitly marked as a private dependency, specifically whether
/// `--extern` contained `priv:`. This is not the same thing as `private_dep` as some
/// dependencies might be marked `priv:` but are public dependencies transitively.
extern_private_dep: bool,
/// The hash for the host proc macro. Used to support `-Z dual-proc-macro`.
host_hash: Option<Svh>,
/// The crate was used non-speculatively.
Expand Down Expand Up @@ -802,14 +806,23 @@ impl MetadataBlob {
let dylib_dependency_formats =
root.dylib_dependency_formats.decode(self).collect::<Vec<_>>();
for (i, dep) in root.crate_deps.decode(self).enumerate() {
let CrateDep { name, extra_filename, hash, host_hash, kind, is_private } =
dep;
let CrateDep {
name,
extra_filename,
hash,
host_hash,
kind,
is_private,
is_extern_private_dep,
} = dep;
let number = i + 1;

writeln!(
out,
"{number} {name}{extra_filename} hash {hash} host_hash {host_hash:?} kind {kind:?} {privacy}{linkage}",
"{number} {name}{extra_filename} hash {hash} host_hash {host_hash:?} kind {kind:?} {privacy}{direct_privacy}{linkage}",
privacy = if is_private { "private" } else { "public" },
direct_privacy =
if is_extern_private_dep { " extern:priv" } else { "" },
linkage = if dylib_dependency_formats.is_empty() {
String::new()
} else {
Expand Down Expand Up @@ -1950,6 +1963,7 @@ impl CrateMetadata {
dep_kind: CrateDepKind,
source: CrateSource,
private_dep: bool,
extern_private_dep: bool,
host_hash: Option<Svh>,
) -> CrateMetadata {
let trait_impls = root
Expand Down Expand Up @@ -1979,6 +1993,7 @@ impl CrateMetadata {
dep_kind,
source: Arc::new(source),
private_dep,
extern_private_dep,
host_hash,
used: false,
extern_crate: None,
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ provide! { tcx, def_id, other, cdata,

dylib_dependency_formats => { cdata.get_dylib_dependency_formats(tcx) }
is_private_dep => { cdata.private_dep }
is_extern_private_dep => { cdata.extern_private_dep }
is_panic_runtime => { cdata.root.panic_runtime }
is_compiler_builtins => { cdata.root.compiler_builtins }

Expand Down Expand Up @@ -437,6 +438,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
allocator_kind: |tcx, ()| CStore::from_tcx(tcx).allocator_kind(),
alloc_error_handler_kind: |tcx, ()| CStore::from_tcx(tcx).alloc_error_handler_kind(),
is_private_dep: |_tcx, LocalCrate| false,
is_extern_private_dep: |_tcx, LocalCrate| false,
native_library: |tcx, id| {
tcx.native_libraries(id.krate)
.iter()
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2097,6 +2097,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
kind: self.tcx.crate_dep_kind(cnum),
extra_filename: self.tcx.extra_filename(cnum).clone(),
is_private: self.tcx.is_private_dep(cnum),
is_extern_private_dep: self.tcx.is_extern_private_dep(cnum),
};
(cnum, dep)
})
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ pub(crate) struct CrateDep {
pub kind: CrateDepKind,
pub extra_filename: String,
pub is_private: bool,
pub is_extern_private_dep: bool,
}

#[derive(MetadataEncodable, LazyDecodable)]
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,6 @@ pub fn provide(providers: &mut Providers) {
|tcx, trait_id| tcx.resolutions(()).trait_impls.get(&trait_id).map_or(&[], |xs| &xs[..]);
providers.expn_that_defined =
|tcx, id| tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root());
providers.paths_from_private_deps =
|tcx, id| tcx.lower_to_hir(id).as_owner().map(|owner| &owner.paths_from_private_deps);
}
10 changes: 10 additions & 0 deletions compiler/rustc_middle/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2359,6 +2359,16 @@ rustc_queries! {
desc { "checking whether crate `{}` is a private dependency", c }
separate_provide_extern
}
/// Returns whether or not the crate with CrateNum 'cnum'
/// is marked as a direct public dependency
query is_extern_private_dep(c: CrateNum) -> bool {
eval_always
desc { "checking whether crate `{}` is a direct public dependency", c }
separate_provide_extern
}
query paths_from_private_deps(key: hir::OwnerId) -> Option<&'tcx SortedMap<hir::ItemLocalId, CrateNum>> {
desc { "getting paths imported from private dependencies in `{}`", tcx.def_path_str(key) }
}
query allocator_kind(_: ()) -> Option<AllocatorKind> {
eval_always
desc { "getting the allocator kind for the current crate" }
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,10 @@ impl<'tcx> TyCtxt<'tcx> {
None => Err(VarError::NotPresent),
}
}

pub fn path_pointing_to_private_crate(self, hir_id: HirId) -> Option<CrateNum> {
self.paths_from_private_deps(hir_id.owner)?.get(&hir_id.local_id).copied()
}
}

impl<'tcx> TyCtxtAt<'tcx> {
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ pub struct PerOwnerResolverData<'tcx> {
/// Lifetime parameters that lowering will have to introduce.
pub extra_lifetime_params_map: NodeMap<Vec<(Ident, ast::NodeId, MissingLifetimeKind)>> = Default::default(),

/// Paths that use items exported or re-exported from a private dependency.
pub paths_from_private_deps: NodeMap<CrateNum> = Default::default(),

/// The id of the owner
pub id: ast::NodeId,
/// The `DefId` of the owner, can't be found in `node_id_to_def_id`.
Expand Down Expand Up @@ -262,6 +265,8 @@ pub struct ResolverAstLowering<'tcx> {
/// Resolutions for nodes that have a single resolution.
pub partial_res_map: NodeMap<hir::def::PartialRes>,

pub paths_from_private_deps: NodeMap<CrateNum>,

pub next_node_id: ast::NodeId,

pub owners: NodeMap<PerOwnerResolverData<'tcx>>,
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_privacy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ rustc_macros = { path = "../rustc_macros" }
rustc_middle = { path = "../rustc_middle" }
rustc_span = { path = "../rustc_span" }
rustc_ty_walk = { path = "../rustc_ty_walk" }
tracing = "0.1"
# tidy-alphabetical-end
Loading
Loading