Skip to content

Comptime-fold all-literal ++ chains in runtime code #556

Closed

Description

@ehartford

Problem

A ++ chain whose parts are all string literals (e.g. the UAT probe builders in build/release_uat.w, #552) is still assembled at runtime via with_str_concat_n, even though the result is a compile-time constant.

ComptimeEval.w:4706 already knows how to fold OP_CONCAT over CV_STR values — it just isn't applied to constant-foldable expressions in ordinary runtime code paths.

Proposed fix

During lowering (e.g. in lower_str_concat_chain, src/MirLower.w:2557), when every collected part is a string literal, emit a single folded literal instead of an RK_STR_CONCAT_N. Zero runtime cost, smaller MIR, and functions like ruat_zlib_program() become a single constant return.

Determinism note: folding must be a pure function of the literal parts (it is), so fixpoint is unaffected.

Mission relevance

"If With can infer it... the programmer should not have to spell it out" — a constant the compiler can compute should never be assembled at runtime.

Related

Activity

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    Fixed in 7d9bd22. ++ chains whose parts are all plain string literals now fold at lowering into a single CK_STR constant (try_fold_literal_str_concat, src/MirLower.w). Raw payload concatenation is exact since escape sequences never span literal boundaries; mixed raw-marked/unmarked chains keep the runtime path. Verified at machine-code level: a literal-chain program's main contains no with_str_concat calls (the only remaining ones are in the runtime's panic_render). Behavior test: test/behavior/behav_str_literal_concat_fold.w.

    Implementing this exposed and was blocked by #557 (string constants >4095 bytes silently corrupted at LLVM emission) — fixed first in 1a42dbf.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions