diff --git a/engine/runtime/src/main/java/org/enso/compiler/TreeToIr.java b/engine/runtime/src/main/java/org/enso/compiler/TreeToIr.java index 1603fd55609..2ee1ea75235 100644 --- a/engine/runtime/src/main/java/org/enso/compiler/TreeToIr.java +++ b/engine/runtime/src/main/java/org/enso/compiler/TreeToIr.java @@ -21,7 +21,8 @@ import org.enso.compiler.core.IR$Error$Syntax$UnexpectedDeclarationInType$; import org.enso.compiler.core.IR$Error$Syntax$UnexpectedExpression$; import org.enso.compiler.core.IR$Error$Syntax$InvalidEscapeSequence$; import org.enso.compiler.core.IR$Error$Syntax$EmptyParentheses$; -import org.enso.compiler.core.IR$Error$Syntax$InvalidImport$; +import org.enso.compiler.core.IR$Error$Syntax$InvalidImport; +import org.enso.compiler.core.IR$Error$Syntax$InvalidExport; import org.enso.compiler.core.IR$Error$Syntax$Reason; import org.enso.compiler.core.IR$Error$Syntax$UnrecognizedToken$; import org.enso.compiler.core.IR$Error$Syntax$UnsupportedSyntax; @@ -70,6 +71,7 @@ import org.enso.syntax2.TextElement; import org.enso.syntax2.Token; import org.enso.syntax2.Tree; +import org.enso.syntax2.Tree.Invalid; import scala.Option; import scala.collection.immutable.LinearSeq; import scala.collection.immutable.List; @@ -1525,10 +1527,24 @@ final class TreeToIr { meta(), diag() ); } catch (SyntaxException err) { - return err.toError(IR$Error$Syntax$InvalidImport$.MODULE$); + if (err.where instanceof Invalid invalid) { + return err.toError(invalidImportReason(invalid.getError())); + } else { + return err.toError(invalidImportReason(null)); + } } } + private IR$Error$Syntax$Reason invalidImportReason(String msg) { + return new IR$Error$Syntax$InvalidImport( + Objects.requireNonNullElse(msg, "Imports must have a valid module path")); + } + + private IR$Error$Syntax$Reason invalidExportReason(String msg) { + return new IR$Error$Syntax$InvalidExport( + Objects.requireNonNullElse(msg, "Exports must have a valid module path")); + } + @SuppressWarnings("unchecked") private String extractPackageAndName(List qualifiedName, StringBuilder pkg) { String cls = null; @@ -1583,7 +1599,11 @@ final class TreeToIr { meta(), diag() ); } catch (SyntaxException err) { - return err.toError(IR$Error$Syntax$InvalidImport$.MODULE$); + if (err.where instanceof Invalid invalid) { + return err.toError(invalidExportReason(invalid.getError())); + } else { + return err.toError(invalidExportReason(null)); + } } } 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 2b43c7ff442..8a4cb1b1e79 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 @@ -7803,9 +7803,18 @@ object IR { s"Cannot define a pattern outside a pattern context" } - case object InvalidImport extends Reason { + case class InvalidImport( + message: String = "Imports must have a valid module path" + ) extends Reason { override def explanation: String = - s"Imports must have a valid module path" + s"Invalid Import: $message" + } + + case class InvalidExport( + message: String = "Exports must have a valid module path" + ) extends Reason { + override def explanation: String = + s"Invalid Export: $message" } case object InvalidStandaloneSignature extends Reason { diff --git a/engine/runtime/src/test/java/org/enso/compiler/ErrorCompilerTest.java b/engine/runtime/src/test/java/org/enso/compiler/ErrorCompilerTest.java index c63a6124f0e..d4c0fc2300b 100644 --- a/engine/runtime/src/test/java/org/enso/compiler/ErrorCompilerTest.java +++ b/engine/runtime/src/test/java/org/enso/compiler/ErrorCompilerTest.java @@ -1,10 +1,12 @@ package org.enso.compiler; +import java.util.Objects; import org.enso.compiler.core.IR; import org.enso.compiler.core.IR$Error$Syntax; import org.enso.compiler.core.IR$Error$Syntax$InvalidEscapeSequence$; import org.enso.compiler.core.IR$Error$Syntax$Reason; -import org.enso.compiler.core.IR$Error$Syntax$InvalidImport$; +import org.enso.compiler.core.IR$Error$Syntax$InvalidImport; +import org.enso.compiler.core.IR$Error$Syntax$InvalidExport; import org.enso.compiler.core.IR$Error$Syntax$UnexpectedExpression$; import org.enso.compiler.core.IR$Error$Syntax$UnrecognizedToken$; import org.enso.compiler.core.IR$Error$Syntax$UnsupportedSyntax; @@ -144,40 +146,49 @@ public class ErrorCompilerTest extends CompilerTest { assertSingleSyntaxError(ir, IR$Error$Syntax$UnexpectedExpression$.MODULE$, "Unexpected expression", 0, 13); } + + private IR$Error$Syntax$InvalidImport invalidImport(String msg) { + return new IR$Error$Syntax$InvalidImport(msg); + } + + private IR$Error$Syntax$InvalidExport invalidExport(String msg) { + return new IR$Error$Syntax$InvalidExport(msg); + } + @Test public void malformedImport3() throws Exception { var ir = parse("import Foo as Foo, Bar"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 14, 22); + assertSingleSyntaxError(ir, invalidImport("Expected identifier."), null, 14, 22); } @Test public void malformedImport4() throws Exception { var ir = parse("import Foo as Foo.Bar"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 14, 21); + assertSingleSyntaxError(ir, invalidImport("Expected identifier."), null, 14, 21); } @Test public void malformedImport5() throws Exception { var ir = parse("import Foo as"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 13, 13); + assertSingleSyntaxError(ir, invalidImport("Expected tokens."), null, 13, 13); } @Test public void malformedImport6() throws Exception { var ir = parse("import Foo as Bar.Baz"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 14, 21); + assertSingleSyntaxError(ir, invalidImport("Expected identifier."), null, 14, 21); } @Test public void malformedImport7() throws Exception { var ir = parse("import Foo hiding"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 7, 17); + assertSingleSyntaxError(ir, invalidImport("Expected qualified name."), null, 7, 17); } @Test public void malformedImport8() throws Exception { var ir = parse("import Foo hiding X,"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 7, 20); + assertSingleSyntaxError(ir, invalidImport("Malformed comma-delimited sequence."), null, 7, 20); } @Test @@ -203,25 +214,25 @@ public class ErrorCompilerTest extends CompilerTest { @Test public void malformedImport11() throws Exception { var ir = parse("from import all"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 4, 4); + assertSingleSyntaxError(ir, invalidImport("Expected tokens."), null, 4, 4); } @Test public void malformedImport12() throws Exception { var ir = parse("from Foo import all hiding"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 26, 26); + assertSingleSyntaxError(ir, invalidImport("Expected tokens."), null, 26, 26); } @Test public void malformedImport13() throws Exception { var ir = parse("from Foo import all hiding X.Y"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 27, 30); + assertSingleSyntaxError(ir, invalidImport("Expected identifier."), null, 27, 30); } @Test public void malformedImport14() throws Exception { var ir = parse("from Foo import Some.Nested.Module.Path"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 16, 39); + assertSingleSyntaxError(ir, invalidImport("Expected identifier."), null, 16, 39); } @Test @@ -239,55 +250,55 @@ public class ErrorCompilerTest extends CompilerTest { @Test public void malformedExport3() throws Exception { var ir = parse("export Foo as Foo, Bar"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 14, 22); + assertSingleSyntaxError(ir, invalidExport("Expected identifier."), null, 14, 22); } @Test public void malformedExport4() throws Exception { var ir = parse("export Foo as Foo.Bar"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 14, 21); + assertSingleSyntaxError(ir, invalidExport("Expected identifier."), null, 14, 21); } @Test public void malformedExport5() throws Exception { var ir = parse("export Foo as"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 13, 13); + assertSingleSyntaxError(ir, invalidExport("Expected tokens."), null, 13, 13); } @Test public void malformedExport6() throws Exception { var ir = parse("export Foo as Bar.Baz"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 14, 21); + assertSingleSyntaxError(ir, invalidExport("Expected identifier."), null, 14, 21); } @Test public void malformedExport7() throws Exception { var ir = parse("export Foo hiding"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 7, 17); + assertSingleSyntaxError(ir, invalidExport("Expected qualified name."), null, 7, 17); } @Test public void malformedExport8() throws Exception { var ir = parse("export Foo hiding X,"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 7, 20); + assertSingleSyntaxError(ir, invalidExport("Malformed comma-delimited sequence."), null, 7, 20); } @Test public void malformedExport9() throws Exception { var ir = parse("from export all"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 4, 4); + assertSingleSyntaxError(ir, invalidExport("Expected tokens."), null, 4, 4); } @Test public void malformedExport10() throws Exception { var ir = parse("from Foo export all hiding"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 26, 26); + assertSingleSyntaxError(ir, invalidExport("Expected tokens."), null, 26, 26); } @Test public void malformedExport11() throws Exception { var ir = parse("from Foo export all hiding X.Y"); - assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidImport$.MODULE$, "Imports must have a valid module path", 27, 30); + assertSingleSyntaxError(ir, invalidExport("Expected identifier."), null, 27, 30); } @Test @@ -367,7 +378,9 @@ public class ErrorCompilerTest extends CompilerTest { ) { var errors = assertIR(ir, IR$Error$Syntax.class, 1); assertEquals(type, errors.head().reason()); - assertEquals(msg, errors.head().message()); + if (msg != null) { + assertEquals(msg, errors.head().message()); + } assertEquals(new Location(start, end), errors.head().location().get().location()); }