-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Tracking issue for associated_consts
feature
#29646
Comments
Move to stable This PR removes unnecessary use of feature flags that bar using Rust on stable([1])([2]) compilers. Reasons being: * associated consts (Tracking issue: rust-lang/rust#29646) are currently buggy and can be replaced by an fn in that case. * associated_type_defaults can be removed without breakage * unboxed_closures can be removed without breakage * StaticMutex has an uncertain future (rust-lang/rust#27717) and can be emulated in that case by using `lazy_static!` (correct me if I'm wrong) Finally, I must admit that I didn't get the test suite running quickly. ([1]) Outstanding: this doesn't _quite_ work on stable yet, as some APIs in use are currently making their way through beta, so they are not feature gated, but also not available in 1.4.0. 1.5.0 beta works and as 1.5.0 is 2 weeks away, this is probably not worth the effort. ([2]) rblas is not on stable yet, see mikkyang/rust-blas#12 for that. You can use that version of rust-blas by checking it out from my https://github.com/skade/rust-blas/ and dropping the following `.cargo/config` in your repository: ``` paths = ["/path/to/rblas/checkout"] ```
I'm curious about the reasons behind this feature. Why aren't ordinary functions good enough for the same purpose? Given that Rust have default methods it shouldn't be much less ergonomic. And combination of monomorphisation and static lifetimes should converge any difference in terms of performance. |
One notable case is that of the |
@ketsuban how would it be different than including |
Having skimmed through #27739 it looks like there're opinions about the concepts of "zero" and "one" as functions as it may give additional flexibility. |
|
The use of associated consts could make things like |
would love to use associated consts for array sizes for example: http://is.gd/VSee6B
but this will leave me at E0250 |
I am not sure about this but I think that associated constants could allow us compile time execution for things similar to non-type template parameters as in C++ which are quite useful. Using functions wouldn't work in the example below since Rust currently has no mechanism to detect if a function can be evaluated at compile-time (constexpr as in C++ or even templates). Example: If the example above really works (haven't tested it, yet) things like BitFlag or the shown BigInt could be implemented very efficiently - not using the heap at all! |
Is there any ETA on the stabilization of this feature? It would be really useful for something which I'm currently experimenting with, namely the implementation of Windows COM interop: One could add a trait that exposes each interface's GUID and/or class ID as constants, then e.g. this API could be mapped to Rust in a way that takes the to-be-activated class as a type parameter that implements said trait. Of course I can work on this using nightly, but it would be nice to have some information whether the feature is going to be stabilized in the near future! |
Should we block on #34344? Are there other known bugs? Are there design questions to resolve here? |
There was a question on the forum here. Looks like a bug to me. |
What is status or what would need to be done to stabilize this feature? It would be really helpful in Octavo as it would allow me to provide HMAC without any heap allocations. |
@hauleth take a look at the associated-items issues. That contains many of the bugs that exist with associated constants. I imagine many of them would need to be fixed before anyone could consider this feature "stable". |
How can we accelerate this? This is a major feature and it doesn't appear to be moving or have a champion. This is used by Rocket. |
Just nominating to put it in front of eyeballs again. |
My understanding is that this is waiting on a re-do of const evaluation in the compiler, since it's so buggy. @eddyb ? |
Maybe I’m not seeing the point but as long as “Associated consts in const expressions must be concrete” is still a restriction I do not see any use of associated constants in traits. Yet you can add them such that all of the error messaged presented in #32344 are still very there and very misleading. Shouldn’t one simply forbid associated constants in traits unless that restriction is lifted? I dont’t think that it was a good idea merge #42809 (i.e. remove the feature gate) with a feature in such an incomplete state. |
@nwin It's not "in const expressions" - but rather "in types", i.e. array types. |
Is there any plan to introduce LEN (or similar attribute) as associated const for the built-in rust-primitives 'arrray' or 'slice'? , for example const N : usize = b"my_byte_string"::LEN; or is there any plan to declare std::intrinsics::size_of_val(v) as const-fn(?) enabling somehing like: |
@frehberg When const generics are implemented you will be able to do that yourself, for example with an |
Anyone landing here from googling "associated const", this was released in Rust 1.20, see the announcement: https://blog.rust-lang.org/2017/08/31/Rust-1.20.html |
Also, if anyone is looking for the documentation of this, it's here: https://doc.rust-lang.org/stable/reference/items.html#associated-constants (It was surprisingly hard to find.) |
The last time I looked at this, rust-lang/rust#29646 wasn't finished, but now it is. This avoids the possibility of a pathological implementer of FixedLengthKey returning varying values for levels. (Thanks Raph!)
Shouldn't equality bounds be possible with associated consts? The following does not compile today: trait Foo {
const BAR: usize;
}
// error: expected type, found `10`
fn f<T: Foo<BAR = 10>>() { } The original associated items RFC makes it clear that this should be possible. I'm wondering: was this simply forgotten about? Or perhaps there are/were complications with implementing this? |
Furthermore, using an associated const as an array size does not appear to work: pub trait Can {
const SIZE: usize;
}
fn f<T: Can>(t: T) {
// error[E0277]: the trait bound `T: Can` is not satisfied
let x = [0u8; <T as Can>::SIZE];
} There error message is clearly wrong, so this is either a bug in |
This usage of associated const was not stabilized, as documented in this issue (it doesn't work on nightly either, and probably won't work until |
I don't know if this is the same issue that @eddyb mentioned here: #29646 (comment) trait Foo { const A: usize; }
fn foo<F: Foo>(f: F) {
const B: usize = F::A; // ERROR
let b = F::A; // OK
} |
@gnzlbg Those are two items nested in eachother, we don't allow "inheriting" type parameters in such a situation and it'd be a significant language change to start doing that. const __HIDDEN_B: usize = F::A;
fn foo<F: Foo>(f: F) {
use self::__HIDDEN_B as B;
} (and this is not in any way, shape, or form, specific to |
Having const fn is this feature still required ? Of course it'd require to have const fn in traits as well. ((Or one could be the desugaring'of the other )) |
@zpgaal this feature is implemented and stable, so regardless of it being "required", it has to stay! |
@steveklabnik It seems like rustdoc does not generate documentation for associated constants in structs. Is this a bug? |
@vks I cannot reproduce this. pub struct Foo;
impl Foo {
pub const BAR: u32 = 7;
} |
@SimonSapin I think the problem is: pub struct Foo;
impl Foo {
/// This doc comment won't be shown anywhere.
pub const BAR: u32 = 7;
} |
Oh I see. Please file a bug separately? |
See #52260. |
Move to stable This PR removes unnecessary use of feature flags that bar using Rust on stable([1])([2]) compilers. Reasons being: * associated consts (Tracking issue: rust-lang/rust#29646) are currently buggy and can be replaced by an fn in that case. * associated_type_defaults can be removed without breakage * unboxed_closures can be removed without breakage * StaticMutex has an uncertain future (rust-lang/rust#27717) and can be emulated in that case by using `lazy_static!` (correct me if I'm wrong) Finally, I must admit that I didn't get the test suite running quickly. ([1]) Outstanding: this doesn't _quite_ work on stable yet, as some APIs in use are currently making their way through beta, so they are not feature gated, but also not available in 1.4.0. 1.5.0 beta works and as 1.5.0 is 2 weeks away, this is probably not worth the effort. ([2]) rblas is not on stable yet, see mikkyang/rust-blas#12 for that. You can use that version of rust-blas by checking it out from my https://github.com/skade/rust-blas/ and dropping the following `.cargo/config` in your repository: ``` paths = ["/path/to/rblas/checkout"] ```
The feature is currently quite buggy, but this issue tracks its eventual stabilization.
Blocked onconst_fn
.Type params in const expressions (associated-constants can not be used in constant expressions #34344 /cannot use an outer type parameter in this context
#39211)T: 'static
forT::CONST
trait
andimpl
(rustc_typeck::check::compare_method::compare_const_impl doesn't drain its fullfillment context nor invoke regionck. #41323)Shortcomings of the current implementation
Associated consts in const expressions must be concrete
Associated consts can only be used in const contexts if their input types are fully concrete. That is, this works:
But this does not work:
Associated consts are never object safe
Associated consts make a trait non-object-safe. There is no way to bound the const
where Self: Sized
, nor to specify the const when creating the object.Associated consts referencing other associated consts have spurious destructor warnings
This works:
But this does not, because we (unnecessarily) assume the const could be a variant that runs a destructor:
The text was updated successfully, but these errors were encountered: