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

#[warn(non_shorthand_field_patterns)] is a menace to pattern macros #49588

Closed
ExpHP opened this issue Apr 2, 2018 · 1 comment
Closed

#[warn(non_shorthand_field_patterns)] is a menace to pattern macros #49588

ExpHP opened this issue Apr 2, 2018 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ExpHP
Copy link
Contributor

ExpHP commented Apr 2, 2018

Nobody responded to my URLO thread, and the rabbit hole has only gotten deeper and deeper since I've started trying to work around this in frunk's hlist_pat. It's time for an issue.

Here's a simple pattern macro:

pub struct Value<A> { pub value: A }

#[macro_export]
macro_rules! pat {
    ($a:pat) => { Value { value: $a }};
}

Trouble is, suppose somebody writes pat!(value). Then they'll get this:

warning: the `value:` in this pattern is redundant
 --> src/main.rs:5:27
  |
5 |     ($a:pat) => { Value { value: $a }};
  |                           ^^^^^ help: remove this
...
9 |     let pat!(value) = Value { value: () };
  |         ----------- in this macro invocation
  |
  = note: #[warn(non_shorthand_field_patterns)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 0.45 secs
     Running `target/debug/playground`

Here is the simplest alternative I can come up with for working around this warning in this simple macro.

macro_rules! pat {
    // Possibly rewrite ident patterns to avoid 'value: value'.
    // We need to duplicate the ident in an intermediate rule first
    // so we can match the ident to a literal without losing hygiene.
    (ref mut $a:ident) => { pat![%HACK% [ref mut] $a $a] };
    (ref $a:ident)     => { pat![%HACK% [ref]     $a $a] };
    (mut $a:ident)     => { pat![%HACK% [mut]     $a $a] };
    ($a:ident)         => { pat![%HACK% []        $a $a] };
    
    // only the specific ident 'value' should be rewritten,
    // since otherwise we might rewrite unit structs or constants.
    (%HACK% [$($kw:tt)*] $ident:ident value ) => { Value { $($kw)* $ident } };
    (%HACK% [$($kw:tt)*] $ident:ident $_x:tt) => { Value { value: $($kw)* $ident } };
    
    // if it is any other kind of pattern, use it normally
    ($a:pat) => { Value { value: $a }};
}

Notice how proper support for ref and mut requires that the macro output uses shorthand field syntax, as opposed to a more scalable workaround like value: value@_ (where the value@_ is at least something that could be produced by a helper macro). A similar pattern macro for a struct with two fields basically requires an incremental muncher now, just to avoid the exponential blowup of 5^n rules.

This is an awful lot of headache just for a little reminder to write more idiomatic code! Isn't that clippy's job, anyhow?

@zackmdavis
Copy link
Member

A crude fix would be to just not trigger this lint in macro expansions (at the cost of missing out on linting macro code where the offending non-shorthand pattern doesn't come as an argument, which, ideally, should be linted): #49614

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 5, 2018
zackmdavis added a commit to zackmdavis/rust that referenced this issue Apr 9, 2018
In issue rust-lang#49588, Michael Lamparski pointed out a scenario in which the
non-shorthand-field-patterns lint could be triggered by a macro-expanded
pattern, in a way which was direly unwieldy for the macro author to guard
against and unreasonable to expect the macro user to take into account. We can
avoid this by not linting patterns that come from macro-expansions. Although
this entails accepting "false negatives" where the lint could genuinely improve
macro-templated code, avoiding the reported "true-but-super-annoying positive"
may be worth the trade? (Some precedent for these relative priorities exists as
no. 47775 (5985b0b).)

Resolves rust-lang#49588.
kennytm added a commit to kennytm/rust that referenced this issue Apr 11, 2018
…trochenkov

in which the non-shorthand patterns lint keeps its own counsel in macros

In issue rust-lang#49588, Michael Lamparski pointed out a scenario in which the
non-shorthand-field-patterns lint could be triggered by a macro-expanded
pattern, in a way which was direly unwieldy for the macro author to guard
against and unreasonable to expect the macro user to take into account. We can
avoid this by not linting patterns that come from macro-expansions. Although
this entails accepting "false negatives" where the lint could genuinely improve
macro-templated code, avoiding the reported "true-but-super-annoying positive"
may be worth the trade? (Some precedent for these relative priorities exists as
no. 47775 (5985b0b).)

Resolves rust-lang#49588.
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-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

3 participants