From 0659d0cba9c0adf5df3b61a3565ed270731bcdda Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 17 Jan 2019 19:14:30 +0100 Subject: [PATCH] JS: do not use getBuiltInsModule to compute module name The intention of this code was to use the name "kotlin" for the builtins module, in case we're using builtins loaded from the compiler class loader (whose module is created in `KotlinBuiltIns.createBuiltInsModule`). However, `getBuiltIns().getBuiltInsModule()` may refer not necessarily to the builtins module, but also to any valid module where the builtins are visible from, therefore its name would be computed incorrectly. Use the module name instead to determine if it's a synthetic builtins module created in `createBuiltInsModule` --- .../js/translate/context/StaticContext.java | 17 ++--------------- .../js/translate/utils/JsDescriptorUtils.java | 11 ++++++++--- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java index a41a2f587e9..86f62b87d75 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java @@ -711,24 +711,13 @@ public final class StaticContext { if (currentModule == module) { return currentModuleAsImported; } - String moduleName = suggestModuleName(module); + String moduleName = JsDescriptorUtils.getModuleName(module); if (UNKNOWN_EXTERNAL_MODULE_NAME.equals(moduleName)) return null; return getImportedModule(moduleName, null); } - @NotNull - private static String suggestModuleName(@NotNull ModuleDescriptor module) { - if (module == module.getBuiltIns().getBuiltInsModule()) { - return Namer.KOTLIN_LOWER_NAME; - } - else { - String moduleName = module.getName().asString(); - return moduleName.substring(1, moduleName.length() - 1); - } - } - @NotNull public JsImportedModule getImportedModule(@NotNull String baseName, @Nullable DeclarationDescriptor descriptor) { String plainName = descriptor != null && config.getModuleKind() == ModuleKind.UMD ? getPlainId(descriptor) : null; @@ -835,9 +824,7 @@ public final class StaticContext { @Nullable public JsExpression exportModuleForInline(@NotNull ModuleDescriptor declaration) { - if (getCurrentModule().getBuiltIns().getBuiltInsModule() == declaration) return null; - - String moduleName = suggestModuleName(declaration); + String moduleName = JsDescriptorUtils.getModuleName(declaration); if (moduleName.equals(Namer.KOTLIN_LOWER_NAME)) return null; JsImportedModule importedModule = getJsImportedModule(declaration); diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/JsDescriptorUtils.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/JsDescriptorUtils.java index 8e11fc61653..a86d3fa2772 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/JsDescriptorUtils.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/JsDescriptorUtils.java @@ -149,11 +149,16 @@ public final class JsDescriptorUtils { @NotNull public static String getModuleName(@NotNull DeclarationDescriptor descriptor) { - ModuleDescriptor moduleDescriptor = DescriptorUtils.getContainingModule(findRealInlineDeclaration(descriptor)); - if (DescriptorUtils.getContainingModule(descriptor) == moduleDescriptor.getBuiltIns().getBuiltInsModule()) { + return getModuleName(DescriptorUtils.getContainingModule(findRealInlineDeclaration(descriptor))); + } + + @NotNull + public static String getModuleName(@NotNull ModuleDescriptor module) { + if (module.getName().equals(KotlinBuiltIns.BUILTINS_MODULE_NAME)) { return Namer.KOTLIN_LOWER_NAME; } - String moduleName = moduleDescriptor.getName().asString(); + + String moduleName = module.getName().asString(); return moduleName.substring(1, moduleName.length() - 1); }