From e78d78868a4c9a4e29a02f70ede75ca8cfd39bef Mon Sep 17 00:00:00 2001 From: Noratrieb <48135649+Noratrieb@users.noreply.github.com> Date: Thu, 17 Oct 2024 19:34:04 +0200 Subject: [PATCH] Allow `#[deny(..)]` inside `#[forbid(..)]` as a no-op with a warning Forbid cannot be overriden. When someome tries to do this anyways, it results in a hard error. That makes sense. Except it doesn't, because macros. Macros may reasonably use `#[deny]` in their expansion to assert that their expanded code follows the lint. This is doesn't work when the output gets expanded into a `forbid()` context. This is pretty silly, since both the macros and the code agree on the lint! Therefore, we allow `#[deny(..)]`ing a lint that's already forbidden, keeping the level at forbid. --- compiler/rustc_lint/src/levels.rs | 5 ++- compiler/rustc_lint_defs/src/builtin.rs | 2 +- src/tools/tidy/src/issues.txt | 1 - tests/ui/lint/auxiliary/deny-macro.rs | 7 ++++ tests/ui/lint/deny-inside-forbid-ignored.rs | 16 +++++++++ .../ui/lint/deny-inside-forbid-ignored.stderr | 35 +++++++++++++++++++ tests/ui/lint/forbid-macro-with-deny.rs | 16 +++++++++ ...0819-dont-override-forbid-in-same-scope.rs | 4 +-- ...-dont-override-forbid-in-same-scope.stderr | 5 +-- tests/ui/lint/issue-80988.rs | 10 ------ tests/ui/lint/issue-80988.stderr | 15 -------- 11 files changed, 84 insertions(+), 32 deletions(-) create mode 100644 tests/ui/lint/auxiliary/deny-macro.rs create mode 100644 tests/ui/lint/deny-inside-forbid-ignored.rs create mode 100644 tests/ui/lint/deny-inside-forbid-ignored.stderr create mode 100644 tests/ui/lint/forbid-macro-with-deny.rs delete mode 100644 tests/ui/lint/issue-80988.rs delete mode 100644 tests/ui/lint/issue-80988.stderr diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 89a67fc0d89..95a8e7625ff 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -493,7 +493,10 @@ fn insert_spec(&mut self, id: LintId, (level, src): LevelAndSource) { // // This means that this only errors if we're truly lowering the lint // level from forbid. - if self.lint_added_lints && level != Level::Forbid && old_level == Level::Forbid { + if self.lint_added_lints && level == Level::Deny && old_level == Level::Forbid { + // Having a deny inside a forbid is fine and is ignored, so we skip this check. + return; + } else if self.lint_added_lints && level != Level::Forbid && old_level == Level::Forbid { // Backwards compatibility check: // // We used to not consider `forbid(lint_group)` diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 45a5ce0ca20..c11571182fe 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -156,7 +156,7 @@ /// /// ```rust /// #![forbid(warnings)] - /// #![deny(bad_style)] + /// #![warn(bad_style)] /// /// fn main() {} /// ``` diff --git a/src/tools/tidy/src/issues.txt b/src/tools/tidy/src/issues.txt index 22126674c15..97c42752c12 100644 --- a/src/tools/tidy/src/issues.txt +++ b/src/tools/tidy/src/issues.txt @@ -2758,7 +2758,6 @@ ui/lint/issue-63364.rs ui/lint/issue-70819-dont-override-forbid-in-same-scope.rs ui/lint/issue-79546-fuel-ice.rs ui/lint/issue-79744.rs -ui/lint/issue-80988.rs ui/lint/issue-81218.rs ui/lint/issue-83477.rs ui/lint/issue-87274-paren-parent.rs diff --git a/tests/ui/lint/auxiliary/deny-macro.rs b/tests/ui/lint/auxiliary/deny-macro.rs new file mode 100644 index 00000000000..6106cd0ef17 --- /dev/null +++ b/tests/ui/lint/auxiliary/deny-macro.rs @@ -0,0 +1,7 @@ +#[macro_export] +macro_rules! emit_deny { + () => { + #[deny(unsafe_code)] + let _so_safe = 0; + }; +} diff --git a/tests/ui/lint/deny-inside-forbid-ignored.rs b/tests/ui/lint/deny-inside-forbid-ignored.rs new file mode 100644 index 00000000000..da5a14ce253 --- /dev/null +++ b/tests/ui/lint/deny-inside-forbid-ignored.rs @@ -0,0 +1,16 @@ +/// Ensure that using deny inside forbid is treated as a no-op, +/// and does not override the level to deny. + +#[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!! +fn main() { + #[deny(unsafe_code)] // m-m-maybe we can have unsafe code in here? + { + #[allow(unsafe_code)] // let's have some unsafe code in here + //~^ ERROR allow(unsafe_code) incompatible with previous forbid + //~| ERROR allow(unsafe_code) incompatible with previous forbid + { + unsafe { /* ≽^•⩊•^≼ */ } + //~^ ERROR usage of an `unsafe` block + } + } +} diff --git a/tests/ui/lint/deny-inside-forbid-ignored.stderr b/tests/ui/lint/deny-inside-forbid-ignored.stderr new file mode 100644 index 00000000000..b7fc0ccf5a2 --- /dev/null +++ b/tests/ui/lint/deny-inside-forbid-ignored.stderr @@ -0,0 +1,35 @@ +error[E0453]: allow(unsafe_code) incompatible with previous forbid + --> $DIR/deny-inside-forbid-ignored.rs:8:17 + | +LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!! + | ----------- `forbid` level set here +... +LL | #[allow(unsafe_code)] // let's have some unsafe code in here + | ^^^^^^^^^^^ overruled by previous forbid + +error[E0453]: allow(unsafe_code) incompatible with previous forbid + --> $DIR/deny-inside-forbid-ignored.rs:8:17 + | +LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!! + | ----------- `forbid` level set here +... +LL | #[allow(unsafe_code)] // let's have some unsafe code in here + | ^^^^^^^^^^^ overruled by previous forbid + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +error: usage of an `unsafe` block + --> $DIR/deny-inside-forbid-ignored.rs:12:13 + | +LL | unsafe { /* ≽^•⩊•^≼ */ } + | ^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/deny-inside-forbid-ignored.rs:4:10 + | +LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!! + | ^^^^^^^^^^^ + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0453`. diff --git a/tests/ui/lint/forbid-macro-with-deny.rs b/tests/ui/lint/forbid-macro-with-deny.rs new file mode 100644 index 00000000000..e6985ab19d1 --- /dev/null +++ b/tests/ui/lint/forbid-macro-with-deny.rs @@ -0,0 +1,16 @@ +//@ aux-build:deny-macro.rs +//@ check-pass + +// Ensure that when a macro (or normal code) does #[deny] inside a #[forbid] +// context, no error is emitted, as both parties agree on the treatment of the lint. + +#![forbid(unsafe_code)] + +extern crate deny_macro; + +fn main() { + deny_macro::emit_deny! {} + + #[deny(unsafe_code)] + let _ = 0; +} diff --git a/tests/ui/lint/issue-70819-dont-override-forbid-in-same-scope.rs b/tests/ui/lint/issue-70819-dont-override-forbid-in-same-scope.rs index 37d96129317..45b78d75b27 100644 --- a/tests/ui/lint/issue-70819-dont-override-forbid-in-same-scope.rs +++ b/tests/ui/lint/issue-70819-dont-override-forbid-in-same-scope.rs @@ -19,9 +19,9 @@ fn forbid_first(num: i32) -> i32 { #![forbid(unused)] #![deny(unused)] - //~^ ERROR: deny(unused) incompatible with previous forbid - //~| WARNING being phased out #![warn(unused)] + //~^ ERROR: warn(unused) incompatible with previous forbid + //~| WARNING being phased out #![allow(unused)] num * num diff --git a/tests/ui/lint/issue-70819-dont-override-forbid-in-same-scope.stderr b/tests/ui/lint/issue-70819-dont-override-forbid-in-same-scope.stderr index f78bf899b84..407eaf1c60a 100644 --- a/tests/ui/lint/issue-70819-dont-override-forbid-in-same-scope.stderr +++ b/tests/ui/lint/issue-70819-dont-override-forbid-in-same-scope.stderr @@ -1,9 +1,10 @@ -error: deny(unused) incompatible with previous forbid - --> $DIR/issue-70819-dont-override-forbid-in-same-scope.rs:21:13 +error: warn(unused) incompatible with previous forbid + --> $DIR/issue-70819-dont-override-forbid-in-same-scope.rs:22:13 | LL | #![forbid(unused)] | ------ `forbid` level set here LL | #![deny(unused)] +LL | #![warn(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! diff --git a/tests/ui/lint/issue-80988.rs b/tests/ui/lint/issue-80988.rs deleted file mode 100644 index 80decd8e736..00000000000 --- a/tests/ui/lint/issue-80988.rs +++ /dev/null @@ -1,10 +0,0 @@ -// Regression test for #80988 -// -//@ check-pass - -#![forbid(warnings)] - -#[deny(warnings)] -//~^ WARNING incompatible with previous forbid -//~| WARNING being phased out -fn main() {} diff --git a/tests/ui/lint/issue-80988.stderr b/tests/ui/lint/issue-80988.stderr deleted file mode 100644 index afc93fcfeef..00000000000 --- a/tests/ui/lint/issue-80988.stderr +++ /dev/null @@ -1,15 +0,0 @@ -warning: deny(warnings) incompatible with previous forbid - --> $DIR/issue-80988.rs:7:8 - | -LL | #![forbid(warnings)] - | -------- `forbid` level set here -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! - = note: for more information, see issue #81670 - = note: `#[warn(forbidden_lint_groups)]` on by default - -warning: 1 warning emitted -