Skip to content

Commit 5628786

Browse files
authored
Rollup merge of #121237 - Urgau:better-cargo-heuristic, r=compiler-errors
Use better heuristic for printing Cargo specific diagnostics It was [reported](#82450 (comment)) in the check-cfg call for testing that the Rust for Linux project is setting the `CARGO` env without compiling with it, which is an issue since we are using the `CARGO` env as a proxy for "was launched from Cargo". This PR switch to the `CARGO_CRATE_NAME` [Cargo env](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates), which shouldn't collide (as much) with other build systems. I also took the opportunity to consolidate all the checks under the same function.
2 parents 9811358 + d988d8f commit 5628786

File tree

10 files changed

+34
-13
lines changed

10 files changed

+34
-13
lines changed

compiler/rustc_codegen_ssa/src/errors.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,11 @@ impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for LinkingFailed<'_> {
362362
// which by now we have no way to translate.
363363
if contains_undefined_ref {
364364
diag.note(fluent::codegen_ssa_extern_funcs_not_found)
365-
.note(fluent::codegen_ssa_specify_libraries_to_link)
366-
.note(fluent::codegen_ssa_use_cargo_directive);
365+
.note(fluent::codegen_ssa_specify_libraries_to_link);
366+
367+
if rustc_session::utils::was_invoked_from_cargo() {
368+
diag.note(fluent::codegen_ssa_use_cargo_directive);
369+
}
367370
}
368371
diag
369372
}

compiler/rustc_hir_typeck/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ pub enum HelpUseLatestEdition {
293293
impl HelpUseLatestEdition {
294294
pub fn new() -> Self {
295295
let edition = LATEST_STABLE_EDITION;
296-
if std::env::var_os("CARGO").is_some() {
296+
if rustc_session::utils::was_invoked_from_cargo() {
297297
Self::Cargo { edition }
298298
} else {
299299
Self::Standalone { edition }

compiler/rustc_incremental/src/persist/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ fn lock_directory(
492492
lock_err,
493493
session_dir,
494494
is_unsupported_lock,
495-
is_cargo: std::env::var_os("CARGO").map(|_| ()),
495+
is_cargo: rustc_session::utils::was_invoked_from_cargo().then_some(()),
496496
}))
497497
}
498498
}

compiler/rustc_lint/src/context/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub(super) fn builtin(
205205
Vec::new()
206206
};
207207

208-
let is_from_cargo = std::env::var_os("CARGO").is_some();
208+
let is_from_cargo = rustc_session::utils::was_invoked_from_cargo();
209209
let mut is_feature_cfg = name == sym::feature;
210210

211211
if is_feature_cfg && is_from_cargo {
@@ -340,7 +340,7 @@ pub(super) fn builtin(
340340
.copied()
341341
.flatten()
342342
.collect();
343-
let is_from_cargo = std::env::var_os("CARGO").is_some();
343+
let is_from_cargo = rustc_session::utils::was_invoked_from_cargo();
344344

345345
// Show the full list if all possible values for a given name, but don't do it
346346
// for names as the possibilities could be very long

compiler/rustc_parse/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2545,7 +2545,7 @@ pub enum HelpUseLatestEdition {
25452545
impl HelpUseLatestEdition {
25462546
pub fn new() -> Self {
25472547
let edition = LATEST_STABLE_EDITION;
2548-
if std::env::var_os("CARGO").is_some() {
2548+
if rustc_session::utils::was_invoked_from_cargo() {
25492549
Self::Cargo { edition }
25502550
} else {
25512551
Self::Standalone { edition }

compiler/rustc_session/src/utils.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use crate::session::Session;
22
use rustc_data_structures::profiling::VerboseTimingGuard;
33
use rustc_fs_util::try_canonicalize;
4-
use std::path::{Path, PathBuf};
4+
use std::{
5+
path::{Path, PathBuf},
6+
sync::OnceLock,
7+
};
58

69
impl Session {
710
pub fn timer(&self, what: &'static str) -> VerboseTimingGuard<'_> {
@@ -158,3 +161,18 @@ pub fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
158161

159162
if !result.is_empty() { Some((result, excluded_cargo_defaults)) } else { None }
160163
}
164+
165+
/// Returns whenever rustc was launched by Cargo as opposed to another build system.
166+
///
167+
/// To be used in diagnostics to avoid printing Cargo specific suggestions to other
168+
/// build systems (like Bazel, Buck2, Makefile, ...).
169+
pub fn was_invoked_from_cargo() -> bool {
170+
static FROM_CARGO: OnceLock<bool> = OnceLock::new();
171+
172+
// To be able to detect Cargo, we use the simplest and least intrusive
173+
// way: we check whenever the `CARGO_CRATE_NAME` env is set.
174+
//
175+
// Note that it is common in Makefiles to define the `CARGO` env even
176+
// though we may not have been called by Cargo, so we avoid using it.
177+
*FROM_CARGO.get_or_init(|| std::env::var_os("CARGO_CRATE_NAME").is_some())
178+
}

tests/ui/async-await/suggest-switching-edition-on-await-cargo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ rustc-env:CARGO=/usr/bin/cargo
1+
//@ rustc-env:CARGO_CRATE_NAME=foo
22

33
use std::pin::Pin;
44
use std::future::Future;

tests/ui/check-cfg/cargo-feature.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55
//@ check-pass
66
//@ revisions: some none
7-
//@ rustc-env:CARGO=/usr/bin/cargo
7+
//@ rustc-env:CARGO_CRATE_NAME=foo
88
//@ compile-flags: -Z unstable-options
99
//@ [none]compile-flags: --check-cfg=cfg(feature,values())
1010
//@ [some]compile-flags: --check-cfg=cfg(feature,values("bitcode"))

tests/ui/check-cfg/diagnotics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ check-pass
22
//@ revisions: cargo rustc
3-
//@ [rustc]unset-rustc-env:CARGO
4-
//@ [cargo]rustc-env:CARGO=/usr/bin/cargo
3+
//@ [rustc]unset-rustc-env:CARGO_CRATE_NAME
4+
//@ [cargo]rustc-env:CARGO_CRATE_NAME=foo
55
//@ compile-flags: --check-cfg=cfg(feature,values("foo")) --check-cfg=cfg(no_values) -Z unstable-options
66

77
#[cfg(featur)]

tests/ui/crate-loading/missing-std.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ compile-flags: --target x86_64-unknown-uefi
22
//@ needs-llvm-components: x86
3-
//@ rustc-env:CARGO=/usr/bin/cargo
3+
//@ rustc-env:CARGO_CRATE_NAME=foo
44
#![feature(no_core)]
55
#![no_core]
66
extern crate core;

0 commit comments

Comments
 (0)