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

c"…" literals are handled incorrectly by proc_macro::Literal #112820

Closed
dtolnay opened this issue Jun 20, 2023 · 3 comments · Fixed by #116124
Closed

c"…" literals are handled incorrectly by proc_macro::Literal #112820

dtolnay opened this issue Jun 20, 2023 · 3 comments · Fixed by #116124
Labels
A-proc-macros Area: Procedural macros C-bug Category: This is a bug. 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

@dtolnay
Copy link
Member

dtolnay commented Jun 20, 2023

# Cargo.toml

[package]
name = "repro"
version = "0.0.0"
edition = "2021"

[lib]
proc-macro = true
// src/main.rs

#![feature(c_str_literals)]

repro::repro!(b"bytestr" c"cstr");

fn main() {}
// src/lib.rs

use proc_macro::{TokenStream, TokenTree};

#[proc_macro]
pub fn repro(input: TokenStream) -> TokenStream {
    println!("{:#?}", input);
    for token in input {
        if let TokenTree::Literal(literal) = token {
            println!("{}", literal);
        }
    }
    TokenStream::new()
}

Current output as of nightly-2023-06-20:

$ cargo check
TokenStream [
    Literal {
        kind: ByteStr,
        symbol: "bytestr",
        suffix: None,
        span: #0 bytes(43..53),
    },
    Literal {
        kind: CStr,
        symbol: "cstr",
        suffix: None,
        span: #0 bytes(54..61),
    },
]
b"bytestr"
cstr

The correct output would have c"cstr".

The ToString of proc_macro::Literal is the only interface by which proc macros are able to inspect literals. It is supposed to include the entire literal, with quotes and escape sequences and everything as originally appear in the source code. Without this, it is impossible for proc macros to operate correctly on c"…" literals. For example, it would currently be impossible for a proc macro to tell the difference between b'B' and c"b'B'", but those are not the same token.

Mentioning tracking issue: #105723.
Mentioning @fee1-dead since you have worked on this feature in the past.

@dtolnay dtolnay added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. A-proc-macros Area: Procedural macros F-c_str_literals `#![feature(c_str_literals)]` labels Jun 20, 2023
@vincenzopalazzo
Copy link
Member

@rustbot claim

@tgross35
Copy link
Contributor

tgross35 commented Aug 3, 2023

Does #113476 resolve this or is there more?

@dtolnay
Copy link
Member Author

dtolnay commented Aug 3, 2023

Not fixed as of rustc 1.73.0-nightly (8131b97 2023-08-02).

vincenzopalazzo added a commit to vincenzopalazzo/rust that referenced this issue Sep 23, 2023
While I am working on trying to patch rust-lang#112820

I think it is useful to have a test for this case as known-bug

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
vincenzopalazzo added a commit to vincenzopalazzo/rust that referenced this issue Sep 24, 2023
While I am working on trying to patch rust-lang#112820

I think it is useful to have a test for this case as known-bug

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
vincenzopalazzo added a commit to vincenzopalazzo/rust that referenced this issue Sep 24, 2023
While I am working on trying to patch rust-lang#112820

I think it is useful to have a test for this case as known-bug

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 25, 2023
…to-string, r=compiler-errors

Properly print cstr literals in `proc_macro::Literal::to_string`

Previously we printed the contents of the string, rather than the actual string literal (e.g. `the c string` instead of `c"the c string"`).

Fixes rust-lang#112820
cc rust-lang#105723
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 25, 2023
…to-string, r=compiler-errors

Properly print cstr literals in `proc_macro::Literal::to_string`

Previously we printed the contents of the string, rather than the actual string literal (e.g. `the c string` instead of `c"the c string"`).

Fixes rust-lang#112820
cc rust-lang#105723
@vincenzopalazzo vincenzopalazzo removed their assignment Sep 25, 2023
@bors bors closed this as completed in a6dce3b Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-proc-macros Area: Procedural macros C-bug Category: This is a bug. 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