Skip to content

Commit 31934b9

Browse files
committed
finish implementing the logic in StaticModuleScopeAnalysis, start phasing out AtomTypeInterface
1 parent 968af64 commit 31934b9

File tree

8 files changed

+36
-38
lines changed

8 files changed

+36
-38
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* <p>This interface declares what methods can be called on instances of that type (or statically)
99
* and what constructors may be called on it to create new instances.
1010
*/
11+
@Deprecated
1112
interface AtomTypeInterface {
1213
List<? extends Constructor> constructors();
1314

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

+3-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import scala.jdk.javaapi.CollectionConverters$;
88

99
/** Implementation of {@link AtomTypeInterface} that is built from a {@link BindingsMap.Type}. */
10+
@Deprecated
1011
public final class AtomTypeInterfaceFromBindingsMap implements AtomTypeInterface {
1112
// TODO this probably is no longer needed since we have StaticModuleScope
1213
private final BindingsMap.Type type;
@@ -38,8 +39,6 @@ public String name() {
3839
return constructor.name();
3940
}
4041

41-
private transient List<? extends Argument> arguments = null;
42-
4342
@Override
4443
public List<? extends Argument> arguments() {
4544
return new ProxyList<>(
@@ -65,14 +64,10 @@ public boolean hasDefaultValue() {
6564
return arg.hasDefaultValue();
6665
}
6766

67+
@Deprecated
6868
@Override
6969
public TypeRepresentation getType(TypeResolver resolver) {
70-
if (arg.typ().isEmpty()) {
71-
return null;
72-
} else {
73-
Expression expression = arg.typ().get();
74-
return resolver.resolveTypeExpression(expression);
75-
}
70+
throw new IllegalStateException("No longer supported - this class will be removed soon.");
7671
}
7772
}
7873
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ protected void encounteredNoSuchMethod(
111111
.getDiagnostics()
112112
.add(new Warning.NoSuchMethod(relatedIr.identifiedLocation(), methodDescription));
113113
}
114+
115+
@Override
116+
protected void encounteredNoSuchConstructor(IR relatedIr, TypeRepresentation type, String constructorName) {
117+
// TODO make sure if NoSuchMethod is right or we need a separate type here
118+
String methodDescription = "constructor `" + constructorName + "` on type " + type;
119+
relatedIr
120+
.getDiagnostics()
121+
.add(new Warning.NoSuchMethod(relatedIr.identifiedLocation(), methodDescription));
122+
}
114123
};
115124
}
116125

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

+1
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ protected TypeRepresentation resolveGlobalName(BindingsMap.ResolvedName resolved
475475
return switch (resolvedName) {
476476
// TODO investigate when do these appear?? I did not yet see them in the wild
477477
case BindingsMap.ResolvedConstructor ctor -> {
478+
// TODO can we replace this with querying StaticModuleScope?
478479
var constructorInterface =
479480
new AtomTypeInterfaceFromBindingsMap.ConstructorFromBindingsMap(ctor.cons());
480481
yield typeResolver.buildAtomConstructorType(

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ private TypeRepresentation getResolvedTypeFromBindingsMap(Name name) {
121121
}
122122

123123
TypeRepresentation.TypeObject resolvedTypeAsTypeObject(BindingsMap.ResolvedType resolvedType) {
124-
var iface = new AtomTypeInterfaceFromBindingsMap(resolvedType.tp());
125-
return new TypeRepresentation.TypeObject(resolvedType.qualifiedName(), iface);
124+
return new TypeRepresentation.TypeObject(resolvedType.qualifiedName());
126125
}
127126

128127
TypeRepresentation resolvedTypeAsAtomType(BindingsMap.ResolvedType resolvedType) {

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

+17-8
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
import org.enso.compiler.pass.resolve.FullyQualifiedNames$;
2626
import org.enso.compiler.pass.resolve.GlobalNames$;
2727
import org.enso.compiler.pass.resolve.TypeNames$;
28+
import org.enso.pkg.QualifiedName;
2829
import org.slf4j.Logger;
2930
import org.slf4j.LoggerFactory;
31+
import scala.Option;
3032
import scala.collection.immutable.Seq;
3133
import scala.jdk.javaapi.CollectionConverters;
3234
import scala.jdk.javaapi.CollectionConverters$;
@@ -136,24 +138,26 @@ protected void processMethodDefinition(Method.Explicit method) {
136138

137139
@Override
138140
protected void processTypeDefinition(Definition.Type typ) {
141+
QualifiedName qualifiedName = scopeBuilder.getModuleName().createChild(typ.name().name());
142+
TypeRepresentation.TypeObject typeObject = new TypeRepresentation.TypeObject(qualifiedName);
139143
List<AtomType.Constructor> constructors =
140144
CollectionConverters$.MODULE$.asJava(typ.members()).stream()
141145
.map(
142146
constructorDef -> {
143-
TypeRepresentation type = buildAtomConstructorType(constructorDef);
147+
TypeRepresentation type = buildAtomConstructorType(typeObject, constructorDef);
144148
return new AtomType.Constructor(
145149
constructorDef.name().name(), constructorDef.isPrivate(), type);
146150
})
147151
.toList();
148152

149153
AtomType atomType = new AtomType(typ.name().name(), constructors);
150-
var qualifiedName = scopeBuilder.getModuleName().createChild(typ.name().name());
151154
var atomTypeScope = TypeScopeReference.atomType(qualifiedName);
152155
scopeBuilder.registerType(atomType);
153156
registerFieldGetters(scopeBuilder, atomTypeScope, typ);
154157
}
155158

156-
private TypeRepresentation buildAtomConstructorType(Definition.Data constructorDef) {
159+
private TypeRepresentation buildAtomConstructorType(
160+
TypeRepresentation.TypeObject associatedType, Definition.Data constructorDef) {
157161
boolean hasDefaults = constructorDef.arguments().exists(a -> a.defaultValue().isDefined());
158162
if (hasDefaults) {
159163
// TODO implement handling of default arguments - not only ctors will need this!
@@ -165,13 +169,18 @@ private TypeRepresentation buildAtomConstructorType(Definition.Data constructorD
165169
.arguments()
166170
.map(
167171
(arg) -> {
168-
var typ = arg.ascribedType();
169-
// TODO
170-
return typ != null ? typ : TypeRepresentation.UNKNOWN;
172+
Option<Expression> typ = arg.ascribedType();
173+
if (typ.isEmpty()) {
174+
return TypeRepresentation.UNKNOWN;
175+
}
176+
177+
var resolvedType = typeResolver.resolveTypeExpression(typ.get());
178+
assert resolvedType != null;
179+
return resolvedType;
171180
})
172181
.toList();
173-
var resultType = parentType.instanceType();
174-
return TypeRepresentation.buildFunction(arguments, resultType);
182+
var resultType = associatedType.instanceType();
183+
return TypeRepresentation.buildFunction(CollectionConverters.asJava(arguments), resultType);
175184
}
176185

177186
@Override

engine/runtime-integration-tests/src/test/scala/org/enso/compiler/test/pass/analyse/BindingAnalysisTest.scala

+3-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import org.enso.compiler.data.BindingsMap.{
1818
import org.enso.compiler.pass.analyse.BindingAnalysis
1919
import org.enso.compiler.pass.{PassConfiguration, PassGroup, PassManager}
2020
import org.enso.compiler.test.CompilerTest
21-
import org.enso.persist.Persistance
2221

2322
class BindingAnalysisTest extends CompilerTest {
2423

@@ -168,18 +167,15 @@ class BindingAnalysisTest extends CompilerTest {
168167
List(
169168
Argument(
170169
"a",
171-
hasDefaultValue = false,
172-
Persistance.Reference.none()
170+
hasDefaultValue = false
173171
),
174172
Argument(
175173
"b",
176-
hasDefaultValue = false,
177-
Persistance.Reference.none()
174+
hasDefaultValue = false
178175
),
179176
Argument(
180177
"c",
181-
hasDefaultValue = false,
182-
Persistance.Reference.none()
178+
hasDefaultValue = false
183179
)
184180
),
185181
isProjectPrivate = false

engine/runtime/src/test/java/org/enso/runtime/test/TypeMetadataPersistanceTest.java

+1-13
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import java.io.IOException;
66
import java.util.List;
7-
import org.enso.compiler.data.BindingsMap;
8-
import org.enso.compiler.pass.analyse.types.AtomTypeInterfaceFromBindingsMap;
97
import org.enso.compiler.pass.analyse.types.InferredType;
108
import org.enso.compiler.pass.analyse.types.TypeRepresentation;
119
import org.enso.persist.Persistance;
@@ -42,16 +40,6 @@ public void writeSomeInferredType() throws Exception {
4240

4341
private TypeRepresentation.TypeObject mockObject() {
4442
var fqn = new QualifiedName(makeScalaList(List.of("mod")), "Test");
45-
return new TypeRepresentation.TypeObject(fqn, mockAtomType());
46-
}
47-
48-
private AtomTypeInterfaceFromBindingsMap mockAtomType() {
49-
scala.collection.immutable.List<String> params = makeScalaList(List.of());
50-
var ctorArgs =
51-
makeScalaList(
52-
List.of(new BindingsMap.Argument("arg", false, Persistance.Reference.none())));
53-
var constructors = makeScalaList(List.of(new BindingsMap.Cons("ctor", ctorArgs, false)));
54-
return new AtomTypeInterfaceFromBindingsMap(
55-
new BindingsMap.Type("Test", params, constructors, false));
43+
return new TypeRepresentation.TypeObject(fqn);
5644
}
5745
}

0 commit comments

Comments
 (0)