Skip to content

Commit

Permalink
Rollup merge of #121237 - Urgau:better-cargo-heuristic, r=compiler-er…
Browse files Browse the repository at this point in the history
…rors

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.
  • Loading branch information
fmease authored Feb 18, 2024
2 parents 9811358 + d988d8f commit 5628786
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 13 deletions.
7 changes: 5 additions & 2 deletions compiler/rustc_codegen_ssa/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_incremental/src/persist/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(()),
}))
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/context/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
20 changes: 19 additions & 1 deletion compiler/rustc_session/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<'_> {
Expand Down Expand Up @@ -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())
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ rustc-env:CARGO=/usr/bin/cargo
//@ rustc-env:CARGO_CRATE_NAME=foo

use std::pin::Pin;
use std::future::Future;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/cargo-feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/check-cfg/diagnotics.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/crate-loading/missing-std.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 5628786

Please sign in to comment.