Skip to content

Commit 51ec0a2

Browse files
authored
fix(compile): build.warnings=allow should not hide denied diagnostics (#16824)
### What does this PR try to resolve? They shouldn't be hidden and this was causing cargo to get confused: ``` [CHECKING] foo v0.0.1 ([ROOT]/foo) [ERROR] could not compile `foo` (bin "foo") Caused by: process didn't exit successfully: `rustc --crate-name foo --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=400 --crate-type bin --emit=dep-info,metadata -C embed-bitcode=no -C debuginfo=2 --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=cbc5c944f9a4e5cb -C extra-filename=-b1a006a0c806d1bb --out-dir [ROOT]/foo/target/debug/deps -L dependency=[ROOT]/foo/target/debug/deps -Dunused_variables` ([EXIT_STATUS]: 1) ``` ### How to test and review this PR? Instead of hiding all diagnostics, regardless of what level, we are post-processing the diagnostics and deciding when to hide them. One day it might be nice to clean up the treating of `level` as a string and having string comparison checks spread around this code.
2 parents b65e6bf + 3229ae7 commit 51ec0a2

4 files changed

Lines changed: 39 additions & 8 deletions

File tree

src/cargo/core/compiler/job_queue/job_state.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::core::compiler::future_incompat::FutureBreakageItem;
88
use crate::core::compiler::locking::LockKey;
99
use crate::core::compiler::timings::SectionTiming;
1010
use crate::util::Queue;
11+
use crate::util::context::WarningHandling;
1112
use crate::{CargoResult, core::compiler::locking::LockManager};
1213

1314
use super::{Artifact, DiagDedupe, Job, JobId, Message};
@@ -51,6 +52,8 @@ pub struct JobState<'a, 'gctx> {
5152
/// Manages locks for build units when fine grain locking is enabled.
5253
lock_manager: Arc<LockManager>,
5354

55+
warning_handling: WarningHandling,
56+
5457
// Historical versions of Cargo made use of the `'a` argument here, so to
5558
// leave the door open to future refactorings keep it here.
5659
_marker: marker::PhantomData<&'a ()>,
@@ -63,13 +66,15 @@ impl<'a, 'gctx> JobState<'a, 'gctx> {
6366
output: Option<&'a DiagDedupe<'gctx>>,
6467
rmeta_required: bool,
6568
lock_manager: Arc<LockManager>,
69+
warning_handling: WarningHandling,
6670
) -> Self {
6771
Self {
6872
id,
6973
messages,
7074
output,
7175
rmeta_required: Cell::new(rmeta_required),
7276
lock_manager,
77+
warning_handling,
7378
_marker: marker::PhantomData,
7479
}
7580
}
@@ -106,7 +111,9 @@ impl<'a, 'gctx> JobState<'a, 'gctx> {
106111
lint: bool,
107112
fixable: bool,
108113
) -> CargoResult<()> {
109-
if let Some(dedupe) = self.output {
114+
if level == "warning" && self.warning_handling == WarningHandling::Allow {
115+
tracing::warn!("{diag}");
116+
} else if let Some(dedupe) = self.output {
110117
let emitted = dedupe.emit_diag(&diag)?;
111118
if level == "warning" {
112119
self.messages.push(Message::WarningCount {

src/cargo/core/compiler/job_queue/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,9 +988,17 @@ impl<'gctx> DrainState<'gctx> {
988988
let is_fresh = job.freshness().is_fresh();
989989
let rmeta_required = build_runner.rmeta_required(unit);
990990
let lock_manager = build_runner.lock_manager.clone();
991+
let warning_handling = build_runner.bcx.gctx.warning_handling().unwrap_or_default();
991992

992993
let doit = move |diag_dedupe| {
993-
let state = JobState::new(id, messages, diag_dedupe, rmeta_required, lock_manager);
994+
let state = JobState::new(
995+
id,
996+
messages,
997+
diag_dedupe,
998+
rmeta_required,
999+
lock_manager,
1000+
warning_handling,
1001+
);
9941002
state.run_to_finish(job);
9951003
};
9961004

src/cargo/core/compiler/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ use crate::core::profiles::{PanicStrategy, Profile, StripInner};
105105
use crate::core::{Feature, PackageId, Target};
106106
use crate::lints::get_key_value;
107107
use crate::util::OnceExt;
108-
use crate::util::context::WarningHandling;
109108
use crate::util::errors::{CargoResult, VerboseError};
110109
use crate::util::interning::InternedString;
111110
use crate::util::machine_message::{self, Message};
@@ -2026,8 +2025,7 @@ impl OutputOptions {
20262025
drop(fs::remove_file(&path));
20272026
let cache_cell = Some((path, OnceCell::new()));
20282027

2029-
let show_diagnostics =
2030-
build_runner.bcx.gctx.warning_handling().unwrap_or_default() != WarningHandling::Allow;
2028+
let show_diagnostics = true;
20312029

20322030
let format = build_runner.bcx.build_config.message_format;
20332031

@@ -2045,9 +2043,7 @@ impl OutputOptions {
20452043

20462044
// We always replay the output cache,
20472045
// since it might contain future-incompat-report messages
2048-
let show_diagnostics = unit.show_warnings(build_runner.bcx.gctx)
2049-
&& build_runner.bcx.gctx.warning_handling().unwrap_or_default()
2050-
!= WarningHandling::Allow;
2046+
let show_diagnostics = unit.show_warnings(build_runner.bcx.gctx);
20512047

20522048
let format = build_runner.bcx.build_config.message_format;
20532049

tests/testsuite/warning_override.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ fn requires_nightly() {
4040
.run();
4141
}
4242

43+
#[cargo_test]
44+
fn always_show_error_diags() {
45+
let p = make_project_with_rustc_warning();
46+
p.cargo("check")
47+
.masquerade_as_nightly_cargo(&["warnings"])
48+
.env("RUSTFLAGS", "-Dunused_variables")
49+
.arg("-Zwarnings")
50+
.arg("--config")
51+
.arg("build.warnings='allow'")
52+
.with_stderr_data(str![[r#"
53+
[CHECKING] foo v0.0.1 ([ROOT]/foo)
54+
[ERROR] unused variable: `x`
55+
...
56+
[ERROR] could not compile `foo` (bin "foo") due to 1 previous error
57+
58+
"#]])
59+
.with_status(101)
60+
.run();
61+
}
62+
4363
#[cargo_test]
4464
fn clippy() {
4565
let p = project()

0 commit comments

Comments
 (0)