Skip to content

Bad suggestion for macro_rules macros generated from a proc-macro #132906

Open
@ehuss

Description

@ehuss
Contributor

In a proc-macro that generates a macro_rules macro that triggers lints like unsafe_op_in_unsafe_fn, or unsafe_attr_outside_unsafe, the suggestion ends up suggesting that the unsafe should go around the attribute, which is invalid syntax.

// proc-macro `pm`
use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn pm(_attr: TokenStream, _item: TokenStream) -> TokenStream {
    "macro_rules! foo {
        () => {pub unsafe fn foo() { let _ = std::mem::zeroed::<i32>(); } };
    }"
    .parse()
    .unwrap()
}
#![warn(unsafe_op_in_unsafe_fn)]

#[pm::pm]
struct S;

foo! {}

Gives a suggestion that ends up being:

{ unsafe #[pm::pm]}

which is invalid syntax.

Seen with the vtable crate.

This is a bit of curious case, as I would have expected the tokens of the macro_rules definition to be 2021 edition, and that the tokens it in turns generates also be 2021. Offhand I don't know how macro_rules determine which edition the tokens it emits should be (I'm guessing it is using the edition of the local crate, instead of the edition of the generated macro_rules definition).

Meta

rustc --version --verbose:

rustc 1.84.0-nightly (143ce0920 2024-11-10)
binary: rustc
commit-hash: 143ce0920a2307b19831160a01f06f107610f1b2
commit-date: 2024-11-10
host: aarch64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.3

Activity

added
C-bugCategory: This is a bug.
D-editionDiagnostics: An error or lint that should account for edition differences.
D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
on Nov 11, 2024
added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Nov 11, 2024
changed the title [-]Bad suggestion for unsafe_op_in_unsafe_fn in macro_rules generated macro[/-] [+]Bad suggestion for macro_rules macros generated from a proc-macro[/+] on Nov 11, 2024
added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
A-diagnosticsArea: Messages for errors, warnings, and lints
A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Nov 11, 2024
compiler-errors

compiler-errors commented on Nov 11, 2024

@compiler-errors
Member

For the record, the macro_rules that is generated will be spanned using the edition of the callsite, so it'll be edition 2024 even if the proc macro is edition 2021.

https://github.com/rust-lang/rust/blob/master/compiler/rustc_expand/src/proc_macro_server.rs#L550

traviscross

traviscross commented on Nov 12, 2024

@traviscross
Contributor
ehuss

ehuss commented on Nov 19, 2024

@ehuss
ContributorAuthor

I'm wondering if the switch of edition is happening here, where it uses the session edition to compile the macro, instead of the edition where the macro was defined.

12 remaining items

Loading
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-diagnosticsArea: Messages for errors, warnings, and lintsA-edition-2024Area: The 2024 editionA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-editionDiagnostics: An error or lint that should account for edition differences.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.I-edition-triagedIssue: This issue has been reviewed and triaged by the Edition team.L-unsafe_op_in_unsafe_fnLint: unsafe_op_in_unsafe_fnT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ehuss@traviscross@compiler-errors@jieyouxu@rustbot

        Issue actions

          Bad suggestion for macro_rules macros generated from a proc-macro · Issue #132906 · rust-lang/rust