Skip to content

Commit 072cba9

Browse files
committed
Auto merge of #25422 - cactorium:unsafe_errors, r=huonw
For #24407
2 parents 0077ffe + 190de69 commit 072cba9

File tree

1 file changed

+70
-4
lines changed

1 file changed

+70
-4
lines changed

src/librustc_typeck/diagnostics.rs

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,76 @@ it has been disabled for now.
364364
[iss20126]: https://github.com/rust-lang/rust/issues/20126
365365
"##,
366366

367+
E0197: r##"
368+
Inherent implementations (one that do not implement a trait but provide
369+
methods associated with a type) are always safe because they are not
370+
implementing an unsafe trait. Removing the `unsafe` keyword from the inherent
371+
implementation will resolve this error.
372+
373+
```
374+
struct Foo;
375+
376+
// this will cause this error
377+
unsafe impl Foo { }
378+
// converting it to this will fix it
379+
impl Foo { }
380+
```
381+
382+
"##,
383+
384+
E0198: r##"
385+
A negative implementation is one that excludes a type from implementing a
386+
particular trait. Not being able to use a trait is always a safe operation,
387+
so negative implementations are always safe and never need to be marked as
388+
unsafe.
389+
390+
```
391+
struct Foo;
392+
393+
// unsafe is unnecessary
394+
unsafe impl !Clone for Foo { }
395+
// this will compile
396+
impl !Clone for Foo { }
397+
```
398+
399+
"##,
400+
401+
E0199: r##"
402+
Safe traits should not have unsafe implementations, therefore marking an
403+
implementation for a safe trait unsafe will cause a compiler error. Removing the
404+
unsafe marker on the trait noted in the error will resolve this problem.
405+
406+
```
407+
struct Foo;
408+
409+
trait Bar { }
410+
411+
// this won't compile because Bar is safe
412+
unsafe impl Bar for Foo { }
413+
// this will compile
414+
impl Bar for Foo { }
415+
```
416+
417+
"##,
418+
419+
E0200: r##"
420+
Unsafe traits must have unsafe implementations. This error occurs when an
421+
implementation for an unsafe trait isn't marked as unsafe. This may be resolved
422+
by marking the unsafe implementation as unsafe.
423+
424+
```
425+
struct Foo;
426+
427+
unsafe trait Bar { }
428+
429+
// this won't compile because Bar is unsafe and impl isn't unsafe
430+
impl Bar for Foo { }
431+
// this will compile
432+
unsafe impl Bar for Foo { }
433+
```
434+
435+
"##,
436+
367437
E0201: r##"
368438
It is an error to define a method--a trait method or an inherent method--more
369439
than once.
@@ -648,10 +718,6 @@ register_diagnostics! {
648718
E0194,
649719
E0195, // lifetime parameters or bounds on method do not match the trait declaration
650720
E0196, // cannot determine a type for this closure
651-
E0197, // inherent impls cannot be declared as unsafe
652-
E0198, // negative implementations are not unsafe
653-
E0199, // implementing trait is not unsafe
654-
E0200, // trait requires an `unsafe impl` declaration
655721
E0202, // associated items are not allowed in inherent impls
656722
E0203, // type parameter has more than one relaxed default bound,
657723
// and only one is supported

0 commit comments

Comments
 (0)