rust-lang / rust-lang/rust

rust_2024_incompatible_pat bad suggestion with proc-macro when brace comes from input

Open
#132,963 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-edition-2024 A-proc-macros C-bug D-edition D-invalid-suggestion I-edition-triaged L-rust_2024_incompatible_pat T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

When a proc-macro generates code, and the opening brace of a pattern from that code comes from the input, then the compiler thinks that the pattern is a 2024 pattern, and thus enforces the new pattern rules using the local edition, not the edition from the proc-macro.

There are two consequences:

  1. This is incompatible with supporting macros that are on different editions.
  2. It generates an invalid suggestion.

Example of the bad suggestion might be:

#[derive(my_macro)]
struct S {
    f1: i32
}

will give a suggestion to modify the struct to add an & which is invalid syntax like this:

struct &S {
    f1: i32
}

This was seen with swc_macros_common here where it uses the brace token from the input.

One option is to change this in the macro to generate a new token with the correct span information.

I'm also wondering if there are options for changing the ways tokens are processed from proc-macros to avoid this altogether.

Meta

rustc --version --verbose:

rustc 1.84.0-nightly (81eef2d36 2024-11-11)
binary: rustc
commit-hash: 81eef2d362a6f03db6f8928f82d94298d31eb81b
commit-date: 2024-11-11
host: aarch64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the proc-macro example described in the issue, then inspect swc_macros_common/src/binder.rs around the referenced line and the rustc proc-macro token handling involved. Done means the compiler respects the proc-macro and local editions and no longer emits the invalid suggestion containing &S.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.