Skip to content

Commit

Permalink
Don't abort compilation after giving a lint error
Browse files Browse the repository at this point in the history
The only reason to use `abort_if_errors` is when the program is so broken that either:
1. later passes get confused and ICE
2. any diagnostics from later passes would be noise

This is never the case for lints, because the compiler has to be able to deal with `allow`-ed lints.
So it can continue to lint and compile even if there are lint errors.
  • Loading branch information
jyn514 committed Nov 8, 2021
1 parent 90a273b commit 0ac13bd
Show file tree
Hide file tree
Showing 64 changed files with 480 additions and 205 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/back/write.rs
Expand Up @@ -286,7 +286,7 @@ fn report_inline_asm(
cookie = 0;
}
let level = match level {
llvm::DiagnosticLevel::Error => Level::Error,
llvm::DiagnosticLevel::Error => Level::Error { lint: false },
llvm::DiagnosticLevel::Warning => Level::Warning,
llvm::DiagnosticLevel::Note | llvm::DiagnosticLevel::Remark => Level::Note,
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/write.rs
Expand Up @@ -1757,7 +1757,7 @@ impl SharedEmitterMain {
let msg = msg.strip_prefix("error: ").unwrap_or(&msg);

let mut err = match level {
Level::Error => sess.struct_err(&msg),
Level::Error { lint: false } => sess.struct_err(&msg),
Level::Warning => sess.struct_warn(&msg),
Level::Note => sess.struct_note_without_error(&msg),
_ => bug!("Invalid inline asm diagnostic level"),
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_driver/src/lib.rs
Expand Up @@ -410,6 +410,10 @@ fn run_compiler(
sess.code_stats.print_type_sizes();
}

if sess.diagnostic().has_errors_or_lint_errors() {
return Err(ErrorReported);
}

let linker = queries.linker()?;
Ok(Some(linker))
})?;
Expand Down
Expand Up @@ -66,7 +66,7 @@ fn source_string(file: Lrc<SourceFile>, line: &Line) -> String {
/// Maps `Diagnostic::Level` to `snippet::AnnotationType`
fn annotation_type_for_level(level: Level) -> AnnotationType {
match level {
Level::Bug | Level::Fatal | Level::Error => AnnotationType::Error,
Level::Bug | Level::Fatal | Level::Error { .. } => AnnotationType::Error,
Level::Warning => AnnotationType::Warning,
Level::Note => AnnotationType::Note,
Level::Help => AnnotationType::Help,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_errors/src/diagnostic.rs
Expand Up @@ -114,7 +114,7 @@ impl Diagnostic {

pub fn is_error(&self) -> bool {
match self.level {
Level::Bug | Level::Fatal | Level::Error | Level::FailureNote => true,
Level::Bug | Level::Fatal | Level::Error { .. } | Level::FailureNote => true,

Level::Warning | Level::Note | Level::Help | Level::Cancelled | Level::Allow => false,
}
Expand Down
48 changes: 39 additions & 9 deletions compiler/rustc_errors/src/lib.rs
Expand Up @@ -411,6 +411,8 @@ pub struct Handler {
/// as well as inconsistent state observation.
struct HandlerInner {
flags: HandlerFlags,
/// The number of lint errors that have been emitted.
lint_err_count: usize,
/// The number of errors that have been emitted, including duplicates.
///
/// This is not necessarily the count that's reported to the user once
Expand Down Expand Up @@ -550,6 +552,7 @@ impl Handler {
flags,
inner: Lock::new(HandlerInner {
flags,
lint_err_count: 0,
err_count: 0,
warn_count: 0,
deduplicated_err_count: 0,
Expand Down Expand Up @@ -726,7 +729,13 @@ impl Handler {
/// Construct a builder at the `Error` level with the `msg`.
// FIXME: This method should be removed (every error should have an associated error code).
pub fn struct_err(&self, msg: &str) -> DiagnosticBuilder<'_> {
DiagnosticBuilder::new(self, Level::Error, msg)
DiagnosticBuilder::new(self, Level::Error { lint: false }, msg)
}

/// This should only be used by `rustc_middle::lint::struct_lint_level`. Do not use it for hard errors.
#[doc(hidden)]
pub fn struct_err_lint(&self, msg: &str) -> DiagnosticBuilder<'_> {
DiagnosticBuilder::new(self, Level::Error { lint: true }, msg)
}

/// Construct a builder at the `Error` level with the `msg` and the `code`.
Expand Down Expand Up @@ -790,11 +799,14 @@ impl Handler {
}

pub fn span_err(&self, span: impl Into<MultiSpan>, msg: &str) {
self.emit_diag_at_span(Diagnostic::new(Error, msg), span);
self.emit_diag_at_span(Diagnostic::new(Error { lint: false }, msg), span);
}

pub fn span_err_with_code(&self, span: impl Into<MultiSpan>, msg: &str, code: DiagnosticId) {
self.emit_diag_at_span(Diagnostic::new_with_code(Error, Some(code), msg), span);
self.emit_diag_at_span(
Diagnostic::new_with_code(Error { lint: false }, Some(code), msg),
span,
);
}

pub fn span_warn(&self, span: impl Into<MultiSpan>, msg: &str) {
Expand Down Expand Up @@ -862,6 +874,9 @@ impl Handler {
pub fn has_errors(&self) -> bool {
self.inner.borrow().has_errors()
}
pub fn has_errors_or_lint_errors(&self) -> bool {
self.inner.borrow().has_errors_or_lint_errors()
}
pub fn has_errors_or_delayed_span_bugs(&self) -> bool {
self.inner.borrow().has_errors_or_delayed_span_bugs()
}
Expand Down Expand Up @@ -979,7 +994,11 @@ impl HandlerInner {
}
}
if diagnostic.is_error() {
self.bump_err_count();
if matches!(diagnostic.level, Level::Error { lint: true }) {
self.bump_lint_err_count();
} else {
self.bump_err_count();
}
} else {
self.bump_warn_count();
}
Expand Down Expand Up @@ -1073,11 +1092,14 @@ impl HandlerInner {
fn has_errors(&self) -> bool {
self.err_count() > 0
}
fn has_errors_or_lint_errors(&self) -> bool {
self.has_errors() || self.lint_err_count > 0
}
fn has_errors_or_delayed_span_bugs(&self) -> bool {
self.has_errors() || !self.delayed_span_bugs.is_empty()
}
fn has_any_message(&self) -> bool {
self.err_count() > 0 || self.warn_count > 0
self.err_count() > 0 || self.lint_err_count > 0 || self.warn_count > 0
}

fn abort_if_errors(&mut self) {
Expand Down Expand Up @@ -1131,7 +1153,7 @@ impl HandlerInner {
}

fn err(&mut self, msg: &str) {
self.emit_error(Error, msg);
self.emit_error(Error { lint: false }, msg);
}

/// Emit an error; level should be `Error` or `Fatal`.
Expand Down Expand Up @@ -1167,6 +1189,11 @@ impl HandlerInner {
}
}

fn bump_lint_err_count(&mut self) {
self.lint_err_count += 1;
self.panic_if_treat_err_as_bug();
}

fn bump_err_count(&mut self) {
self.err_count += 1;
self.panic_if_treat_err_as_bug();
Expand Down Expand Up @@ -1210,7 +1237,10 @@ impl DelayedDiagnostic {
pub enum Level {
Bug,
Fatal,
Error,
Error {
/// If this error comes from a lint, don't abort compilation even when abort_if_errors() is called.
lint: bool,
},
Warning,
Note,
Help,
Expand All @@ -1229,7 +1259,7 @@ impl Level {
fn color(self) -> ColorSpec {
let mut spec = ColorSpec::new();
match self {
Bug | Fatal | Error => {
Bug | Fatal | Error { .. } => {
spec.set_fg(Some(Color::Red)).set_intense(true);
}
Warning => {
Expand All @@ -1250,7 +1280,7 @@ impl Level {
pub fn to_str(self) -> &'static str {
match self {
Bug => "error: internal compiler error",
Fatal | Error => "error",
Fatal | Error { .. } => "error",
Warning => "warning",
Note => "note",
Help => "help",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/proc_macro_server.rs
Expand Up @@ -273,7 +273,7 @@ impl ToInternal<TokenStream> for TokenTree<Group, Punct, Ident, Literal> {
impl ToInternal<rustc_errors::Level> for Level {
fn to_internal(self) -> rustc_errors::Level {
match self {
Level::Error => rustc_errors::Level::Error,
Level::Error => rustc_errors::Level::Error { lint: false },
Level::Warning => rustc_errors::Level::Warning,
Level::Note => rustc_errors::Level::Note,
Level::Help => rustc_errors::Level::Help,
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_middle/src/lint.rs
Expand Up @@ -248,8 +248,12 @@ pub fn struct_lint_level<'s, 'd>(
(Level::Warn, None) => sess.struct_warn(""),
(Level::ForceWarn, Some(span)) => sess.struct_span_force_warn(span, ""),
(Level::ForceWarn, None) => sess.struct_force_warn(""),
(Level::Deny | Level::Forbid, Some(span)) => sess.struct_span_err(span, ""),
(Level::Deny | Level::Forbid, None) => sess.struct_err(""),
(Level::Deny | Level::Forbid, Some(span)) => {
let mut builder = sess.diagnostic().struct_err_lint("");
builder.set_span(span);
builder
}
(Level::Deny | Level::Forbid, None) => sess.diagnostic().struct_err_lint(""),
};

// If this code originates in a foreign macro, aka something that this crate
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui-fulldeps/lint-tool-test.rs
Expand Up @@ -10,10 +10,12 @@
//~^ WARNING lint name `test_lint` is deprecated and may not have an effect in the future
//~| WARNING lint name `test_lint` is deprecated and may not have an effect in the future
//~| WARNING lint name `test_lint` is deprecated and may not have an effect in the future
//~| WARNING lint name `test_lint` is deprecated and may not have an effect in the future
#![deny(clippy_group)]
//~^ WARNING lint name `clippy_group` is deprecated and may not have an effect in the future
//~| WARNING lint name `clippy_group` is deprecated and may not have an effect in the future
//~| WARNING lint name `clippy_group` is deprecated and may not have an effect in the future
//~| WARNING lint name `clippy_group` is deprecated and may not have an effect in the future

fn lintme() { } //~ ERROR item is named 'lintme'

Expand All @@ -30,6 +32,7 @@ pub fn main() {
//~^ WARNING lint name `test_group` is deprecated and may not have an effect in the future
//~| WARNING lint name `test_group` is deprecated and may not have an effect in the future
//~| WARNING lint name `test_group` is deprecated and may not have an effect in the future
//~| WARNING lint name `test_group` is deprecated and may not have an effect in the future
#[deny(this_lint_does_not_exist)] //~ WARNING unknown lint: `this_lint_does_not_exist`
fn hello() {
fn lintmetoo() { }
Expand Down
42 changes: 30 additions & 12 deletions src/test/ui-fulldeps/lint-tool-test.stderr
Expand Up @@ -7,19 +7,19 @@ LL | #![cfg_attr(foo, warn(test_lint))]
= note: `#[warn(renamed_and_removed_lints)]` on by default

warning: lint name `clippy_group` is deprecated and may not have an effect in the future.
--> $DIR/lint-tool-test.rs:13:9
--> $DIR/lint-tool-test.rs:14:9
|
LL | #![deny(clippy_group)]
| ^^^^^^^^^^^^ help: change it to: `clippy::group`

warning: lint name `test_group` is deprecated and may not have an effect in the future.
--> $DIR/lint-tool-test.rs:29:9
--> $DIR/lint-tool-test.rs:31:9
|
LL | #[allow(test_group)]
| ^^^^^^^^^^ help: change it to: `clippy::test_group`

warning: unknown lint: `this_lint_does_not_exist`
--> $DIR/lint-tool-test.rs:33:8
--> $DIR/lint-tool-test.rs:36:8
|
LL | #[deny(this_lint_does_not_exist)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -33,13 +33,13 @@ LL | #![cfg_attr(foo, warn(test_lint))]
| ^^^^^^^^^ help: change it to: `clippy::test_lint`

warning: lint name `clippy_group` is deprecated and may not have an effect in the future.
--> $DIR/lint-tool-test.rs:13:9
--> $DIR/lint-tool-test.rs:14:9
|
LL | #![deny(clippy_group)]
| ^^^^^^^^^^^^ help: change it to: `clippy::group`

warning: lint name `test_group` is deprecated and may not have an effect in the future.
--> $DIR/lint-tool-test.rs:29:9
--> $DIR/lint-tool-test.rs:31:9
|
LL | #[allow(test_group)]
| ^^^^^^^^^^ help: change it to: `clippy::test_group`
Expand All @@ -59,42 +59,60 @@ LL | #![cfg_attr(foo, warn(test_lint))]
| ^^^^^^^^^ help: change it to: `clippy::test_lint`

warning: lint name `clippy_group` is deprecated and may not have an effect in the future.
--> $DIR/lint-tool-test.rs:13:9
--> $DIR/lint-tool-test.rs:14:9
|
LL | #![deny(clippy_group)]
| ^^^^^^^^^^^^ help: change it to: `clippy::group`

error: item is named 'lintme'
--> $DIR/lint-tool-test.rs:18:1
--> $DIR/lint-tool-test.rs:20:1
|
LL | fn lintme() { }
| ^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/lint-tool-test.rs:13:9
--> $DIR/lint-tool-test.rs:14:9
|
LL | #![deny(clippy_group)]
| ^^^^^^^^^^^^
= note: `#[deny(clippy::test_lint)]` implied by `#[deny(clippy::group)]`

error: item is named 'lintmetoo'
--> $DIR/lint-tool-test.rs:26:5
--> $DIR/lint-tool-test.rs:28:5
|
LL | fn lintmetoo() { }
| ^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/lint-tool-test.rs:13:9
--> $DIR/lint-tool-test.rs:14:9
|
LL | #![deny(clippy_group)]
| ^^^^^^^^^^^^
= note: `#[deny(clippy::test_group)]` implied by `#[deny(clippy::group)]`

warning: lint name `test_group` is deprecated and may not have an effect in the future.
--> $DIR/lint-tool-test.rs:29:9
--> $DIR/lint-tool-test.rs:31:9
|
LL | #[allow(test_group)]
| ^^^^^^^^^^ help: change it to: `clippy::test_group`

error: aborting due to 2 previous errors; 11 warnings emitted
warning: lint name `test_lint` is deprecated and may not have an effect in the future.
--> $DIR/lint-tool-test.rs:9:23
|
LL | #![cfg_attr(foo, warn(test_lint))]
| ^^^^^^^^^ help: change it to: `clippy::test_lint`

warning: lint name `clippy_group` is deprecated and may not have an effect in the future.
--> $DIR/lint-tool-test.rs:14:9
|
LL | #![deny(clippy_group)]
| ^^^^^^^^^^^^ help: change it to: `clippy::group`

warning: lint name `test_group` is deprecated and may not have an effect in the future.
--> $DIR/lint-tool-test.rs:31:9
|
LL | #[allow(test_group)]
| ^^^^^^^^^^ help: change it to: `clippy::test_group`

error: aborting due to 2 previous errors; 14 warnings emitted

1 change: 1 addition & 0 deletions src/test/ui/deprecation/deprecation-lint.rs
Expand Up @@ -278,6 +278,7 @@ mod this_crate {
let _ = nested::DeprecatedStruct {
//~^ ERROR use of deprecated struct `this_crate::nested::DeprecatedStruct`: text
i: 0 //~ ERROR use of deprecated field `this_crate::nested::DeprecatedStruct::i`: text
//~| ERROR field `i` of struct `this_crate::nested::DeprecatedStruct` is private
};

let _ = nested::DeprecatedUnitStruct; //~ ERROR use of deprecated unit struct `this_crate::nested::DeprecatedUnitStruct`: text
Expand Down

0 comments on commit 0ac13bd

Please sign in to comment.