Skip to content

emit-c: prelude programs fail to compile — regex runtime functions used without extern declarations #300

Closed

Description

@ehartford

Summary

The C backend (--emit-c) emits calls to with_regex_* runtime functions but never emits extern declarations for them. Because the prelude always pulls in lib/std/regex.w, every prelude program produces C that fails to compile (implicit-function-declaration errors, plus int→with_str/pointer type mismatches from the default int return type).

This is independent of any single program — even an empty print reproduces it.

Reproduction

fn main:
    print("hi")
with build --emit-c trivial.w -o trivial.c
cc -I runtime trivial.c out/lib/rt_core.o out/lib/fiber_runtime.o \
   out/lib/regex_runtime.o out/lib/rt_darwin_aarch64.o \
   runtime/fiber_asm_aarch64.s -o trivial_c

Expected

Emitted C compiles and links against the runtime.

Actual

error: call to undeclared function 'with_regex_error_message'
error: assigning to 'with_str' from incompatible type 'int'
...

10 distinct undeclared functions; 13 with_regex_* references in the emitted C, 0 extern declarations:

with_regex_capture_count
with_regex_capture_name_at
with_regex_capture_name_count
with_regex_code_copy
with_regex_code_free
with_regex_compile
with_regex_error_message
with_regex_group_name_to_index
with_regex_match_spans_alloc_at
with_regex_substitute

Root cause / fix pointer

CCodegen.emit_module (src/CCodegen.w, the runtime-extern block ending around with_str_clone at src/CCodegen.w:7506) declares the fmt/alloc/str runtime functions but omits the regex family. The fix is to emit extern prototypes for the with_regex_* functions there (matching the signatures in runtime/with_runtime.h / rt/regex_runtime.w).

Notes

  • Pre-existing; reproduces on an untouched code path. Found while completing the §18.6 collection length methods (Implement skipped test: test/spec/spec_ss18_6_collection_length_methods.w #282), where it blocked end-to-end C-backend run verification. The length-method C emission itself is correct; this is a separate emit-c/regex gap.
  • The LLVM backend is unaffected.
  • The emit-c smoke test passes, so the gap isn't currently caught — a prelude-using emit-c→cc→run roundtrip test would surface it.

Repro env: with v0.13.1-g7104bb5d7, darwin-aarch64.

Activity

  1. added
    bug
    on May 30, 2026
  2. ehartford commented on May 30, 2026

    @ehartford
    ContributorAuthor

    Plan: wire in the (migrated) PCRE2 regex runtime for emit-c

    The regex runtime is PCRE2 migrated into With — rt/regex_runtime.w wraps the std.re.pcre2_* modules and @[c_export]s a with_regex_* surface. It is not an external libpcre2, so there's no system lib to link; the gap is purely that the C backend never declares these exports (it hardcodes externs for fmt/alloc/str in CCodegen.emit_module and the regex family was missed).

    Fix

    Emit extern prototypes for the full with_regex_* surface in the runtime-extern block of CCodegen.emit_module (src/CCodegen.w, near the with_str_clone declaration at src/CCodegen.w:7506), and ensure out/lib/regex_runtime.o (+ the std.re.pcre2_* objects) is on the emit-c link line.

    Prototypes (from the @[c_export] signatures in rt/regex_runtime.w; str→with_str, *const i8→int8_t*, *mut i32/*const i32→int32_t*):

    extern with_str  with_regex_error_message(int32_t);
    extern int8_t*   with_regex_compile(with_str, int32_t, int32_t*, int32_t*);
    extern int8_t*   with_regex_code_copy(int8_t*);
    extern void      with_regex_code_free(int8_t*);
    extern int32_t   with_regex_capture_count(int8_t*);
    extern int32_t*  with_regex_match_spans_alloc(int8_t*, with_str, int32_t*);
    extern int32_t*  with_regex_match_spans_alloc_at(int8_t*, with_str, int32_t, int32_t*);
    extern int32_t   with_regex_capture_name_count(int8_t*);
    extern with_str  with_regex_capture_name_at(int8_t*, int32_t);
    extern int32_t   with_regex_group_name_to_index(int8_t*, with_str);
    extern with_str  with_regex_substitute(int8_t*, with_str, with_str, int32_t);

    Preferred approach: rather than hardcoding another extern list (which is exactly how this family got missed), have the C backend derive externs for all @[c_export] runtime functions referenced by the emitted module from their declared signatures. That closes this class of bug permanently.

    Relationship to assert_matches (#283)

    Per maintainer: assert_matches is gaining a regex-string-matching form backed by this same PCRE2 runtime, so a regex assert_matches used in an emit-c program will hit this gap. #283 depends on this being fulfilled for the C backend.

  3. added a commit that references this issue on Jun 8, 2026
    47e55d1
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions