Skip to content

sem: name a sum type constructor's type with T.Branch(...) - #3179

Merged
Araq merged 8 commits into
nim-lang:masterfrom
jasagiri:sumctor-3078
Dec 5, 2026
Merged

Araq merged 8 commits into
nim-lang:masterfrom
jasagiri:sumctor-3078

Conversation

@jasagiri

Copy link
Copy Markdown
Contributor

Refs #3078

Araq's answers to the three questions in #3078: the constructor only for now, T.Branch is canonical, and a field that disagrees with what is written is reported on the field. This follows the plan in the comment that promised this PR.

What changes

type
  Res[T, E] = object
    case
    of Ok: v: T
    of Err: e: E
  IntRes = Res[int, string]

proc show(r: Res[int, string]): string = ...

show(Res[int, string].Ok(v: 5))   # was: sum type constructor requires named arguments
show(IntRes.Ok(v: 5))             # was: undeclared field: 'Ok' for type typedesc[...]
show(Res[_, string].Ok(v: 5))     # T comes from the field, E is written

The type before the dot is the constructor's type, so no callee has to be resolved first and a branch name that two sum types share is told apart by the type in front of it. T may be the sum type, an alias of it or an instance of it, for plain, generic and ref sum types.

Commits

  1. T.Branch(...) with the type written in full. A field that does not fit it is reported on the field, as for let r: Res[int, string] = Err(e: 5).
  2. _ in the type arguments of the type before the dot. The written arguments seed the table that inferSumTypeFromFields fills from the fields, so a written argument is never overridden. A written argument that a field contradicts is reported on the field, also while another parameter stays open; a numeric literal takes the written type instead (Both[uint8, _].Pair(a: 5, b: "z")), and a mismatch nested in a field type such as seq[T] is left to the constructor's own check. A parameter that is neither written nor determined by a field is named in the error, and so is a wrong number of arguments. _ is accepted only in this position.

Commit 2 depends on commit 1; commit 1 stands alone.

Placement

The inference already lives in inferSumTypeFromFields (src/nimony/sem.nim). The only new thing is where its table starts: it now takes the table as an argument, so the written type arguments seed it instead of an empty one. The existing caller passes an empty table, and semQualifiedSumTypeConstr calls the same entry, so there is one inference.

Not in this PR

Branch[A, B](...) as a short form and a better message for show(Ok(v: 5)) are not touched.

Tests

New: tests/nimony/object/tsumtype_qualified.nim and tsumtype_qualified_errors.nim (with .msgs). Both fail on master. Checked on Linux x86_64 (C backend), master e82e6b6 against this branch, hastur tests/nimony --jobs:4: master 1279 of 1302 tests pass, this branch 1281 of 1304 (the two new tests). The 23 failures are the same set in both: the same 16 entries, from the missing arkham (no nativenif checkout) and no valgrind on the host.

Both commits were built and tested on their own.

🤖 Generated with Claude Code

jasagiri and others added 4 commits November 26, 2026 07:40
A constructor like `Ok(v: 5)` leaves the type to its context, and a call
argument has none. `Res[int, string].Ok(v: 5)` and `IntRes.Ok(v: 5)` were
read as a dot call of `Ok` on the type and failed with "sum type
constructor requires named arguments". The type written before the dot is
now the constructor's type, whatever the callee, so it needs no overload to
be resolved first, and a branch name two sum types share is told apart by
the type that precedes it.

A field that does not fit the written type is reported on the field.

Refs nim-lang#3078

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
`Res[_, string].Ok(v: 5)` writes only what the fields cannot tell, as
`Ok(v: 5)` already leaves `T` to its field. `inferSumTypeFromFields` takes
the table it fills as an argument, so the written arguments seed it and are
never overridden by a field; the existing callers pass an empty one.

`inferFieldTypes` notices a field whose type is a typevar the table already
binds, with a value of another type, and reports it on the field even while
another parameter stays open. A numeric literal is left to the constructor's
own check, since it takes the type written for it. A parameter that is
neither written nor determined by a field is named in the error, and so is
a wrong number of arguments.

`_` is accepted only here, as the type arguments of the type before the
dot, not as a general "infer this" in type expressions.

Refs nim-lang#3078

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Comment thread src/nimony/sem.nim Resolved
Araq and others added 2 commits December 4, 2026 11:29
The branch head, `Branch` or `T.Branch`, is resolved where the type
head is, and the `kind` field is set in the field loop, so the three
entry points no longer build `(oconstr T (kv kind Branch) ...)` and
re-enter semObjConstr. Of equally named branches, an expected type's
own one is taken.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Araq

Araq commented Dec 4, 2026

Copy link
Copy Markdown
Member

Still not good...

Araq and others added 2 commits December 5, 2026 06:44
Drop the pre-check at the top of semCall. The parser already makes
every call with named arguments an `oconstr`, so `T.Branch(v: 5)`
reaches semObjConstr, whose head now also accepts `T.Branch`. The
remaining call forms (`T.Branch()`, positional arguments) resolve
like `Enum.field`: tryBuiltinDot finds the branch in a sum type's
anum, and the call keeps `T` for semSumTypeConstrFromCall.

Also fixes a crash: a failed inference left a nil `it.typ`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`_` is now handled per argument in semInvoke's loop over the generic
parameters. Its new entry semConstrInvoke, used for a constructor's
type, leaves an argument written `_` open and returns the bare generic
type with the written arguments (OpenInvoke) instead of instantiating;
the constructor seeds the field inference with them. This replaces the
"has any wildcard" pre-scans and the second argument loop of
inferWildcardSumType, and works for plain generic objects as well:
`Pair[int8, _](first: 1, second: "x")`. A wrong argument count is
semInvoke's own error now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Araq
Araq merged commit c1407eb into nim-lang:master Dec 5, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants