Skip to content

inconsistent stringify! #140060

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

Open
loynoir opened this issue Apr 20, 2025 · 2 comments
Open

inconsistent stringify! #140060

loynoir opened this issue Apr 20, 2025 · 2 comments
Labels
A-pretty Area: Pretty printing (including `-Z unpretty`) C-discussion Category: Discussion or questions that doesn't represent real issues.

Comments

@loynoir
Copy link

loynoir commented Apr 20, 2025

I tried this code:

#[macro_export]
macro_rules! reproduce_macro {
    () => {{
        [stringify!(a::b), stringify!(&a)]
    }};
}

#[test]
pub fn reproduce() {
    let expected = [stringify!(a::b), stringify!(&a)];
    assert_eq!(expected, ["a::b", "&a"]);

    let actual = reproduce_macro!();
    assert_eq!(actual, ["a :: b", "& a"]);
}

I expected to see this happen: explanation

actual equal expected

Instead, this happened: explanation

actual not equal expected

$ cargo test -r
       Fresh reproduce v0.1.0 (/tmp/reproduce)
    Finished `release` profile [optimized] target(s) in 0.00s
     Running `/tmp/reproduce/target/release/deps/reproduce-7e181f6f7847a00f`

running 1 test
test reproduce ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests reproduce
     Running `/home/vscode/.rustup/toolchains/nightly-2025-04-14-x86_64-unknown-linux-gnu/bin/rustdoc --edition=2021 --crate-type lib --color auto --crate-name reproduce --test src/lib.rs --test-run-directory /tmp/reproduce -L dependency=/tmp/reproduce/target/release/deps -L dependency=/tmp/reproduce/target/release/deps --extern reproduce=/tmp/reproduce/target/release/deps/libreproduce-c271e11eea147c9e.rlib -C embed-bitcode=no --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' --error-format human`

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Meta

rustc --version --verbose:

<version>
channel = "nightly-2025-04-14"
Backtrace

<backtrace>

@loynoir loynoir added the C-bug Category: This is a bug. label Apr 20, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 20, 2025
@Kivooeo
Copy link
Contributor

Kivooeo commented Apr 20, 2025

This isn't a bug in stringify!, but rather a reflection of how Rust's macro system and token spacing work. That said, it's definitely confusing, and better documentation or diagnostics around this behavior could help.

Briefly:
the tokens a, ::, and b are re-tokenized when the macro is expanded. During this expansion, Rust may insert Alone spacing between tokens unless they are explicitly joined. As a result, stringify! receives tokens like a, ::, b — but with Alone spacing — and renders them with spaces: "a :: b".

This doesn't happen in regular functions because there's no macro expansion involved — the tokens come from the original source without re-parsing

@fmease fmease added A-pretty Area: Pretty printing (including `-Z unpretty`) C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 20, 2025
@loynoir
Copy link
Author

loynoir commented Apr 20, 2025

This isn't a bug in stringify!, but rather a reflection of how Rust's macro system and token spacing work.

Given

  • X not equals to Y

To me, this sounds like Circular Reasoning

  • X not equals to Y, not bug because X is designed to X

I would expected explanation like

  • X not equals to Y, not bug because X is designed to X, because of problem Z

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-pretty Area: Pretty printing (including `-Z unpretty`) C-discussion Category: Discussion or questions that doesn't represent real issues.
Projects
None yet
Development

No branches or pull requests

4 participants