Skip to content

Cross-package extension coherence (§11.4) #435

Closed

Description

@ehartford

Summary

Implement cross-package extension-method coherence so extension methods stay ergonomic without silently changing meaning when imports change. Inherent methods must win, ambiguous imported extensions must error, and fully qualified package syntax must disambiguate.

Spec

§11.4 says extend blocks may add methods to any type, but if two packages in scope extend the same type with the same method name, a call is ambiguous and must be rejected. Inherent methods never get shadowed by extensions. Extension methods are import-scoped candidates, and pkg.method(value) selects a package explicitly.

Current state

Trait impl orphan/duplicate checks exist for impl blocks: orphan rule at src/SemaDecl.w:1569-1575, duplicate impl at src/SemaDecl.w:1710-1717, and overlap at src/SemaDecl.w:1547-1556. Those do not cover extension-method packages.

Extension methods are currently collected into method lookup by qualified owner/method names (src/SemaDecl.w:1355-1360) and method calls primarily ask lookup_method_fn/lookup_method_sig for a single owner and method (src/SemaCheck.w:11173-11183). There is no visible import-scoped candidate set, ambiguity diagnostic, inherent-vs-extension priority, or pkg.method(value) disambiguation path described by §11.4.

Implementation plan

  1. Model extension methods separately from inherent methods in sema: record defining package/module, extended type symbol, method symbol, signature, source location, and visibility.
  2. Teach import resolution to produce the extension candidate set for the current module. Only imported packages/modules should contribute candidates.
  3. During method lookup, check inherent methods first and return them without considering extensions. If no inherent method exists, collect visible extensions for the receiver type and method name.
  4. If exactly one extension candidate exists, resolve to it. If more than one exists, emit ambiguous extension method '<name>'; candidates: pkg_a.<name>, pkg_b.<name>; use pkg.method(value).
  5. Parse/resolve pkg.method(value, ...) as a qualified extension call when pkg is an imported package/module and no ordinary value call applies.
  6. Ensure cross-package behavior is tested using fixture packages rather than same-file declarations only.

Test plan

Add a multi-file spec fixture under test/spec/packages/extension_coherence/ or equivalent harness coverage:

  • two packages extend the same foreign type with the same method
  • importing one package works
  • importing both and calling value.method() errors
  • pkg_a.method(value) and pkg_b.method(value) both work
  • an inherent method on the type wins over imported extensions
  • unimported package extension is not a candidate

Add compile-error cases for ambiguous extension and unknown qualified package method.

Acceptance criteria

  • Extension method lookup is import-scoped and deterministic.
  • Inherent methods always win over extensions.
  • Ambiguous imported extensions fail loudly with actionable candidates.
  • Fully qualified package syntax disambiguates.
  • Existing trait impl coherence diagnostics remain unchanged.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

#405 is needed for generic extend blocks but should not block non-generic extension coherence. #488/#533 should include coherence diagnostics for this behavior.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 11.4.1.3, 11.4.1.5, 11.4.1.6, 11.4.1.7.

Activity

  1. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    Implemented in 4c43327. Extension methods are now import-scoped candidates, inherent methods keep precedence, ambiguous imported extensions diagnose deterministically, and qualified package calls disambiguate through direct imports. Added positive and negative coverage for single import, inherent precedence, qualified disambiguation, ambiguous unqualified calls, unknown qualified methods, and the std span-collision regression. Verification passed: build, fixpoint, full test suite, and test-green.

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