Skip to content

Commit

Permalink
Better Module_Does_Not_Exist exception (#11713)
Browse files Browse the repository at this point in the history
The previous code was throwing `UnknownIdentifierException` when a module wasn't found. That's pretty misleading. Especially when Truffle infrastructure converts such exception to another one. Enso has a dedicated error when module cannot be found. Let's use that one.
  • Loading branch information
JaroslavTulach authored Nov 29, 2024
1 parent 85c8f76 commit 52feef8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.enso.interpreter.runtime;

import static org.enso.scala.wrapper.ScalaConversions.nil;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
Expand All @@ -17,6 +20,7 @@
import org.enso.pkg.QualifiedName;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.PolyglotException;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.io.IOAccess;
import org.junit.After;
Expand Down Expand Up @@ -52,6 +56,17 @@ public void cleanup() {
this.ctx = null;
}

@Test
public void noSuchModuleError() {
var b = ctx.getBindings(LanguageInfo.ID);
try {
var r = b.invokeMember(MethodNames.TopScope.GET_MODULE, "Does.Not.Exist.Module");
fail("Expecting failure, but got: " + r);
} catch (PolyglotException ex) {
assertThat(ex.getMessage(), containsString("Module_Does_Not_Exist"));
}
}

@Test
public void moduleKeepsFileRefAfterSourceUnset() {
var name = QualifiedName.simpleName("local.Unnamed_1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ private static void reportSlowContextAccess(Node n) {
with root nodes: {r}
"""
.replace("{n}", "" + n)
.replace("{s}", "" + n.getEncapsulatingSourceSection())
.replace("{r}", "" + n.getRootNode()));
.replace("{s}", "" + (n != null ? n.getEncapsulatingSourceSection() : null))
.replace("{r}", "" + (n != null ? n.getRootNode() : null)));
ex.printStackTrace();
checkUntil = System.currentTimeMillis() + 10000;
var assertsOn = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.TruffleFile;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.ArityException;
import com.oracle.truffle.api.interop.InteropLibrary;
Expand All @@ -10,6 +11,7 @@
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.nodes.Node;
import java.io.File;
import java.util.Collection;
import java.util.Optional;
Expand All @@ -22,6 +24,7 @@
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.data.EnsoObject;
import org.enso.interpreter.runtime.data.vector.ArrayLikeHelpers;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.type.Types;
import org.enso.pkg.Package;
import org.enso.pkg.QualifiedName;
Expand Down Expand Up @@ -130,7 +133,8 @@ private static Module getModule(TopLevelScope scope, Object[] arguments)

var module = scope.getModule(moduleName);
if (module.isEmpty()) {
throw UnknownIdentifierException.create(moduleName);
throw new PanicException(
scope.builtins.error().makeModuleDoesNotExistError(moduleName), null);
}

return module.get();
Expand Down Expand Up @@ -194,21 +198,23 @@ private static Object compile(Object[] arguments, EnsoContext context)
}

@Specialization
static Object doInvoke(TopLevelScope scope, String member, Object[] arguments)
static Object doInvoke(
TopLevelScope scope, String member, Object[] arguments, @Bind("$node") Node node)
throws UnknownIdentifierException, ArityException, UnsupportedTypeException {
var ctx = EnsoContext.get(node);
switch (member) {
case MethodNames.TopScope.GET_MODULE:
return getModule(scope, arguments);
case MethodNames.TopScope.CREATE_MODULE:
return createModule(scope, arguments, EnsoContext.get(null));
return createModule(scope, arguments, ctx);
case MethodNames.TopScope.REGISTER_MODULE:
return registerModule(scope, arguments, EnsoContext.get(null));
return registerModule(scope, arguments, ctx);
case MethodNames.TopScope.UNREGISTER_MODULE:
return unregisterModule(scope, arguments, EnsoContext.get(null));
return unregisterModule(scope, arguments, ctx);
case MethodNames.TopScope.LEAK_CONTEXT:
return leakContext(EnsoContext.get(null));
return leakContext(ctx);
case MethodNames.TopScope.COMPILE:
return compile(arguments, EnsoContext.get(null));
return compile(arguments, ctx);
default:
throw UnknownIdentifierException.create(member);
}
Expand Down

0 comments on commit 52feef8

Please sign in to comment.