roc icon indicating copy to clipboard operation
roc copied to clipboard

Report an error for exposing a type module's own name

Open lukewilliamboswell opened this issue 2 months ago • 8 comments

Summary

Importing a type module brings its main type into scope automatically:

import pf.Elem exposing [foo, bar]
# Elem, foo, and bar are all in scope

Writing import pf.Elem exposing [Elem] is common, especially in generated code. This issue originally proposed treating it as a redundant-expose warning with a formatter autofix. Discussion on Zulip (#ideas > Make import pf.Elem exposing [Elem] a warning) changed direction, so it should be a compile error instead.

Design direction

Items in an exposing list are always looked up inside the imported module. exposing [foo] means Elem.foo. By the same rule, exposing [Elem] means Elem.Elem, not "the main type again."

Nested types may share their parent's name. Elem.Elem is silly but allowed. So the same spelling can refer to a different type, and we shouldn't quietly treat it as a no-op.

For import M exposing [X] where M is a type module and X has the same name as the import:

Case Result
M.X does not exist Error. Elem is already in scope because importing a type module exposes its main type automatically. Remove it from exposing.
M.X exists (a nested type) Error. It clashes with the automatically exposed main type. Rename it with exposing [Elem as Inner].
exposing [Elem as E] A normal lookup of Elem.Elem. It is never read as the main type.
exposing [foo, bar] No change.
Elem.* No change.
Non-type (deprecated regular) modules No change. exposing [Elem] is an ordinary lookup there.

Current behavior to change

In scopeIntroduceModuleAliasAt (src/canonicalize/Can.zig), an exposed item with the same local name as the import becomes "the binding that decides what the name denotes." It replaces the automatic main-type binding without any diagnostic. Once nested types with the same name are possible, that can silently switch Elem to a different type. Remove that override so this case goes through the error path above.

Diagnostics

  • The existing redundant_expose_main_type diagnostic (Diagnostic.zig, report in ModuleEnv.zig) has no producer. Reuse it with error severity, or rename it (e.g. expose_import_main_type). Keep the explanation that type-module imports expose their main type automatically, and tell the user to delete the item.
  • Highlight only the offending exposed item.
  • The same-name nested case should reuse the existing shadowing/conflict diagnostic if it fits, pointing at both the import and the exposed item.
  • Detection must use resolved identities: the item's local name equals the name bound automatically by a type-module import. Don't rely on string or path conventions.

Formatter

No autofix. The formatter has no name resolution, so it can't tell "redundant" apart from "exposes a nested type with the same name." Removing the item could change what the program means. The error message says how to fix it.

To verify first

  • Can a nested type with the same name as its parent (Elem.Elem) be declared and exposed today? Do module exports keep it separate from the main type? If not, the second table row is future-proofing for now, but the lookup rule should still be written that way.

Tests / exit criteria

  • import pf.Elem exposing [Elem] produces exactly one error on the Elem item, for both local and package-qualified imports.
  • exposing [Elem, foo] produces one error, and foo still resolves.
  • exposing [foo, bar] produces no diagnostic, and Elem is still usable.
  • A nested Elem.Elem exposed as Elem produces a conflict error. Exposed as Elem as Inner, it works.
  • Non-type modules, Elem.*, and as aliases get no false error.
  • Existing Roc sources, examples, and fixtures that use this spelling are updated.
  • Focused canonicalization and CLI tests pass, and zig build minici passes.

lukewilliamboswell avatar Sep 10 '26 03:09 lukewilliamboswell

Hi! I'd like to work on this. Will investigate and post updates as i go

danielle060821 avatar Sep 12 '26 19:09 danielle060821

Actually theres a zulip discussion and I think we might want to clarify if this should be an error instead?

Would you mind commenting on #ideas > Make `import pf.Elem exposing [Elem]` a warning @ 💬

lukewilliamboswell avatar Sep 12 '26 20:09 lukewilliamboswell

Oh thanks for clarifying! I'll check the discussion then

danielle060821 avatar Sep 12 '26 22:09 danielle060821

@danielle060821 I've updated this Issue -- would you still like to take this on?

lukewilliamboswell avatar Sep 25 '26 05:09 lukewilliamboswell

@danielle060821 I've updated this Issue -- would you still like to take this on?

yes pls! Thanks for letting me know!

danielle060821 avatar Sep 25 '26 14:09 danielle060821

Hi! I noticed this issue has now been assigned and linked to a PR that will close it. Just checking whether there’s still anything here that would be useful for me to work on, or if I should pick up another issue instead. Thanks!

danielle060821 avatar Oct 02 '26 00:10 danielle060821

Oops, sorry! I saw this was unassigned and didn't read the comments, already implemented a fix. 😅

rtfeldman avatar Oct 02 '26 02:10 rtfeldman

Oops, sorry! I saw this was unassigned and didn't read the comments, already implemented a fix. 😅

no worries!

danielle060821 avatar Oct 02 '26 03:10 danielle060821