diff --git a/compiler/modulegraphs.nim b/compiler/modulegraphs.nim index 8fb275d7a26f0..604d787228beb 100644 --- a/compiler/modulegraphs.nim +++ b/compiler/modulegraphs.nim @@ -106,11 +106,13 @@ type icOfferedInstances*: HashSet[string] # backend: NIF names of the generic # instances some loaded module `(offer)`s # (see `cgen.homeEmitsInstance`) - icQualIfaces*: IntSet # module positions whose interface tables were - # populated ONLY for qualified access through a - # module re-export (`import x; export x`); the - # Iface.module stays nil so a later direct - # import still takes the full load path + icQualModules: Table[int, seq[PSym]] # module positions whose interface + # tables were populated for qualified access + # through a module re-export (`import x; + # export x`), and the module symbol made for + # each name the module is re-exported as; the + # symbols are not registered, so a later + # direct import still takes the full load path vmInjecting*: bool # the VM injects destructors: hooks that # are missing are not lifted (that could # conflict with hooks declared later) @@ -1216,13 +1218,17 @@ when not defined(nimKochBootstrap): g.ifaces[fIdx.int].module.name.s == mname: # properly registered already (directly imported earlier): reuse it return g.ifaces[fIdx.int].module + g.icQualModules.withValue(fIdx.int, aliases): + for m in aliases[]: + if m.name.s == mname: return m + let firstAlias = not g.icQualModules.hasKey(fIdx.int) result = PSym(kindImpl: skModule, itemId: itemId(int32(fIdx), 0'i32), name: getIdent(g.cache, mname), infoImpl: newLineInfo(fIdx, 1, 1), positionImpl: int(fIdx)) setOwner(result, getPackage(g.config, g.cache, fIdx)) - if g.ifaces[fIdx.int].module == nil and - not g.icQualIfaces.containsOrIncl(fIdx.int): + g.icQualModules.mgetOrPut(fIdx.int, @[]).add result + if firstAlias and g.ifaces[fIdx.int].module == nil: var interf = initStrTable() var interfHidden = initStrTable() let precomp = loadNifModule(ast.program, ModuleSuffix(msuffix), diff --git a/tests/ic/mmodaliasas.nim b/tests/ic/mmodaliasas.nim new file mode 100644 index 0000000000000..1d0b7c12152a2 --- /dev/null +++ b/tests/ic/mmodaliasas.nim @@ -0,0 +1,3 @@ +# Helper for tmodalias.nim: re-exports mmodaliasbase as `aliased`. +import mmodaliasbase as aliased +export aliased diff --git a/tests/ic/mmodaliasbase.nim b/tests/ic/mmodaliasbase.nim new file mode 100644 index 0000000000000..662064a2b7bff --- /dev/null +++ b/tests/ic/mmodaliasbase.nim @@ -0,0 +1,2 @@ +# Helper for tmodalias.nim: the module re-exported under two names. +proc baseValue*(): int = 7 diff --git a/tests/ic/mmodaliasplain.nim b/tests/ic/mmodaliasplain.nim new file mode 100644 index 0000000000000..48b5a53fa0c70 --- /dev/null +++ b/tests/ic/mmodaliasplain.nim @@ -0,0 +1,3 @@ +# Helper for tmodalias.nim: re-exports mmodaliasbase under its own name. +import mmodaliasbase +export mmodaliasbase diff --git a/tests/ic/tmodalias.nim b/tests/ic/tmodalias.nim new file mode 100644 index 0000000000000..215658b788bd8 --- /dev/null +++ b/tests/ic/tmodalias.nim @@ -0,0 +1,11 @@ +discard """ +output: '''14''' +""" + +# A module re-exported under two names, `aliased` (mmodaliasas) and +# `mmodaliasbase` (mmodaliasplain), is a qualifier under both names here, where +# both re-exporters are loaded from their NIFs. The loader keeps one module +# symbol per name, not one per module. + +import mmodaliasas, mmodaliasplain +echo aliased.baseValue() + mmodaliasbase.baseValue()