about summary refs log tree commit diff
path: root/compiler/rustc_error_codes/src
diff options
context:
space:
mode:
authorPrincess Entrapta <nomail@gmail.com>2024-07-19 04:15:42 +0200
committerPrincess Entrapta <nomail@gmail.com>2024-07-19 15:37:13 +0200
commitaf7ecb6333950031bf07e5a2fadc7327b7f84963 (patch)
treeda069e19d43105202c5dc3437e877c0c4845c636 /compiler/rustc_error_codes/src
parent8c3a94a1c79c67924558a4adf7fb6d98f5f0f741 (diff)
downloadrust-af7ecb6333950031bf07e5a2fadc7327b7f84963.tar.gz
rust-af7ecb6333950031bf07e5a2fadc7327b7f84963.zip
fix: explain E0120 better cover cases when its raised
Diffstat (limited to 'compiler/rustc_error_codes/src')
-rw-r--r--compiler/rustc_error_codes/src/error_codes/E0120.md24
1 files changed, 16 insertions, 8 deletions
diff --git a/compiler/rustc_error_codes/src/error_codes/E0120.md b/compiler/rustc_error_codes/src/error_codes/E0120.md
index dc7258d8731..aa701df5774 100644
--- a/compiler/rustc_error_codes/src/error_codes/E0120.md
+++ b/compiler/rustc_error_codes/src/error_codes/E0120.md
@@ -1,7 +1,7 @@
-Drop was implemented on a trait, which is not allowed: only structs and
-enums can implement Drop.
+`Drop` was implemented on a trait object or reference, which is not allowed;
+only structs, enums, and unions can implement Drop.
 
-Erroneous code example:
+Erroneous code examples:
 
 ```compile_fail,E0120
 trait MyTrait {}
@@ -11,8 +11,16 @@ impl Drop for MyTrait {
 }
 ```
 
-A workaround for this problem is to wrap the trait up in a struct, and implement
-Drop on that:
+```compile_fail,E0120
+struct Concrete {}
+
+impl Drop for &'_ mut Concrete  {
+    fn drop(&mut self) {}
+}
+```
+
+A workaround for traits is to create a wrapper struct with a generic type,
+add a trait bound to the type, and implement `Drop` on the wrapper:
 
 ```
 trait MyTrait {}
@@ -24,13 +32,13 @@ impl <T: MyTrait> Drop for MyWrapper<T> {
 
 ```
 
-Alternatively, wrapping trait objects requires something:
+Alternatively, the `Drop` wrapper can contain the trait object:
 
 ```
 trait MyTrait {}
 
-//or Box<MyTrait>, if you wanted an owned trait object
-struct MyWrapper<'a> { foo: &'a MyTrait }
+// or Box<dyn MyTrait>, if you wanted an owned trait object
+struct MyWrapper<'a> { foo: &'a dyn MyTrait }
 
 impl <'a> Drop for MyWrapper<'a> {
     fn drop(&mut self) {}