From d6a9d424b4a7f7a3f7d43f3b99fad970721a308c Mon Sep 17 00:00:00 2001 From: Hubert Plociniczak Date: Thu, 28 Jul 2022 17:29:32 +0200 Subject: [PATCH] documentation and minor code cleanups --- .../java/org/enso/interpreter/runtime/Module.java | 13 +++++++++++-- .../main/scala/org/enso/compiler/Compiler.scala | 15 ++++++++++----- .../org/enso/compiler/PackageRepository.scala | 5 ++--- .../main/scala/org/enso/compiler/core/IR.scala | 8 ++++++++ .../org/enso/compiler/data/BindingsMap.scala | 3 +-- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/engine/runtime/src/main/java/org/enso/interpreter/runtime/Module.java b/engine/runtime/src/main/java/org/enso/interpreter/runtime/Module.java index 5fb6e14ed9fb1..c11ccbdb71114 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/runtime/Module.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/runtime/Module.java @@ -187,6 +187,15 @@ public static Module empty(QualifiedName name, Package pkg) { return new Module(name, pkg, false, null); } + /** + * Creates a virtual module which only purpose is to export symbols of other modules. + * + * @param name the qualified name of the newly created module. + * @param pkg the package this module belongs to. May be {@code null}, if the module does not + * belong to a package. + * @param source source of the module declaring exports of the desired modules + * @return the virtual module + */ public static Module virtual(QualifiedName name, Package pkg, Rope source) { return new Module(name, pkg, true, source); } @@ -228,9 +237,9 @@ public void setDirectVirtualModulesRefs(List names) { } /** - * Return a list of directly referencing virtual modules of this one, if any. + * Return a list of directly referencing submodules of this one, if any. * - * @return a non-null, possibly empty, array of fully qualified names of modules + * @return a non-null, possibly empty, list of fully qualified names of modules */ public List getDirectVirtualModulesRefs() { if (directVirtualModulesRefs == null) { diff --git a/engine/runtime/src/main/scala/org/enso/compiler/Compiler.scala b/engine/runtime/src/main/scala/org/enso/compiler/Compiler.scala index 1143da643b32e..375b1c63a0a9c 100644 --- a/engine/runtime/src/main/scala/org/enso/compiler/Compiler.scala +++ b/engine/runtime/src/main/scala/org/enso/compiler/Compiler.scala @@ -424,7 +424,10 @@ class Compiler( val parsedAST = parse(module.getSource) val expr = generateIR(parsedAST) val exprWithModuleExports = - injectVirtualModuleExports(expr, module.getDirectVirtualModulesRefs) + if (module.isVirtual) + expr + else + injectVirtualModuleExports(expr, module.getDirectVirtualModulesRefs) val discoveredModule = recognizeBindings(exprWithModuleExports, moduleContext) module.unsafeSetIr(discoveredModule) @@ -586,7 +589,7 @@ class Compiler( * ```` * * @param ir IR to be enhanced - * @param modules fully qualified names pf modules + * @param modules fully qualified names of modules * @return enhanced */ private def injectVirtualModuleExports( @@ -596,9 +599,11 @@ class Compiler( import scala.jdk.CollectionConverters._ val moduleNames = modules.asScala.map { q => - val name = q.path.map( - IR.Name.Literal(_, isMethod = false, location = None) - ) ++ List(IR.Name.Literal(q.item, isMethod = false, location = None)) + val name = q.path.foldRight( + List(IR.Name.Literal(q.item, isMethod = false, location = None)) + ) { case (part, acc) => + IR.Name.Literal(part, isMethod = false, location = None) :: acc + } IR.Name.Qualified(name, location = None) }.toList ir.copy( diff --git a/engine/runtime/src/main/scala/org/enso/compiler/PackageRepository.scala b/engine/runtime/src/main/scala/org/enso/compiler/PackageRepository.scala index 2ef487ed17d7b..6c5a91729e08a 100644 --- a/engine/runtime/src/main/scala/org/enso/compiler/PackageRepository.scala +++ b/engine/runtime/src/main/scala/org/enso/compiler/PackageRepository.scala @@ -556,9 +556,8 @@ object PackageRepository { loadedModules.put(virtualModule.getName.toString, virtualModule) } else { val loaded = loadedModules(virtualModule.getName.toString) - if (!loaded.isVirtual) { - loaded.setDirectVirtualModulesRefs(refs.asJava) - } + assert(!loaded.isVirtual) + loaded.setDirectVirtualModulesRefs(refs.asJava) } } diff --git a/engine/runtime/src/main/scala/org/enso/compiler/core/IR.scala b/engine/runtime/src/main/scala/org/enso/compiler/core/IR.scala index 4f42496605215..20be07fbf197f 100644 --- a/engine/runtime/src/main/scala/org/enso/compiler/core/IR.scala +++ b/engine/runtime/src/main/scala/org/enso/compiler/core/IR.scala @@ -6493,6 +6493,14 @@ object IR { Array(shadowedName, shadower) } + /** A warning that a submodule is being shadowed by the type of the same name + * therefore preventing the user from accessing the module via a qualified name. + * + * @param typename the type name shadowing the module + * @param moduleName the module being shadowed + * @param shadower the expression shadowing `moduleName` + * @param location the location at which the shadowing takes place + */ sealed case class VirtualModule( typeName: String, moduleName: IR.Name.Qualified, diff --git a/engine/runtime/src/main/scala/org/enso/compiler/data/BindingsMap.scala b/engine/runtime/src/main/scala/org/enso/compiler/data/BindingsMap.scala index f5921b42fbf72..e45eeab1f2cb7 100644 --- a/engine/runtime/src/main/scala/org/enso/compiler/data/BindingsMap.scala +++ b/engine/runtime/src/main/scala/org/enso/compiler/data/BindingsMap.scala @@ -211,8 +211,7 @@ case class BindingsMap( candidates.distinct match { case List() => Left(ResolutionNotFound) case List(it) => Right(it) - case items => - Left(ResolutionAmbiguous(items)) + case items => Left(ResolutionAmbiguous(items)) } }