Skip to content

feat(lower): TypeVar/Generic annotation erasure + per-call-site monomorphization (T2-1) - #125

Merged
devin-ai-integration[bot] merged 3 commits into
masterfrom
work/t2-typevar-erasure
Oct 18, 2026
Merged

devin-ai-integration[bot] merged 3 commits into
masterfrom
work/t2-typevar-erasure

Conversation

@espetro

@espetro espetro commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Task T2-1 of .agents/plans/2026-10-18-tiered-lowering-and-typefacts.md: TypeVar/Generic annotation erasure via per-call-site monomorphization (tier-2 typed compile-time lowering).

  • T = TypeVar('T') declarations lower as no-ops (_declare_typevar); unbound T in annotations becomes an internal typevar placeholder that never reaches IR (_llvm_type tripwires on it), bound= erases to the bound, covariant=/contravariant= are ignored.
  • Generic[T] bases are erased; the class records declared typevars in info["typevars"] and is skipped by the main class loop.
  • Generic fns registered in self.generic_fns are skipped by the sig loop; each call site unifies arg types against sig typevars (_unify) and emits a specialized copy (ident.int, ident.str) through inst_queue/_drain_instances. Params specializing to none drop from the signature and bind as consts.
  • Box[int]/constructor calls emit specialized class records (Box_int) via class_queue with substituted fields/methods; builtin aliases (list[int]) untouched.

Fences (existing PS2xxx style; no existing messages reworded)

  • PS1001 — typevar placeholder as a concrete local annotation
  • PS1003 — cannot infer {tv} for {name}, {tv} is {a}, not {b}, bound violation, cannot instantiate {base} with none
  • PS2002 — TypeVar / generic function used as a runtime value
  • PS2003 — TypeVar str-name / unsupported keyword / arg-count

Checks

  • tests/test_t2_typevar.py: 16 new tests incl. oracle-identical runs on int/str/None.
  • uv run pytest: 575 passed; 4 failures reproduce identically on origin/master (env Python 3.13 vs tests expecting 3.14; pre-broken py2-style except in benchmarks/compat2/cache.py and examples/gates_probe.py).
  • Self-census: modules 27 (unchanged), fences 1023 → 1050 (+27), lines 20954.
  • uv run ruff check and ruff format --check clean on changed files.

Link to Devin session: https://app.devin.ai/sessions/4dc1d8884421473cb28bacb88891f6e0
Open in Devin Desktop: https://app.devin.ai/desktop/session/4dc1d8884421473cb28bacb88891f6e0?variant=devin
Requested by: @espetro

…nomorphization

- T = TypeVar('T') declarations lower as no-ops; annotation uses of T
  erase to the declared bound or, when unbound, an internal typevar
  placeholder resolved at call sites.
- Variance keywords (covariant=/contravariant=) are ignored.
- Generic[T] bases are erased; the class lowers as if unparametrized
  and records the declared typevars for specialization.
- Calls to generic functions unify argument types with the declared
  typevars and emit a specialized copy per call site (ident.int).
- Construction/subscripts of user generic classes (Box[int], Box(1))
  emit specialized records per instantiated type set (Box_int);
  builtin aliases (list[int]) are untouched.
- Params specializing to none drop from the emitted signature and
  bind as constants; class instantiation at none fences PS1003.
- Stays fenced: TypeVar/generic fn used as a value (PS2002), TypeVar
  needing a str name or unsupported keywords (PS2003), unresolved
  typevar locals (PS1001), inference conflicts and missing
  instantiations (PS1003), isinstance on generic aliases.
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Deploying pyscriptc with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2ad797d
Status: ✅  Deploy successful!
Preview URL: https://b78d76fb.pyscriptc.pages.dev
Branch Preview URL: https://work-t2-typevar-erasure.pyscriptc.pages.dev

View logs

@devin-ai-integration
devin-ai-integration Bot marked this pull request as ready for review October 18, 2026 19:28
@devin-ai-integration
devin-ai-integration Bot merged commit 89da8b8 into master Oct 18, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant