-
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
Associated constants in traits can not be used in const generics #60551
Comments
Potentially related to #43408. |
The error now is:
|
The error with
which can definitely be improved. |
I think we ought to prioritise good diagnostics for |
…stic, r=lcnr `min_const_generics` diagnostics improvements As disscussed in [zulip/project-const-generics/non-trivial anonymous constant](https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/non-trivial.20anonymous.20constants). This is my first PR on the compiler. @lcnr is mentoring me on this PR. Related to rust-lang#60551.
…stic, r=lcnr `min_const_generics` diagnostics improvements As disscussed in [zulip/project-const-generics/non-trivial anonymous constant](https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/non-trivial.20anonymous.20constants). This is my first PR on the compiler. @lcnr is mentoring me on this PR. Related to rust-lang#60551.
…stic, r=lcnr `min_const_generics` diagnostics improvements As disscussed in [zulip/project-const-generics/non-trivial anonymous constant](https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/non-trivial.20anonymous.20constants). This is my first PR on the compiler. @lcnr is mentoring me on this PR. Related to rust-lang#60551.
#77825 landed, should |
Yes, thanks. |
I think the following problem is related to this issue. If not, I apologize in advance. I don't quite understand why this code works: #![feature(min_const_generics)]
struct WithArray<T, const SIZE: usize> {
data: [T; SIZE]
} while this other one doesn't: trait WithAConstant {
const SIZE: usize;
}
struct WithArray<T, U: WithAConstant> {
data: [T; U::SIZE]
} This last one produces the following error:
Thanks! |
Hi there, Is there a fix to this issue on the stable branch of Rust? This issue happens with Thanks |
@parraman: sorry, I missed your comment. Your example should eventually work; it is a current implementation limitation. If you enable #![feature(const_generics)]
#![feature(const_evaluatable_checked)]
trait WithAConstant {
const SIZE: usize;
}
struct WithArray<T, U: WithAConstant> where [(); U::SIZE]: {
data: [T; U::SIZE]
} @ChristopherRabotin: this is a limitation of the current stable version of const generics that we hope to fix in a future version. |
Okay thanks for the quick answer @varkor . If you were to bet, would you say this will be fix in the the next six months or more? I'm asking because I prefer keeping my crate working with rust stable, but I would be OK switching to nightly for a few months. Thanks |
There are still a number of design and implementation questions that need to be resolved with |
Ideally instead of specifying a `Curve::UInt` associated type, we could instead specify a constant, and then pass that as a const generic parameter to `crypto_bigint::UInt`. Unfortunately that requires a lot more functionality than is implemented in `min_const_generics`: rust-lang/rust#60551 This commit makes a note of as much in the comments, calling it out as future work.
If I understand, rustc cannot correctly identify some bound when using the associated constant internally to a default method? https://play.rust-lang.org/?version=nightly&mode=debug&edition=2015&gist=e1114345b12197fdb922ef1acb2e80ae |
Similarly to burdges' playground, I'd also like to throw in the ugly syntax necessary for using bool associated constants. It requiring that bound, and especially having to cast it to a usize, is not intuitive. I planned to use this feature similarly to my playground, have a trait with a ton of default implementations for a few types, while still being able to use const type parameters as feature/optimization flags for different internal algorithms, operating on those types. #![feature(generic_const_exprs)]
fn _internal_impl<const FLAG: bool>() -> &'static str {
if FLAG {
"run for accuracy"
} else {
"run for speed"
}
}
trait Operation {
const FLAG: bool;
fn run() -> &'static str where [(); Self::FLAG as usize]: {
// default implementation
_internal_impl::<{ Self::FLAG }>()
}
} |
Can someone explain to a mere mortal what is the purpose of where [(); TRAIT::CONST_NAME] : syntax? What does it achieve in particular? I've seen something about variance but it gets muddy from there. |
My very naive understanding is that without the bound either (1) I don't think the syntax is intended to stay - at least I sure hope not, it's pretty unfriendly and doesn't at all hint what it's doing. I think it's just some existing syntax that happens to do the trick. If it needs to be expressed, then some struct Foo<T, U: WithAConstant>
// Only one of these options
where AnyArray<U::Size>: Covariant
where AnyArray<U::Size>: CovariantOn<U::SIZE>
{
data: [T; U::SIZE]
} But I think the compiler should be able to infer the correct usage and just error if something is too complex for a simple representation. At least I hope that's possible, because any sort of bound is unneeded noise for something that "should work" in the default case. If inference isn't possible, I'd be kind of curious to see some examples explaining why. Or at least a better explanation from somebody about what the actual variance issue is |
Well this makes the purpose clear. As you suggested, this will likely go away eventually, since for more "normal" cases the conversion from [T; N] to [T;3] when N=3 happens automatically without messy syntax. The bigger question that is still bugging me is how someone came up with that construct and what does it truly mean... |
It doesn't seem like anybody came up with the syntax specifically for this feature, I just think it's something that exists in the type system already. For example, on stable this compiles (even though it doesn't do anything): struct Foo where [(); 100]: {} And it seems like you can use any type, #![feature(generic_const_exprs)]
trait WithAConstant {
const SIZE: usize;
}
struct WithArray<T, U: WithAConstant>
where [String; U::SIZE]: // using any type here has the same result
{
data: [T; U::SIZE]
} Since the syntax already exists, I don't think anything syntax-related would at all block stabilization of But I don't know if this has been discussed anywhere already, and would be curious to hear from anybody more involved in the RFC / decision making of this feature. As far as status of stabilizing (Not sure if rustbot will let me do this since it's not my issue, but seems like this one belongs in that group too since it's related) @rustbot label +A-generic-exprs (edit: nope 😔) |
The stupid solution that I found trait Trait<const SIZE: usize> {
const SIZE: usize = SIZE;
fn fun() -> [u8; SIZE];
} |
@igorechek06 It's great that you found a workaround that works for you, but I need to point out that your solution doesn't help when you need the trait implementation to specify the value of the constant ( |
as of today all examples raised in this issue can be made compiled with sufficient
So I think the questions remain for this issue are:
|
Thanks to a github comment on an issue I was running into with consts it turns out we can simplify things more which is great: rust-lang/rust#60551 (comment) I will look at simplifying things further now that my testing has also shown that not all the additional const bounds are needed for various functions if the compiler can deduce them.
Initially reported here.
The following code:
Playground
Results in a "no associated item named
N
found for typeSelf
in the current scope" compilation error on current Nightly.Note that associated constants in
impl
blocks work without any issues.cc @varkor @yodaldevoid
The text was updated successfully, but these errors were encountered: