From 498ef20a2a2ce5da889440fb8708406b6f17e8d0 Mon Sep 17 00:00:00 2001 From: Badel2 <2badel2@gmail.com> Date: Tue, 6 Feb 2018 02:13:39 +0100 Subject: [PATCH] Trait objects cannot contain associated constants --- src/librustc/diagnostics.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 8bd89b834d6b6..4c256556191fa 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -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