Use better heuristic for printing Cargo specific diagnostics

This commit is contained in:
Urgau 2024-02-17 16:45:59 +01:00
parent bccb9bbb41
commit d988d8f4ba
10 changed files with 34 additions and 13 deletions

View File

@ -362,8 +362,11 @@ impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for LinkingFailed<'_> {
// which by now we have no way to translate. // which by now we have no way to translate.
if contains_undefined_ref { if contains_undefined_ref {
diag.note(fluent::codegen_ssa_extern_funcs_not_found) diag.note(fluent::codegen_ssa_extern_funcs_not_found)
.note(fluent::codegen_ssa_specify_libraries_to_link) .note(fluent::codegen_ssa_specify_libraries_to_link);
.note(fluent::codegen_ssa_use_cargo_directive);
if rustc_session::utils::was_invoked_from_cargo() {
diag.note(fluent::codegen_ssa_use_cargo_directive);
}
} }
diag diag
} }

View File

@ -293,7 +293,7 @@ pub enum HelpUseLatestEdition {
impl HelpUseLatestEdition { impl HelpUseLatestEdition {
pub fn new() -> Self { pub fn new() -> Self {
let edition = LATEST_STABLE_EDITION; let edition = LATEST_STABLE_EDITION;
if std::env::var_os("CARGO").is_some() { if rustc_session::utils::was_invoked_from_cargo() {
Self::Cargo { edition } Self::Cargo { edition }
} else { } else {
Self::Standalone { edition } Self::Standalone { edition }

View File

@ -492,7 +492,7 @@ fn lock_directory(
lock_err, lock_err,
session_dir, session_dir,
is_unsupported_lock, is_unsupported_lock,
is_cargo: std::env::var_os("CARGO").map(|_| ()), is_cargo: rustc_session::utils::was_invoked_from_cargo().then_some(()),
})) }))
} }
} }

View File

@ -205,7 +205,7 @@ pub(super) fn builtin(
Vec::new() 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; let mut is_feature_cfg = name == sym::feature;
if is_feature_cfg && is_from_cargo { if is_feature_cfg && is_from_cargo {
@ -340,7 +340,7 @@ pub(super) fn builtin(
.copied() .copied()
.flatten() .flatten()
.collect(); .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 // 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 // for names as the possibilities could be very long

View File

@ -2545,7 +2545,7 @@ pub enum HelpUseLatestEdition {
impl HelpUseLatestEdition { impl HelpUseLatestEdition {
pub fn new() -> Self { pub fn new() -> Self {
let edition = LATEST_STABLE_EDITION; let edition = LATEST_STABLE_EDITION;
if std::env::var_os("CARGO").is_some() { if rustc_session::utils::was_invoked_from_cargo() {
Self::Cargo { edition } Self::Cargo { edition }
} else { } else {
Self::Standalone { edition } Self::Standalone { edition }

View File

@ -1,7 +1,10 @@
use crate::session::Session; use crate::session::Session;
use rustc_data_structures::profiling::VerboseTimingGuard; use rustc_data_structures::profiling::VerboseTimingGuard;
use rustc_fs_util::try_canonicalize; use rustc_fs_util::try_canonicalize;
use std::path::{Path, PathBuf}; use std::{
path::{Path, PathBuf},
sync::OnceLock,
};
impl Session { impl Session {
pub fn timer(&self, what: &'static str) -> VerboseTimingGuard<'_> { 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 } 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())
}

View File

@ -1,4 +1,4 @@
//@ rustc-env:CARGO=/usr/bin/cargo //@ rustc-env:CARGO_CRATE_NAME=foo
use std::pin::Pin; use std::pin::Pin;
use std::future::Future; use std::future::Future;

View File

@ -4,7 +4,7 @@
// //
//@ check-pass //@ check-pass
//@ revisions: some none //@ revisions: some none
//@ rustc-env:CARGO=/usr/bin/cargo //@ rustc-env:CARGO_CRATE_NAME=foo
//@ compile-flags: -Z unstable-options //@ compile-flags: -Z unstable-options
//@ [none]compile-flags: --check-cfg=cfg(feature,values()) //@ [none]compile-flags: --check-cfg=cfg(feature,values())
//@ [some]compile-flags: --check-cfg=cfg(feature,values("bitcode")) //@ [some]compile-flags: --check-cfg=cfg(feature,values("bitcode"))

View File

@ -1,7 +1,7 @@
//@ check-pass //@ check-pass
//@ revisions: cargo rustc //@ revisions: cargo rustc
//@ [rustc]unset-rustc-env:CARGO //@ [rustc]unset-rustc-env:CARGO_CRATE_NAME
//@ [cargo]rustc-env:CARGO=/usr/bin/cargo //@ [cargo]rustc-env:CARGO_CRATE_NAME=foo
//@ compile-flags: --check-cfg=cfg(feature,values("foo")) --check-cfg=cfg(no_values) -Z unstable-options //@ compile-flags: --check-cfg=cfg(feature,values("foo")) --check-cfg=cfg(no_values) -Z unstable-options
#[cfg(featur)] #[cfg(featur)]

View File

@ -1,6 +1,6 @@
//@ compile-flags: --target x86_64-unknown-uefi //@ compile-flags: --target x86_64-unknown-uefi
//@ needs-llvm-components: x86 //@ needs-llvm-components: x86
//@ rustc-env:CARGO=/usr/bin/cargo //@ rustc-env:CARGO_CRATE_NAME=foo
#![feature(no_core)] #![feature(no_core)]
#![no_core] #![no_core]
extern crate core; extern crate core;