Skip to content

Namespace-specified wildcards in import/export lists (#25901)

Vladislav Zavialov requested to merge wip/int-index/imp-exp-whole-namespace into master

This change adds support for top-level namespace-specified wildcards type .. and data .. to import and export lists (part of GHC Proposal #581 "Namespace-specified imports").

Examples:

import M (type ..) -- imports all type and class constructors from M
import M (data ..) -- imports all data constructors and terms from M
module M (type .., f) where
  -- exports all type and class constructors defined in M,
  -- plus the function 'f'

The primary intended usage of this feature is in combination with module aliases, allowing namespace disambiguation:

import Data.Proxy as T (type ..)  -- T.Proxy is unambiguously the type constructor
import Data.Proxy as D (data ..)  -- D.Proxy is unambiguously the data constructor

The patch accounts for the interactions of wildcards with:

  • Imports with hiding clauses
  • Import warnings -Wunused-imports, -Wdodgy-imports
  • Export warnings -Wduplicate-exports, -Wdodgy-exports

Summary of the changes:

  1. Move the NamespaceSpecifier type from GHC.Hs.Binds to GHC.Hs.Basic, making it possible to use it in more places in the AST.

  2. Extend the AST (type: IE) with a representation of .., type .., and data .. (constructor: IEWholeNamespace). Per the proposal, the plain .. is always rejected with a dedicated error message.

  3. Extend the grammar in Parser.y with productions for .., type .., and data .. in both import and export lists.

  4. Implement wildcard imports by updating the filterImports function in GHC.Rename.Names; the logic for IEWholeNamespace is roughly modeled after the Nothing (no explicit import list) case.

  5. Implement wildcard exports by updating the exports_from_avail function in GHC.Tc.Gen.Export; the logic for IEWholeNamespace is closely modeled after the IEModuleContents case.

  6. Refactor and extend diagnostics to report the new warnings and errors. See PsErrPlainWildcardImport, DodgyImportsWildcard, PsErrPlainWildcardExport, DodgyExportsWildcard, TcRnDupeWildcardExport.

Note that this patch is specifically about top-level import/export items. Subordinate import/export items are left unchanged.

Edited by Vladislav Zavialov

Merge request reports

Loading