Skip to content

Commit 8b3723a

Browse files
authored
Remove builtin methods with two self arguments (#14234)
Builtin methods do not accept two self arguments. Remove boolean field from IR node. Decrease expected IR cache sizes.
1 parent 20827c0 commit 8b3723a

File tree

12 files changed

+32
-153
lines changed

12 files changed

+32
-153
lines changed

engine/runtime-compiler/src/main/java/org/enso/compiler/docs/DocsGenerate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public static void visitModule(
122122
}
123123
for (var mb : moduleBindings) {
124124
if (mb instanceof Method.Explicit m) {
125-
if (m.isStaticWrapperForInstanceMethod() || m.isPrivate()) {
125+
if (m.isPrivate()) {
126126
alreadyDispatched.put(m, m);
127127
continue;
128128
}
@@ -139,7 +139,7 @@ public static void visitModule(
139139
}
140140
}
141141
case Method.Explicit m -> {
142-
if (!m.isPrivate() && !m.isStaticWrapperForInstanceMethod()) {
142+
if (!m.isPrivate()) {
143143
dispatch.dispatchMethod(null, m);
144144
}
145145
}

engine/runtime-compiler/src/main/java/org/enso/compiler/pass/analyse/types/TypeInferenceSignatures.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ public Module runModule(Module ir, ModuleContext moduleContext) {
8888
(def) ->
8989
switch (def) {
9090
case Method.Explicit b -> {
91-
boolean keepSelfArgument = b.isStaticWrapperForInstanceMethod();
92-
TypeRepresentation resolvedType =
93-
resolveTopLevelTypeSignature(b.body(), keepSelfArgument);
91+
TypeRepresentation resolvedType = resolveTopLevelTypeSignature(b.body());
9492
if (resolvedType != null) {
9593
b.passData().update(INSTANCE, new InferredType(resolvedType));
9694
}
@@ -115,13 +113,8 @@ public Expression runExpression(Expression ir, InlineContext inlineContext) {
115113
* Constructs the type signature for a given method body.
116114
*
117115
* @param body the method body
118-
* @param keepSelfArgument whether to keep the self argument. For regular static methods, the self
119-
* argument is synthetic and does not take part in type inference. For member methods, it is
120-
* provided implicitly when resolving the call, so again it is ignored for type inference. It
121-
* should only be kept for such static methods that are wrappers for instance methods.
122116
*/
123-
private TypeRepresentation resolveTopLevelTypeSignature(
124-
Expression body, boolean keepSelfArgument) {
117+
private TypeRepresentation resolveTopLevelTypeSignature(Expression body) {
125118
return switch (body) {
126119
// Combine argument types with ascribed type (if available) for a function type signature
127120
case Function.Lambda lambda -> {
@@ -135,21 +128,7 @@ private TypeRepresentation resolveTopLevelTypeSignature(
135128
scala.collection.immutable.List<TypeRepresentation> argTypesScala =
136129
lambda
137130
.arguments()
138-
// Filter out the self argument, unless it should be kept.
139-
.filter(
140-
(arg) -> {
141-
if (arg.name() instanceof Name.Self selfArg) {
142-
if (selfArg.synthetic()) {
143-
// The 'synthetic' self is always dropped.
144-
return false;
145-
} else {
146-
return keepSelfArgument;
147-
}
148-
} else {
149-
// We keep all other args.
150-
return true;
151-
}
152-
})
131+
.filter((arg) -> !(arg.name() instanceof Name.Self))
153132
.map(
154133
(arg) -> {
155134
if (arg.ascribedType().isDefined()) {

engine/runtime-integration-tests/src/test/java/org/enso/compiler/dump/test/BindingSorterTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ private static Method.Explicit method(
188188
.bodyReference(bodyRef)
189189
.isStatic(isStatic)
190190
.isPrivate(isPrivate)
191-
.isStaticWrapperForInstanceMethod(false)
192191
.build();
193192
}
194193

engine/runtime-parser/src/main/java/org/enso/compiler/core/ir/Function.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,6 @@ default String showCode(int indent) {
4242
return args + " -> " + bodyStr;
4343
}
4444

45-
static boolean computeIsStaticWrapperForInstanceMethod(IR body) {
46-
if (body instanceof Function.Lambda function) {
47-
var argNames = function.arguments().map(DefinitionArgument::name);
48-
if (argNames.size() >= 2
49-
&& argNames.apply(0) instanceof Name.Self self1
50-
&& argNames.apply(1) instanceof Name.Self self2
51-
&& self1.synthetic()
52-
&& !self2.synthetic()) {
53-
return true;
54-
}
55-
}
56-
return false;
57-
}
58-
5945
static boolean computeIsStatic(IR body) {
6046
if (body instanceof Function.Lambda function) {
6147
var firstArgName = function.arguments().headOption().map(DefinitionArgument::name);

engine/runtime-parser/src/main/java/org/enso/compiler/core/ir/module/scope/definition/Method.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,13 @@ final class Explicit extends MethodExplicitGen {
5858
* @param bodyReference the body of the method
5959
* @param isStatic true if this method is static, false otherwise
6060
* @param isPrivate true if this method is private, false otherwise
61-
* @param isStaticWrapperForInstanceMethod true if this method represents a static wrapper for
62-
* instance method, false otherwise
6361
*/
6462
@GenerateFields
6563
public Explicit(
6664
@IRChild Name.MethodReference methodReference,
6765
@IRChild Persistance.Reference<Expression> bodyReference,
6866
@IRField boolean isPrivate,
6967
@IRField boolean isStatic,
70-
@IRField boolean isStaticWrapperForInstanceMethod,
7168
IdentifiedLocation identifiedLocation,
7269
MetadataStorage passData,
7370
DiagnosticStorage diagnostics) {
@@ -76,7 +73,6 @@ public Explicit(
7673
bodyReference,
7774
isPrivate,
7875
isStatic,
79-
isStaticWrapperForInstanceMethod,
8076
identifiedLocation,
8177
passData,
8278
diagnostics);
@@ -102,8 +98,6 @@ public static Explicit fromMethodBinding(Method.Binding ir, Expression body) {
10298
.bodyReference(Persistance.Reference.of(body, false))
10399
.isStatic(org.enso.compiler.core.ir.Function.computeIsStatic(body))
104100
.isPrivate(ir.isPrivate())
105-
.isStaticWrapperForInstanceMethod(
106-
org.enso.compiler.core.ir.Function.computeIsStaticWrapperForInstanceMethod(body))
107101
.location(ir.identifiedLocation())
108102
.passData(ir.passData())
109103
.diagnostics(ir.diagnostics())

engine/runtime-suggestions/src/main/scala/org/enso/compiler/suggestions/SuggestionBuilder.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ final class SuggestionBuilder[A: IndexedSource](
120120

121121
case m: definition.Method.Explicit
122122
if m.body().isInstanceOf[Function.Lambda] &&
123-
!m.isStaticWrapperForInstanceMethod &&
124123
!m.isPrivate =>
125124
val typePtr = m.methodReference().typePointer
126125
val methodName = m.methodReference().methodName

engine/runtime/src/main/java/org/enso/interpreter/runtime/builtin/Builtins.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,13 @@ public void initializeBuiltinsIr(
214214
* builtin method was ever registerd
215215
*/
216216
public Optional<BuiltinFunction> getBuiltinFunction(
217-
String type, String methodName, EnsoLanguage language, boolean isStaticInstance) {
218-
return builtins.getBuiltinFunction(type, methodName, language, isStaticInstance);
217+
String type, String methodName, EnsoLanguage language) {
218+
return builtins.getBuiltinFunction(type, methodName, language);
219219
}
220220

221221
public Optional<BuiltinFunction> getBuiltinFunction(
222222
Type type, String methodName, EnsoLanguage language) {
223-
return getBuiltinFunction(type.getName(), methodName, language, false);
223+
return getBuiltinFunction(type.getName(), methodName, language);
224224
}
225225

226226
public <T extends Builtin> T getBuiltinType(Class<T> clazz) {

engine/runtime/src/main/java/org/enso/interpreter/runtime/builtin/BuiltinsRegistry.java

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ final Builtin getBuiltinType(String name) {
8989
* builtin method was ever registerd
9090
*/
9191
final Optional<BuiltinFunction> getBuiltinFunction(
92-
String type, String methodName, EnsoLanguage language, boolean isStaticInstance) {
92+
String type, String methodName, EnsoLanguage language) {
9393
// TODO: move away from String mapping once Builtins is gone
9494
Map<String, Supplier<LoadedBuiltinMethod>> atomNodes = builtinMethodNodes.get(type);
9595
if (atomNodes == null) {
@@ -103,7 +103,7 @@ final Optional<BuiltinFunction> getBuiltinFunction(
103103
if (builtin == null) {
104104
return Optional.empty();
105105
}
106-
return builtin.toFunction(language, isStaticInstance);
106+
return builtin.toFunction(language);
107107
}
108108

109109
private static Map<String, LoadedBuiltinMethod> loadBuiltinMethodClassesEarly(
@@ -116,39 +116,6 @@ private static Map<String, LoadedBuiltinMethod> loadBuiltinMethodClassesEarly(
116116
return methods;
117117
}
118118

119-
/**
120-
* Registers builtin methods with their corresponding Atom Constructor's owners. That way
121-
* "special" builtin types have builtin methods in the scope without requiring everyone to always
122-
* import full stdlib.
123-
*
124-
* @param scope Builtins scope
125-
* @param language The language the resulting function nodes should be associated with
126-
*/
127-
private void registerBuiltinMethods(ModuleScopeBuilder scope, EnsoLanguage language) {
128-
for (Builtin builtin : builtins.values()) {
129-
var type = builtin.getType();
130-
Map<String, Supplier<LoadedBuiltinMethod>> methods = builtinMethodNodes.get(type.getName());
131-
if (methods != null) {
132-
// Register a builtin method iff it is marked as auto-register.
133-
// Methods can only register under a type or, if we deal with a static method, it's
134-
// eigen-type.
135-
// Such builtins are available on certain types without importing the whole stdlib, e.g. Any
136-
// or Number.
137-
methods.forEach(
138-
(key, value) -> {
139-
LoadedBuiltinMethod meth = value.get();
140-
Type tpe =
141-
meth.isAutoRegister ? (!meth.isStatic() ? type : type.getEigentype()) : null;
142-
if (tpe != null) {
143-
Optional<BuiltinFunction> fun = meth.toFunction(language, false);
144-
fun.ifPresent(
145-
f -> scope.registerMethod(tpe, key, CachingSupplier.forValue(f.getFunction())));
146-
}
147-
});
148-
}
149-
}
150-
}
151-
152119
/**
153120
* Returns a list of supported builtins.
154121
*
@@ -267,9 +234,7 @@ private static Map<String, LoadedBuiltinMetaMethod> readBuiltinMethodsMeta() {
267234
}
268235

269236
/**
270-
* Register builtin methods and initialize them lazily in the provided scope. This method differs
271-
* from `registerBuiltinMethods` where all methods are initialized by the time they are
272-
* registered..
237+
* Register builtin methods and initialize them lazily in the provided scope.
273238
*
274239
* @param scope Builtins scope
275240
* @param language The language the resulting function nodes should be associated with
@@ -291,15 +256,15 @@ private Map<String, Map<String, Supplier<LoadedBuiltinMethod>>> registerBuiltinM
291256
if (constr != null) {
292257
Map<String, Supplier<LoadedBuiltinMethod>> atomNodes =
293258
getOrUpdate(builtinMethodNodes, constr.getName());
294-
atomNodes.put(builtinMethodName, CachingSupplier.wrap(() -> meta.toMethod()));
259+
atomNodes.put(builtinMethodName, CachingSupplier.wrap(meta::toMethod));
295260

296261
Map<String, LoadedBuiltinMetaMethod> atomNodesMeta =
297262
getOrUpdate(builtinMetaMethods, constr.getName());
298263
atomNodesMeta.put(builtinMethodName, meta);
299264
} else {
300265
Map<String, Supplier<LoadedBuiltinMethod>> atomNodes =
301266
getOrUpdate(builtinMethodNodes, builtinMethodOwner);
302-
atomNodes.put(builtinMethodName, CachingSupplier.wrap(() -> meta.toMethod()));
267+
atomNodes.put(builtinMethodName, CachingSupplier.wrap(meta::toMethod));
303268

304269
Map<String, LoadedBuiltinMetaMethod> atomNodesMeta =
305270
getOrUpdate(builtinMetaMethods, builtinMethodOwner);
@@ -322,7 +287,7 @@ private Map<String, Map<String, Supplier<LoadedBuiltinMethod>>> registerBuiltinM
322287
value.isAutoRegister() ? (!value.isStatic() ? type : type.getEigentype()) : null;
323288
if (tpe != null) {
324289
Supplier<Function> supplier =
325-
() -> value.toMethod().toFunction(language, false).get().getFunction();
290+
() -> value.toMethod().toFunction(language).get().getFunction();
326291
scope.registerMethod(tpe, key, supplier);
327292
}
328293
});
@@ -367,7 +332,7 @@ LoadedBuiltinMethod toMethod() {
367332
try {
368333
@SuppressWarnings("unchecked")
369334
Class<BuiltinRootNode> clazz = (Class<BuiltinRootNode>) Class.forName(className);
370-
Method meth = clazz.getMethod("makeFunction", EnsoLanguage.class, boolean.class);
335+
Method meth = clazz.getMethod("makeFunction", EnsoLanguage.class);
371336
method = new LoadedBuiltinMethod(meth, staticMethod, autoRegister);
372337
} catch (ClassNotFoundException | NoSuchMethodException e) {
373338
throw new CompilerError("Invalid builtin method " + className, e);
@@ -378,9 +343,9 @@ LoadedBuiltinMethod toMethod() {
378343
}
379344

380345
private record LoadedBuiltinMethod(Method meth, boolean isStatic, boolean isAutoRegister) {
381-
Optional<BuiltinFunction> toFunction(EnsoLanguage language, boolean isStaticInstance) {
346+
Optional<BuiltinFunction> toFunction(EnsoLanguage language) {
382347
try {
383-
var f = (Function) meth.invoke(null, language, isStaticInstance);
348+
var f = (Function) meth.invoke(null, language);
384349
if (f != null) {
385350
var bf = new BuiltinFunction(f, isAutoRegister);
386351
return Optional.of(bf);

engine/runtime/src/main/scala/org/enso/interpreter/runtime/IrToTruffle.scala

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -750,14 +750,11 @@ private[runtime] class IrToTruffle(
750750
val methodName = builtinNameElements(1)
751751
val methodOwnerName = builtinNameElements(0)
752752

753-
val staticWrapper = methodDef.isStaticWrapperForInstanceMethod
754-
755753
val builtinFunction = getBuiltins
756754
.getBuiltinFunction(
757755
methodOwnerName,
758756
methodName,
759-
language,
760-
staticWrapper
757+
language
761758
)
762759
builtinFunction.toScala
763760
.map(Some(_))
@@ -770,9 +767,7 @@ private[runtime] class IrToTruffle(
770767
.flatMap { l =>
771768
// Builtin Types Number and Integer have methods only for documentation purposes
772769
val number = getBuiltins.number()
773-
val ok =
774-
staticWrapper && (cons == number.getNumber.getEigentype || cons == number.getInteger.getEigentype) ||
775-
!staticWrapper && (cons == number.getNumber || cons == number.getInteger)
770+
val ok = cons == number.getNumber || cons == number.getInteger
776771
if (ok) Right(None)
777772
else Left(l)
778773
}

0 commit comments

Comments
 (0)