Skip to content

Commit c1c3e29

Browse files
committed
fix(compile): build.warnings=deny shouldn't block hard warnings
Motivation for this: rustc is trying to adopt this feature but hard warnings are getting in the way, see rust-lang/rust#148332 Reasons to do this: - Less flexibility in how hard warnings are resolved - Matches Cargo not erroring for Cargo hard warnings Reasons not do this: - A user may see a hard warning from rustc and get confused - Cargo's hard warnings are not blocking in part because we would need to audit them to see if they should actually be hard warnings We do have some flexibility on warnings evolving over time, so this is likely a two-way door (on an unstable feature). See also the discussion at #14802 (comment)
1 parent a87753a commit c1c3e29

File tree

6 files changed

+32
-13
lines changed

6 files changed

+32
-13
lines changed

src/cargo/core/compiler/compilation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ pub struct Compilation<'gctx> {
123123
/// The linker to use for each host or target.
124124
target_linkers: HashMap<CompileKind, Option<PathBuf>>,
125125

126-
/// The total number of warnings emitted by the compilation.
127-
pub warning_count: usize,
126+
/// The total number of lint warnings emitted by the compilation.
127+
pub lint_warning_count: usize,
128128
}
129129

130130
impl<'gctx> Compilation<'gctx> {
@@ -162,7 +162,7 @@ impl<'gctx> Compilation<'gctx> {
162162
.chain(Some(&CompileKind::Host))
163163
.map(|kind| Ok((*kind, target_linker(bcx, *kind)?)))
164164
.collect::<CargoResult<HashMap<_, _>>>()?,
165-
warning_count: 0,
165+
lint_warning_count: 0,
166166
})
167167
}
168168

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,19 @@ impl<'a, 'gctx> JobState<'a, 'gctx> {
104104
}
105105

106106
/// See [`Message::Diagnostic`] and [`Message::WarningCount`].
107-
pub fn emit_diag(&self, level: &str, diag: String, fixable: bool) -> CargoResult<()> {
107+
pub fn emit_diag(
108+
&self,
109+
level: &str,
110+
diag: String,
111+
lint: bool,
112+
fixable: bool,
113+
) -> CargoResult<()> {
108114
if let Some(dedupe) = self.output {
109115
let emitted = dedupe.emit_diag(&diag)?;
110116
if level == "warning" {
111117
self.messages.push(Message::WarningCount {
112118
id: self.id,
119+
lint,
113120
emitted,
114121
fixable,
115122
});
@@ -119,6 +126,7 @@ impl<'a, 'gctx> JobState<'a, 'gctx> {
119126
id: self.id,
120127
level: level.to_string(),
121128
diag,
129+
lint,
122130
fixable,
123131
});
124132
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ struct DrainState<'gctx> {
209209
pub struct WarningCount {
210210
/// total number of warnings
211211
pub total: usize,
212+
/// number of lint warnings
213+
pub lints: usize,
212214
/// number of warnings that were suppressed because they
213215
/// were duplicates of a previous warning
214216
pub duplicates: usize,
@@ -353,12 +355,14 @@ enum Message {
353355
id: JobId,
354356
level: String,
355357
diag: String,
358+
lint: bool,
356359
fixable: bool,
357360
},
358361
// This handles duplicate output that is suppressed, for showing
359362
// only a count of duplicate messages instead
360363
WarningCount {
361364
id: JobId,
365+
lint: bool,
362366
emitted: bool,
363367
fixable: bool,
364368
},
@@ -629,11 +633,12 @@ impl<'gctx> DrainState<'gctx> {
629633
id,
630634
level,
631635
diag,
636+
lint,
632637
fixable,
633638
} => {
634639
let emitted = self.diag_dedupe.emit_diag(&diag)?;
635640
if level == "warning" {
636-
self.bump_warning_count(id, emitted, fixable);
641+
self.bump_warning_count(id, lint, emitted, fixable);
637642
}
638643
if level == "error" {
639644
let cnts = self.warning_count.entry(id).or_default();
@@ -645,16 +650,18 @@ impl<'gctx> DrainState<'gctx> {
645650
if warning_handling != WarningHandling::Allow {
646651
build_runner.bcx.gctx.shell().warn(warning)?;
647652
}
653+
let lint = false;
648654
let emitted = true;
649655
let fixable = false;
650-
self.bump_warning_count(id, emitted, fixable);
656+
self.bump_warning_count(id, lint, emitted, fixable);
651657
}
652658
Message::WarningCount {
653659
id,
660+
lint,
654661
emitted,
655662
fixable,
656663
} => {
657-
self.bump_warning_count(id, emitted, fixable);
664+
self.bump_warning_count(id, lint, emitted, fixable);
658665
}
659666
Message::FixDiagnostic(msg) => {
660667
self.print.print(&msg)?;
@@ -1020,9 +1027,12 @@ impl<'gctx> DrainState<'gctx> {
10201027
Ok(())
10211028
}
10221029

1023-
fn bump_warning_count(&mut self, id: JobId, emitted: bool, fixable: bool) {
1030+
fn bump_warning_count(&mut self, id: JobId, lint: bool, emitted: bool, fixable: bool) {
10241031
let cnts = self.warning_count.entry(id).or_default();
10251032
cnts.total += 1;
1033+
if lint {
1034+
cnts.lints += 1;
1035+
}
10261036
if !emitted {
10271037
cnts.duplicates += 1;
10281038
// Don't add to fixable if it's already been emitted
@@ -1055,7 +1065,7 @@ impl<'gctx> DrainState<'gctx> {
10551065
Some(count) if count.total > 0 => count,
10561066
None | Some(_) => return,
10571067
};
1058-
runner.compilation.warning_count += count.total;
1068+
runner.compilation.lint_warning_count += count.lints;
10591069
let unit = &self.active[&id];
10601070
let mut message = descriptive_pkg_name(&unit.pkg.name(), &unit.target, &unit.mode);
10611071
message.push_str(" generated ");

src/cargo/core/compiler/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2185,12 +2185,14 @@ fn on_stderr_line_inner(
21852185
count_diagnostic(&msg.level, options);
21862186
if msg
21872187
.code
2188+
.as_ref()
21882189
.is_some_and(|c| c.code == "exported_private_dependencies")
21892190
&& options.format != MessageFormat::Short
21902191
{
21912192
add_pub_in_priv_diagnostic(&mut rendered);
21922193
}
2193-
state.emit_diag(&msg.level, rendered, machine_applicable)?;
2194+
let lint = msg.code.is_some();
2195+
state.emit_diag(&msg.level, rendered, lint, machine_applicable)?;
21942196
}
21952197
return Ok(true);
21962198
}

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ pub fn compile_with_exec<'a>(
143143
) -> CargoResult<Compilation<'a>> {
144144
ws.emit_warnings()?;
145145
let compilation = compile_ws(ws, options, exec)?;
146-
if ws.gctx().warning_handling()? == WarningHandling::Deny && compilation.warning_count > 0 {
146+
if ws.gctx().warning_handling()? == WarningHandling::Deny && compilation.lint_warning_count > 0
147+
{
147148
anyhow::bail!("warnings are denied by `build.warnings` configuration")
148149
}
149150
Ok(compilation)

tests/testsuite/warning_override.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,8 @@ fn hard_warning_deny() {
253253
254254
[WARNING] `foo` (bin "foo") generated 2 warnings
255255
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
256-
[ERROR] warnings are denied by `build.warnings` configuration
257256
258257
"#]])
259-
.with_status(101)
260258
.run();
261259
}
262260

0 commit comments

Comments
 (0)