Repository navigation
fix(dang): reject operations on generic type params - #99
Merged
Merged
Conversation
Deploying dang with
|
| Latest commit: |
8eb77fb
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://132e1dd4.dang-3kk.pages.dev |
| Branch Preview URL: | https://fix-rigid-signature-typevars.dang-3kk.pages.dev |
A function like `do(&yield: b): b { yield * 2 }` previously passed type
checking and only failed at the call site, because the signature's type
variable `b` unified flexibly with whatever the body required and that
constraint was then discarded. Calling it with a string produced a
confusing runtime evaluation error.
Signature type variables are now skolemized while their body is checked:
within the body each parameter is rigid, standing for "some caller-chosen
type", so it supports no operations and cannot be pinned to a concrete
type. Arithmetic on such a variable, or unifying it with a concrete type,
is now a definition-time error. The stored function type keeps the
flexible variables, so callers remain fully generic and a genuine
passthrough such as `id(&yield: b): b { yield }` still works.
Rigid variables live in a reserved Private Use Area rune range so they
never collide with the fresh flexible variables used elsewhere in
inference, and they print as their original letter.
Signed-off-by: Alex Suraci <[email protected]>
The skolemSubs helper was inserted between inferFunctionType's doc comment and its declaration, leaving the comment attached to the wrong function. Move it back so each function carries its own doc comment. Signed-off-by: Alex Suraci <[email protected]>
Arithmetic operators only checked that their two operands matched each other, so `"a" * "b"` type-checked and failed at runtime, while `1 * 2.0` was wrongly rejected even though evaluation supports it. Each operator now declares the operand categories it accepts: numeric for `* - / %`, and numeric plus string and list for `+`. The result of a numeric operation widens to Float when either side is a Float. Operands outside the operator's domain are rejected at definition time with a message naming the operator and the offending types, mirroring the existing check on unary minus. Existing error-message goldens are updated to the clearer wording, and new fixtures cover string multiplication and Int/Float mixing. Signed-off-by: Alex Suraci <[email protected]>
Ordering comparisons (< > <= >=) are numeric-only in evaluation, but the checker validated nothing, so `"a" < "b"` type-checked and then crashed at runtime with a Go-level type name. They now share the arithmetic domain check: operands must be numeric (Int and Float may mix), and non-numeric or unconstrained-generic operands are rejected at definition time. Equality (== !=) is intentionally defined across all types and keeps that behavior; only operators carrying a declared operand domain are checked. The shared logic is factored into resolveOperands, which arithmetic uses for its result type and ordering uses for validation before returning Boolean. The four ordering type-mismatch goldens previously captured runtime failures and now capture the clearer definition-time errors. Signed-off-by: Alex Suraci <[email protected]>
The diagnostics test asserted the old "cannot use Int! as String!" message for `"hello" + 42`. Arithmetic operands are now domain-checked, so the diagnostic reads "operator addition is not defined between types String! and Int!". Update the expectation to match. Signed-off-by: Alex Suraci <[email protected]>
NullableTypeVariable embeds TypeVariable, so IsRigid can be called directly rather than through the embedded field. Satisfies staticcheck QF1008. Signed-off-by: Alex Suraci <[email protected]>
vito
force-pushed
the
fix/rigid-signature-typevars
branch
from
June 6, 2026 03:46
0fa146f to
6263e86
Compare
Reflect the type checker's new rules. The block example used
`do(&yield: b): b { yield * 2 }`, which is now a type error; change it to
identity passthrough and note that a type variable is opaque. Add the
same rule to the type-variables section. In operators, state that
out-of-domain operands (`"a" * "b"`, `1 + "foo"`, `"a" < "b"`) are static
errors rather than runtime failures, and that Int/Float now widen.
Signed-off-by: Alex Suraci <[email protected]>
Add a `docs` function that runs docs/build.sh in the toolchain container and returns the rendered site directory. It is annotated @check so a broken docs build fails CI, and can also be called to export the built site. The output is gitignored, so this builds an artifact rather than generating committed files. Signed-off-by: Alex Suraci <[email protected]>
Changeset is a diff against a base, not a git commit, so it can carry the gitignored docs build artifacts. Convert docs from a @check to a @generate returning a Changeset, scoped to docs/ (overlaying the built directory on source) so the diff excludes .git and the rest of the tree. `dagger generate` now populates docs/ for local preview or deploy, and `dagger check` verifies the generator without demanding committed output. Signed-off-by: Alex Suraci <[email protected]>
A generator's changeset would always appear unapplied in `dagger check`: CI runs without the gitignored docs build artifacts, so the generator would report them as pending changes and fail. Since the output is gitignored, the useful CI role is verifying the build succeeds, so make docs a @check returning the rendered directory instead of a @generate. Signed-off-by: Alex Suraci <[email protected]>
Keep the docs check returning the changeset it builds under docs/ (useful to inspect or apply the rendered output), but keep the @check directive so it gates the build in CI rather than being verified for application. Signed-off-by: Alex Suraci <[email protected]>
Ordering comparisons were numeric-only; extend them to strings, compared lexicographically. Each ordering operator now also accepts the string operand domain, and the four eval functions handle String/String. Mixed operands like `"a" < 1` remain a static type error, and equality is unchanged. Drop the comparison_string_ordering error fixture (no longer an error), add positive coverage in test_comparisons, and update the operators doc. Signed-off-by: Alex Suraci <[email protected]>
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.
Problem
A function like
do(&yield: b): b { yield * 2 }passed type checking but failed at the call site:The signature's type variable
bis universally quantified, but the body'syield * 2unified it flexibly withIntand that constraint was then discarded — sobstayed generic in the stored type and the unsoundness only surfaced when a caller picked a non-numeric type.Fix
Signature type variables are now skolemized while their body is checked. Within the body each parameter is rigid: it stands for "some caller-chosen type", so it supports no operations and cannot be pinned to a concrete type. Arithmetic on a rigid variable — or unifying one with a concrete type — is now a definition-time error:
The stored function type keeps the flexible variables, so callers remain fully generic and a genuine passthrough such as
id(&yield: b): b { yield }still works for any type.This is the rigid-type-variable substrate that typeclasses would later sit on, without adding any surface syntax — it just makes "if you promise it works for all
b, you can't assume anything aboutb" the actual rule.Implementation
pkg/hm/types.go— rigid type variables occupy a reserved Private Use Area rune range (Rigid,IsRigid) so they never collide with the fresh flexible variables inference mints elsewhere; they print as their original letter.pkg/hm/unify.go—bindVarrefuses to pin a rigid variable to a concrete type (a flexible variable on the other side may still absorb it). This is the soundness rule itself.pkg/dang/ast_declarations.go—inferFunctionTypeskolemizes the signature's free variables into the body's environment, then restores them to flexible for the stored, caller-facing type.pkg/dang/ast_operators.go— arithmetic on a rigid variable is rejected with a clear message.Rigidity is deliberately confined to the body of a definition — a flexible inference variable elsewhere (e.g. the element type of an empty
[]in[].map { x => x * 2 }) is untouched, and generic call sites likesingleton(42)keep working.Tests
tests/errors/arithmetic_on_generic_type.dang(+ golden).test_symbol_block_arg.dangpreviously encoded the old unsound behavior; its signature is changed to the concreteIntit actually intended.pkg/dangandtestspass. The only failing tests are the neovim LSP integration tests, which fail identically onmain(environmental, unrelated to this change).