]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #48026 - Badel2:doc-assoc-const-object-safe, r=nikomatsakis
authorkennytm <kennytm@gmail.com>
Tue, 6 Feb 2018 19:23:28 +0000 (03:23 +0800)
committerGitHub <noreply@github.com>
Tue, 6 Feb 2018 19:23:28 +0000 (03:23 +0800)
Document that associated constants prevent a trait from being made into an object

Fixes https://github.com/rust-lang/rust/issues/47952

Add a short mention of associated constants to E0038

src/librustc/diagnostics.rs

index 8bd89b834d6b6ce3d03da35280edb5530bc0f186..4c256556191fa58f3577ef2903b976f27b291d75 100644 (file)
@@ -256,6 +256,28 @@ trait Foo {
 }
 ```
 
+### The trait cannot contain associated constants
+
+Just like static functions, associated constants aren't stored on the method
+table. If the trait or any subtrait contain an associated constant, they cannot
+be made into an object.
+
+```compile_fail,E0038
+trait Foo {
+    const X: i32;
+}
+
+impl Foo {}
+```
+
+A simple workaround is to use a helper method instead:
+
+```
+trait Foo {
+    fn x(&self) -> i32;
+}
+```
+
 ### The trait cannot use `Self` as a type parameter in the supertrait listing
 
 This is similar to the second sub-error, but subtler. It happens in situations