Skip to content

Suppress suggestions while span is in external library #139316

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
52 changes: 52 additions & 0 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1996,6 +1996,14 @@ impl HumanEmitter {
return Ok(());
};

if suggestion
.substitutions
.iter()
.any(|s| s.parts.iter().any(|p| is_external_library(sm, p.span)))
{
return Ok(());
}

// Render the replacements for each suggestion
let suggestions = suggestion.splice_lines(sm);
debug!(?suggestions);
Expand Down Expand Up @@ -3532,3 +3540,47 @@ pub(crate) fn should_show_source_code(
.map(|path| ignored_directories.iter().all(|dir| !path.starts_with(dir)))
.unwrap_or(true)
}

fn is_external_library(sm: &SourceMap, span: Span) -> bool {
let filename = sm.span_to_filename(span);
if let Some(path) = filename.into_local_path() {
// use env variable to get path, avoid hardcode
// use platform independent path

let cargo_home = match std::env::var("CARGO_HOME") {
Ok(dir) => std::path::PathBuf::from(dir),
Err(_) => {
if let Ok(home) = std::env::var("HOME") {
std::path::PathBuf::from(home).join(".cargo")
} else {
return false;
}
}
};
Comment on lines +3550 to +3559
Copy link
Member

@jieyouxu jieyouxu May 1, 2025

Choose a reason for hiding this comment

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

Discussion: this feels really iffy to me, that we're querying so many env vars. E.g. I think on Windows, HOME isn't even set. This also magically encodes directory structures that are not guaranteed to remain the same (rustup, cargo, sysroot directory layouts). See for instance #135278 and #135501. Note that this also can't account for remapped paths or devirtualizing of /rustc/$hash, I think.

Copy link
Member

@fmease fmease May 1, 2025

Choose a reason for hiding this comment

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

Yeah, for sure. This isn't the approach we should be taking. When I mentioned RUSTUP_HOME and CARGO_HOME in a previous review comment, I intended to dissuade the author from implementing this kind of approach by demonstrating the fragile nature, the amount of hacks/assumptions and unnecessary coupling to downstream tools.

I haven't re-reviewed yet because I need to think about a non-hacky approach but I first wanted to investigate things.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that's the feeling I had from reading the previous review comments. I mostly wrote my own comment to prevent myself from forgetting 😆


let rustup_home = match std::env::var("RUSTUP_HOME") {
Ok(dir) => std::path::PathBuf::from(dir),
Err(_) => {
if let Ok(home) = std::env::var("HOME") {
std::path::PathBuf::from(home).join(".rustup")
} else {
return false;
}
}
};

let sysroot = match std::env::var("RUSTC_SYSROOT") {
Ok(dir) => Some(std::path::PathBuf::from(dir)),
Err(_) => None,
};

let registry_path = cargo_home.join("registry").join("src");
let toolchain_path = rustup_home.join("toolchains");

path.starts_with(&registry_path)
|| path.starts_with(&toolchain_path)
|| sysroot.as_ref().map_or(false, |sr| path.starts_with(sr))
} else {
false
}
}
Loading