rust-lang / rust

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

Unsoundness: Rust since 1.78 accepts invalid ? #132463

Closed shao-hua-li closed 1 hour ago

shao-hua-li commented 2 hours ago

Code

I tried this code:

pub trait Frob<T: ?Frob> {}

I expected to see this happen: rustc rejects this code

Instead, this happened: rustc since 1.78 accepts the code

Version it worked on

It most recently worked on: Rust 1.77

% rustc -Awarnings --crate-type=lib test.rs
error[E0107]: missing generics for trait `Frob`
 --> src/lib.rs:1:20
  |
1 | pub trait Frob<T: ?Frob> {}
  |                    ^^^^ expected 1 generic argument
  |
note: trait defined here, with 1 generic parameter: `T`
 --> src/lib.rs:1:11
  |
1 | pub trait Frob<T: ?Frob> {}
  |           ^^^^ -
help: add missing generic argument
  |
1 | pub trait Frob<T: ?Frob<T>> {}
  |                        +++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0107`.
%
% rustc --version --verbose
rustc 1.77.0-nightly (e51e98dde 2023-12-31)
binary: rustc
commit-hash: e51e98dde6a60637b6a71b8105245b629ac3fe77
commit-date: 2023-12-31
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6
%

Version with regression

% rustc -Awarnings --crate-type=lib test.rs
%
% rustc --version --verbose
rustc 1.84.0-nightly (a0d98ff0e 2024-10-31)
binary: rustc
commit-hash: a0d98ff0e5b6e1f2c63fd26f68484792621b235c
commit-date: 2024-10-31
host: x86_64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.1
%
compiler-errors commented 1 hour ago

This is not unsound. This is just nonsense code being accepted by the compiler. There is no meaning to ? on arbitrary traits, so ignoring the bound outright is obviously not desirable, but totally fine for the compiler to do.

I fixed this in https://github.com/rust-lang/rust/pull/132209.