Skip to content

Exclude dependencies of std for diagnostics #135278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
49 changes: 29 additions & 20 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,12 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
None
}

// The `dependency` type is determined by the command line arguments(`--extern`) and
// `private_dep`. However, 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`
/// The `dependency` type is determined by the command line arguments(`--extern`) and
/// `private_dep`.
///
/// 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: &str, private_dep: Option<bool>) -> bool {
self.sess.opts.externs.get(name).map_or(private_dep.unwrap_or(false), |e| e.is_private_dep)
&& private_dep.unwrap_or(true)
Expand All @@ -402,7 +404,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
fn register_crate(
&mut self,
host_lib: Option<Library>,
root: Option<&CratePaths>,
dep_root: Option<&CratePaths>,
lib: Library,
dep_kind: CrateDepKind,
name: Symbol,
Expand Down Expand Up @@ -430,14 +432,14 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
// Maintain a reference to the top most crate.
// Stash paths for top-most crate locally if necessary.
let crate_paths;
let root = if let Some(root) = root {
root
let dep_root = if let Some(dep_root) = dep_root {
dep_root
} else {
crate_paths = CratePaths::new(crate_root.name(), source.clone());
&crate_paths
};

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

let raw_proc_macros = if crate_root.is_proc_macro_crate() {
let temp_root;
Expand Down Expand Up @@ -559,15 +561,15 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
&'b mut self,
name: Symbol,
mut dep_kind: CrateDepKind,
dep: Option<(&'b CratePaths, &'b CrateDep)>,
dep_of: Option<(&'b CratePaths, &'b CrateDep)>,
) -> Result<CrateNum, CrateError> {
info!("resolving crate `{}`", name);
if !name.as_str().is_ascii() {
return Err(CrateError::NonAsciiName(name));
}
let (root, hash, host_hash, extra_filename, path_kind, private_dep) = match dep {
Some((root, dep)) => (
Some(root),
let (dep_root, hash, host_hash, extra_filename, path_kind, private_dep) = match dep_of {
Some((dep_root, dep)) => (
Some(dep_root),
Some(dep.hash),
dep.host_hash,
Some(&dep.extra_filename[..]),
Expand Down Expand Up @@ -599,7 +601,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
dep_kind = CrateDepKind::MacrosOnly;
match self.load_proc_macro(&mut locator, path_kind, host_hash)? {
Some(res) => res,
None => return Err(locator.into_error(root.cloned())),
None => return Err(locator.into_error(dep_root.cloned())),
}
}
}
Expand All @@ -623,7 +625,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
}
(LoadResult::Loaded(library), host_library) => {
info!("register newly loaded library for `{}`", name);
self.register_crate(host_library, root, library, dep_kind, name, private_dep)
self.register_crate(host_library, dep_root, library, dep_kind, name, private_dep)
}
_ => panic!(),
}
Expand Down Expand Up @@ -663,16 +665,20 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
}))
}

// Go through the crate metadata and load any crates that it references
/// Go through the crate metadata and load any crates that it references.
fn resolve_crate_deps(
&mut self,
root: &CratePaths,
dep_root: &CratePaths,
crate_root: &CrateRoot,
metadata: &MetadataBlob,
krate: CrateNum,
dep_kind: CrateDepKind,
) -> Result<CrateNumMap, CrateError> {
debug!("resolving deps of external crate");
debug!(
"resolving deps of external crate `{}` with dep root `{}`",
crate_root.name(),
dep_root.name
);
if crate_root.is_proc_macro_crate() {
return Ok(CrateNumMap::new());
}
Expand All @@ -685,14 +691,17 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
crate_num_map.push(krate);
for dep in deps {
info!(
"resolving dep crate {} hash: `{}` extra filename: `{}`",
dep.name, dep.hash, dep.extra_filename
"resolving dep `{}`->`{}` hash: `{}` extra filename: `{}`",
crate_root.name(),
dep.name,
dep.hash,
dep.extra_filename
);
let dep_kind = match dep_kind {
CrateDepKind::MacrosOnly => CrateDepKind::MacrosOnly,
_ => dep.kind,
};
let cnum = self.maybe_resolve_crate(dep.name, dep_kind, Some((root, &dep)))?;
let cnum = self.maybe_resolve_crate(dep.name, dep_kind, Some((dep_root, &dep)))?;
crate_num_map.push(cnum);
}

Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ pub(crate) struct CrateLocator<'a> {

#[derive(Clone)]
pub(crate) struct CratePaths {
name: Symbol,
pub(crate) name: Symbol,
source: CrateSource,
}

Expand Down Expand Up @@ -765,10 +765,10 @@ impl<'a> CrateLocator<'a> {
self.extract_lib(rlibs, rmetas, dylibs).map(|opt| opt.map(|(_, lib)| lib))
}

pub(crate) fn into_error(self, root: Option<CratePaths>) -> CrateError {
pub(crate) fn into_error(self, dep_root: Option<CratePaths>) -> CrateError {
CrateError::LocatorCombined(Box::new(CombinedLocatorError {
crate_name: self.crate_name,
root,
dep_root,
triple: self.tuple,
dll_prefix: self.target.dll_prefix.to_string(),
dll_suffix: self.target.dll_suffix.to_string(),
Expand Down Expand Up @@ -914,7 +914,7 @@ struct CrateRejections {
/// otherwise they are ignored.
pub(crate) struct CombinedLocatorError {
crate_name: Symbol,
root: Option<CratePaths>,
dep_root: Option<CratePaths>,
triple: TargetTuple,
dll_prefix: String,
dll_suffix: String,
Expand Down Expand Up @@ -987,7 +987,7 @@ impl CrateError {
}
CrateError::LocatorCombined(locator) => {
let crate_name = locator.crate_name;
let add_info = match &locator.root {
let add_info = match &locator.dep_root {
None => String::new(),
Some(r) => format!(" which `{}` depends on", r.name),
};
Expand All @@ -1012,7 +1012,7 @@ impl CrateError {
path.display()
));
}
if let Some(r) = locator.root {
if let Some(r) = locator.dep_root {
for path in r.source.paths() {
found_crates.push_str(&format!(
"\ncrate `{}`: {}",
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,11 @@ impl<'tcx> TyCtxt<'tcx> {
/// [public]: TyCtxt::is_private_dep
/// [direct]: rustc_session::cstore::ExternCrate::is_direct
pub fn is_user_visible_dep(self, key: CrateNum) -> bool {
// `#![rustc_private]` overrides defaults to make private dependencies usable.
if self.features().enabled(sym::rustc_private) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, this is a very internal feature, so it doesn't seem worth doing any additional filtering here.

Not really. There are a lot of external custom drivers that need to enable this feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you suggesting that it is worth filtering here to only expose crates that are dependencies of std? Or just that the message is inaccurate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the override for rustc_private should be removed. Even with rustc_private enabled you generally shouldn't do something like extern crate libc; from the sysroot.

return true;
}

// | Private | Direct | Visible | |
// |---------|--------|---------|--------------------|
// | Yes | Yes | Yes | !true || true |
Expand Down
39 changes: 39 additions & 0 deletions tests/ui/privacy/sysroot-private.default.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
error[E0405]: cannot find trait `Equivalent` in this scope
--> $DIR/sysroot-private.rs:26:18
|
LL | trait Trait2<K>: Equivalent<K> {}
| ^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `K` in this scope
--> $DIR/sysroot-private.rs:31:35
|
LL | fn trait_member<T>(val: &T, key: &K) -> bool {
| - ^
| |
| similarly named type parameter `T` defined here
|
help: a type parameter with a similar name exists
|
LL | fn trait_member<T>(val: &T, key: &T) -> bool {
| ~
help: you might be missing a type parameter
|
LL | fn trait_member<T, K>(val: &T, key: &K) -> bool {
| +++

error[E0220]: associated type `ExpressionStack` not found for `Trait`
--> $DIR/sysroot-private.rs:21:31
|
LL | type AssociatedTy = dyn Trait<ExpressionStack = i32, Bar = i32>;
| ^^^^^^^^^^^^^^^ there is an associated type `ExpressionStack` in the trait `gimli::read::op::EvaluationStorage`

error[E0425]: cannot find function `memchr2` in this scope
--> $DIR/sysroot-private.rs:39:5
|
LL | memchr2(b'a', b'b', buf)
| ^^^^^^^ not found in this scope

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0220, E0405, E0412, E0425.
For more information about an error, try `rustc --explain E0220`.
42 changes: 42 additions & 0 deletions tests/ui/privacy/sysroot-private.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Test that private dependencies of `std` that live in the sysroot do not reach through to
//! diagnostics.
//!
//! This test would be more robust if we could patch the sysroot with an "evil" crate that
//! provided known types that we control; however, this would effectively require rebuilding
//! `std` (or patching crate metadata). So, this test relies on what is currently public API
//! of `std`'s dependencies, but may not be robust against dependency upgrades/changes.

//@ only-unix Windows sysroots seem to not expose this dependency
//@ revisions: default rustc_private_enabled

// Enabling `rustc_private` should `std`'s dependencies accessible, so they should show up
// in diagnostics. NB: not all diagnostics are affected by this.
#![cfg_attr(rustc_private_enabled, feature(rustc_private))]
#![crate_type = "lib"]

trait Trait { type Bar; }

// Attempt to get a suggestion for `gimli::read::op::EvaluationStoreage`, which should not be
// present in diagnostics (it is a dependency of the compiler).
type AssociatedTy = dyn Trait<ExpressionStack = i32, Bar = i32>;
//~^ ERROR associated type `ExpressionStack` not found
//~| NOTE there is an associated type `ExpressionStack` in the trait `gimli::read::op::EvaluationStorage`

// Attempt to get a suggestion for `hashbrown::Equivalent`
trait Trait2<K>: Equivalent<K> {}
//~^ ERROR cannot find trait
//~| NOTE not found

// Attempt to get a suggestion for `hashbrown::Equivalent::equivalent`
fn trait_member<T>(val: &T, key: &K) -> bool {
//~^ ERROR cannot find type `K`
//~| NOTE similarly named
val.equivalent(key)
}

// Attempt to get a suggestion for `memchr::memchr2`
fn free_function(buf: &[u8]) -> Option<usize> {
memchr2(b'a', b'b', buf)
//~^ ERROR cannot find function
//~| NOTE not found
}
39 changes: 39 additions & 0 deletions tests/ui/privacy/sysroot-private.rustc_private_enabled.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
error[E0405]: cannot find trait `Equivalent` in this scope
--> $DIR/sysroot-private.rs:26:18
|
LL | trait Trait2<K>: Equivalent<K> {}
| ^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `K` in this scope
--> $DIR/sysroot-private.rs:31:35
|
LL | fn trait_member<T>(val: &T, key: &K) -> bool {
| - ^
| |
| similarly named type parameter `T` defined here
|
help: a type parameter with a similar name exists
|
LL | fn trait_member<T>(val: &T, key: &T) -> bool {
| ~
help: you might be missing a type parameter
|
LL | fn trait_member<T, K>(val: &T, key: &K) -> bool {
| +++

error[E0220]: associated type `ExpressionStack` not found for `Trait`
--> $DIR/sysroot-private.rs:21:31
|
LL | type AssociatedTy = dyn Trait<ExpressionStack = i32, Bar = i32>;
| ^^^^^^^^^^^^^^^ there is an associated type `ExpressionStack` in the trait `gimli::read::op::EvaluationStorage`

error[E0425]: cannot find function `memchr2` in this scope
--> $DIR/sysroot-private.rs:39:5
|
LL | memchr2(b'a', b'b', buf)
| ^^^^^^^ not found in this scope

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0220, E0405, E0412, E0425.
For more information about an error, try `rustc --explain E0220`.