From e279793d951317da36d2728315acb63f0afd4661 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 22 Feb 2024 16:20:01 +0100 Subject: [PATCH] Yield Type_Error on wrong self argument --- .../interpreter/node/callable/InvokeMethodNode.java | 11 +++++++++++ test/Base_Tests/src/Data/Polyglot_Spec.enso | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/engine/runtime/src/main/java/org/enso/interpreter/node/callable/InvokeMethodNode.java b/engine/runtime/src/main/java/org/enso/interpreter/node/callable/InvokeMethodNode.java index ce7a10e938..fd58bbfa17 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/node/callable/InvokeMethodNode.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/node/callable/InvokeMethodNode.java @@ -850,6 +850,17 @@ public abstract class InvokeMethodNode extends BaseNode { instanceof Function function) { if (function.getSchema().getArgumentsCount() == arguments.length - 1 && "self".equals(function.getSchema().getArgumentInfos()[0].getName())) { + var ok = false; + try { + if (interop.isMetaInstance(arguments[0], arguments[1])) { + ok = true; + } + } catch (UnsupportedMessageException ex) { + } + if (!ok) { + var err = ctx.getBuiltins().error().makeTypeError(arguments[0], arguments[1], "self"); + throw new PanicException(err, this); + } var lessArgs = Arrays.copyOfRange(arguments, 1, arguments.length); var lessSchema = Arrays.copyOfRange( diff --git a/test/Base_Tests/src/Data/Polyglot_Spec.enso b/test/Base_Tests/src/Data/Polyglot_Spec.enso index 2bc009ab04..54b7b3cb7a 100644 --- a/test/Base_Tests/src/Data/Polyglot_Spec.enso +++ b/test/Base_Tests/src/Data/Polyglot_Spec.enso @@ -1,4 +1,5 @@ from Standard.Base import all +import Standard.Base.Errors.Common.Type_Error from Standard.Test import all from project.Data.Polyglot_Extensions import all @@ -52,6 +53,9 @@ add_specs suite_builder = suite_builder.group "Polyglot" group_builder-> v = IntHolder.value_plus hold 3 v . should_equal 45 + group_builder.specify "extension method (static syntax) checks self type" <| + Test.expect_panic Type_Error (IntHolder.value_plus 10 12) + group_builder.specify "static extension method on IntHolder" <| hold = IntHolder.create (6 * 7) hold.value . should_equal 42