allow named/literal args in generic type positions #66

Merged
jacksonopp merged 1 commit from fix/array-and-list-structure into main 2026-09-16 20:56:36 +00:00
Owner

Found while implementing #55: `docs/syntax-notes.md`'s array/list desugaring ladder wrote `[3]Int[1, 2, 3] ↔ Array(Int)[1, 2, 3]`, silently dropping the size — the desugared canonical form had nowhere to put it, since `TypeExpr::Generic`'s args were always full `TypeExpr`s.

Decided `Array`'s size should be a real type parameter (`Array(T, N: Comptime(Int))`, Zig/Rust-style fixed-size array), not just a one-off literal check on the array literal itself. That needs generic-argument position to accept a bare comptime value (`3`) alongside ordinary type arguments (`T`), not just types.

  • `ast.rs` / `hir.rs`: new `GenericArg { Type(SpannedTypeExpr), Value(Spanned) }`, mirroring how `EffectArg` already lets one clause's args be either types or operators. `TypeExpr::Generic.args` is now `Vec` instead of `Vec`.
  • `parser.rs`: each generic argument parses as either a `TypeExpr` or a bare literal (`Int`/`Float`/`Bool`), combined via `.or()` per-argument, so `Array(Int, 3)` and `Array(Int, size)` both parse — the identifier case (`size`) already worked for free via the existing `Named` path.
  • `lower.rs`: new `lower_generic_arg`, reusing the existing `lower_literal` helper.
  • `ty.rs` needed no changes — both places matching `TypeExpr::Generic` already used `{ .. }`.
  • `docs/syntax-notes.md`: fixed the desugaring ladder (`Array(Int, 3)`), updated the `Comptime` section's `lookupTable` example to actually use the size (`-> Array(Int, size)`), and updated `List`'s illustrative desugared definition (`items: Array(T)` → `items: *T`, since a growable list can't be backed by a fixed-size array).

This is parser/AST-level only — no `TyKind::Array` or `Comptime(Int)`-parameter checking semantics yet; that's future work for #56.

Test plan

  • `cargo test --workspace` (existing generic-arg snapshots reviewed/accepted via `cargo insta review`; two new snapshot tests for the literal and identifier value-arg cases)
  • `cargo clippy --workspace --all-targets --all-features`
Found while implementing #55: \`docs/syntax-notes.md\`'s array/list desugaring ladder wrote \`[3]Int[1, 2, 3] ↔ Array(Int)[1, 2, 3]\`, silently dropping the size — the desugared canonical form had nowhere to put it, since \`TypeExpr::Generic\`'s args were always full \`TypeExpr\`s. Decided \`Array\`'s size should be a real type parameter (\`Array(T, N: Comptime(Int))\`, Zig/Rust-style fixed-size array), not just a one-off literal check on the array literal itself. That needs generic-argument position to accept a bare comptime value (\`3\`) alongside ordinary type arguments (\`T\`), not just types. - \`ast.rs\` / \`hir.rs\`: new \`GenericArg { Type(SpannedTypeExpr), Value(Spanned<Literal>) }\`, mirroring how \`EffectArg\` already lets one clause's args be either types or operators. \`TypeExpr::Generic.args\` is now \`Vec<GenericArg>\` instead of \`Vec<SpannedTypeExpr>\`. - \`parser.rs\`: each generic argument parses as either a \`TypeExpr\` or a bare literal (\`Int\`/\`Float\`/\`Bool\`), combined via \`.or()\` per-argument, so \`Array(Int, 3)\` and \`Array(Int, size)\` both parse — the identifier case (\`size\`) already worked for free via the existing \`Named\` path. - \`lower.rs\`: new \`lower_generic_arg\`, reusing the existing \`lower_literal\` helper. - \`ty.rs\` needed no changes — both places matching \`TypeExpr::Generic\` already used \`{ .. }\`. - \`docs/syntax-notes.md\`: fixed the desugaring ladder (\`Array(Int, 3)\`), updated the \`Comptime\` section's \`lookupTable\` example to actually use the size (\`-> Array(Int, size)\`), and updated \`List\`'s illustrative desugared definition (\`items: Array(T)\` → \`items: *T\`, since a growable list can't be backed by a fixed-size array). This is parser/AST-level only — no \`TyKind::Array\` or \`Comptime(Int)\`-parameter checking semantics yet; that's future work for #56. ## Test plan - [x] \`cargo test --workspace\` (existing generic-arg snapshots reviewed/accepted via \`cargo insta review\`; two new snapshot tests for the literal and identifier value-arg cases) - [x] \`cargo clippy --workspace --all-targets --all-features\`
refactor generic arguments to allow for named/literal
All checks were successful
CI / check (pull_request) Successful in 3m10s
b8205fd8d0
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jacksonopp/twill!66
No description provided.