Repository navigation
sem: name a sum type constructor's type with T.Branch(...) - #3179
Merged
Merged
Conversation
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>
Araq
reviewed
Dec 1, 2026
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>
# Conflicts: # src/nimony/sem.nim
Member
|
Still not good... |
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #3078
Araq's answers to the three questions in #3078: the constructor only for now,
T.Branchis 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
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.
Tmay be the sum type, an alias of it or an instance of it, for plain, generic andrefsum types.Commits
T.Branch(...)with the type written in full. A field that does not fit it is reported on the field, as forlet r: Res[int, string] = Err(e: 5)._in the type arguments of the type before the dot. The written arguments seed the table thatinferSumTypeFromFieldsfills 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 asseq[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, andsemQualifiedSumTypeConstrcalls the same entry, so there is one inference.Not in this PR
Branch[A, B](...)as a short form and a better message forshow(Ok(v: 5))are not touched.Tests
New:
tests/nimony/object/tsumtype_qualified.nimandtsumtype_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 missingarkham(nonativenifcheckout) and no valgrind on the host.Both commits were built and tested on their own.
🤖 Generated with Claude Code