Skip to content

compiler: use inlines definitions instead of importing symbols #103

Closed

Description

@ehartford

Summary

`use foo.bar` currently performs textual inclusion — all declarations from `bar.w` are parsed and re-emitted into the importing module's compilation unit. This means:

  • Every function in bar.w gets a copy in every importer's .o file
  • Every `let` constant gets a copy
  • Every `var` global gets a separate storage allocation

For single-file programs this works fine. For multi-module libraries (like PCRE2 with 32 .w files all doing `use std.re.defs`), it creates massive duplication and linkage conflicts.

Expected behavior

`use foo.bar` should make bar's symbols available to the importer without re-emitting their definitions. When compiling to .o files:

  • Imported functions should be declared as `extern` (resolved at link time)
  • Imported constants should be declared as `extern` (or inlined if small)
  • Imported `var` globals should be declared as `extern var` (reference only)

The actual definitions should exist only in bar.o.

Current impact

  • 32 PCRE2 modules each contain full copies of defs.w (type aliases, helper functions, preamble constants)
  • Functions like `is_alpha`, `is_digit`, `string_find_char` are emitted 32 times
  • Variables like `_pcre2_OP_lengths_8` get 32 separate internal copies
  • Required workaround: internal linkage for everything, losing cross-module sharing

Relation to #101

#101 describes the symptom (shared globals can't work). This issue is the root cause — `use` should import, not inline.

Design note

This is the same distinction as:

  • C: `#include` (textual) vs module import (C++20 modules)
  • Rust: `use` imports symbols from a separately-compiled crate
  • Go: `import` links against a compiled package

The With compiler already has the concept of modules (the resolver tracks module boundaries). The codegen just needs to emit `extern` declarations for imported symbols instead of re-compiling them.

Activity

  1. ehartford commented on Apr 10, 2026

    @ehartford
    ContributorAuthor

    Closing as fixed by 3a4a57c (Fix module object import ownership).

    I rechecked the behavior described here on current main with a minimal three-file repro (defs.w defining a let and helper fn, two importers using use defs). Current object emission behavior is:

    • defs.o defines the owner symbols
    • importer objects see those symbols as U
    • importer objects do not carry local copies of the imported top-level let or fn

    So the original observable bug in this issue no longer reproduces. The frontend still flattens imports internally, but after the #101 ownership fix that is now an implementation detail rather than the duplicate-definition/runtime bug described here.

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