Repository navigation
Description
Problem
The type index (htl::typeindex) cannot say what a function takes or returns. TypeExpr::Fn is a unit variant, and its doc says so: "A function; its parameters and results are not kept." A tool built on the index therefore cannot describe a Teal function field to the host.
That blocks the next step of generation from Teal: a typed Rust caller for the functions a Teal script provides (a policy hook, an entry point). Today a host calls such a function by looking it up in the returned table by a string key and converting the arguments and results by hand. Nothing ties the Rust side to the Teal declaration, so a changed parameter list is found at run time.
Evidence
- Read, not reproduced:
crates/htl-core/src/typeindex.rs:437(Fnwith the doc above), andcrates/htl-core/src/prelude.lua:3004, wheretype_exprreturns{ k = "fn" }for every function type. - Read: the checker does keep the signature. Teal's function type carries
args(a tuple withis_va),rets(the same shape),min_arityandis_method(crates/htl-core/vendor/tl.lua:2908-2928,:3614-3626). Parameter names and a per-parameter optional flag are not part of the type (:3606). - Read: the users of
TypeExpr::Fnareshapes.rs:1222(T.fn),rsgen.rs:960(a problem) and three assertions intests/type_index.rs. Each only asks whether a type is a function. - Measured on a dogfood project (about 690 lines of Teal and about 3,800 lines of Rust in four crates, a host that loads Teal pipeline scripts and calls their hooks): about 1,100 of the Rust lines are hand-written glue for calling Teal functions and converting values (the VM thread and calls about 450, value conversion about 500, call sites about 165), counted with
wc -l.
Proposal
An architecture change, small in files:
TypeExpr::Fn(Box<FnSig>), withFnSig { params: TypeList, required: usize, results: TypeList, method: bool, type_params: Vec<String> }andTypeList { items: Vec<TypeExpr>, rest: Option<TypeExpr> }. It mirrors what Teal's type holds and adds nothing Teal does not know.requiredis Teal'smin_arity: the number of parameters not marked optional, wherever they sit (function(a?: string, b: integer)has 1). Under--#pragma arity offit is 0, which cannot be told apart from "all optional".methodis the checker's resolvedis_method(aselfof the record the function belongs to), not the parse-time guess. A generic field (function<T>(self: R, x: T)) is never promoted to a method by the checker, so it isfalsethere.- A method's
selfparameter is the checker'sselftype (Other("self")), and type variables inside a generic signature stayOther("T"). A generator that refuses methods and generics at first does not need more; resolving them is left to the generator's own issue. FnSig,TypeListandTypeExprbecome#[non_exhaustive]. This is a breaking change tohtl::typeindex.- Overloads (
poly) stayOtherfor now.
Rejected: a side table from position to signature (the signature of a callback parameter could not be reached from the tree), keeping only the parameter and result types (a generator could not refuse a variadic or generic function), and landing this together with the generator (one large change instead of an independent part).
Acceptance
- A field
cb: function(string, integer?): booleanhasTypeExpr::Fnwithparams.items=[string, integer],required= 1,results.items=[boolean], norest,method= false. ...: Tsetsparams.resttoT, and: T...setsresults.resttoT.- A generic function
function<T>(T): Thastype_params=["T"]. - A record field
f: function(self: R)inside recordRhasmethod= true;f: function(self: Other)andf: function<T>(self: R, x: T)havemethod= false. - In a file with
--#pragma arity off,requiredis 0. - A bare
functiontype hasparamsandresultseach{ items: [], rest: any }. htl shapesandhtl rsprint what they printed before for every existing test (cargo test --workspacepasses), andhtl rson a project with records and enums writes a byte-identical file.
Activity
Merge pull request #554 from ynishi/feat/fn-signature