-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
rustc: Change byte literals to fixed-size arrays #18480
Conversation
This commit alters the type of `b"foo"` from `&'static [u8]` to `&'static [u8, ..3]` and is an implementation of RFC 339. This is a breaking change because not all operations are always compatible with fixed-size arrays currently when compared with slices. As seen in the diff, if a fixed-size array is the left hand size of an equality then the operator may not resolve. Breakage may require some shuffling or explicitly converting to a slice via `.as_slice()` or `[]`. [breaking-change] Closes rust-lang#18465
Hm, this is bouncing primarily because this doesn't compile: const FOO: &'static [u8, ..3] = [1, 2, 3];
fn main() {
let a = [1u8, 2, 3].as_slice();
match a {
FOO => {}
_ => {}
}
} @jakub-, do you know if this would be an easy change to make to checking matches? |
@alexcrichton Just to say, I didn't miss your comment, will investigate soon! At the first glance, for the above to work it would require an implicit coercion which we try to stay away from in patterns, I think. We definitely need more principled thinking here before 1.0, though! |
I'll reopen this when I have a chance to fix this. |
@alexcrichton Can you help me understand what is the conceptual issue with that code snippet not compiling? Is it that there would be no way to match a What are the implications of allowing this specific coercion in match patterns? Given that they have to be constants, the specific case of allowing either sized or unsized arrays to match an unsized array seems like it ought to be fine (for any array type), but I'm sure there's complexity here that I'm missing. I'm interested in this feature (as I understand it, it's required for C-ABI dynamic libraries to export string constants / static pointers to strings without trickery) so I'd like to know what needs to be done to get this change into 1.0. It would also be fine for my use case if there were special syntax to make a bytestring (or string!) literal sized, but they remained unsized by default; it would certainly be a readability improvement over using an external macro. |
@geofft the problem was getting this code to compile: const FOO: &'static [u8; 3] = [1, 2, 3];
fn main() {
let a = [1u8, 2, 3].as_slice();
match a {
FOO => {}
_ => {}
}
} This would prevent match arms using |
@alexcrichton
The true problem is in the LLVM error #17233 , which apparently is't related to b"..." being a DST, as @huonw assumed, because it appears exactly when I change b"..." to be a non-DST and try to compile libstd. I'd really like to have this LLVM issue fixed, but have no idea how to do it myself. (Any chances that #21744 will resolve it?) |
I suspect this is likely a problem in our own compiler vs LLVM, and I wouldn't personally take this patch with the require workaround you listed unfortunately (although it should in theory be easy to fix, I just don't know where to do so) |
@geofft |
oh awesome! I'll play around with it when the next nightly hits. From the discussion on #22838, sounds like you went with the approach of having bytestring literals in patterns work as either |
@petrochenkov Thanks a bunch, I can now use bytestring literals in static variables. Double-casting |
This commit alters the type of
b"foo"
from&'static [u8]
to&'static [u8, ..3]
and is an implementation of RFC 339.This is a breaking change because not all operations are always compatible with
fixed-size arrays currently when compared with slices. As seen in the diff, if a
fixed-size array is the left hand size of an equality then the operator may not
resolve. Breakage may require some shuffling or explicitly converting to a slice
via
.as_slice()
or[]
.[breaking-change]
Closes #18465