You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
associated-constants can not be used in constant expressions #34344
It's actually very irritating, and make associated consts in traits of limited value. For example, I need to model 4 different fixed large arrays of data that represent hash distributions. These are modelled as a trait. A consumer function trying to then use the size in a derived constant can't. (in this case, to compute a bit pattern from a size). It also means that one can't scope constants correctly (eg @nwin) and so rely on fallbacks to global constants. Yuck.
Another problem currently is that if e.g. an associated constant is implemented on an enum and the enum imported in another module, the constant is invisible (today's nightly)
Well I've seen this issue linked in various places when pointing people at the problem. But if there's a more appropriate "master" issue then I don't see the point in keeping extra appendage ones open (as long as they link to the right one). Your call.
I think we should fill a different issue to improve the error message that @RalfJung mentions.
Associated consts work in constant expressions today, what doesn't work is using them from a type parameter in type position IIRC. Fixing this is not part of the original example, and we should track that in a separate issue as well.
It looks like the problem may be because the trait does not define a value for the associated constant (this is by design), so I'm not sure if this falls under "associated constants cannot be used in constant expressions" or something else.
Note this issue is distinct from the "incorrect error message" issue--I would like to see this compile as valid Rust. Does anyone know if this is already being tracked somewhere (I've not been able to find it), or should I open a new issue?
@gnzlbg ah, I had not heard of const generics before. In reading through the RFC's solution I am reminded that my ideal solution is an associated solution rather than a generic solution or as defaulted const generics (which is not in the RFC).
For others, who may come across this thread, it does look like this issue is being addressed here using generics.
Activity
Aatch commentedon Jun 21, 2016
It should be allowed in the long run. It just requires some significant changes to how we handle both type-checking and constant evaluation.
crumblingstatue commentedon Jul 12, 2016
I think this should be resolved before associated consts get stabilized.
Ref #29646
burdges commentedon Oct 18, 2016
I've run into this problem as well. It creates minor issues with working around the larger limits discussed in rust-lang/rfcs#1772
nwin commentedon Oct 21, 2016
Maybe related:
https://is.gd/a1qsuL
raphaelcohn commentedon Nov 4, 2016
It's actually very irritating, and make associated consts in traits of limited value. For example, I need to model 4 different fixed large arrays of data that represent hash distributions. These are modelled as a trait. A consumer function trying to then use the size in a derived constant can't. (in this case, to compute a bit pattern from a size). It also means that one can't scope constants correctly (eg @nwin) and so rely on fallbacks to global constants. Yuck.
gnzlbg commentedon Nov 10, 2016
I would like to fix this, would anybody be able to mentor me on it?
przygienda commentedon Jan 5, 2017
Another problem currently is that if e.g. an associated constant is implemented on an enum and the enum imported in another module, the constant is invisible (today's nightly)
prz-mbp:mini_igp prz$ rustc --version
rustc 1.16.0-nightly (4ecc85b 2016-12-28)
it works if the mod is in same file?
https://is.gd/eKVLJR
tforgione commentedon Feb 23, 2017
Furthermore, if you try to make generics with associated constants, it'll crash the compiler :
crashes with this error.
Hope this can help.
withoutboats commentedon Mar 27, 2017
Status update:
The example in the original post compiles today; as long as the type the associated const is associated with is a concrete type, this works.
The example that @tforgione posted, in which the const is associated with a type parameter returns an error today.
nwin commentedon Mar 31, 2017
Which is essential identical with my example which doesn’t compile as well, at least the error message changed. to
Rollup merge of rust-lang#41939 - eddyb:trait-assoc-const-default, r=…
Rollup merge of rust-lang#41939 - eddyb:trait-assoc-const-default, r=…
RalfJung commentedon Jul 25, 2017
In some cases the error message shown is pretty confusing and leaves the user in the air about what's going on:
The associated type actually exists. So I started at this a while, wondering whether I just stopped knowing how to read...
steveklabnik commentedon Aug 29, 2017
This now compiles, closing!
vitalyd commentedon Aug 29, 2017
@steveklabnik, what compiles? Associated consts in arrays still don't work.
steveklabnik commentedon Aug 29, 2017
The example in the OP.
vitalyd commentedon Aug 29, 2017
Ah I thought you were referring to @RalfJung example right above, along with similar ones in this issue, which doesn't compile.
steveklabnik commentedon Aug 29, 2017
If this should be re-opened thanks to those ones, then yeah, let's re-open it. I just knew the OP's code compiled
vitalyd commentedon Aug 29, 2017
Well I've seen this issue linked in various places when pointing people at the problem. But if there's a more appropriate "master" issue then I don't see the point in keeping extra appendage ones open (as long as they link to the right one). Your call.
gnzlbg commentedon Aug 30, 2017
I think we should fill a different issue to improve the error message that @RalfJung mentions.
Associated consts work in constant expressions today, what doesn't work is using them from a type parameter in type position IIRC. Fixing this is not part of the original example, and we should track that in a separate issue as well.
RalfJung commentedon Sep 1, 2017
I opened a new issue for the incorrect error at #44243.
U007D commentedon Mar 8, 2018
In doing some embedded work using traits, I ran into an issue with associated constants which do not seem to work as the
Nin[T; N]: https://play.rust-lang.org/?gist=c7f999d864cc995d36584aa595641dce&version=stableIt looks like the problem may be because the trait does not define a value for the associated constant (this is by design), so I'm not sure if this falls under "associated constants cannot be used in constant expressions" or something else.
Note this issue is distinct from the "incorrect error message" issue--I would like to see this compile as valid Rust. Does anyone know if this is already being tracked somewhere (I've not been able to find it), or should I open a new issue?
gnzlbg commentedon Mar 8, 2018
This is by design. Associated constants are not const generics.
U007D commentedon Mar 8, 2018
@gnzlbg ah, I had not heard of const generics before. In reading through the RFC's solution I am reminded that my ideal solution is an associated solution rather than a generic solution or as defaulted const generics (which is not in the RFC).
For others, who may come across this thread, it does look like this issue is being addressed here using generics.