Skip to content

Commit 7b714d4

Browse files
authored
Rollup merge of #121560 - Noratrieb:stop-lint-macro-nonsense, r=jieyouxu
Allow `#[deny]` inside `#[forbid]` as a no-op 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]` (or `#[warn]` for an allow-by-default lint) 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! By making it a warning instead, we remove the problem with the macro, which is now nothing as warnings are suppressed in macro expanded code, while still telling users that something is up. fixes #121483
2 parents bfab34a + 1af9d11 commit 7b714d4

18 files changed

+260
-32
lines changed

compiler/rustc_lint/src/levels.rs

Lines changed: 4 additions & 1 deletion
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)`

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ declare_lint! {
156156
///
157157
/// ```rust
158158
/// #![forbid(warnings)]
159-
/// #![deny(bad_style)]
159+
/// #![warn(bad_style)]
160160
///
161161
/// fn main() {}
162162
/// ```

src/tools/tidy/src/issues.txt

Lines changed: 0 additions & 1 deletion
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
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[macro_export]
2+
macro_rules! emit_allow {
3+
() => {
4+
#[allow(unsafe_code)]
5+
let _so_safe = 0;
6+
};
7+
}

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

Lines changed: 7 additions & 0 deletions
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+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[macro_export]
2+
macro_rules! emit_forbid {
3+
() => {
4+
#[forbid(unsafe_code)]
5+
let _so_safe = 0;
6+
};
7+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[macro_export]
2+
macro_rules! emit_warn {
3+
() => {
4+
#[warn(unsafe_code)]
5+
let _so_safe = 0;
6+
};
7+
}
Lines changed: 35 additions & 0 deletions
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:12: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:12: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:16:13
23+
|
24+
LL | unsafe { /* ≽^•⩊•^≼ */ }
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^
26+
|
27+
note: the lint level is defined here
28+
--> $DIR/deny-inside-forbid-ignored.rs:8: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`.
Lines changed: 35 additions & 0 deletions
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:12: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:12: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:16:13
23+
|
24+
LL | unsafe { /* ≽^•⩊•^≼ */ }
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^
26+
|
27+
note: the lint level is defined here
28+
--> $DIR/deny-inside-forbid-ignored.rs:8: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`.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Ensure that using deny inside forbid is treated as a no-op, and does not override the level to
2+
//! deny.
3+
4+
//@ revisions: source_only cli_forbid cli_forbid_warnings
5+
//@[cli_forbid] compile-flags: -F unsafe_code
6+
//@[cli_forbid_warnings] compile-flags: -F warnings
7+
8+
#[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
9+
fn main() {
10+
#[deny(unsafe_code)] // m-m-maybe we can have unsafe code in here?
11+
{
12+
#[allow(unsafe_code)] // let's have some unsafe code in here
13+
//~^ ERROR allow(unsafe_code) incompatible with previous forbid
14+
//~| ERROR allow(unsafe_code) incompatible with previous forbid
15+
{
16+
unsafe { /* ≽^•⩊•^≼ */ }
17+
//~^ ERROR usage of an `unsafe` block
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)