Skip to content
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

types of b"foo" and bytes!("foo") differ #19749

Closed
sp3d opened this issue Dec 11, 2014 · 3 comments
Closed

types of b"foo" and bytes!("foo") differ #19749

sp3d opened this issue Dec 11, 2014 · 3 comments

Comments

@sp3d
Copy link
Contributor

sp3d commented Dec 11, 2014

Rustc gives a message that bytes!() is deprecated:

b.rs:1:33: 1:47 warning: `bytes!` is deprecated, use `b"foo"` literals instead
b.rs:1 fn main() { let x: [u8, ..3] = *bytes!("foo"); }
                                       ^~~~~~~~~~~~~~
b.rs:1:33: 1:47 help: see http://doc.rust-lang.org/reference.html#byte-and-byte-string-literals for documentation
b.rs:1 fn main() { let x: [u8, ..3] = *bytes!("foo"); }
                                       ^~~~~~~~~~~~~~
b.rs:1:33: 1:47 help: see https://github.com/rust-lang/rust/blob/master/src/etc/2014-06-rewrite-bytes-macros.py for an automated migration
b.rs:1 fn main() { let x: [u8, ..3] = *bytes!("foo"); }
                                       ^~~~~~~~~~~~~~
b.rs:1:17: 1:18 warning: unused variable: `x`, #[warn(unused_variables)] on by default
b.rs:1 fn main() { let x: [u8, ..3] = *bytes!("foo"); }
                       ^

But the suggested replacement, as implemented by the python script, no longer compiles. Compiling the program resulting from applying the script's generated patch gives the error:

b.rs:1:32: 1:39 error: mismatched types: expected `[u8, ..3]`, found `[u8]` (expected array of 3 elements, found slice)
b.rs:1 fn main() { let x: [u8, ..3] = *b"foo"; }
                                      ^~~~~~~

The b"foo" syntax should not be suggested as a replacement for bytes!("foo") if it cannot fulfill the same roles. In particular, it seems odd to me that b"foo" syntax results in a less flexible type than bytes!("foo").

Note that [u8, ..n] can be converted to &[u8], which can be converted to the DST [u8]--but &[u8], which is what b"foo" produces, cannot be statically converted to [u8, ..n], so the change suggested by the deprecation warning results in loss of generality.

@steveklabnik
Copy link
Member

See #18702

@ftxqxd
Copy link
Contributor

ftxqxd commented Dec 11, 2014

Looks like a dupe of #18465.

@thestinger
Copy link
Contributor

Closing as a duplicate of #18465

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants