Skip to content

Refutable parameter patterns / multiple function clauses (§9.7) #472

Closed

Description

@ehartford

Summary

Support refutable parameter patterns through multiple function clauses, while keeping irrefutable parameter patterns as straightforward function-entry destructuring. This gives With the spec's concise head([first, ..]) / head([]) style without making individual function parameters silently fail at runtime.

Spec

§9.7 allows pattern matching in function parameters and gives examples for struct and slice patterns (docs/with-specification.md:3713-3724). Parameter patterns desugar to a match at function entry; irrefutable patterns need no special handling, while refutable patterns require multiple function clauses or an else (docs/with-specification.md:3726-3730).

Current state

Irrefutable parameter-pattern coverage exists in test/spec/spec_ss09_7_parameter_patterns.w, but the multiple-clause refutable form is unsupported. Parser function declarations still expect a normal parameter list with identifiers/types (src/Parser.w:2818-2827 for methods; top-level function parsing uses the same parameter metadata shape), and there is no representation for multiple function clauses sharing a name. Pattern checking supports refutable forms in match and let-style positions (src/SemaCheck.w:6679-6890), but not as a dispatch mechanism across same-named function definitions.

Implementation plan

  1. Extend function parameter parsing to accept pattern syntax plus a type annotation, preserving the existing simple identifier fast path. Store parameter patterns in function metadata parallel to parameter names/types.
  2. For irrefutable parameter patterns, lower at function entry: bind the raw parameter to a synthetic local, then call existing lower_pattern to bind the user names. Keep diagnostics for tuple/struct arity at sema time.
  3. For refutable parameter patterns, group same-name function clauses during declaration collection. All clauses must have compatible signatures/return types and be contiguous or otherwise deterministic by source order.
  4. Lower a multiple-clause function as a generated dispatcher: evaluate parameters once, test each clause's patterns in source order, execute the matching body, and emit an exhaustiveness/implicit failure diagnostic if no clause can match and no fallback clause exists.
  5. Reject a single function declaration containing a refutable parameter pattern unless it is part of a complete multiple-clause group or has an explicit else design accepted by the maintainer. The diagnostic should name the parameter pattern and say refutable parameter pattern requires another function clause or an else.
  6. Keep overload resolution out of scope: multiple clauses are pattern dispatch for the same function, not type-based overloads.

Test plan

Add test/behavior/behav_refutable_param_clauses.w:

  • fn head([first, ..]: [i32; 3]) -> ?i32: Some(first) plus fn head([]: [i32; 0]) -> ?i32: None or an equivalent slice/array form supported by the type system.
  • enum clause dispatch such as fn value(Some(x): ?i32) -> i32 and fn value(None: ?i32) -> i32.

Add test/compile_errors/err_refutable_param_pattern_single_clause.w.

Add test/compile_errors/err_function_clause_signature_mismatch.w.

Extend test/spec/spec_ss09_7_parameter_patterns.w with the §9.7 examples.

Acceptance criteria

  • Irrefutable parameter patterns still work.
  • Refutable parameter patterns require and use multiple clauses.
  • Clauses dispatch in source order with deterministic lowering.
  • Incompatible clause signatures are rejected.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

#474 for slice ..rest binding quality. #518 for pattern-form coverage.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 9.7.1.43.

Activity

  1. ehartford commented on Jun 14, 2026

    @ehartford
    ContributorAuthor

    Implemented in ddc8767. Added function clause grouping for duplicate same-file function declarations, refutable parameter pattern parsing/checking, MIR dispatcher generation for clause selection, and native codegen support for generated clause bodies. Added positive behavior/spec coverage and negative tests for single-clause refutable patterns and mismatched clause signatures. Verification passed with WITH=$PWD/out/bin/with ./out/release/bin/with: build, fixpoint, full test suite, and test-green.

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

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