]> git.lizzy.rs Git - rust.git/commit - compiler/rustc_codegen_cranelift/src/inline_asm.rs
Rollup merge of #103283 - nbarrios1337:unsafe-impl-suggestions, r=cjgillot
authorMatthias Krüger <matthias.krueger@famsik.de>
Fri, 28 Oct 2022 05:06:42 +0000 (07:06 +0200)
committerGitHub <noreply@github.com>
Fri, 28 Oct 2022 05:06:42 +0000 (07:06 +0200)
commit270e0c53574b938af0492b4321984d9a09ef5b47
treec383aa725ee76bd5f73072fe8c7e408643db3e6a
parentc404092084c518d126f77c1369ab63e340ce693e
parent770538ef9b9a93038983ee5d82e07754c279df06
Rollup merge of #103283 - nbarrios1337:unsafe-impl-suggestions, r=cjgillot

Add suggestions for unsafe impl error codes

Adds suggestions for users to add `unsafe` to trait impls that should be `unsafe`, and remove `unsafe` from trait impls that do not require `unsafe`

With the folllowing code:

```rust
struct Foo {}

struct Bar {}

trait Safe {}

unsafe trait Unsafe {}

impl Safe for Foo {} // ok

impl Unsafe for Foo {} // E0200

unsafe impl Safe for Bar {} // E0199

unsafe impl Unsafe for Bar {} // ok

// omitted empty main fn
```

The current rustc output is:
```
error[E0199]: implementing the trait `Safe` is not unsafe
  --> e0200.rs:13:1
   |
13 | unsafe impl Safe for Bar {} // E0199
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0200]: the trait `Unsafe` requires an `unsafe impl` declaration
  --> e0200.rs:11:1
   |
11 | impl Unsafe for Foo {} // E0200
   | ^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0199, E0200.
For more information about an error, try `rustc --explain E0199`.
```

With this PR, the future rustc output would be:
```
error[E0199]: implementing the trait `Safe` is not unsafe
  --> ../../temp/e0200.rs:13:1
   |
13 | unsafe impl Safe for Bar {} // E0199
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: remove `unsafe` from this trait implementation
   |
13 - unsafe impl Safe for Bar {} // E0199
13 + impl Safe for Bar {} // E0199
   |

error[E0200]: the trait `Unsafe` requires an `unsafe impl` declaration
  --> ../../temp/e0200.rs:11:1
   |
11 | impl Unsafe for Foo {} // E0200
   | ^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: the trait `Unsafe` enforces invariants that the compiler can't check. Review the trait documentation and make sure this implementation upholds those invariants before adding the `unsafe` keyword
help: add `unsafe` to this trait implementation
   |
11 | unsafe impl Unsafe for Foo {} // E0200
   | ++++++

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0199, E0200.
For more information about an error, try `rustc --explain E0199`.
```

``@rustbot`` label +T-compiler +A-diagnostics +A-suggestion-diagnostics