-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Can't write generic const fn
s
#116354
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
Comments
Consider the following code: let arr = [UnsafeCell::new(1_u32)];
foo(arr); your code would then have |
If |
And |
We actually intend to make mutability in const fn possible, and that will probably include |
I don't suppose it'd be possible for the compiler to detect the presence of For instance, const fn foo<const N: usize>(values: [String; N]) -> [String; N] {
let slice = values.as_slice();
let len = slice.len();
values
} compiles just fine while use std::cell::UnsafeCell;
struct Foo(UnsafeCell<()>);
const fn foo<const N: usize>(values: [Foo; N]) -> [Foo; N] {
let slice = values.as_slice();
let len = slice.len();
values
} does not -- clearly the compiler knows that |
What you are describing (sometimes called a "post-monomorphization error") is both absurdly controversial (Rust allows them to happen, but it's still controversial in terms of API design), and also will become unnecessary the moment Probably not? |
Not to put too fine a point on it, but "when feature X stabilizes" in Rust can mean "by the next version" or "in over a decade," so that doesn't mean much and could leave a huge class of useful generic const functions unwritable for a very long time for the sake of making something unnecessary later. But if it's basically a policy (or at least the uncontroversial approach) that it's better to error out on the basis of "any given |
Oh, I agree. I'm basically always pushing for more const stuff reaching anywhere near stable precisely because of things like this. There's... currently a lot of "huge class(es) of useful generic const functions (that are) unwritable". I think the hardest problem here is that even if you PRed the changes necessary to the compiler to make this happen, you'd mostly be trying to convince all the people who are trying to fix the exact problems to simply make this work take a look at your code which may make their work take longer as they now have to work around it as well. Also it would prooobably be another feature with gating, and probably work to test and stabilize it even if accepted, not just a "hey let's do this" and then in it goes into 1.75? So! |
I was going to leave this as a remark on #80384, but you're apparently supposed to make separate issues instead of leaving comments on tracking issues, so here goes:
I was just playing around and couldn't even get past this simple code before getting yelled at:
Both
as_slice()
andlen()
are declared asconst fn
, so you'd think they'd be fine to use in anotherconst fn
, but no:I don't even understand what this is trying to tell me. Issue #80384, which is the one linked, does not at all explain the source/cause of this error. I'm not working with
UnsafeCell
or interior mutability here. I'm attempting to make a&[T]
from a[T; N]
.The text was updated successfully, but these errors were encountered: