On this page

0279. Module aliases and qualified names

Context

Today import a.b { f, T } binds selected public items as bare names, and f as local renames one of them. Larger apps (TUI widgets, an HTTP layer and the agent stack in one root) collide on short names such as render, Style and Event. The only fix is to alias item by item. A module that is loaded only for its impls (a coherence bridge) needs a dummy item import, because IDIOMS says "there is no empty import bridge {}". ADR-0064 already rewrites module items to dotted internal names (tui.widgets.button) before resolution, so a qualified use site only needs a resolver mapping. In compact source an import is I [segments] [name Opt alias], and both lists must be nonempty.

Decision

Whole-module import. Familiar import tui.widgets binds the module name widgets, the last segment as in Go. import tui.widgets as w binds w. as stays contextual. The compact form reuses I with a zero-length item list followed by one Opt identifier: I2;3:tui7:widgets0;~ or I2;3:tui7:widgets0;+1:w. A zero-length list is invalid today, so existing files keep the same bytes. An alias equal to the last segment is not canonical: E0125 with an edit to ~.

One form per module per file. A file imports a given module either selectively or whole, never both: E0229 (free today; allocate at implementation). Importing the same whole module twice is E0224.

Qualified names are exactly alias.item, one level deep. Modules do not nest.

Resolution (modules.rs only). alias.item resolves through the alias's ModuleId to the existing dotted internal name. A missing item is E0222, and a private item is E0223. Newtype constructors and .value stay file-private (ADR-0069). Module names live in file scope. A clash with an item, an imported name or another alias is E0224. A local or parameter spelled like an alias is rejected by the no-shadowing rule (ADR-0013). That is what makes w.f() unambiguous.

Coherence and methods. Impls stay global (ADR-0027/0069). An alias neither scopes nor hides an impl. For method lookup, a whole-module import counts every pub interface of that module as imported in the file. Existing ambiguity diagnostics apply. A whole-module import with no qualified uses still loads the module, which replaces the dummy bridge import.

Generated companions (ADR-0080). A companion sees its host's module names. An import edge whose alias is used only in derive heads is generator-only.

Tools (ADR-0049).

Formatting. Imports keep source order, as today. fmt never converts between selective and whole forms.

LANGUAGE.md. Replace "No module aliases or qualified paths." (+≈30 tokens; spec-keeper trims ≈30) with:

import a.b or import a.b as m binds module b/m (no items) for qualified m.f(x), m.T, m.E.v; one import form per module per file.

IDIOMS line 128: the bridge idiom becomes import bridge.

Prior art

Alternatives considered

Consequences

Implementation slices

  1. Compact: zero-item I with an optional alias, qualified frames in name slots, the M→O canonical check, and the encoder. Adds ImportDecl.module_alias: Option<Ident>, with reader unit tests. Areas: compact.rs, compact/{reader,expr,encode}.rs, ast.rs, PRIMITIVE_GRAMMAR.md. Frozen files: no. Agent: Claude Sonnet.
  2. Resolver: alias binding, qualified rewrite to dotted internal names, E0222/E0223/E0224/E0229, interface lookup set, and generator-only edges. Areas: modules.rs, resolve.rs, types.rs (per-file interfaces), gen.rs. Frozen files: no. Agent: Claude Sonnet, with Opus review.
  3. Tooling: outline.rs, references.rs (rename), compact_tools.rs primer, and item_hashes.rs if import rows hash. Frozen files: no. Agent: Claude Sonnet.
  4. Docs: LANGUAGE.md, IDIOMS.md, diagnostics.md (E0229 and E0224 text), and a teach-core.tsv card. Frozen files: no. Agent: Gemini (agy Argon Low).

Open questions (resolved)

Resolved by maintainer on 2026-11-22: every question takes its stated default.

  1. Should the default name be the last segment, with optional as? Default: yes (Go-style).
  2. Should mixing selective and whole imports of one module be forbidden? Default: yes (E0229).
  3. Should a whole-module import expose its pub interfaces for method lookup? Default: yes. Otherwise users need a selective import as well, which would conflict with question 2.
  4. Should a package import import a (selecting a.lib) bind a rather than lib? Default: a, the written segment.
  5. Should a qualified frame be allowed in an O callee for plugin modules (import cli)? Default: yes. Plugins are ordinary modules (ADR-0006).