Skip to content

Commit d1dc4be

Browse files
authored
Rollup merge of rust-lang#122757 - h1467792822:priv-dep, r=davidtwco
Fixed the `private-dependency` bug Fixed the private-dependency bug: If the directly dependent crate is loaded last and is not configured with `--extern`, it may be incorrectly set to `private-dependency` Fixes rust-lang#122756
2 parents 388da0a + 92325a9 commit d1dc4be

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

compiler/rustc_metadata/src/creader.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,15 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
389389
None
390390
}
391391

392+
// The `dependency` type is determined by the command line arguments(`--extern`) and
393+
// `private_dep`. However, sometimes the directly dependent crate is not specified by
394+
// `--extern`, in this case, `private-dep` is none during loading. This is equivalent to the
395+
// scenario where the command parameter is set to `public-dependency`
396+
fn is_private_dep(&self, name: &str, private_dep: Option<bool>) -> bool {
397+
self.sess.opts.externs.get(name).map_or(private_dep.unwrap_or(false), |e| e.is_private_dep)
398+
&& private_dep.unwrap_or(true)
399+
}
400+
392401
fn register_crate(
393402
&mut self,
394403
host_lib: Option<Library>,
@@ -404,14 +413,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
404413
let Library { source, metadata } = lib;
405414
let crate_root = metadata.get_root();
406415
let host_hash = host_lib.as_ref().map(|lib| lib.metadata.get_root().hash());
407-
408-
let private_dep = self
409-
.sess
410-
.opts
411-
.externs
412-
.get(name.as_str())
413-
.map_or(private_dep.unwrap_or(false), |e| e.is_private_dep)
414-
&& private_dep.unwrap_or(true);
416+
let private_dep = self.is_private_dep(name.as_str(), private_dep);
415417

416418
// Claim this crate number and cache it
417419
let cnum = self.cstore.intern_stable_crate_id(&crate_root)?;
@@ -601,14 +603,17 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
601603

602604
match result {
603605
(LoadResult::Previous(cnum), None) => {
606+
// When `private_dep` is none, it indicates the directly dependent crate. If it is
607+
// not specified by `--extern` on command line parameters, it may be
608+
// `private-dependency` when `register_crate` is called for the first time. Then it must be updated to
609+
// `public-dependency` here.
610+
let private_dep = self.is_private_dep(name.as_str(), private_dep);
604611
let data = self.cstore.get_crate_data_mut(cnum);
605612
if data.is_proc_macro_crate() {
606613
dep_kind = CrateDepKind::MacrosOnly;
607614
}
608615
data.set_dep_kind(cmp::max(data.dep_kind(), dep_kind));
609-
if let Some(private_dep) = private_dep {
610-
data.update_and_private_dep(private_dep);
611-
}
616+
data.update_and_private_dep(private_dep);
612617
Ok(cnum)
613618
}
614619
(LoadResult::Loaded(library), host_library) => {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//@ aux-crate:priv:foo=foo.rs
2+
//@ compile-flags: -Zunstable-options
3+
4+
#![crate_type = "rlib"]
5+
extern crate foo;
6+
pub struct Bar(pub i32);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#![crate_type = "rlib"]
2+
pub struct Foo(pub i32);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ aux-build: bar.rs
2+
//@ aux-build: foo.rs
3+
//@ build-pass
4+
5+
#![deny(exported_private_dependencies)]
6+
7+
// Ensure the libbar.rlib is loaded first. If the command line parameter `--extern foo` does not
8+
// exist, previus version would fail to compile
9+
#![crate_type = "rlib"]
10+
extern crate bar;
11+
extern crate foo;
12+
pub fn baz() -> (Option<foo::Foo>, Option<bar::Bar>) { (None, None) }

0 commit comments

Comments
 (0)