Skip to content

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

Closed
@sp3d

Description

@sp3d

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions