Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAssociated constants should not be object safe #26847
Comments
steveklabnik
added
A-typesystem
A-traits
A-associated-items
labels
Jul 7, 2015
This comment has been minimized.
This comment has been minimized.
|
I think the code should be legal to compile. It's impossible to get the value of |
This comment has been minimized.
This comment has been minimized.
|
@oli-obk |
brson
added
T-lang
I-wrong
I-unsound 💥
labels
Jan 26, 2017
This comment has been minimized.
This comment has been minimized.
|
I don't know if this can lead to unsoundness or not. Correct me. |
brson
removed
the
I-unsound 💥
label
Jan 26, 2017
This comment has been minimized.
This comment has been minimized.
|
@eddyb says no |
brson
referenced this issue
Jan 26, 2017
Closed
Tracking issue for `associated_consts` feature #29646
This comment has been minimized.
This comment has been minimized.
|
Update on this, the example in the original post compiles today. But this ICEs: #![feature(associated_consts)]
trait Foo {
const FOO: u32;
}
impl Foo for () {
const FOO: u32 = 1;
}
fn main() {
let _ = <Foo as Foo>::FOO;
}AFAICT there are two options here:
I think its sensible to start with the first. The second makes more sense in the context of a world with const params, but the first can be extended to the second. It might also make sense some day to allow |
This comment has been minimized.
This comment has been minimized.
withoutboats
referenced this issue
Apr 24, 2017
Merged
Associated consts are not object safe. #41494
bors
added a commit
that referenced
this issue
Apr 24, 2017
bors
closed this
in
#41494
Apr 24, 2017
This comment has been minimized.
This comment has been minimized.
|
Can't associated consts be part of the vtable/fat pointers? |
This comment has been minimized.
This comment has been minimized.
|
Associated consts are inlined at compiletime, they can't have a value which is determined at runtime. This is a hard requirement for making things like const fn and const generics work. Associated statics could be object safe if they existed. |
This comment has been minimized.
This comment has been minimized.
|
I think that having to write: trait Foo {
fn get_x(&self) -> u32;
}
trait FooC {
const X: u32;
}
impl FooC for () {
const X: u32 = 3;
}
impl Foo for () {
fn get_x(&self) -> u32 { <() as FooC>::X } ; // or just 3
}
instead of just: trait Foo {
const X: u32;
}or trait Foo {
const X: u32;
fn get_x(&self) -> u32 { Self::X }
}is a pain.
The value is determined at compile-time, but I am talking about being able to access that value at run-time via dynamic dispatch. |
This comment has been minimized.
This comment has been minimized.
|
The value is not determined at compile time for the virtually dispatched type |
This comment has been minimized.
This comment has been minimized.
I did not say otherwise (the value is only determined at compile-time for the types that implement |
This comment has been minimized.
This comment has been minimized.
|
Maybe the problem is that traits support associated consts, and associated consts are values, but traits do not support associated values (yet). |
P1start commentedJul 7, 2015
The following code compiles:
Furthermore, adding the line
<Foo>::FOO;tomaincauses an ICE: