mirror of https://github.com/rust-lang/rust.git
Rollup merge of #121237 - Urgau:better-cargo-heuristic, r=compiler-errors
Use better heuristic for printing Cargo specific diagnostics It was [reported](https://github.com/rust-lang/rust/issues/82450#issuecomment-1948574677) 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.
This commit is contained in:
commit
5628786484
|
@ -362,8 +362,11 @@ impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for LinkingFailed<'_> {
|
|||
// which by now we have no way to translate.
|
||||
if contains_undefined_ref {
|
||||
diag.note(fluent::codegen_ssa_extern_funcs_not_found)
|
||||
.note(fluent::codegen_ssa_specify_libraries_to_link)
|
||||
.note(fluent::codegen_ssa_use_cargo_directive);
|
||||
.note(fluent::codegen_ssa_specify_libraries_to_link);
|
||||
|
||||
if rustc_session::utils::was_invoked_from_cargo() {
|
||||
diag.note(fluent::codegen_ssa_use_cargo_directive);
|
||||
}
|
||||
}
|
||||
diag
|
||||
}
|
||||
|
|
|
@ -293,7 +293,7 @@ pub enum HelpUseLatestEdition {
|
|||
impl HelpUseLatestEdition {
|
||||
pub fn new() -> Self {
|
||||
let edition = LATEST_STABLE_EDITION;
|
||||
if std::env::var_os("CARGO").is_some() {
|
||||
if rustc_session::utils::was_invoked_from_cargo() {
|
||||
Self::Cargo { edition }
|
||||
} else {
|
||||
Self::Standalone { edition }
|
||||
|
|
|
@ -492,7 +492,7 @@ fn lock_directory(
|
|||
lock_err,
|
||||
session_dir,
|
||||
is_unsupported_lock,
|
||||
is_cargo: std::env::var_os("CARGO").map(|_| ()),
|
||||
is_cargo: rustc_session::utils::was_invoked_from_cargo().then_some(()),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -205,7 +205,7 @@ pub(super) fn builtin(
|
|||
Vec::new()
|
||||
};
|
||||
|
||||
let is_from_cargo = std::env::var_os("CARGO").is_some();
|
||||
let is_from_cargo = rustc_session::utils::was_invoked_from_cargo();
|
||||
let mut is_feature_cfg = name == sym::feature;
|
||||
|
||||
if is_feature_cfg && is_from_cargo {
|
||||
|
@ -340,7 +340,7 @@ pub(super) fn builtin(
|
|||
.copied()
|
||||
.flatten()
|
||||
.collect();
|
||||
let is_from_cargo = std::env::var_os("CARGO").is_some();
|
||||
let is_from_cargo = rustc_session::utils::was_invoked_from_cargo();
|
||||
|
||||
// Show the full list if all possible values for a given name, but don't do it
|
||||
// for names as the possibilities could be very long
|
||||
|
|
|
@ -2545,7 +2545,7 @@ pub enum HelpUseLatestEdition {
|
|||
impl HelpUseLatestEdition {
|
||||
pub fn new() -> Self {
|
||||
let edition = LATEST_STABLE_EDITION;
|
||||
if std::env::var_os("CARGO").is_some() {
|
||||
if rustc_session::utils::was_invoked_from_cargo() {
|
||||
Self::Cargo { edition }
|
||||
} else {
|
||||
Self::Standalone { edition }
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
use crate::session::Session;
|
||||
use rustc_data_structures::profiling::VerboseTimingGuard;
|
||||
use rustc_fs_util::try_canonicalize;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
sync::OnceLock,
|
||||
};
|
||||
|
||||
impl Session {
|
||||
pub fn timer(&self, what: &'static str) -> VerboseTimingGuard<'_> {
|
||||
|
@ -158,3 +161,18 @@ pub fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
|
|||
|
||||
if !result.is_empty() { Some((result, excluded_cargo_defaults)) } else { None }
|
||||
}
|
||||
|
||||
/// Returns whenever rustc was launched by Cargo as opposed to another build system.
|
||||
///
|
||||
/// To be used in diagnostics to avoid printing Cargo specific suggestions to other
|
||||
/// build systems (like Bazel, Buck2, Makefile, ...).
|
||||
pub fn was_invoked_from_cargo() -> bool {
|
||||
static FROM_CARGO: OnceLock<bool> = OnceLock::new();
|
||||
|
||||
// To be able to detect Cargo, we use the simplest and least intrusive
|
||||
// way: we check whenever the `CARGO_CRATE_NAME` env is set.
|
||||
//
|
||||
// Note that it is common in Makefiles to define the `CARGO` env even
|
||||
// though we may not have been called by Cargo, so we avoid using it.
|
||||
*FROM_CARGO.get_or_init(|| std::env::var_os("CARGO_CRATE_NAME").is_some())
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
//@ rustc-env:CARGO=/usr/bin/cargo
|
||||
//@ rustc-env:CARGO_CRATE_NAME=foo
|
||||
|
||||
use std::pin::Pin;
|
||||
use std::future::Future;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
//
|
||||
//@ check-pass
|
||||
//@ revisions: some none
|
||||
//@ rustc-env:CARGO=/usr/bin/cargo
|
||||
//@ rustc-env:CARGO_CRATE_NAME=foo
|
||||
//@ compile-flags: -Z unstable-options
|
||||
//@ [none]compile-flags: --check-cfg=cfg(feature,values())
|
||||
//@ [some]compile-flags: --check-cfg=cfg(feature,values("bitcode"))
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
//@ check-pass
|
||||
//@ revisions: cargo rustc
|
||||
//@ [rustc]unset-rustc-env:CARGO
|
||||
//@ [cargo]rustc-env:CARGO=/usr/bin/cargo
|
||||
//@ [rustc]unset-rustc-env:CARGO_CRATE_NAME
|
||||
//@ [cargo]rustc-env:CARGO_CRATE_NAME=foo
|
||||
//@ compile-flags: --check-cfg=cfg(feature,values("foo")) --check-cfg=cfg(no_values) -Z unstable-options
|
||||
|
||||
#[cfg(featur)]
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//@ compile-flags: --target x86_64-unknown-uefi
|
||||
//@ needs-llvm-components: x86
|
||||
//@ rustc-env:CARGO=/usr/bin/cargo
|
||||
//@ rustc-env:CARGO_CRATE_NAME=foo
|
||||
#![feature(no_core)]
|
||||
#![no_core]
|
||||
extern crate core;
|
||||
|
|
Loading…
Reference in New Issue