Skip to content

Migrate: fold paste-suffix macros through glibc's "# define" spelling - #997

Closed
ropoctl wants to merge 2 commits into
fix-comptime-sha256-textfrom
fix-migrate-paste-suffix-glibc
Closed

ropoctl wants to merge 2 commits into
fix-comptime-sha256-textfrom
fix-migrate-paste-suffix-glibc

Conversation

@ropoctl

@ropoctl ropoctl commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

What

Fixes the c-migrator-core-tests paste_suffix_* failures on glibc Linux
hosts. The paste-suffix macro folder (#945) only worked against darwin's
<stdint.h>; on a glibc host it left INTMAX_MAX / UINTMAX_MAX unfolded.

Stacked on #956.

Root cause

The folder reads the token-paste suffix (L→i64, UL→u64, …) from a macro
session built out of the preprocessed #define lines. Two glibc <stdint.h>
facts defeated it:

  • glibc writes # define (whitespace between # and define); the
    define-line recognizer, keyed on a literal #define, did not match it.
  • glibc routes the width constants through __-prefixed helpers — INTMAX_MAX
    is __INT64_C(9223372036854775807), UINTMAX_MAX is __UINT64_C(...) —
    which the session builder dropped outright on its leading-underscore rule.

Together those made __INT64_C / __INTMAX_C / __UINT64_C invisible to the
folder, so those constants could not paste their suffix and stayed unfolded on
Linux while folding correctly on darwin.

Fix

Commit 1 (compiler): recognize # + optional whitespace + define, and retain
__-prefixed paste macros in the session. Emission already filters
leading-underscore names, so a retained __INT64_C folds callers without ever
being emitted.

Commit 2 (tests): two fixture assertions were pinned to darwin's exact output.
The migrated form is fixed by the host header text and is equally correct either
way — INTMAX_MIN (-INTMAX_MAX - 1 vs the literal inline) and the kept generic
helper's echoed parameter name (INTMAX_C(v) vs INTMAX_C(c)). Added
bs_assert_contains_either and accept both intended spellings — this loosens
the fixture to the set of correct results, not to accept a wrong one.

Verification

with build :c-migrator-core-tests green on a glibc host (rebuilds + self-hosts
stage2, migrates the fixture, and runs it): survey: all targets green.

The paste-suffix migrator (#945) folds INTMAX_C(9223372036854775807) and
friends to a suffixed literal by reading the token-paste suffix from the
macro session built out of the preprocessed #define lines. Two facts about
glibc's <stdint.h> defeated that on a glibc host:

  - glibc writes "#  define" (whitespace between # and define), which the
    define-line recognizer, keyed on a literal "#define", did not match; and
  - the width constants route through __-prefixed helpers -- INTMAX_MAX is
    __INT64_C(9223372036854775807), UINTMAX_MAX is __UINT64_C(...) -- which
    the session builder dropped outright on the leading-underscore rule.

Together those made __INT64_C/__INTMAX_C/__UINT64_C invisible to the folder,
so INTMAX_MAX/UINTMAX_MAX could not paste their suffix and stayed unfolded on
Linux while folding correctly on darwin.

Recognize # + optional whitespace + define, and retain __-prefixed paste
macros in the session. Emission already filters leading-underscore names, so
a retained __INT64_C folds callers without ever being emitted.
…xture

The paste-suffix migrator fixture runs against the host's real <stdint.h>, so
two of its assertions were pinned to darwin's exact output and failed on a
glibc host even though the migration was correct:

  - INTMAX_MIN: darwin references INTMAX_MAX (`-INTMAX_MAX - 1`) while glibc
    pastes the literal inline (`-9223372036854775807i64 - 1`); both fold to
    i64::MIN.
  - the kept generic helper echoes the header's own macro parameter name --
    darwin `INTMAX_C(v)`, glibc `INTMAX_C(c)` -- so the signature is
    `INTMAX_C[T](v: T)` vs `INTMAX_C[T](c: T)`, the same translation.

Add bs_assert_contains_either and accept both intended spellings. Each
alternative is a valid, correct output for its host -- this loosens the fixture
to the set of correct results, not to accept a wrong one.
@ropoctl
ropoctl force-pushed the fix-migrate-paste-suffix-glibc branch from dbccd4f to 9bf2327 Compare September 4, 2026 11:04
ehartford pushed a commit that referenced this pull request Sep 5, 2026
…untranslated, not a self-referential global

#997 (`#  define` recognition) makes glibc's builtin-valued float macros
reach the object-macro translator's bare-identifier path, where nothing
rejected a self-reference: `#define NAN (__builtin_nanf(""))` and
`HUGE_VALF (__builtin_huge_valf())` collapse to their own name and were
emitted as `let NAN = NAN` / `let HUGE_VALF = HUGE_VALF`, and
`#define INFINITY HUGE_VALF` as `let INFINITY = HUGE_VALF` naming one of
them — globals codegen cannot lower ("global 'NAN' has no initializer
codegen can lower"). test/spec/spec_ss16_ffi_and_c_import.w (a
c_import("math.h")) reds on the fresh compiler; the previous compiler
never saw these macros because their `#  define` lines were not
recognized. §16.2: a value With cannot express is recorded untranslated,
not emitted as a broken decl.

The bare-identifier emit now records the macro untranslated when its value
is its own name (self-reference) or names a macro already omitted; a
forward reference to a macro that will be emitted is untouched. Verified:
math.h now omits the three (0 errors), spec_ss16 passes, and inttypes.h
still emits its 42 PRI* lets with PRIdLEAST16 usable (the forward-reference
class the guard must not break).
@ehartford

Copy link
Copy Markdown
Contributor

Landed on main via cherry-pick under new hashes: 66c44de (fold paste-suffix macros through glibc's # define spelling) and 7526ee4 (accept both host stdint.h spellings in the fixture). A follow-up on top, 1f826ac, guards a c_import regression this change exposed (glibc's builtin-valued float macros — NAN/INFINITY/HUGE_VALF — were emitted as self-referential globals). Closing as merged-by-content; the PR branch's original SHAs differ because it was cherry-picked, not merged.

@ehartford ehartford closed this Sep 6, 2026
@ehartford
ehartford deleted the fix-migrate-paste-suffix-glibc branch September 24, 2026 16:56
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.

2 participants