Skip to content

Commit e3c12d2

Browse files
committed
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.
1 parent 3a85d3f commit e3c12d2

10 files changed

+83
-31
lines changed

compiler/rustc_lint/src/levels.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,10 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
493493
//
494494
// This means that this only errors if we're truly lowering the lint
495495
// level from forbid.
496-
if self.lint_added_lints && level != Level::Forbid && old_level == Level::Forbid {
496+
if self.lint_added_lints && level == Level::Deny && old_level == Level::Forbid {
497+
// Having a deny inside a forbid is fine and is ignored, so we skip this check.
498+
return;
499+
} else if self.lint_added_lints && level != Level::Forbid && old_level == Level::Forbid {
497500
// Backwards compatibility check:
498501
//
499502
// We used to not consider `forbid(lint_group)`

src/tools/tidy/src/issues.txt

-1
Original file line numberDiff line numberDiff line change
@@ -2758,7 +2758,6 @@ ui/lint/issue-63364.rs
27582758
ui/lint/issue-70819-dont-override-forbid-in-same-scope.rs
27592759
ui/lint/issue-79546-fuel-ice.rs
27602760
ui/lint/issue-79744.rs
2761-
ui/lint/issue-80988.rs
27622761
ui/lint/issue-81218.rs
27632762
ui/lint/issue-83477.rs
27642763
ui/lint/issue-87274-paren-parent.rs

tests/ui/lint/auxiliary/deny-macro.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[macro_export]
2+
macro_rules! emit_deny {
3+
() => {
4+
#[deny(unsafe_code)]
5+
let _so_safe = 0;
6+
};
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// Ensure that using deny inside forbid is treated as a no-op,
2+
/// and does not override the level to deny.
3+
4+
#[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
5+
fn main() {
6+
#[deny(unsafe_code)] // m-m-maybe we can have unsafe code in here?
7+
{
8+
#[allow(unsafe_code)] // let's have some unsafe code in here
9+
//~^ ERROR allow(unsafe_code) incompatible with previous forbid
10+
//~| ERROR allow(unsafe_code) incompatible with previous forbid
11+
{
12+
unsafe { /* ≽^•⩊•^≼ */ }
13+
//~^ ERROR usage of an `unsafe` block
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0453]: allow(unsafe_code) incompatible with previous forbid
2+
--> $DIR/deny-inside-forbid-ignored.rs:8:17
3+
|
4+
LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
5+
| ----------- `forbid` level set here
6+
...
7+
LL | #[allow(unsafe_code)] // let's have some unsafe code in here
8+
| ^^^^^^^^^^^ overruled by previous forbid
9+
10+
error[E0453]: allow(unsafe_code) incompatible with previous forbid
11+
--> $DIR/deny-inside-forbid-ignored.rs:8:17
12+
|
13+
LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
14+
| ----------- `forbid` level set here
15+
...
16+
LL | #[allow(unsafe_code)] // let's have some unsafe code in here
17+
| ^^^^^^^^^^^ overruled by previous forbid
18+
|
19+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20+
21+
error: usage of an `unsafe` block
22+
--> $DIR/deny-inside-forbid-ignored.rs:12:13
23+
|
24+
LL | unsafe { /* ≽^•⩊•^≼ */ }
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^
26+
|
27+
note: the lint level is defined here
28+
--> $DIR/deny-inside-forbid-ignored.rs:4:10
29+
|
30+
LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
31+
| ^^^^^^^^^^^
32+
33+
error: aborting due to 3 previous errors
34+
35+
For more information about this error, try `rustc --explain E0453`.
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ aux-build:deny-macro.rs
2+
//@ check-pass
3+
4+
// Ensure that when a macro (or normal code) does #[deny] inside a #[forbid]
5+
// context, no error is emitted, as both parties agree on the treatment of the lint.
6+
7+
#![forbid(unsafe_code)]
8+
9+
extern crate deny_macro;
10+
11+
fn main() {
12+
deny_macro::emit_deny! {}
13+
14+
#[deny(unsafe_code)]
15+
let _ = 0;
16+
}

tests/ui/lint/issue-70819-dont-override-forbid-in-same-scope.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
fn forbid_first(num: i32) -> i32 {
2020
#![forbid(unused)]
2121
#![deny(unused)]
22-
//~^ ERROR: deny(unused) incompatible with previous forbid
23-
//~| WARNING being phased out
2422
#![warn(unused)]
23+
//~^ ERROR: warn(unused) incompatible with previous forbid
24+
//~| WARNING being phased out
2525
#![allow(unused)]
2626

2727
num * num

tests/ui/lint/issue-70819-dont-override-forbid-in-same-scope.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
error: deny(unused) incompatible with previous forbid
2-
--> $DIR/issue-70819-dont-override-forbid-in-same-scope.rs:21:13
1+
error: warn(unused) incompatible with previous forbid
2+
--> $DIR/issue-70819-dont-override-forbid-in-same-scope.rs:22:13
33
|
44
LL | #![forbid(unused)]
55
| ------ `forbid` level set here
66
LL | #![deny(unused)]
7+
LL | #![warn(unused)]
78
| ^^^^^^ overruled by previous forbid
89
|
910
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

tests/ui/lint/issue-80988.rs

-10
This file was deleted.

tests/ui/lint/issue-80988.stderr

-15
This file was deleted.

0 commit comments

Comments
 (0)