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

Don't use parse_group for a visibility. #1598

Closed

Conversation

nnethercote
Copy link

There is no need to explicitly handle an empty group, because the standard group ignoring code has the same effect.

There is no need to explicitly handle an empty group, because the
standard group ignoring code has the same effect.
@nnethercote
Copy link
Author

I'm not 100% certain this PR is valid, but it passes tests, as run with cargo test --release --all-features. Having said that, I do get the "This is not a nightly compiler so not all tests were able to run" warning because I couldn't work out how to get things working with a nightly compiler. (cargo +nightly test --release --all-features gave the same warning.)

If the PR isn't valid, it would be good to have a test that fails when this change is applied.

Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is an example of the kind of thing that this breaks.

# Cargo.toml

[package]
name = "repro"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
syn = { version = "2", features = ["extra-traits"] }
// src/main.rs

macro_rules! m {
    ($vis:vis $ty:ty) => {
        #[repro::repro]
        struct Wrapper($vis $ty);
    };
}

m!(str);

fn main() {}
// src/lib.rs

use proc_macro::TokenStream;
use syn::parse::{ParseStream, Result};
use syn::{parenthesized, parse_macro_input, Ident, Token, Type, Visibility};

#[proc_macro_attribute]
pub fn repro(_args: TokenStream, input: TokenStream) -> TokenStream {
    parse_macro_input!(input with parse);
    TokenStream::new()
}

fn parse(input: ParseStream) -> Result<()> {
    let _: Token![struct] = input.parse()?;
    let _: Ident = input.parse()?;

    let paren;
    parenthesized!(paren in input);
    let vis: Visibility = paren.parse()?;
    let ty: Type = paren.parse()?;

    let _: Token![;] = input.parse()?;

    println!("{:#?}", vis);
    println!("{:#?}", ty);
    Ok(())
}

@dtolnay
Copy link
Owner

dtolnay commented Mar 22, 2024

I couldn't work out how to get things working with a nightly compiler

Nightly was broken by this LLVM linkage issue: rust-lang/rust#121889, 7aef1ed

@dtolnay
Copy link
Owner

dtolnay commented Mar 22, 2024

I have added a test in #1601.

Thank you!

@nnethercote nnethercote deleted the no-parse_group-for-Visibility branch March 24, 2024 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants