Skip to content

confusing error when using c-string literals on older editions #118654

Closed
@ehuss

Description

@ehuss

Code

fn main() {
    let _ = c"foo";
}

Current output

error: expected one of `!`, `.`, `::`, `;`, `?`, `else`, `{`, or an operator, found `"foo"`
 --> src/main.rs:2:14
  |
2 |     let _ = c"foo";
  |              ^^^^^ expected one of 8 possible tokens

error: could not compile `foo` (bin "foo") due to 1 previous error

Desired output

error: c-string literals are not supported in Rust 2018
 --> src/main.rs:2:14
  |
2 |     let _ = c"foo";
  |             ^^^^^^ to use c-string literals, switch to Rust 2021 or later
  |
  = help: set `edition = "2021"` in `Cargo.toml`
  = note: for more on editions, read https://doc.rust-lang.org/edition-guide

Rationale and extra context

c-string literals are only supported in the 2021 edition or newer. Attempts to use them in older editions may cause confusion.

I realize this may be difficult to implement since it needs to be careful to not assume that c followed by " is invalid due to macros. But perhaps it may not be too difficult.

Other cases

No response

Anything else?

rustc 1.76.0-nightly (0e2dac837 2023-12-04)
binary: rustc
commit-hash: 0e2dac8375950a12812ec65868e42b43ed214ef9
commit-date: 2023-12-04
host: aarch64-apple-darwin
release: 1.76.0-nightly
LLVM version: 17.0.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsF-c_str_literals`#![feature(c_str_literals)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions