Skip to content

A + on a field binder in a braced pattern, then a nested pattern in the same column: 'an annotated term (cannot infer)' on a valid program #810

Closed

Description

@nicolas-abril

What you did

Ran this file from the current main checkout:

import Base
def f(ys: List<&2, U32>) -> U32:
  match ys:
    case Con{h, +z}:
      1
    case [c, d]:
      (c + d : U32)
    case _:
      0
def main() -> IO(Unit):
  IO.print(U32.show(f([3, 4])))
bun bend2/main.ts plus_field_nested.bend

What happened

The checker rejects the file:

Error:
- expected : an annotated term (cannot infer)
- observed : [d]
Context:
- h : U32
- d : U32
Location: f
3 |   match ys:
4>|     case Con{h, +z}:
5 |       1

The same file with Con{h, z} in the first row, no +, checks and prints 1. So does the file with a flat Con{c, r} in the second row instead of [c, d]. It needs both: a + on a field binder in a braced pattern, and a later row of the same column whose pattern nests a constructor under that field. The number of columns and what the other columns hold do not matter; a [h, +z] list pattern in the first row passes.

The observed term [d] is Con{d, Nil{}}, the tail of the second row's pattern, shown as a list.

Why

A + on a field binder is parsed as a rebinding let around the row's body, +z' = z (parse_bind pushes it to p.plus, body_plus wraps it as a Local of quantity Many), the same mechanism as +x => e on a lambda. The let's value is a variable and infers.

match_flatten splits a column on the first constructor row it finds. For column z that is the second row, Con{d, Nil{}}, so the reconstruction term for the column is Con{d, Nil{}}, and body_sub rewrites every z in the earlier rows' bodies to it, because inside that arm only the fields exist. Every other occurrence of z sits in a checked position and takes the substitution. The one inside the let becomes +z' = Con{d, Nil{}}, and the let rule infers its value: a constructor has no inference rule, since its fields do not fix the datatype's parameters (d : U32 fixes the element, nothing fixes the list's quantity, and a bare Nil{} fixes nothing), so the checker stops there.

The reconstruction is the right term; it is what makes the aliasing between z and its fields visible to the use counts. The problem is only that its type exists in one place, the Mat rule, which builds the same Ctr(k, fields) inside the motive when it checks the arm, and never hands it to the term.

A + on a lambda binder cannot hit this, because a lambda's variable is never substituted. Only a pattern column can be split by a later row.

Bend version

bend 2.0.4
main @ 438bf744

Operating system

Darwin arm64

clang --version

Apple clang version 17.0.0 (clang-1700.6.4.2)

Activity

  1. added
    bug
    on Sep 18, 2026
  2. VictorTaelin commented on Sep 18, 2026

    @VictorTaelin
    Contributor

    Confirmed, and your diagnosis is exact: +z on a field is a rebinding let around the row body; once a later row's nested pattern refines that column, the let's value becomes a constructor, and a let must infer its value. We looked for a small fix and found none that does not either reach into the match flattener or add a type slot to lets or pattern binders (about 20-30 lines). We are not taking that today, so this stays open as a known gap. Workaround: bind the field plainly in the pattern and rebind after the match (+z2 = z as the first line of the body), or keep the nested-pattern row above the + row.

    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.

  3. VictorTaelin commented on Sep 18, 2026

    @VictorTaelin
    Contributor

    Fixed on main (929157bc), ships in 2.0.8.

    Cause: +z on a matched field was not a mark on the binder. The parser bound a fresh z and inserted a hidden let +z' = z after the match. When a later row split the same column, the flattener replaced that let's value with the reconstructed constructor, whose type the checker cannot infer: hence "cannot infer" on a program that is fine.

    Fix: the + mark now lives on the pattern binder and travels to the lambda the flattener emits; the checker grants Many at a + binder whose type is Data. The hidden-let path is deleted. One visible change: a + on a field applies to that field's column in every row of the match, not only its own row.

    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