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 upTracking issue for `associated_consts` feature #29646
Comments
aturon
added
T-lang
B-unstable
labels
Nov 5, 2015
homu
added a commit
to autumnai/collenchyma
that referenced
this issue
Dec 2, 2015
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
One notable case is that of the |
This comment has been minimized.
This comment has been minimized.
|
@ketsuban how would it be different than including |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
|
The use of associated consts could make things like |
This comment has been minimized.
This comment has been minimized.
Roxxik
commented
Feb 19, 2016
|
would love to use associated consts for array sizes for example: http://is.gd/VSee6B
but this will leave me at E0250 |
This comment has been minimized.
This comment has been minimized.
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! |
nagisa
added
the
A-associated-items
label
May 16, 2016
This comment has been minimized.
This comment has been minimized.
|
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! |
This was referenced Jul 12, 2016
This comment has been minimized.
This comment has been minimized.
|
Should we block on #34344? Are there other known bugs? Are there design questions to resolve here? |
nrc
added
the
B-RFC-implemented
label
Aug 29, 2016
This comment has been minimized.
This comment has been minimized.
|
There was a question on the forum here. Looks like a bug to me. |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
@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". |
This comment has been minimized.
This comment has been minimized.
|
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. |
brson
added
the
I-nominated
label
Jan 11, 2017
This comment has been minimized.
This comment has been minimized.
|
Just nominating to put it in front of eyeballs again. |
This comment has been minimized.
This comment has been minimized.
|
My understanding is that this is waiting on a re-do of const evaluation in the compiler, since it's so buggy. @eddyb ? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Yes, it will be in 1.20. |
withoutboats
closed this
Aug 28, 2017
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
@nwin It's not "in const expressions" - but rather "in types", i.e. array types. |
nwin
referenced this issue
Aug 29, 2017
Closed
Error message should be improved for associated consts in array lengths #44168
This comment has been minimized.
This comment has been minimized.
|
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: |
This comment has been minimized.
This comment has been minimized.
|
@frehberg When const generics are implemented you will be able to do that yourself, for example with an |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
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.) |
robinst
referenced this issue
Sep 28, 2017
Open
Documentation for new stable features can be hard to find (e.g. for associated const) #44894
tokenrove
added a commit
to tokenrove/fixie-trie
that referenced
this issue
Oct 30, 2017
codethief
referenced this issue
Nov 23, 2017
Closed
Please specify minimum Rust version (and add reference to rustup) #117
Badel2
referenced this issue
Feb 6, 2018
Merged
Document that associated constants prevent a trait from being made into an object #48026
This comment has been minimized.
This comment has been minimized.
|
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? |
This comment has been minimized.
This comment has been minimized.
|
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 comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
|
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
} |
This comment has been minimized.
This comment has been minimized.
|
@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 |
bergus
referenced this issue
Jun 5, 2018
Closed
Misleading error message when using associated const in where clause for fixed size array #44774
This comment has been minimized.
This comment has been minimized.
zpgaal
commented
Jun 26, 2018
•
|
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 )) |
This comment has been minimized.
This comment has been minimized.
|
@zpgaal this feature is implemented and stable, so regardless of it being "required", it has to stay! |
This comment has been minimized.
This comment has been minimized.
|
@steveklabnik It seems like rustdoc does not generate documentation for associated constants in structs. Is this a bug? |
This comment has been minimized.
This comment has been minimized.
|
@vks I cannot reproduce this. pub struct Foo;
impl Foo {
pub const BAR: u32 = 7;
} |
This comment has been minimized.
This comment has been minimized.
|
@SimonSapin I think the problem is: pub struct Foo;
impl Foo {
/// This doc comment won't be shown anywhere.
pub const BAR: u32 = 7;
} |
This comment has been minimized.
This comment has been minimized.
|
Oh I see. Please file a bug separately? |
This comment has been minimized.
This comment has been minimized.
See #52260. |
aturon commentedNov 5, 2015
•
edited by nikomatsakis
The feature is currently quite buggy, but this issue tracks its eventual stabilization.
Blocked onconst_fn.Type params in const expressions (#34344 / #39211)T: 'staticforT::CONSTtraitandimpl(#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: