Skip to content

migrate: stop emitting pub extern fn for libm names that are now compiler builtins #1153

Closed

Description

@ehartford

src/CiMigrate.w (around line 642) writes a fixed prelude of libm declarations into every migrated module:

extern fn sqrt(x: f64) -> f64
extern fn pow(base: f64, exp: f64) -> f64
extern fn floor(x: f64) -> f64
... sin, cos, tan, log, log10, exp, fabs, fmod, asin, acos, atan, atan2

and the checked-in corpora carry them as pub extern fn (lib/std/re/defs.w:57, lib/std/zl/defs.w:57, lib/std/c_algorithms/defs.w:57, lib/std/tommyds/defs.w:57).

Two things changed under them:

  1. These names are now compiler builtins (src/MathBuiltins.w, D42): cos(x) and x.cos() are width-generic over f32/f64 and lower to the LLVM intrinsic or the width-correct libm symbol. The extern decls are redundant with the builtin.
  2. Per D29 (symbol_visible_from_current, src/Sema.w:2732), every extern fn is globally visible, ungated. So the migrator's extern fn cos(x: f64) is ambient in every program — including a plain user program that imports nothing — because std.re is in the prelude closure (regex literals).

Before the builtin-precedence rule landed, that meant a user's cos(0.0f32) resolved to the corpus extern and returned f64, silently widening the argument:

$ with analyze cos_only.w 'explain:call:cos'
fact mir call ... lib/std/re/defs.w cos sig=106 ... intrinsic=0

check_call now makes a math builtin outrank an extern declaration of its own name (a real, non-extern definition still shadows the builtin), so user code is correct. The remaining cleanup is at the source:

  • CiMigrate.w should not emit an extern decl for any name in src/MathBuiltins.w. Inside migrated C the call cos(double) resolves to the builtin and lowers to the identical symbol/intrinsic, so behavior is unchanged.
  • The four corpora then get re-migrated (never hand-edited) so their defs.w drop the decls.

Not blocking: user code is already correct via precedence; this removes the redundant, globally-leaking declarations and the dead branch of resolution.

Acceptance

  • grep -n 'extern fn cos' src/CiMigrate.w -> none for MathBuiltins names.
  • After re-migration, grep -rn 'pub extern fn cos' lib/std -> none.
  • with analyze cos_only.w 'select:stage=mir,kind=call,name~cos' shows intrinsic=<MATH_FN> with no sig from a corpus.
  • Corpus test suites still green.

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced. The migrator still writes the libm prelude (src/CiMigrate.w:787-789: extern fn sqrt, extern fn pow, extern fn floor, …), and all six promoted engine corpora carry it in their defs.w. These names are width-generic builtins since D42 (0a0ff13, #1154).

    post-release: the output compiles and behaves; per D112 the corpora change only at their next re-promotion.

  2. added this to the post-v0.16.0 milestone on Oct 28, 2026
  3. added a commit that references this issue on Nov 11, 2026
  4. ehartford commented on Nov 11, 2026

    @ehartford
    ContributorAuthor

    Fixed in #3257: the migrator's preamble no longer declares extern fn for the 17 names that are math builtins; a migrated call of one is the builtin (the c99_math case asserts the output has no extern fn sqrt/cos). The promoted corpora keep their lines until their next promotion: a migrator change never re-promotes a corpus (D112), so the issue's re-migration step is not done by this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions