-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Associated constants should not be object safe #26847
Comments
I think the code should be legal to compile. It's impossible to get the value of |
@oli-obk |
I don't know if this can lead to unsoundness or not. Correct me. |
@eddyb says no |
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 |
Can't associated consts be part of the vtable/fat pointers? |
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. |
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. |
The value is not determined at compile time for the virtually dispatched type |
I did not say otherwise (the value is only determined at compile-time for the types that implement |
Maybe the problem is that traits support associated consts, and associated consts are values, but traits do not support associated values (yet). |
Will they be object-safe in the future? |
The following code compiles:
Furthermore, adding the line
<Foo>::FOO;
tomain
causes an ICE:The text was updated successfully, but these errors were encountered: