Skip to content

feat(lower): T2-5 static folds for builtin calls on constants - #123

Draft
espetro wants to merge 3 commits into
masterfrom
work/t2-static-folds
Draft

espetro wants to merge 3 commits into
masterfrom
work/t2-static-folds

Conversation

@espetro

@espetro espetro commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Tier-2 static folds per .agents/plans/2026-10-18-tiered-lowering-and-typefacts.md: repr/str/hash/len/chr/ord/int/bool/abs on compile-time-constant arguments now lower to const nodes instead of runtime calls. Non-foldable args fall through to the same handlers/fences as before — no fence message reworded.

Fold boundary (documented in the plan file):

  • eval(<string-const>) folds only through ast.literal_eval with a scalar result; arbitrary eval/exec stay fenced (tier-3 dynamic).
  • hash folds only on int/bool — str/bytes hashing is seed-randomized in CPython.
  • chr folds only inside the Latin-1 literal range (PS2005 boundary); ord only on single chars.
  • int of str/bytes consts keeps the ps_int_from_bytes path so the subset's i64 fence and underscore rejection survive.
  • len of a list/dict literal folds only when every element is itself a const (a call element's side effect must survive).

tests/test_t2_static_folds.py oracle-diffs compiled stdout against CPython, including repr edge chars (embedded quotes, escapes, \n, é, b"\x00\xff"), pins the folded IR shape (no call nodes), and pins the remaining fences. test_module_cond: len(NAME) folding makes the annotated module assignment a constant again, so the two len() cases moved to a new un-fenced test.

Frozen test_census_self: self-census is now modules 27, lines 20571, fences 1031 (+8 from the new lowering code).

uv run pytest green apart from that frozen pin and pre-existing benchmarks/compat2/cache.py (py314 except A, B: under the 3.13 venv, fails on master too). ruff check/ruff format clean on changed files.

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

repr/str/hash/len/chr/ord/int/bool/abs fold to const nodes when the
argument is compile-time constant. eval folds only through
ast.literal_eval with a scalar result; hash folds only on int/bool;
chr only inside Latin-1; int of text keeps ps_int_from_bytes.
Oracle-diff vs CPython including repr edge chars; fence assertions for
arbitrary eval/exec, non-scalar eval, and hash of text. module_cond:
len(NAME) folding makes the annotated assignment a constant again.
@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

Copy link
Copy Markdown

Deploying pyscriptc with  Cloudflare Pages  Cloudflare Pages

Latest commit: 7be3256
Status: ✅  Deploy successful!
Preview URL: https://81500b57.pyscriptc.pages.dev
Branch Preview URL: https://work-t2-static-folds.pyscriptc.pages.dev

View logs

This branch has not been deployed

No deployments
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