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

+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)`

compiler/rustc_lint_defs/src/builtin.rs

+1-1
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

-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
+7
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

+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+
}
+7
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

+7
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+
}
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`.
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`.
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+
}
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`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0453]: allow(unsafe_code) incompatible with previous forbid
2+
--> $DIR/forbid-macro-with-deny.rs:39:5
3+
|
4+
LL | #![forbid(unsafe_code)]
5+
| ----------- `forbid` level set here
6+
...
7+
LL | allow_macro::emit_allow! {}
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ overruled by previous forbid
9+
|
10+
= note: this error originates in the macro `allow_macro::emit_allow` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error[E0453]: allow(unsafe_code) incompatible with previous forbid
13+
--> $DIR/forbid-macro-with-deny.rs:39:5
14+
|
15+
LL | #![forbid(unsafe_code)]
16+
| ----------- `forbid` level set here
17+
...
18+
LL | allow_macro::emit_allow! {}
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ overruled by previous forbid
20+
|
21+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
22+
= note: this error originates in the macro `allow_macro::emit_allow` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0453`.
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! Ensure that when a macro (or normal code) does `#[deny]` inside a `#[forbid]` context, no error
2+
//! is emitted, as both parties agree on the treatment of the lint.
3+
//!
4+
//! However, still emit an error if the macro does `#[allow]` or `#[warn]`.
5+
6+
//@ revisions: forbid deny warn allow
7+
//@[forbid] aux-build:forbid-macro.rs
8+
//@[deny] aux-build:deny-macro.rs
9+
//@[warn] aux-build:warn-macro.rs
10+
//@[allow] aux-build:allow-macro.rs
11+
12+
//@[forbid] check-pass
13+
//@[deny] check-pass
14+
15+
#![forbid(unsafe_code)]
16+
17+
#[cfg(allow)]
18+
extern crate allow_macro;
19+
#[cfg(deny)]
20+
extern crate deny_macro;
21+
#[cfg(forbid)]
22+
extern crate forbid_macro;
23+
#[cfg(warn)]
24+
extern crate warn_macro;
25+
26+
fn main() {
27+
#[cfg(forbid)]
28+
forbid_macro::emit_forbid! {} // OK
29+
30+
#[cfg(deny)]
31+
deny_macro::emit_deny! {} // OK
32+
33+
#[cfg(warn)]
34+
warn_macro::emit_warn! {}
35+
//[warn]~^ ERROR warn(unsafe_code) incompatible with previous forbid
36+
//[warn]~| ERROR warn(unsafe_code) incompatible with previous forbid
37+
38+
#[cfg(allow)]
39+
allow_macro::emit_allow! {}
40+
//[allow]~^ ERROR allow(unsafe_code) incompatible with previous forbid
41+
//[allow]~| ERROR allow(unsafe_code) incompatible with previous forbid
42+
43+
#[deny(unsafe_code)] // OK
44+
let _ = 0;
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0453]: warn(unsafe_code) incompatible with previous forbid
2+
--> $DIR/forbid-macro-with-deny.rs:34:5
3+
|
4+
LL | #![forbid(unsafe_code)]
5+
| ----------- `forbid` level set here
6+
...
7+
LL | warn_macro::emit_warn! {}
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ overruled by previous forbid
9+
|
10+
= note: this error originates in the macro `warn_macro::emit_warn` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error[E0453]: warn(unsafe_code) incompatible with previous forbid
13+
--> $DIR/forbid-macro-with-deny.rs:34:5
14+
|
15+
LL | #![forbid(unsafe_code)]
16+
| ----------- `forbid` level set here
17+
...
18+
LL | warn_macro::emit_warn! {}
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ overruled by previous forbid
20+
|
21+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
22+
= note: this error originates in the macro `warn_macro::emit_warn` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0453`.

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)