Skip to content

Spurious unreachable_patterns with macro #80501

Closed
@lnicola

Description

@lnicola

Code

I tried this code:

pub enum TypeCtor {
    Bool,
    Slice,
    Array,
}

pub struct ApplicationTy {
    pub ctor: TypeCtor,
    pub parameters: (),
}

pub enum Ty {
    Apply(ApplicationTy),
}

macro_rules! ty_app {
    ($ctor:pat, $param:pat) => {
        crate::Ty::Apply(crate::ApplicationTy {
            ctor: $ctor,
            parameters: $param,
        })
    };
    ($ctor:pat) => {
        ty_app!($ctor, _)
    };
}

fn _foo(ty: Ty) {
    match ty {
        ty_app!(TypeCtor::Array, _st) | ty_app!(TypeCtor::Slice, _st) => {}
        _ => {}
    }

    // same as above, with the macro expanded
    match ty {
        Ty::Apply(crate::ApplicationTy {
            ctor: TypeCtor::Array,
            parameters: _st,
        })
        | Ty::Apply(crate::ApplicationTy {
            ctor: TypeCtor::Slice,
            parameters: _st,
        }) => {}
        _ => {}
    }
}

fn main() {}

I expected to see this happen: code should build fine

Instead, this happened: unreachable pattern in the first, but not on the second match

warning: unreachable pattern
  --> src/main.rs:18:9
   |
18 | /         crate::Ty::Apply(crate::ApplicationTy {
19 | |             ctor: $ctor,
20 | |             parameters: $param,
21 | |         })
   | |__________^
...
30 |           ty_app!(TypeCtor::Array, _st) | ty_app!(TypeCtor::Slice, _st) => {}
   |                                           ----------------------------- in this macro invocation
   |
   = note: `#[warn(unreachable_patterns)]` on by default
   = note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

warning: 1 warning emitted

Version it worked on

It most recently worked on:

rustc 1.48.0 (7eac88abb 2020-11-16)
binary: rustc
commit-hash: 7eac88abb2e57e752f3302f02be5f3ce3d7adfb4
commit-date: 2020-11-16
host: x86_64-unknown-linux-gnu
release: 1.48.0
LLVM version: 11.0

Version with regression

rustc 1.50.0-beta.1 (05b602367 2020-12-29)
binary: rustc
commit-hash: 05b6023675d77979637b04a350c85903fbf59257
commit-date: 2020-12-29
host: x86_64-unknown-linux-gnu
release: 1.50.0-beta.1

@rustbot modify labels: +regression-from-stable-to-beta -regression-untriaged

Metadata

Metadata

Assignees

Labels

A-exhaustiveness-checkingRelating to exhaustiveness / usefulness checking of patternsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions