Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow #[deny] inside #[forbid] as a no-op with a warning #121560

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0453.md
@@ -1,3 +1,5 @@
#### Note: this error code is no longer emitted by the compiler.

A lint check attribute was overruled by a `forbid` directive set as an
attribute on an enclosing scope, or on the command line with the `-F` option.

Expand Down
12 changes: 0 additions & 12 deletions compiler/rustc_lint/src/errors.rs
Expand Up @@ -6,18 +6,6 @@ use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_session::lint::Level;
use rustc_span::{Span, Symbol};

#[derive(Diagnostic)]
#[diag(lint_overruled_attribute, code = E0453)]
pub struct OverruledAttribute<'a> {
#[primary_span]
pub span: Span,
#[label]
pub overruled: Span,
pub lint_level: &'a str,
pub lint_source: Symbol,
#[subdiagnostic]
pub sub: OverruledAttributeSub,
}
//
pub enum OverruledAttributeSub {
DefaultSource { id: String },
Expand Down
34 changes: 20 additions & 14 deletions compiler/rustc_lint/src/levels.rs
Expand Up @@ -9,8 +9,9 @@ use crate::{
fluent_generated as fluent,
late::unerased_lint_store,
lints::{
DeprecatedLintName, IgnoredUnlessCrateSpecified, OverruledAttributeLint, RemovedLint,
RenamedLint, RenamedLintSuggestion, UnknownLint, UnknownLintSuggestion,
DeprecatedLintName, IgnoredUnlessCrateSpecified, OverruledAttributeFcw,
OverruledAttributeLint, RemovedLint, RenamedLint, RenamedLintSuggestion, UnknownLint,
UnknownLintSuggestion,
},
};
use rustc_ast as ast;
Expand All @@ -29,6 +30,7 @@ use rustc_middle::lint::{
};
use rustc_middle::query::Providers;
use rustc_middle::ty::{RegisteredTools, TyCtxt};
use rustc_session::lint::builtin::UNOVERRIDABLE_LINT_LEVEL;
use rustc_session::lint::{
builtin::{
self, FORBIDDEN_LINT_GROUPS, RENAMED_AND_REMOVED_LINTS, SINGLE_USE_LIFETIMES,
Expand All @@ -42,8 +44,7 @@ use rustc_span::symbol::{sym, Symbol};
use rustc_span::{Span, DUMMY_SP};

use crate::errors::{
MalformedAttribute, MalformedAttributeSub, OverruledAttribute, OverruledAttributeSub,
UnknownToolInScopedLint,
MalformedAttribute, MalformedAttributeSub, OverruledAttributeSub, UnknownToolInScopedLint,
};

/// Collection of lint levels for the whole crate.
Expand Down Expand Up @@ -637,11 +638,13 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
{
*id = id.normalize();
}
// Setting to a non-forbid level is an error if the lint previously had
// Setting to a non-forbid level is a mistake if the lint previously had
// a forbid level. Note that this is not necessarily true even with a
// `#[forbid(..)]` attribute present, as that is overridden by `--cap-lints`.
// While this could be a hard error (and was in the past), this causes issues
// with macros expanding to deny() in contexts where it's already forbid().
//
// This means that this only errors if we're truly lowering the lint
// This means that this only warns if we're truly lowering the lint
// level from forbid.
if self.lint_added_lints && level != Level::Forbid {
if let Level::Forbid = old_level {
Expand Down Expand Up @@ -674,18 +677,21 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
LintLevelSource::CommandLine(_, _) => OverruledAttributeSub::CommandLineSource,
};
if !fcw_warning {
self.sess.dcx().emit_err(OverruledAttribute {
span: src.span(),
overruled: src.span(),
lint_level: level.as_str(),
lint_source: src.name(),
sub,
});
self.emit_span_lint(
UNOVERRIDABLE_LINT_LEVEL,
src.span().into(),
OverruledAttributeLint {
overruled: src.span(),
lint_level: level.as_str(),
lint_source: src.name(),
sub,
},
);
} else {
self.emit_span_lint(
FORBIDDEN_LINT_GROUPS,
src.span().into(),
OverruledAttributeLint {
OverruledAttributeFcw {
overruled: src.span(),
lint_level: level.as_str(),
lint_source: src.name(),
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Expand Up @@ -991,6 +991,17 @@ impl AddToDiagnostic for NonBindingLetSub {
// levels.rs
#[derive(LintDiagnostic)]
#[diag(lint_overruled_attribute)]
pub struct OverruledAttributeFcw<'a> {
#[label]
pub overruled: Span,
pub lint_level: &'a str,
pub lint_source: Symbol,
#[subdiagnostic]
pub sub: OverruledAttributeSub,
}

#[derive(LintDiagnostic)]
#[diag(lint_overruled_attribute, code = E0453)]
pub struct OverruledAttributeLint<'a> {
#[label]
pub overruled: Span,
Expand Down
29 changes: 28 additions & 1 deletion compiler/rustc_lint_defs/src/builtin.rs
Expand Up @@ -166,7 +166,7 @@ declare_lint! {
Warn,
"applying forbid to lint-groups",
@future_incompatible = FutureIncompatibleInfo {
reason: FutureIncompatibilityReason::FutureReleaseErrorDontReportInDeps,
reason: FutureIncompatibilityReason::FutureReleaseSemanticsChange,
reference: "issue #81670 <https://github.com/rust-lang/rust/issues/81670>",
};
}
Expand Down Expand Up @@ -4534,3 +4534,30 @@ declare_lint! {
reference: "issue #120192 <https://github.com/rust-lang/rust/issues/120192>",
};
}

declare_lint! {
/// The `unoverridable_lint_level` lint detects attempts at overriding a `forbid()`
/// lint. This does **nothing**, as `forbid()` cannot be overridden.
///
/// ### Example
///
/// ```rust
/// #![forbid(unsafe_code)]
///
/// #[deny(unsafe_code)]
/// fn main() {}
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// Then `forbid()` is used on a lint, this lint is guaranteed to be a hard error
/// and cannot be overriden. But because macros may use their own `deny()` in their
/// expanded output, which might get expanded into a context that is `forbid()` already,
/// this is not a hard error.
/// As lints in macros are suppressed, so this lint will not show up in these cases.
pub UNOVERRIDABLE_LINT_LEVEL,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better lint name ideas welcome

Warn,
"attempting to overriding a forbid lint",
}
6 changes: 0 additions & 6 deletions tests/ui/error-codes/E0453.rs

This file was deleted.

12 changes: 0 additions & 12 deletions tests/ui/error-codes/E0453.stderr

This file was deleted.

7 changes: 7 additions & 0 deletions tests/ui/lint/auxiliary/deny-macro.rs
@@ -0,0 +1,7 @@
#[macro_export]
macro_rules! emit_deny {
() => {
#[deny(unsafe_code)]
let _so_safe = 0;
};
}
6 changes: 3 additions & 3 deletions tests/ui/lint/forbid-group-group-2.rs
Expand Up @@ -6,9 +6,9 @@

#[allow(nonstandard_style)]
//~^ ERROR incompatible with previous
//~| WARNING previously accepted by the compiler
//~| WARNING this will change its meaning in a future release!
//~| ERROR incompatible with previous
//~| WARNING previously accepted by the compiler
//~| WARNING this will change its meaning in a future release!
//~| ERROR incompatible with previous
//~| WARNING previously accepted by the compiler
//~| WARNING this will change its meaning in a future release!
fn main() {}
6 changes: 3 additions & 3 deletions tests/ui/lint/forbid-group-group-2.stderr
Expand Up @@ -7,7 +7,7 @@ LL | #![forbid(warnings)]
LL | #[allow(nonstandard_style)]
| ^^^^^^^^^^^^^^^^^ overruled by previous forbid
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= warning: this will change its meaning in a future release!
= note: for more information, see issue #81670 <https://github.com/rust-lang/rust/issues/81670>
note: the lint level is defined here
--> $DIR/forbid-group-group-2.rs:5:9
Expand All @@ -24,7 +24,7 @@ LL | #![forbid(warnings)]
LL | #[allow(nonstandard_style)]
| ^^^^^^^^^^^^^^^^^ overruled by previous forbid
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= warning: this will change its meaning in a future release!
= note: for more information, see issue #81670 <https://github.com/rust-lang/rust/issues/81670>
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

Expand All @@ -37,7 +37,7 @@ LL | #![forbid(warnings)]
LL | #[allow(nonstandard_style)]
| ^^^^^^^^^^^^^^^^^ overruled by previous forbid
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= warning: this will change its meaning in a future release!
= note: for more information, see issue #81670 <https://github.com/rust-lang/rust/issues/81670>
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lint/forbid-group-member.rs
Expand Up @@ -7,7 +7,7 @@

#[allow(unused_variables)]
//~^ WARNING incompatible with previous forbid
//~| WARNING previously accepted
//~| WARNING this will change its meaning in a future release!
fn main() {
let a: ();
}
2 changes: 1 addition & 1 deletion tests/ui/lint/forbid-group-member.stderr
Expand Up @@ -7,7 +7,7 @@ LL |
LL | #[allow(unused_variables)]
| ^^^^^^^^^^^^^^^^ overruled by previous forbid
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= warning: this will change its meaning in a future release!
= note: for more information, see issue #81670 <https://github.com/rust-lang/rust/issues/81670>
= note: `#[warn(forbidden_lint_groups)]` on by default

Expand Down
3 changes: 2 additions & 1 deletion tests/ui/lint/forbid-member-group.rs
@@ -1,10 +1,11 @@
// Check what happens when we forbid a member of
// a group but then allow the group.
//@ check-pass

#![forbid(unused_variables)]

#[allow(unused)]
//~^ ERROR incompatible with previous forbid
//~^ WARNING incompatible with previous forbid
fn main() {
let a: ();
}
8 changes: 5 additions & 3 deletions tests/ui/lint/forbid-member-group.stderr
@@ -1,12 +1,14 @@
error[E0453]: allow(unused) incompatible with previous forbid
--> $DIR/forbid-member-group.rs:6:9
warning[E0453]: allow(unused) incompatible with previous forbid
--> $DIR/forbid-member-group.rs:7:9
|
LL | #![forbid(unused_variables)]
| ---------------- `forbid` level set here
LL |
LL | #[allow(unused)]
| ^^^^^^ overruled by previous forbid
|
= note: `#[warn(unoverridable_lint_level)]` on by default

error: aborting due to 1 previous error
warning: 1 warning emitted

For more information about this error, try `rustc --explain E0453`.
10 changes: 10 additions & 0 deletions tests/ui/lint/forbid-with-macro-deny.rs
@@ -0,0 +1,10 @@
//@ aux-build:deny-macro.rs
//@ check-pass

#![forbid(unsafe_code)]

extern crate deny_macro;

fn main() {
deny_macro::emit_deny! {}
}
Expand Up @@ -20,7 +20,7 @@ fn forbid_first(num: i32) -> i32 {
#![forbid(unused)]
#![deny(unused)]
//~^ ERROR: deny(unused) incompatible with previous forbid
//~| WARNING being phased out
//~| WARNING this will change its meaning in a future release!
#![warn(unused)]
#![allow(unused)]

Expand Down
Expand Up @@ -6,7 +6,7 @@ LL | #![forbid(unused)]
LL | #![deny(unused)]
| ^^^^^^ overruled by previous forbid
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= warning: this will change its meaning in a future release!
= note: for more information, see issue #81670 <https://github.com/rust-lang/rust/issues/81670>
note: the lint level is defined here
--> $DIR/issue-70819-dont-override-forbid-in-same-scope.rs:17:11
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lint/issue-80988.rs
Expand Up @@ -6,5 +6,5 @@

#[deny(warnings)]
//~^ WARNING incompatible with previous forbid
//~| WARNING being phased out
//~| WARNING this will change its meaning in a future release!
fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/lint/issue-80988.stderr
Expand Up @@ -7,7 +7,7 @@ LL |
LL | #[deny(warnings)]
| ^^^^^^^^ overruled by previous forbid
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= warning: this will change its meaning in a future release!
= note: for more information, see issue #81670 <https://github.com/rust-lang/rust/issues/81670>
= note: `#[warn(forbidden_lint_groups)]` on by default

Expand Down
3 changes: 2 additions & 1 deletion tests/ui/lint/lint-forbid-attr.rs
@@ -1,6 +1,7 @@
//@ check-pass
#![forbid(deprecated)]

#[allow(deprecated)]
//~^ ERROR allow(deprecated) incompatible
//~^ WARNING allow(deprecated) incompatible
fn main() {
}
8 changes: 5 additions & 3 deletions tests/ui/lint/lint-forbid-attr.stderr
@@ -1,12 +1,14 @@
error[E0453]: allow(deprecated) incompatible with previous forbid
--> $DIR/lint-forbid-attr.rs:3:9
warning[E0453]: allow(deprecated) incompatible with previous forbid
--> $DIR/lint-forbid-attr.rs:4:9
|
LL | #![forbid(deprecated)]
| ---------- `forbid` level set here
LL |
LL | #[allow(deprecated)]
| ^^^^^^^^^^ overruled by previous forbid
|
= note: `#[warn(unoverridable_lint_level)]` on by default

error: aborting due to 1 previous error
warning: 1 warning emitted

For more information about this error, try `rustc --explain E0453`.
3 changes: 2 additions & 1 deletion tests/ui/lint/lint-forbid-cmdline.rs
@@ -1,5 +1,6 @@
//@ compile-flags: -F deprecated
//@ check-pass

#[allow(deprecated)] //~ ERROR allow(deprecated) incompatible
#[allow(deprecated)] //~ WARNING allow(deprecated) incompatible
fn main() {
}
7 changes: 4 additions & 3 deletions tests/ui/lint/lint-forbid-cmdline.stderr
@@ -1,11 +1,12 @@
error[E0453]: allow(deprecated) incompatible with previous forbid
--> $DIR/lint-forbid-cmdline.rs:3:9
warning[E0453]: allow(deprecated) incompatible with previous forbid
--> $DIR/lint-forbid-cmdline.rs:4:9
|
LL | #[allow(deprecated)]
| ^^^^^^^^^^ overruled by previous forbid
|
= note: `forbid` lint level was set on command line
= note: `#[warn(unoverridable_lint_level)]` on by default

error: aborting due to 1 previous error
warning: 1 warning emitted

For more information about this error, try `rustc --explain E0453`.
7 changes: 4 additions & 3 deletions tests/ui/lint/outer-forbid.rs
Expand Up @@ -19,14 +19,15 @@

#[allow(unused_variables)]
//~^ ERROR incompatible with previous
//~| WARNING this was previously accepted by the compiler
//~| WARNING this will change its meaning in a future release!
fn foo() {}
//~^ ERROR function `foo` is never used

#[allow(unused)] //~ ERROR incompatible with previous
//~^ WARNING this was previously accepted by the compiler
//~^ WARNING this will change its meaning in a future release!
fn bar() {}

#[allow(nonstandard_style)] //~ ERROR incompatible with previous
#[allow(nonstandard_style)] //~ WARNING incompatible with previous
fn main() {
println!("hello forbidden world")
}