Skip to content

Match ergonomics means bindings have different types in patterns and match arm; cannot deref references in pattern #64586

Open
@varkor

Description

@varkor

Consider the following:

struct S(u32);

fn foo(x: &S) {
    let _: &u32 = match x {
        S(y) => y,
        _ => &0,
    };
}

Here, the variable binding y has type &u32 outside the pattern. However, inside the pattern it has type u32. The fact that these types do not align is confusing, as it means you can't dereference y inside the pattern like this:

fn foo(x: &S) {
    let _: u32 = match x {
        S(&y) => y,
        _ => 0,
    };
}

and instead have to dereference it outside the pattern:

fn foo(x: &S) {
    let _: u32 = match x {
        S(y) => *y,
        _ => 0,
    };
}

Is there any reason this was chosen to be the case, or is required to be the case? As it stands, this behaviour is very counter-intuitive.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-patternsRelating to patterns and pattern matchingC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    Idea

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions