Skip to content

compiler: importing multiple modules that share a common dependency causes shadowing #105

Closed

Description

@ehartford

Summary

When a program imports two modules that both `use std.re.defs`:

```with
use std.re.pcre2_compile
use std.re.pcre2_match
```

Both modules inline defs.w's constants (ucp_C, ucp_L, etc.). The compiler sees the same `let` binding twice and errors with "shadowing is not allowed".

Reproduction

```with
use std.re.defs
use std.re.pcre2_compile
use std.re.pcre2_match

fn main:
print("ok")
```

Produces 200+ "shadowing is not allowed" errors for every constant defined in defs.w.

Root cause

`use` performs textual inclusion. When module A and module B both `use std.re.defs`, importing both A and B into a program includes defs.w twice. The compiler treats each inclusion as a separate definition, causing shadowing.

Relation to #103

This is the diamond dependency problem — the immediate consequence of `use` being textual inclusion rather than symbol-level import.

Impact

Blocks writing any With program that uses the PCRE2 library, since the library's modules all share defs.w as a common dependency.

Workaround

Import only one module at a time, or create a single combined module file that includes everything needed.

Activity

  1. ehartford commented on Apr 10, 2026

    @ehartford
    ContributorAuthor

    Fixed on main in c26adb9 (Fix shared std.re import shadowing).

    Exact repro now passes:

    use std.re.defs
    use std.re.pcre2_compile
    use std.re.pcre2_match
    
    fn main:
        print("ok")
    

    Root fix:

    • scripts/pcre2_generated_workflow.sh now hoists duplicate shared immutable PCRE let constants into std.re.defs during generation
    • src/compiler/Frontend.w now drops later identical imported extern var / extern let declarations during import merge, matching the existing imported-fn dedup behavior

    Verified with:

    • WITH=./out/bin/with-stage2 ./scripts/run_cli_selfhost_tests.sh
    • WITH=./out/bin/with-stage2 make build
    • WITH=./out/bin/with-stage2 make selfcheck
    • WITH=./out/bin/with-stage2 make fixpoint

    make test still ends in the repo's existing async/channel/comptime failures and was not clean before this fix either.

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