8000 typeindex: keep a function's signature in TypeExpr::Fn · Issue #553 · ynishi/htl · GitHub
[go: up one dir, main page]

Skip to content

typeindex: keep a function's signature in TypeExpr::Fn #553

Open

Description

@ynishi

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 (Fn with the doc above), and crates/htl-core/src/prelude.lua:3004, where type_expr returns { k = "fn" } for every function type.
  • Read: the checker does keep the signature. Teal's function type carries args (a tuple with is_va), rets (the same shape), min_arity and is_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::Fn are shapes.rs:1222 (T.fn), rsgen.rs:960 (a problem) and three assertions in tests/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>), with FnSig { params: TypeList, required: usize, results: TypeList, method: bool, type_params: Vec<String> } and TypeList { items: Vec<TypeExpr>, rest: Option<TypeExpr> }. It mirrors what Teal's type holds and adds nothing Teal does not know.
  • required is Teal's min_arity: the number of parameters not marked optional, wherever they sit (function(a?: string, b: integer) has 1). Under --#pragma arity off it is 0, which cannot be told apart from "all optional".
  • method is the checker's resolved is_method (a self of 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 is false there.
  • A method's self parameter is the checker's self type (Other("self")), and type variables inside a generic signature stay Other("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, TypeList and TypeExpr become #[non_exhaustive]. This is a breaking change to htl::typeindex.
  • Overloads (poly) stay Other for 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

  1. A field cb: function(string, integer?): boolean has TypeExpr::Fn with params.items = [string, integer], required = 1, results.items = [boolean], no rest, method = false.
  2. ...: T sets params.rest to T, and : T... sets results.rest to T.
  3. A generic function function<T>(T): T has type_params = ["T"].
  4. A record field f: function(self: R) inside record R has method = true; f: function(self: Other) and f: function<T>(self: R, x: T) have method = false.
  5. In a file with --#pragma arity off, required is 0.
  6. A bare function type has params and results each { items: [], rest: any }.
  7. htl shapes and htl rs print what they printed before for every existing test (cargo test --workspace passes), and htl rs on a project with records and enums writes a byte-identical file.

Activity

  1. added
    enhancement
    on Oct 2 5A54 5, 2026
  2. added a commit that references this issue on Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0