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

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

Closed
ehuss opened this issue Dec 5, 2023 · 0 comments · Fixed by #118691
Closed

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

ehuss opened this issue Dec 5, 2023 · 0 comments · Fixed by #118691
Labels
A-diagnostics Area: Messages for errors, warnings, and lints F-c_str_literals `#![feature(c_str_literals)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ehuss
Copy link
Contributor

ehuss commented Dec 5, 2023

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
@ehuss ehuss added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. F-c_str_literals `#![feature(c_str_literals)]` labels Dec 5, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 5, 2023
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 6, 2023
compiler-errors added a commit to compiler-errors/rust that referenced this issue Dec 19, 2023
…mease

Add check for possible CStr literals in pre-2021

Fixes [rust-lang#118654](rust-lang#118654)

Adds information to errors caused by possible CStr literals in pre-2021.

The lexer separates `c"str"` into two tokens if the edition is less than 2021, which later causes an error when parsing. This error now has a more helpful message that directs them to information about editions. However, the user might also have written `c "str"` in a later edition, so to not confuse people who _are_ using a recent edition, I also added a note about whitespace.

We could probably figure out exactly which scenario has been encountered by examining spans and editions, but I figured it would be better not to overcomplicate the creation of the error too much.

This is my first code PR and I tried to follow existing conventions as much as possible, but I probably missed something, so let me know!
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 20, 2023
…mease

Add check for possible CStr literals in pre-2021

Fixes [rust-lang#118654](rust-lang#118654)

Adds information to errors caused by possible CStr literals in pre-2021.

The lexer separates `c"str"` into two tokens if the edition is less than 2021, which later causes an error when parsing. This error now has a more helpful message that directs them to information about editions. However, the user might also have written `c "str"` in a later edition, so to not confuse people who _are_ using a recent edition, I also added a note about whitespace.

We could probably figure out exactly which scenario has been encountered by examining spans and editions, but I figured it would be better not to overcomplicate the creation of the error too much.

This is my first code PR and I tried to follow existing conventions as much as possible, but I probably missed something, so let me know!
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 20, 2023
Rollup merge of rust-lang#118691 - chfogelman:improve-cstr-error, r=fmease

Add check for possible CStr literals in pre-2021

Fixes [rust-lang#118654](rust-lang#118654)

Adds information to errors caused by possible CStr literals in pre-2021.

The lexer separates `c"str"` into two tokens if the edition is less than 2021, which later causes an error when parsing. This error now has a more helpful message that directs them to information about editions. However, the user might also have written `c "str"` in a later edition, so to not confuse people who _are_ using a recent edition, I also added a note about whitespace.

We could probably figure out exactly which scenario has been encountered by examining spans and editions, but I figured it would be better not to overcomplicate the creation of the error too much.

This is my first code PR and I tried to follow existing conventions as much as possible, but I probably missed something, so let me know!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints F-c_str_literals `#![feature(c_str_literals)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants