You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Copy file name to clipboardExpand all lines: src/test/ui/coherence/coherence-default-trait-impl.stderr
+12
Original file line number
Diff line number
Diff line change
@@ -3,12 +3,24 @@ error[E0199]: implementing the trait `MySafeTrait` is not unsafe
3
3
|
4
4
LL | unsafe impl MySafeTrait for Foo {}
5
5
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6
+
|
7
+
help: remove `unsafe` from this trait implementation
8
+
|
9
+
LL - unsafe impl MySafeTrait for Foo {}
10
+
LL + impl MySafeTrait for Foo {}
11
+
|
6
12
7
13
error[E0200]: the trait `MyUnsafeTrait` requires an `unsafe impl` declaration
8
14
--> $DIR/coherence-default-trait-impl.rs:13:1
9
15
|
10
16
LL | impl MyUnsafeTrait for Foo {}
11
17
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18
+
|
19
+
= note: the trait `MyUnsafeTrait` 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
= note: the trait `Drop` 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
13
+
help: add `unsafe` to this trait implementation
14
+
|
15
+
LL | unsafe impl<#[may_dangle] A, B: fmt::Debug> Drop for Pt<A, B> {
16
+
| ++++++
11
17
12
18
error[E0569]: requires an `unsafe impl` declaration due to `#[may_dangle]` attribute
= note: the trait `Drop` 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
30
+
help: add `unsafe` to this trait implementation
31
+
|
32
+
LL | unsafe impl<#[may_dangle] 'a, 'b, B: fmt::Debug> Drop for Pr<'a, 'b, B> {
Copy file name to clipboardExpand all lines: src/test/ui/error-codes/E0200.stderr
+6
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,12 @@ error[E0200]: the trait `Bar` requires an `unsafe impl` declaration
3
3
|
4
4
LL | impl Bar for Foo { }
5
5
| ^^^^^^^^^^^^^^^^^^^^
6
+
|
7
+
= note: the trait `Bar` 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
Copy file name to clipboardExpand all lines: src/test/ui/traits/safety-trait-impl-cc.stderr
+6
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,12 @@ LL | | panic!();
7
7
LL | | }
8
8
LL | | }
9
9
| |_^
10
+
|
11
+
= note: the trait `Foo` 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
Copy file name to clipboardExpand all lines: src/test/ui/traits/safety-trait-impl.stderr
+12
Original file line number
Diff line number
Diff line change
@@ -3,12 +3,24 @@ error[E0200]: the trait `UnsafeTrait` requires an `unsafe impl` declaration
3
3
|
4
4
LL | impl UnsafeTrait for u16 { }
5
5
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6
+
|
7
+
= note: the trait `UnsafeTrait` 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
8
+
help: add `unsafe` to this trait implementation
9
+
|
10
+
LL | unsafe impl UnsafeTrait for u16 { }
11
+
| ++++++
6
12
7
13
error[E0199]: implementing the trait `SafeTrait` is not unsafe
8
14
--> $DIR/safety-trait-impl.rs:16:1
9
15
|
10
16
LL | unsafe impl SafeTrait for u32 { }
11
17
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18
+
|
19
+
help: remove `unsafe` from this trait implementation
0 commit comments