Skip to content

Subtle change in how ty fragments get stringified #128992

Closed as not planned
Closed as not planned

Description

@workingjubilee

Something in lexing, parsing, or even stringification or pretty-printing is now causing code that previously yielded strings like "Component1" to serde, based on the type name, to now emit strings like " Component1". This has been extracted from #128899 because many of the regressions there proved to be unrelated to each other, and it was hard to triage out what the "real" problem was. We have and most of them depended on unstable library details, but the bbarker/bevy_serde_macros case was distinct.

See @Nemo157's comment:

Equality doesn't care about which order you check the keys in, the debug output is different but that shouldn't affect the comparison result. There seem to be extra spaces in the keys on one side, which I assume means the test itself is broken. EDIT: looking at it a little more, seems to be related to not accounting for whitespace in stringify! of ty fragments, so tests :: Component1 gets split at the :: and given the name Component1 instead of the expected Component1, maybe #125174 related.

Originally posted by @Nemo157 in #128899 (comment)

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Aug 11, 2024
  2. Nemo157 commented on Aug 11, 2024

    @Nemo157
    Contributor

    Minimal example (playground):

    macro_rules! test { ($t:ty) => { dbg!(stringify!($t)); } }
    macro_rules! via_callback { () => { test!(foo::Bar); } }
    fn main() { via_callback!(); }

    on stable:

    [src/main.rs:3:13] stringify!(foo::Bar) = "foo::Bar"
    

    on beta and nightly:

    [src/main.rs:3:13] stringify! (foo :: Bar) = "foo :: Bar"
    
  3. workingjubilee commented on Aug 11, 2024

    @workingjubilee
    MemberAuthor

    Thank you very much for that repro, @Nemo157!

  4. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Aug 11, 2024
  5. added
    S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issue
    on Aug 11, 2024
  6. compiler-errors commented on Aug 12, 2024

    @compiler-errors
    Contributor

    This regressed in #125174

  7. tgross35 commented on Aug 12, 2024

    @tgross35
    Member

    Cc @nnethercote from the above

  8. nnethercote commented on Aug 12, 2024

    @nnethercote
    Contributor

    Minimal example (playground):

    macro_rules! test { ($t:ty) => { dbg!(stringify!($t)); } }
    macro_rules! via_callback { () => { test!(foo::Bar); } }
    fn main() { via_callback!(); }

    on stable:

    [src/main.rs:3:13] stringify!(foo::Bar) = "foo::Bar"
    

    on beta and nightly:

    [src/main.rs:3:13] stringify! (foo :: Bar) = "foo :: Bar"
    

    Things like that aren't surprising. #125174 did change whitespace production in pretty printing output, the diff in the PR identifies quit a few other cases where output changed. These changes are valid.

    • The docs for stringify! say "Note that the expanded results of the input tokens may change in the future. You should be careful if you rely on the output."
    • Relatedly, the docs for impl Display for TokenStream say "Note: the exact form of the output is subject to change, e.g. there might be changes in the whitespace used between tokens. Therefore, you should not do any kind of simple substring matching on the output string (as produced by to_string) to implement a proc macro, because that matching might stop working if such changes happen. Instead, you should work at the TokenTree level, e.g. matching against TokenTree::Ident, TokenTree::Punct, or TokenTree::Literal."
    • Of course people don't always follow that advice, so compilation failure with "cargo check --tests" for espidf #125714 did a crater run and found very small amounts of breakage.

    My initial guess is that somebody has recently written a new proc macro that uses impl Display for TokenStream and doesn't account for possible whitespace changes. And that this was written since #125174, which is why it didn't show up in the crater run for that PR.

  9. nnethercote commented on Aug 12, 2024

    @nnethercote
    Contributor

    We have and most of them depended on unstable library details, but the bbarker/bevy_serde_macros case was distinct.

    I overlooked this sentence before my earlier reply. I agree that, of the failures in #128899, bbarker/bevy_serde is probably caused by the changes in #125174.

  10. workingjubilee commented on Aug 12, 2024

    @workingjubilee
    MemberAuthor

    @nnethercote Those lines date to November 25th, 2023, and your PR landed June 11, 2024. While it is technically possible to falsify git histories, I think that the real reason that your PR didn't reveal this in crater is that the "craterbot check" and "craterbot build-and-test" commands are non-identical, and beta crater runs use the build-and-test mode.

  11. nnethercote commented on Aug 12, 2024

    @nnethercote
    Contributor

    Thanks for the info! I think the proper fix here needs to be on the bevy_serde_macros side, given that it was relying on behaviour that is subject to change.

  12. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    C-discussionCategory: Discussion or questions that doesn't represent real issues.
    on Aug 13, 2024
  13. added
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Aug 21, 2024
  14. apiraino commented on Oct 7, 2024

    @apiraino
    Contributor

    Hope it's fine to this as WONTFIX (as per prev. comment)

  15. removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-discussionCategory: Discussion or questions that doesn't represent real issues.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions