rust-lang / rust-lang/rust

Attributes within patterns do not work if there is another attribute in the same pattern or above

Open
#118,682 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-attributes A-patterns C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

This issue was originally discussed on https://users.rust-lang.org/t/cfg-attr-to-allow-unused-variables-doesnt-work-in-patterns/103511.

I noticed some weird behavior when it comes to attributes within destructuring patterns. For example, consider the following:

#![deny(unused_variables)]

// All `macro_use` attributes can be replaced with something else too,
// e.g., `#[allow()]`

// #[macro_use]
fn main() {
    #[derive(Default)]
    struct A {
        a: u8,
        b: u8
    }

    // #[macro_use]
    let A {
        // #[macro_use]
        a,
        #[allow(unused_variables)]
        b,
    } = Default::default();
    dbg!(a);
}

This code currently compiles fine. However, if I uncomment any of the commented-out #[macro_use] lines, I get an error from the compiler complaing about b being unused.

For example, if I uncomment the #[macro_use] line on top of the main function, I receive the following error when running it in Rust playground:

   Compiling playground v0.0.1 (/playground)
warning: `#[macro_use]` only has an effect on `extern crate` and modules
 --> src/main.rs:3:2
  |
3 |  #[macro_use]
  |  ^^^^^^^^^^^^
  |
  = note: `#[warn(unused_attributes)]` on by default

error: unused variable: `b`
  --> src/main.rs:16:9
   |
16 |         b,
   |         ^ help: try ignoring the field: `b: _`
   |
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![deny(unused_variables)]
   |         ^^^^^^^^^^^^^^^^

warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` (bin "playground") due to previous error; 1 warning emitted

I would have expected that the #[allow(unused_variables)] attribute on b would be honored by the compiler, and hence not using b anywhere in the code would not trigger a compiler error. However, that is not what seems to be happening.

I have been able to reproduce this in Rust Playground, using the Rust 2021 edition and the stable channel (1.74.0), and under both debug and release mode. I have attempted all seven possible combinations of uncommented #[macro_use] attributes; each one complains about the unused variable b.

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 with the Rust Playground reproducer under Rust 2021 and verify the seven attribute-placement combinations described in the issue. Investigate how lint attributes on destructuring-pattern fields interact with surrounding attributes; done means #[allow(unused_variables)] on b is honored consistently without triggering the reported error.

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
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.