Skip to content

A nat-literal pattern skips the constructor arity check, so a non-decreasing self-call passes and a closed term inhabits Empty #852

Closed

Description

@TheNewJavaman

What you did

bend EMPTY.bend

What happened

All terms check.

The file

type U is Data:
  Un{}

type N is Data:
  Zero{w: U}
  Succ{p: N}

type D is Data:
  K{z: N}

type Empty is Data:

def f(b: N) -> Empty:
  match b:
    case U32.to_nat(0):
      {\{K: q => w => f(q)} : D -> U -> Empty}(K{Zero{Un{}}})
    case Succ{p}:
      f(p)

def boom() -> Empty:
  f(Zero{Un{}})

bend --version

bend 2.0.16

uname -sm

Linux x86_64

clang --version (the first line)

Ubuntu clang version 21.1.8 (6ubuntu1)

Activity

  1. added
    bug
    on Sep 19, 2026
  2. PedroVIOliv commented on Sep 19, 2026

    @PedroVIOliv
    Contributor

    Two things that may help narrow it.

    Any literal above 256 takes the same path. U32.to_nat(0) is the longhand, but plain literals reach it too, because nat_to_term expands a literal up to NAT_LITERAL_MAX = 256 into a Succ chain and anything larger into U32.to_nat(n). On the type from the report:

    case 255n  ->  "a Zero pattern with 1 field"   caught
    case 256n  ->  "a Zero pattern with 1 field"   caught
    case 257n  ->  accepted, branch goal becomes @w:U -> U
    case 300n  ->  accepted, branch goal becomes @w:U -> U
    

    So the checker's verdict on a pattern changes at 256, and an ordinary literal is enough to hit it.

    The two pattern paths differ in parse_patt (bend.ts:1727). The literal branch builds the pattern and returns before reaching the constructor case:

    const lit = t.$ === "App" ? nat_from_term(t) : null;
    if (lit !== null) {
      let q: Patt = { $: "PCtr", k: "Zero", x: [], s: t.s };
      for (let i = 0; i < lit; i++) {
        q = { $: "PCtr", k: "Succ", x: [q], s: t.s };
      }
      return q;
    }

    It never calls book_ctr, so neither the unknown-constructor check nor the arity check at :1744-1750 runs. Written honestly both checks fire: case Zero{}: gives "a Zero pattern with 1 field", and the self-call gives "expected: a decreasing self-call". Only the literal spelling escapes both.

    Related: bend.ts:2763 does its own arity check with a different message, so there is more than one place building patterns without the shared check.

  3. nicolas-abril commented on Sep 19, 2026

    @nicolas-abril
    Collaborator

    Probably due to fb36663

  4. VictorTaelin commented on Sep 19, 2026

    @VictorTaelin
    Contributor

    Fixed in 2.0.17. A literal pattern is now built as a constructor term and falls through to the same check every other pattern takes, so its arity is verified; the second arity check that used to sit in match_flatten was unreachable after that and is gone. Your file is refused with a Zero pattern with 1 field.

    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions