rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.46k stars 12.73k forks source link

`#![allow(unknown_lints)]` forward compatibility is broken by unstable lints #103713

Open lopopolo opened 2 years ago

lopopolo commented 2 years ago

I tried this code:

#![allow(unknown_lints)]
#![warn(unsafe_op_in_unsafe_fn)]

pub fn add(left: usize, right: usize) -> usize {
    left + right
}

I expected to see this happen: The crate compiles on all Rust versions. #![allow(unknown_lints)] treats unstable lints as unknown on stable compilers.

Instead, this happened: On versions where the unsafe_op_in_unsafe_fn lint is unstable, like Rust 1.48.0, the crate fails to compile.

$ cargo +1.64.0 check
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
$ cargo +1.38.0 check
    Finished dev [unoptimized + debuginfo] target(s) in 0.15s
$ cargo +1.48.0 check
    Checking unknown-lints-forward-compat v0.1.0 (/Users/lopopolo/Downloads/unknown-lints-forward-compat)
error[E0658]: the `unsafe_op_in_unsafe_fn` lint is unstable
 --> src/lib.rs:2:1
  |
2 | #![warn(unsafe_op_in_unsafe_fn)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #71668 <https://github.com/rust-lang/rust/issues/71668> for more information

error: aborting due to previous error

For more information about this error, try `rustc --explain E0658`.
error: could not compile `unknown-lints-forward-compat`

To learn more, run the command again with --verbose.

Meta

rustc --version --verbose:

$ rustc +1.64.0 --version --verbose
rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: x86_64-apple-darwin
release: 1.64.0
LLVM version: 14.0.6
$ rustc +1.38.0 --version --verbose
rustc 1.38.0 (625451e37 2019-09-23)
binary: rustc
commit-hash: 625451e376bb2e5283fc4741caa0a3e8a2ca4d54
commit-date: 2019-09-23
host: x86_64-apple-darwin
release: 1.38.0
LLVM version: 9.0
$ rustc +1.48.0 --version --verbose
rustc 1.48.0 (7eac88abb 2020-11-16)
binary: rustc
commit-hash: 7eac88abb2e57e752f3302f02be5f3ce3d7adfb4
commit-date: 2020-11-16
host: x86_64-apple-darwin
release: 1.48.0
LLVM version: 11.0
Backtrace

``` ```

This ticket is a re-filing of https://github.com/rust-lang/rust/issues/103698 which uses the correct issue template and includes a reproducer.

est31 commented 2 years ago

I agree that this is not good. I think that in general the concept of unstable lints is weird: lints are unstable already, so any addition of a lint could theoretically break your compilation if you have stuff like #[deny(warnings)]. What you should do is cap lints either at allow or warn.

And if you really want to have unstable lints, then it should not be an error to use them but a warn-by-default lint, whether it's unknown_lints or unstable_lints or such.

The "unstable lints" mechanism has been added by bb6791502846b62e752c99396953e4db7739f28c, and there have been attempts to treat unstable lints as unknown in 885275207810dc0143b56bc4357461aa4c8ed07b , but it seems they were not enough or were regressed upon.

est31 commented 2 years ago

Also there is obviously nothing we can do about the unsafe_op_in_unsafe_fn lint here, as it is already stable. we can only affect future lints.