Skip to content

Commit

Permalink
Force warnings even when can_emit_warnings == false
Browse files Browse the repository at this point in the history
  • Loading branch information
rylev committed Jun 30, 2021
1 parent e98897e commit a3d6905
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 8 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ impl AnnotateSnippetEmitterWriter {
title: Some(Annotation {
label: Some(&message),
id: code.as_ref().map(|c| match c {
DiagnosticId::Error(val)
| DiagnosticId::Lint { name: val, has_future_breakage: _ } => val.as_str(),
DiagnosticId::Error(val) | DiagnosticId::Lint { name: val, .. } => {
val.as_str()
}
}),
annotation_type: annotation_type_for_level(*level),
}),
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Diagnostic {
#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
pub enum DiagnosticId {
Error(String),
Lint { name: String, has_future_breakage: bool },
Lint { name: String, has_future_breakage: bool, is_force_warn: bool },
}

/// A "sub"-diagnostic attached to a parent diagnostic.
Expand Down Expand Up @@ -109,6 +109,13 @@ impl Diagnostic {
}
}

pub fn is_force_warn(&self) -> bool {
match self.code {
Some(DiagnosticId::Lint { is_force_warn, .. }) => is_force_warn,
_ => false,
}
}

/// Cancel the diagnostic (a structured diagnostic must either be emitted or
/// canceled or it will panic when dropped).
pub fn cancel(&mut self) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_errors/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ impl DiagnosticCode {
s.map(|s| {
let s = match s {
DiagnosticId::Error(s) => s,
DiagnosticId::Lint { name, has_future_breakage: _ } => name,
DiagnosticId::Lint { name, .. } => name,
};
let je_result =
je.registry.as_ref().map(|registry| registry.try_find_description(&s)).unwrap();
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,10 @@ impl HandlerInner {
self.future_breakage_diagnostics.push(diagnostic.clone());
}

if diagnostic.level == Warning && !self.flags.can_emit_warnings {
if diagnostic.level == Warning
&& !self.flags.can_emit_warnings
&& !diagnostic.is_force_warn()
{
if diagnostic.has_future_breakage() {
(*TRACK_DIAGNOSTICS)(diagnostic);
}
Expand Down Expand Up @@ -874,7 +877,7 @@ impl HandlerInner {

match (errors.len(), warnings.len()) {
(0, 0) => return,
(0, _) => self.emit_diagnostic(&Diagnostic::new(Level::Warning, &warnings)),
(0, _) => self.emitter.emit_diagnostic(&Diagnostic::new(Level::Warning, &warnings)),
(_, 0) => {
let _ = self.fatal(&errors);
}
Expand Down
15 changes: 14 additions & 1 deletion compiler/rustc_middle/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ pub fn struct_lint_level<'s, 'd>(
let has_future_breakage =
future_incompatible.map_or(false, |incompat| incompat.future_breakage.is_some());

let is_force_warn = matches!(level, Level::ForceWarn)
|| matches!(src, LintLevelSource::CommandLine(_, Level::ForceWarn));

let mut err = match (level, span) {
(Level::Allow, span) => {
if has_future_breakage {
Expand All @@ -254,6 +257,16 @@ pub fn struct_lint_level<'s, 'd>(
} else {
sess.struct_allow("")
}
} else if is_force_warn {
let mut err = if let Some(span) = span {
sess.struct_span_warn(span, "")
} else {
sess.struct_warn("")
};
// Ensure force-warn warns even if the diagnostic has
// been canceled for reasons like `--cap-lints`
err.level = rustc_errors::Level::Warning;
err
} else {
return;
}
Expand Down Expand Up @@ -349,7 +362,7 @@ pub fn struct_lint_level<'s, 'd>(
}
}

err.code(DiagnosticId::Lint { name, has_future_breakage });
err.code(DiagnosticId::Lint { name, has_future_breakage, is_force_warn });

if let Some(future_incompatible) = future_incompatible {
let explanation = if lint_id == LintId::of(builtin::UNSTABLE_NAME_COLLISIONS) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ impl Session {
.into_iter()
.map(|diag| {
let lint_name = match &diag.code {
Some(DiagnosticId::Lint { name, has_future_breakage: true }) => name,
Some(DiagnosticId::Lint { name, has_future_breakage: true, .. }) => name,
_ => panic!("Unexpected code in diagnostic {:?}", diag),
};
let lint = lint_store.name_to_lint(&lint_name);
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/lint/force-warn/force-warns-cap-lints.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// compile-flags: --cap-lints allow --force-warns bare_trait_objects -Zunstable-options
// check-pass

pub trait SomeTrait {}

pub fn function(_x: Box<SomeTrait>) {}
//~^ WARN trait objects without an explicit `dyn` are deprecated
//~| WARN this is accepted in the current edition

fn main() {}
12 changes: 12 additions & 0 deletions src/test/ui/lint/force-warn/force-warns-cap-lints.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/force-warns-cap-lints.rs:6:25
|
LL | pub fn function(_x: Box<SomeTrait>) {}
| ^^^^^^^^^ help: use `dyn`: `dyn SomeTrait`
|
= note: requested on the command line with `--force-warns bare-trait-objects`
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
= note: for more information, see issue #80165 <https://github.com/rust-lang/rust/issues/80165>

warning: 1 warning emitted

0 comments on commit a3d6905

Please sign in to comment.