Skip to content

proc-macro param attrs dropping first attrs in impl fns #64682

Closed
@bbqsrc

Description

@bbqsrc

I have been able to reproduce #64282.

If an fn inside an impl has a param attr on the first parameter, no matter how many, they are not visible in the TokenStream.

Repro repo: https://github.com/bbqsrc/params-attribute-example

Source:

#[rename_params(send_help)]
fn hello(#[angery(true)] a: i32, #[a2] b: i32, #[what = "how"] c: u32) {}

#[rename_params(send_help)]
impl Foo {
  fn hello(#[angery(true)] a: i32, #[a2] b: i32, #[what = "how"] c: u32) {}
  fn hello2(#[a1] #[a2] a: i32, #[what = "how"] b: i32, #[angery(true)] c: u32) {}
  fn hello_self(#[a1] #[a2] &self, #[a1] #[a2] a: i32, #[what = "how"] b: i32, #[angery(true)] c: u32) {}
}

Printed output:

fn hello(#[angery(true)] a: i32, #[a2] b: i32, #[what = "how"] c: u32) { }
impl Foo {
    fn hello(a: i32, #[a2] b: i32, #[what = "how"] c: u32) { }
    fn hello2(a: i32, #[what = "how"] b: i32, #[angery(true)] c: u32) { }
    fn hello_self(#[a1] #[a2] &self, #[a1] #[a2] a: i32,
                  #[what = "how"] b: i32, #[angery(true)] c: u32) {
    }
}

As you can see, hello and hello2 are missing their attrs on the a param. The addition of self however gives both self and the a param their attrs. Weird.

$ rustc --version
rustc 1.39.0-nightly (ed8b708c1 2019-09-21)

Activity

bbqsrc

bbqsrc commented on Sep 22, 2019

@bbqsrc
Author
bbqsrc

bbqsrc commented on Sep 22, 2019

@bbqsrc
Author

Also pinging @c410-f3r due to impact on #64010.

added
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
C-bugCategory: This is a bug.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
A-attributesArea: Attributes (`#[…]`, `#![…]`)
on Sep 22, 2019
c410-f3r

c410-f3r commented on Sep 25, 2019

@c410-f3r
Contributor

I'm rather busy this week and I can only take a look into it in this weekend

pnkfelix

pnkfelix commented on Sep 26, 2019

@pnkfelix
Member

triage: P-high. Leaving nominated in hopes to find someone to look into it, since @c410-f3r won't have time for next few days.

pnkfelix

pnkfelix commented on Sep 26, 2019

@pnkfelix
Member

assigning to @Centril

Centril

Centril commented on Sep 29, 2019

@Centril
Contributor

Investigation thus far: I don't think the problem is in pretty printing but somewhere else.

#![deny(unused_variables)]

struct Foo;

fn _f0(#[allow(unused_variables)] p0: u8) {}

impl Foo {
    fn _f1(#[allow(unused_variables)] p1: u8) {}
    
    fn _f2(&self, #[allow(unused_variables)] p2: u8) {}
}

results in:

error: unused variable: `p1`
 --> src/lib.rs:8:39
  |
8 |     fn _f1(#[allow(unused_variables)] p1: u8) {}
  |                                       ^^ help: consider prefixing with an underscore: `_p1`
  |
note: lint level defined here
 --> src/lib.rs:1:9
  |
1 | #![deny(unused_variables)]
  |         ^^^^^^^^^^^^^^^^

However: the problem is not in the parser:

pub struct S;

impl S {
    fn _f1(#[allow(lint)] p1: u8) {}
}

results in:

error[E0658]: attributes on function parameters are unstable
 --> src/lib.rs:4:12
  |
4 |     fn _f1(#[allow(lint)] p1: u8) {}
  |            ^^^^^^^^^^^^^^
  |
  = note: for more information, see https://github.com/rust-lang/rust/issues/60406

on stable.

But the problem does appear at least in AST validation:

pub struct S;

impl S {
    fn _f2(
        /// abc  <-- compiles but it should be rejected!
        _p1: u8
    ) {}
}

Actually, it turns out the problem is in the parser, and we have a bug that allows:

trait T {
    fn _f2(#[foo]) {}
}

10 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-attributesArea: Attributes (`#[…]`, `#![…]`)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.P-highHigh priorityT-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

    Participants

    @pnkfelix@bbqsrc@Centril@jonas-schievink@c410-f3r

    Issue actions

      proc-macro param attrs dropping first attrs in impl fns · Issue #64682 · rust-lang/rust