-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
bytes!() is now hard to use in a lot of cases due to lifetime issues #11641
Comments
/cc @nikomatsakis |
Yeah, this is kind of annoying. I worked around it in one specific case by having bytes! expand as follows:
I certainly think one should be allowed to write a concrete lifetime as part of a slice or borrow expression, and Interestingly, even if we used full inference, the expression you gave above would be illegal unless we introduce a special case for static byte strings, because it generates a conditional temporary (only on one arm of the match) and we can't handle the cleanup for such a case without zeroing, which we plan to remove (well, I guess we could also have a special case for temporary types [like |
Perhaps the simplest fix for now is to adjust the |
@nikomatsakis Won't that prevent static FOO: &'static [u8] = {
static F: &'static [u8] = &'static [10u8];
F
};
|
@nikomatsakis I already adjusted |
Ah, unfortunate. (I actually think we should allow statics to include nested blocks with other statics, but I guess that's a separate bug) Well, I'm not sure what's the easiest fix in that case, I guess just to fix the semantics of |
cc #11640 |
Change `bytes!()` to return { static BYTES: &'static [u8] = &[...]; BYTES } This gives it the `'static` lifetime, whereas before it had an rvalue lifetime. Until recently this would have prevented assigning `bytes!()` to a static, as in static FOO: &'static [u8] = bytes!(1,2,3); but rust-lang#14183 fixed it so blocks are now allowed in constant expressions (with restrictions). Fixes rust-lang#11641.
Change `bytes!()` to return { static BYTES: &'static [u8] = &[...]; BYTES } This gives it the `'static` lifetime, whereas before it had an rvalue lifetime. Until recently this would have prevented assigning `bytes!()` to a static, as in static FOO: &'static [u8] = bytes!(1,2,3); but #14183 fixed it so blocks are now allowed in constant expressions (with restrictions). Fixes #11641.
…r=Jarcho Allow negative literals in `redundant_guards` changelog: none
With #3511 landed, the lifetime of
bytes!()
is now problematic. Specifically, the following used to work and now fails:The lifetime of
bytes!("test")
is now limited to that branch of the match, so it doesn't live long enough to be assigned tob
.I'm not sure if this means lifetimes need to change. The other option is that
bytes!()
needs to return a&'static [u8]
. Unfortunately, the only way to do that right now is to make it equivalent to{static b: &'static [u8] = &[...]; b }
, which makes it no longer usable as the value of a constant (e.g.static foo: &'static [u8] = bytes!("foo")
).I'm thinking the right solution is to make it legal for me to say
&'static [1u8]
. I can type that right now, but the'static
lifetime seems to be wholly ignored, as I get the exact same error with the following as I do with thebytes!()
example above:If
&'static [1u8]
becomes a valid expression with the'static
lifetime, thenbytes!()
could start evaluating to that and everything should work as expected. Furthermore, it would be nice (but perhaps not required) if&[1u8]
, where all elements are compile-time constants, would implicitly be given the'static
lifetime.The text was updated successfully, but these errors were encountered: