-
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
Constructing Option<&'static dyn T>::None
in a const fn
requires const_fn_trait_bounds
#87240
Comments
It looks like #64992 has more context:
matches my experimentation above. I see #85078 is marked as 1.54.0; however, this example still fails to compile on beta. I see const_eval says:
Perhaps that is a better warning / error to provide? "Cannot construct |
Indeed the current check basically ensures that So the error is expected, and we can't allow such code until we commit to what Cc @rust-lang/wg-const-eval |
I have come around to just allowing everything related to function pointers, trait bounds and dyn trait that works in const initializers in const fn. The current design will require extra annotations when you want to actually use them beyond copying their value around or accessing associated types or constants. Maybe we could write up sth along those lines and ask T-lang to consider it? |
So basically, you have given up on trying to do anything with the existing syntax, and think we need new syntax for everything anyway? That is my preferred solution I think, but I am nevertheless a bit hesitant to close down the design space. But maybe we have kept it open long enough. |
Well, we can let the experimental new syntax bake a little on master, and if it pans out, we can stabilize non-const behaviour of the regular syntax that already works in const items |
Thank you for continuing to improve Rust!
Given the following code: playground
The current output is:
After reading this message and reading #57563, I'm not sure what trait bound is a problem. The code that is literally written here does not have any trait bounds - no syntax
R: T
; nor does it have any (explicit) genericconst fn
.I assume there is a good reason why this doesn't immediately work - I know the Rust team has put tons of work into making all this analysis correct, so this is not a complaint about the analysis. :-) I'm just not sure how to go from the compiler's message to an action other than "switch to nightly".
(I can confirm that switching to nightly and enabling
#![feature(const_fn_trait_bounds)]
does quiet the warning. It's not clear to me what more-specific issue than #57563 is tracking it?)Experiments / speculation:
I'm assuming there's some sort of desugaring here that's causing a trait bound to appear - e.g. the
None
constructor becoming aconst fn
(per #61456) , and something in theOption
's generic argument turning into a trait bound. However,Option<T>
itself doesn't have any trait bounds onT
(...other than the defaultSized
, which the error message says is OK), so I'm not sure what to change.Some experimenting shows that
Option<&static T>
its OK for a built-inT
:compiles on stable (1.53.0).
And even using a different
?Sized
type -[u8]
- works out alright:compiles as well.
That's based on a not-great understanding of
Sized
- if I understand correctly bothdyn T
and[u8]
are?Sized
.The text was updated successfully, but these errors were encountered: