Skip to content

Commit

Permalink
Auto merge of rust-lang#135501 - tgross35:stdlib-dependencies-private…
Browse files Browse the repository at this point in the history
…, r=<try>

Resolve `compiler_builtins` not being treated as private; clean up rust-lang#135278

Follow up of rust-lang#135278

try-job: test-various
  • Loading branch information
bors committed Jan 28, 2025
2 parents bf1b174 + 9c9ea48 commit a36bbad
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 43 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_builtin_macros/src/standard_library_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub fn inject(
//
// FIXME(#113634) We should inject this during post-processing like
// we do for the panic runtime, profiler runtime, etc.
//
// See also `is_private_dep` within `rustc_metadata`.
cx.item(
span,
Ident::new(kw::Underscore, ident_span),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ index 7165c3e48af..968552ad435 100644

[dependencies]
core = { path = "../core" }
-compiler_builtins = { version = "=0.1.143", features = ['rustc-dep-of-std'] }
+compiler_builtins = { version = "=0.1.143", features = ['rustc-dep-of-std', 'no-f16-f128'] }
-compiler_builtins = { version = "=0.1.144", features = ['rustc-dep-of-std'] }
+compiler_builtins = { version = "=0.1.144", features = ['rustc-dep-of-std', 'no-f16-f128'] }

[dev-dependencies]
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }
Expand Down
58 changes: 27 additions & 31 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use rustc_session::lint::{self, BuiltinLintDiag};
use rustc_session::output::validate_crate_name;
use rustc_session::search_paths::PathKind;
use rustc_span::edition::Edition;
use rustc_span::{DUMMY_SP, Ident, STDLIB_STABLE_CRATES, Span, Symbol, sym};
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
use rustc_target::spec::{PanicStrategy, Target, TargetTuple};
use tracing::{debug, info, trace};

Expand Down Expand Up @@ -400,26 +400,12 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
/// Sometimes the directly dependent crate is not specified by `--extern`, in this case,
/// `private-dep` is none during loading. This is equivalent to the scenario where the
/// command parameter is set to `public-dependency`
fn is_private_dep(
&self,
name: Symbol,
private_dep: Option<bool>,
dep_root: Option<&CratePaths>,
) -> bool {
// Standard library crates are never private.
if STDLIB_STABLE_CRATES.contains(&name) {
tracing::info!("returning false for {name} is private");
return false;
}

fn is_private_dep(&self, name: Symbol, private_dep: Option<bool>) -> bool {
let extern_private = self.sess.opts.externs.get(name.as_str()).map(|e| e.is_private_dep);

// Any descendants of `std` should be private. These crates are usually not marked
// private in metadata, so we ignore that field.
if extern_private.is_none()
&& let Some(dep) = dep_root
&& STDLIB_STABLE_CRATES.contains(&dep.name)
{
if name == sym::compiler_builtins {
// compiler_builtins is a private implementation detail and should never show up in
// diagnostics. See also the note referencing #113634 in
// `rustc_builtin_macros::...::inject`.
return true;
}

Expand Down Expand Up @@ -447,7 +433,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
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 private_dep = self.is_private_dep(name, private_dep, dep_root);
let private_dep = self.is_private_dep(name, private_dep);

// Claim this crate number and cache it
let feed = self.cstore.intern_stable_crate_id(&crate_root, self.tcx)?;
Expand All @@ -470,7 +456,8 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
&crate_paths
};

let cnum_map = self.resolve_crate_deps(dep_root, &crate_root, &metadata, cnum, dep_kind)?;
let cnum_map =
self.resolve_crate_deps(dep_root, &crate_root, &metadata, cnum, dep_kind, private_dep)?;

let raw_proc_macros = if crate_root.is_proc_macro_crate() {
let temp_root;
Expand Down Expand Up @@ -573,15 +560,16 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
dep_kind: CrateDepKind,
) -> Option<CrateNum> {
self.used_extern_options.insert(name);
match self.maybe_resolve_crate(name, dep_kind, None) {
match self.maybe_resolve_crate(name, dep_kind, None, false) {
Ok(cnum) => {
self.cstore.set_used_recursively(cnum);
Some(cnum)
}
Err(err) => {
debug!("failed to resolve crate {} {:?}", name, dep_kind);
let missing_core =
self.maybe_resolve_crate(sym::core, CrateDepKind::Explicit, None).is_err();
let missing_core = self
.maybe_resolve_crate(sym::core, CrateDepKind::Explicit, None, false)
.is_err();
err.report(self.sess, span, missing_core);
None
}
Expand All @@ -593,6 +581,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
name: Symbol,
mut dep_kind: CrateDepKind,
dep_of: Option<(&'b CratePaths, &'b CrateDep)>,
parent_is_private: bool,
) -> Result<CrateNum, CrateError> {
info!("resolving crate `{}`", name);
if !name.as_str().is_ascii() {
Expand All @@ -605,7 +594,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
let host_hash = dep.map(|d| d.host_hash).flatten();
let extra_filename = dep.map(|d| &d.extra_filename[..]);
let path_kind = if dep.is_some() { PathKind::Dependency } else { PathKind::Crate };
let private_dep = dep.map(|d| d.is_private);
let private_dep = dep.map(|d| d.is_private || parent_is_private);

let result = if let Some(cnum) = self.existing_match(name, hash, path_kind) {
(LoadResult::Previous(cnum), None)
Expand Down Expand Up @@ -643,7 +632,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
// not specified by `--extern` on command line parameters, it may be
// `private-dependency` when `register_crate` is called for the first time. Then it must be updated to
// `public-dependency` here.
let private_dep = self.is_private_dep(name, private_dep, dep_root);
let private_dep = self.is_private_dep(name, private_dep);
let data = self.cstore.get_crate_data_mut(cnum);
if data.is_proc_macro_crate() {
dep_kind = CrateDepKind::MacrosOnly;
Expand Down Expand Up @@ -702,6 +691,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
metadata: &MetadataBlob,
krate: CrateNum,
dep_kind: CrateDepKind,
parent_is_private: bool,
) -> Result<CrateNumMap, CrateError> {
debug!(
"resolving deps of external crate `{}` with dep root `{}`",
Expand All @@ -720,17 +710,23 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
crate_num_map.push(krate);
for dep in deps {
info!(
"resolving dep `{}`->`{}` hash: `{}` extra filename: `{}`",
"resolving dep `{}`->`{}` hash: `{}` extra filename: `{}` private {}",
crate_root.name(),
dep.name,
dep.hash,
dep.extra_filename
dep.extra_filename,
dep.is_private,
);
let dep_kind = match dep_kind {
CrateDepKind::MacrosOnly => CrateDepKind::MacrosOnly,
_ => dep.kind,
};
let cnum = self.maybe_resolve_crate(dep.name, dep_kind, Some((dep_root, &dep)))?;
let cnum = self.maybe_resolve_crate(
dep.name,
dep_kind,
Some((dep_root, &dep)),
parent_is_private,
)?;
crate_num_map.push(cnum);
}

Expand Down Expand Up @@ -1147,7 +1143,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
}

pub fn maybe_process_path_extern(&mut self, name: Symbol) -> Option<CrateNum> {
self.maybe_resolve_crate(name, CrateDepKind::Explicit, None).ok()
self.maybe_resolve_crate(name, CrateDepKind::Explicit, None, false).ok()
}
}

Expand Down
4 changes: 2 additions & 2 deletions library/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ dependencies = [

[[package]]
name = "compiler_builtins"
version = "0.1.143"
version = "0.1.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c85ba2077e3eab3dd81be4ece6b7fb2ad0887c1fb813e9a45400baf75c6c7c29"
checksum = "d18a7b7b5a56aa131e62314b4d862c9f6aa2860f615f3770094ec9064d7ec572"
dependencies = [
"cc",
"rustc-std-workspace-core",
Expand Down
6 changes: 4 additions & 2 deletions library/alloc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cargo-features = ["public-dependency"]

[package]
name = "alloc"
version = "0.0.0"
Expand All @@ -9,8 +11,8 @@ autobenches = false
edition = "2021"

[dependencies]
core = { path = "../core" }
compiler_builtins = { version = "=0.1.143", features = ['rustc-dep-of-std'] }
core = { path = "../core", public = true }
compiler_builtins = { version = "=0.1.144", features = ['rustc-dep-of-std'] }

[dev-dependencies]
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }
Expand Down
2 changes: 1 addition & 1 deletion library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cfg-if = { version = "1.0", features = ['rustc-dep-of-std'] }
panic_unwind = { path = "../panic_unwind", optional = true }
panic_abort = { path = "../panic_abort" }
core = { path = "../core", public = true }
compiler_builtins = { version = "=0.1.143" }
compiler_builtins = { version = "=0.1.144" }
unwind = { path = "../unwind" }
hashbrown = { version = "0.15", default-features = false, features = [
'rustc-dep-of-std',
Expand Down
8 changes: 5 additions & 3 deletions library/sysroot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
cargo-features = ["public-dependency"]

[package]
name = "sysroot"
version = "0.0.0"
edition = "2021"

# this is a dummy crate to ensure that all required crates appear in the sysroot
[dependencies]
proc_macro = { path = "../proc_macro" }
proc_macro = { path = "../proc_macro", public = true }
profiler_builtins = { path = "../profiler_builtins", optional = true }
std = { path = "../std" }
test = { path = "../test" }
std = { path = "../std", public = true }
test = { path = "../test", public = true }

# Forward features to the `std` crate as necessary
[features]
Expand Down
6 changes: 4 additions & 2 deletions library/test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
cargo-features = ["public-dependency"]

[package]
name = "test"
version = "0.0.0"
edition = "2021"

[dependencies]
getopts = { version = "0.2.21", features = ['rustc-dep-of-std'] }
std = { path = "../std" }
core = { path = "../core" }
std = { path = "../std", public = true }
core = { path = "../core", public = true }

[target.'cfg(not(all(windows, target_env = "msvc")))'.dependencies]
libc = { version = "0.2.150", default-features = false }

0 comments on commit a36bbad

Please sign in to comment.