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

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

Open
ehuss opened this issue Nov 11, 2024 · 6 comments
Open

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

ehuss opened this issue Nov 11, 2024 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-edition-2024 Area: The 2024 edition A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-edition Diagnostics: An error or lint that should account for edition differences. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. L-unsafe_op_in_unsafe_fn Lint: unsafe_op_in_unsafe_fn T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ehuss
Copy link
Contributor

ehuss commented Nov 11, 2024

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
@ehuss ehuss added A-edition-2024 Area: The 2024 edition C-bug Category: This is a bug. D-edition Diagnostics: An error or lint that should account for edition differences. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. L-unsafe_op_in_unsafe_fn Lint: unsafe_op_in_unsafe_fn labels Nov 11, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 11, 2024
@ehuss ehuss 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 Nov 11, 2024
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 11, 2024
@compiler-errors
Copy link
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
Copy link
Contributor

cc @eholk @vincenzopalazzo

@ehuss
Copy link
Contributor Author

ehuss commented Nov 19, 2024

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.

@traviscross
Copy link
Contributor

traviscross commented Nov 19, 2024

Another test case, which is a bit different in that it's testing in which edition the macro fragment specifiers are evaluated:

// In the proc macro crate:

//@ edition: 2021
extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn pm(_attr: TokenStream, _item: TokenStream) -> TokenStream {
    r#"macro_rules! edition {
    ($_:expr) => {
        println!("Rust 2024")
    };
    (const {}) => {
        println!("Rust 2021")
    };
}
"#
    .parse()
    .unwrap()
}

// In another crate:

//@ edition: 2024
use macros_proc::pm;

#[macros_proc::pm]
struct S;

fn main() {
    edition!(const {});
}

This prints "Rust 2021".

@traviscross
Copy link
Contributor

Here's another test case:

// In the proc macro crate:

//@ edition: 2021
extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn pm(_attr: TokenStream, _item: TokenStream) -> TokenStream {
    r#"macro_rules! edition {
        ($d:tt) => { macro_rules! edition_inner {
            ($d_:expr) => {
                println!("Rust 2024")
            };
            (const {}) => {
                println!("Rust 2021")
            };
        }};
    }
"#
    .parse()
    .unwrap()
}

// In another crate:

//@ edition: 2024
use macros_proc::pm;

#[macros_proc::pm]
struct S;

fn main() {
    edition!($);
    edition_inner!(const {});
}

This prints "Rust 2024".

@ehuss
Copy link
Contributor Author

ehuss commented Nov 21, 2024

I posted a partial fix in #133274. That fixes the problem that a 2024 crate would error when using a proc-macro from an older edition that emits a macro_rules macro.

I still don't know how to fix the lint firing in 2021 in that same situation. Somehow there should be a way for the lint to recognize that the place it is adding a suggestion to is not correct, and that it doesn't need to fire at all...

bors added a commit to rust-lang-ci/rust that referenced this issue Nov 21, 2024
…=<try>

Use edition of `macro_rules` when compiling the macro

This changes the edition assigned to a macro_rules macro when it is compiled to use the edition of where the macro came from instead of the local crate's edition.

This fixes a problem when a macro_rules macro is created by a proc-macro. Previously that macro would be tagged with the local edition, which would cause problems with using the correct edition behavior inside the macro. For example, the check for unsafe attributes would cause errors in 2024 when using proc-macros from older editions.

This is partially related to rust-lang#132906. Unfortunately this is only a half fix for that issue. It fixes the error that happens in 2024, but does not fix the lint firing in 2021. I'm still trying to think of some way to fix that, but I'm running low on ideas.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-edition-2024 Area: The 2024 edition A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-edition Diagnostics: An error or lint that should account for edition differences. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. L-unsafe_op_in_unsafe_fn Lint: unsafe_op_in_unsafe_fn T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants