Skip to content

Commit ccdb320

Browse files
authored
Rollup merge of rust-lang#48026 - Badel2:doc-assoc-const-object-safe, r=nikomatsakis
Document that associated constants prevent a trait from being made into an object Fixes rust-lang#47952 Add a short mention of associated constants to E0038
2 parents 3373f65 + 498ef20 commit ccdb320

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/librustc/diagnostics.rs

+22
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,28 @@ trait Foo {
256256
}
257257
```
258258
259+
### The trait cannot contain associated constants
260+
261+
Just like static functions, associated constants aren't stored on the method
262+
table. If the trait or any subtrait contain an associated constant, they cannot
263+
be made into an object.
264+
265+
```compile_fail,E0038
266+
trait Foo {
267+
const X: i32;
268+
}
269+
270+
impl Foo {}
271+
```
272+
273+
A simple workaround is to use a helper method instead:
274+
275+
```
276+
trait Foo {
277+
fn x(&self) -> i32;
278+
}
279+
```
280+
259281
### The trait cannot use `Self` as a type parameter in the supertrait listing
260282
261283
This is similar to the second sub-error, but subtler. It happens in situations

0 commit comments

Comments
 (0)