Skip to content

Parser: a qualified variant pattern (let Shape.Named(n) = s else: ...) is rejected in let; match accepts it #1373

Closed

Description

@ehartford

A type-qualified enum-variant pattern parses in a match arm but not in a let / let ... else pattern. Patterns are one grammar (§9.7); the spec examples use .Variant shorthand in let-else (§9.7 "let ... else with enum shorthand"), but nothing restricts a let pattern to the shorthand.

Repro (v0.15.2.1 and origin/main 8c859a7):

enum Shape:
    Named(i32)
    Empty
fn f(s: Shape) -> i32:
    match s:
        Shape.Named(n) => n
        Shape.Empty => 0
fn g(s: Shape) -> i32:
    let Shape.Named(n) = s else: return 0
    n
fn main:
    print(f"{f(Shape.Named(2))} {g(Shape.Named(3))}")

Actual:

error: let binding requires initializer
 --> q.w:9:14
9 |     let Shape.Named(n) = s else: return 0
  |              ^^^^^^

Expected: 2 3.

Workaround: .Named(n) shorthand (used in the #1365 let-else fixtures and drop-audit cells). Found while building the #1365 matrix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions