Design: arbitrary comptime-evaluated expressions in Comptime(_) generic params #76
Labels
No labels
bug
design
epic
risk
user-story
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
jacksonopp/twill#76
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Follow-up to #67, split out rather than folded in.
#67 settled the checker semantics for
Comptime(Int)-kinded generic params (Array(Int, N)sizes), but only for literal values —Array(Int, 3)works,Array(Int, computeSize())doesn't. That ceiling is already baked into the AST/checker types from #66/#71:GenericArg::Valueholds aSpanned<Literal>, not aSpanned<Expr>.InstantiationArg::Valueholds aLiteral, not a resolved comptime result.check_array_sizeexplicitly skips non-literal sizes, sinceScopeonly tracksTy, not values.To support arbitrary comptime-evaluated expressions in
Comptime(_)position, we need an actual comptime evaluator — not just a checker tweak. Open design questions for whoever picks this up:let N = 3; Array(Int, N))? Calls to other functions, if those functions are themselves comptime-pure (Zig-style comptime-ness tracked through the call graph)?GenericArg::Valuewould need to hold aSpanned<Expr>instead of aLiteral, with the checker reducing it to aLiteral/concrete value or failing with a diagnostic.check_comptime_expralongsidecheck_expr, needingScope(or a sibling) to track actual bound values, not just types.computeSize()isn't re-evaluated on every reference.InvalidArraySize.No implementation yet — this is a design/risk issue, same shape as #69.
Cross-reference: macros (
docs/syntax-notes.md's Macros section) are the same underlying problem — a macro isfn @name(...) -> Expr { ... }, an ordinary function that runs at compile time and returns a realExprvalue to splice in. Whatever evaluator this issue builds forComptime(_)-kinded generic params (running a Twill expression at compile time to get a concrete value) is the same kind of machine macro expansion will eventually need.Not merging the two now, though —
docs/project-plan.mdexplicitly lists 'Macro splice syntax + hygiene' under 'Deferred/open (not blocking the numbered steps)', so full macro-execution semantics are intentionally out of scope for the roadmap right now. Keep this issue's evaluator narrow (just enough forComptime(_)sizes), but worth designing it loosely compatible with eventually being reused for macro bodies, rather than hard-coding assumptions that only hold for integer sizes.Not spinning this into its own milestone for now, for the same reason — that would mean scoping up to a general-purpose comptime evaluator ahead of where the project plan currently wants to be. Revisit milestone-worthiness once macro splicing actually gets scheduled.