Skip to content

wart: non-Eq [T; 0] can be matched as if T were #[structural_match] #62336

Closed

Description

Spawned off of investigation of issue #62307 and PR #55837

We currently allow constants that are empty arrays of any type to be used as patterns in match. We allow this regardless of whether they are an array of an ADT that does not derive PartialEq/Eq

This may be a feature or a bug depending on one's perspective.

Here is an example of the behavior in question (play):

struct B(i32);

const B0: [B; 0] = [];

//#[derive(PartialEq, Eq)] // can't uncomment b/c B doesn't impl PartialEq+Eq.
struct UhOh([B; 0]);

const _UH_OH: UhOh = UhOh(B0);

fn main() {
    match [] {
        B0 => { println!("B0 matched []"); }
    }

    match UhOh([]) {
        UhOh(B0) => { println!("UhOh(B0) matched UhOh([])"); }
    }

    #[cfg(this_wont_compile_without_derive_of_partial_eq_and_eq)]
    match UhOh([]) {
        _UH_OH => { println!("_UH_OH matched UhOh([]])"); }
    }
}

To be clear: This behavior might be fine.

It is just a little weird, because on other uses of consts in patterns, we do tend to require that their types derive PartialEq and Eq

But we can treat an empty array as an exceptional case here, if that's what people want.

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

Metadata

Assignees

No one assigned

    Labels

    A-arrayArea: `[T; N]`A-patternsRelating to patterns and pattern matchingA-zstArea: Zero-sized types (ZST).T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions