Skip to content

LLVM codegen: FetchGetWithAuth and DateToLocaleString expressions not lowered (regression vs v0.5.491) #600

Closed

Description

@proggeramlug

Summary

The LLVM Phase 2 codegen backend (default in current perry) doesn't lower two expression node kinds that are otherwise wired up across HIR, JS codegen, WASM codegen, and type analysis. This regressed between v0.5.491 (compiles fine) and v0.5.696 (current HEAD b6c3afc1).

Errors

Error compiling module 'lib/gsc/client.ts' with --backend llvm:
  lowering closure func_id=35: lowering closure body func_id=35:
  perry-codegen Phase 2: expression FetchGetWithAuth not yet supported

Error compiling module 'lib/notifications/scheduler.ts' with --backend llvm:
  lowering function 'processUserNotification':
  lowering body of 'processUserNotification':
  perry-codegen Phase 2: expression DateToLocaleString not yet supported

Minimal repros

FetchGetWithAuth (any use of the recognized fetchWithAuth built-in):

async function fetchSites(token: string) {
  const r = await fetchWithAuth("https://example.com/api", "Bearer " + token);
  return await r.text();
}

DateToLocaleString (any n.toLocaleString() on a number):

const formatted = (12345).toLocaleString();

Both compile cleanly on v0.5.491 and fail on v0.5.696.

Where the gap is

The expression kinds exist throughout the compiler:

  • HIR walker: crates/perry-hir/src/walker.rs:279 (Expr::DateToLocaleString)
  • JS codegen: crates/perry-codegen-js/src/emit.rs:1717 (FetchGetWithAuth), :2155 (DateToLocaleString)
  • WASM codegen: crates/perry-codegen-wasm/src/emit.rs:3813,9087 (FetchGetWithAuth), :3710,8046 (DateToLocaleString)
  • Type analysis: crates/perry-codegen/src/type_analysis.rs:132,838
  • Collectors: crates/perry-codegen/src/collectors.rs:5044

But neither expression has an arm in the LLVM codegen (crates/perry-codegen/src/expr.rs, codegen.rs):

$ grep -n "FetchGetWithAuth\|DateToLocaleString" crates/perry-codegen/src/expr.rs crates/perry-codegen/src/codegen.rs
(no matches)

So they fall through to the "expression … not yet supported" arm in Phase 2 lowering.

Impact

Any TS source using either pattern can no longer be compiled with the default LLVM backend. In our case, this affects the production gscmaster.com API — fetchWithAuth/fetchPostWithAuth are documented as the recommended way to call authenticated HTTP endpoints (because regular fetch() silently drops headers from variables, known perry quirk), and .toLocaleString() is used for formatting numbers in notification emails.

Suggested fix

Implement the two expression arms in crates/perry-codegen/src/expr.rs. The JS codegen at emit.rs:1717-1722 (FetchGetWithAuth) and :2155 (DateToLocaleString) are concise semantic references:

  • FetchGetWithAuth { url, auth_header } → call js_fetch_get_with_auth(url, auth_header) (similarly FetchPostWithAuth → js_fetch_post_with_auth(url, auth_header, body))
  • DateToLocaleString(n) → emit a runtime helper that formats a number with locale-specific thousands separators (matches JS behavior of (12345).toLocaleString() === "12,345" in en-US)

Happy to test a fix against the gscmaster API codebase if helpful.

Versions

  • Local perry: HEAD b6c3afc1 (v0.5.696)
  • Production perry: v0.5.491 (compiles fine)
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