Skip to content

Duplicate explicit imports resolve silently last-wins (no ambiguity error) #993

Closed

Description

@eas4ai

Classification

Silent name-resolution defect (audit finding IMPORT-001). Deterministic but fragile: reordering use lines changes which function is called with no diagnostic.

Source chain

Two explicit imports of the same name (use a.helper + use b.helper) pass with check silently; at runtime the LAST import wins (probe prints 2; .audit/probes/import_ambig/). Spec §18.2 defines precedence tiers but is silent on collisions within the explicit-import tier — while mandating a hard ambiguity error for the lower stdlib-fallback tier ('Two or more standard-library candidates for one name is a hard ambiguity error'). The language forbids local rebinding/shadowing yet silently shadows at import scope: the strictness is inverted across tiers.

Repair boundary

Either emit the same hard ambiguity error for duplicate explicit imports (with both candidates shown, consistent with the fallback tier), or bless last-wins explicitly in §18.2 with a same-file duplicate-import lint. The silent middle (no error, no spec text) is the defect.

Activity

  1. eas4ai commented on Sep 3, 2026

    @eas4ai
    ContributorAuthor

    Same defect class, wider scope: duplicate TYPE declarations are also silently last-wins (type Dup { v: i32 } then type Dup { w: i32 } checks clean; constructing with v fails as 'unknown field' — first decl invisible; probes .audit/probes/readonly_pass_20260903/duptype{,2}_probe.w). But duplicate FUNCTIONS are rejected ('function is already defined', probe dupfn_probe.w). So the duplicate check exists for one declaration-table path and is missing for types and imports alike — the fix is one rule applied uniformly, and the fn diagnostic is the template.

  2. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced (imports and types; functions are the control that works)

    Verdict. Established on current main. Two explicit imports of the same name from different modules pass with check and the later use line silently wins: swapping the two lines changes the program's output with no diagnostic. The follow-up comment's wider claim also holds: two type Dup declarations in one file check clean and the first becomes invisible (constructing with its field reports unknown field), while two fn f declarations are correctly rejected with function 'f' is already defined. So the duplicate-declaration rule exists for one table and is missing for the other two.

    Tested revision and compiler

    item value
    source main @ 5809ac1 (2026-09-14)
    compiler with build of 5809ac1 in a detached worktree, self-reports with v0.15.2.1
    platform macOS Darwin 25.6.0, arm64

    Probes

    a.w: pub fn helper() -> i32: 1; b.w: pub fn helper() -> i32: 2.

    program with check with run
    use a.helper then use b.helper; print(f"{helper()}") ok 2
    use b.helper then use a.helper; same body ok 1
    type Dup { v: i32 } then type Dup { w: i32 }; Dup { v: 1 } error: unknown field 'v' for type 'Dup' (first declaration silently replaced; no duplicate diagnostic) n/a
    control: fn f() -> i32: 1 then fn f() -> i32: 2 error: function 'f' is already defined at the second declaration n/a

    Source

    • Function duplicates are rejected at src/SemaDecl.w:1532 (is already defined) and :1803 (extern shadowing a fn). grep finds no counterpart diagnostic for imports (already imported, ambiguous import, duplicate import) anywhere in src/Sema*.w or src/Resolve.w.
    • Named types are registered by an unconditional named_types.insert(sym, tid) at src/Sema.w:2604; a second insert for the same symbol replaces the first, which is exactly the observed "first declaration invisible" behaviour.
    • No commit since 450733e adds either check.

    Spec

    • §18.2 (spec line 10337 onward) defines the precedence tiers and says of tier 5 that "two or more standard-library candidates for one name is a hard ambiguity error", but says nothing about two candidates within tier 3 (explicit imports). The report's characterisation that strictness is inverted across tiers is accurate.
    • The spec has no text at all on duplicate type declarations in one module (no "already defined", "redefinition", or "declared twice" anywhere), so the type case is unspecified rather than contradicted; the function diagnostic shows the intended rule.
    • §29.8 is cited by the spec itself as "the no-shadowing rule" (line 8657); silent last-wins at import and type scope is shadowing by another name.

    Related PRs

    None merged or open address import or type duplicate detection.

    Would I champion a fix? Yes, one rule applied uniformly.

    Acceptance criteria:

    1. Two explicit imports (or an import and an alias) binding the same unqualified name in one module produce a hard error naming both candidates, mirroring the tier-5 wording, unless they resolve to the same declaration (re-importing the same item twice can be a lint).
    2. A second type/enum/trait declaration with the same name in one module produces the same 'X' is already defined error the function path emits, anchored at the second declaration with a note at the first.
    3. test/compile_errors fixtures for both, and the two probe programs above become negative fixtures.
    4. §18.2 gains one sentence stating the within-tier ambiguity rule, authored by the maintainer.

    Untested

    Glob/module-level imports (use a.* style, if supported) and cross-file duplicates of the same type name in different modules were not probed; the latter is legitimately allowed by module scoping and is not the defect here.

  3. added 6 commits that reference this issue on Sep 26, 2026
    78755f1
    c4935f1
    05620f3
    3751d95
    f81daeb
    90fd33a
  4. added a commit that references this issue on Sep 27, 2026
    682b77a
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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions