Accept type argument in instance method signatures (#7848)

This commit is contained in:
Jaroslav Tulach 2023-09-20 12:58:20 +02:00 committed by GitHub
parent 0efa1afc61
commit 2e1791b63e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 13 deletions

View File

@ -731,6 +731,7 @@ object BindingsMap {
*/
case class Type(
override val name: String,
params: Seq[String],
members: Seq[Cons],
builtinType: Boolean
) extends DefinedEntity {

View File

@ -57,6 +57,7 @@ case object BindingAnalysis extends IRPass {
.exists(_.annotations.exists(_.name == "@Builtin_Type"))
BindingsMap.Type(
sumType.name.name,
sumType.params.map(_.name.name),
sumType.members.map(m =>
Cons(
m.name.name,

View File

@ -159,6 +159,7 @@ case object FullyQualifiedNames extends IRPass {
processExpression(
ir,
scopeMap,
List(),
freshNameSupply,
None,
inlineContext.pkgRepo
@ -179,7 +180,14 @@ case object FullyQualifiedNames extends IRPass {
_.getMetadata(MethodDefinitions)
)
method.mapExpressions(
processExpression(_, bindings, freshNameSupply, resolution, pkgRepo)
processExpression(
_,
bindings,
List(),
freshNameSupply,
resolution,
pkgRepo
)
)
case tp: Definition.Type =>
tp.copy(members =
@ -188,6 +196,7 @@ case object FullyQualifiedNames extends IRPass {
processExpression(
_,
bindings,
tp.params.map(_.name),
freshNameSupply,
bindings.resolveName(tp.name.name).toOption.map(Resolution),
pkgRepo
@ -198,7 +207,7 @@ case object FullyQualifiedNames extends IRPass {
case a =>
a.mapExpressions(
processExpression(_, bindings, freshNameSupply, None, pkgRepo)
processExpression(_, bindings, List(), freshNameSupply, None, pkgRepo)
)
}
}
@ -206,13 +215,15 @@ case object FullyQualifiedNames extends IRPass {
private def processExpression(
ir: Expression,
bindings: BindingsMap,
typeParams: List[Name],
freshNameSupply: FreshNameSupply,
selfTypeResolution: Option[Resolution],
pkgRepo: Option[PackageRepository]
): Expression =
ir.transformExpressions {
case lit: Name.Literal =>
if (!lit.isMethod && !isLocalVar(lit)) {
val isTypeName = typeParams.find(_.name == lit.name).nonEmpty
if (!lit.isMethod && !isLocalVar(lit) && !isTypeName) {
val resolution = bindings.resolveName(lit.name)
resolution match {
case Left(_) =>
@ -240,6 +251,7 @@ case object FullyQualifiedNames extends IRPass {
resolveLocalApplication(
app,
bindings,
typeParams,
freshNameSupply,
pkgRepo,
selfTypeResolution
@ -249,6 +261,7 @@ case object FullyQualifiedNames extends IRPass {
processExpression(
_,
bindings,
typeParams,
freshNameSupply,
selfTypeResolution,
pkgRepo
@ -259,6 +272,7 @@ case object FullyQualifiedNames extends IRPass {
processExpression(
_,
bindings,
typeParams,
freshNameSupply,
selfTypeResolution,
pkgRepo
@ -272,6 +286,7 @@ case object FullyQualifiedNames extends IRPass {
private def resolveLocalApplication(
app: Application.Prefix,
bindings: BindingsMap,
typeParams: List[Name],
freshNameSupply: FreshNameSupply,
pkgRepo: Option[PackageRepository],
selfTypeResolution: Option[Resolution]
@ -280,6 +295,7 @@ case object FullyQualifiedNames extends IRPass {
processExpression(
app.function,
bindings,
typeParams,
freshNameSupply,
selfTypeResolution,
pkgRepo
@ -290,6 +306,7 @@ case object FullyQualifiedNames extends IRPass {
processExpression(
_,
bindings,
typeParams,
freshNameSupply,
selfTypeResolution,
pkgRepo

View File

@ -5,6 +5,7 @@ import org.enso.compiler.core.IR
import org.enso.compiler.core.ir.{Expression, Function, Module, Name}
import org.enso.compiler.core.ir.expression.errors
import org.enso.compiler.core.ir.module.scope.Definition
import org.enso.compiler.core.ir.module.scope.definition.Method
import org.enso.compiler.core.ir.MetadataStorage.ToPair
import org.enso.compiler.core.ir.`type`
import org.enso.compiler.data.BindingsMap
@ -45,9 +46,21 @@ case object TypeNames extends IRPass {
val bindingsMap =
ir.unsafeGetMetadata(BindingAnalysis, "bindings analysis did not run")
ir.copy(bindings = ir.bindings.map { d =>
val typeParams = d match {
val typeParams: List[Name] = d match {
case t: Definition.Type => t.params.map(_.name)
case _ => Nil
case m: Method.Explicit =>
val params: List[Name] = m.methodReference.typePointer
.flatMap { p =>
p.getMetadata(MethodDefinitions)
.map(_.target match {
case typ: BindingsMap.ResolvedType =>
typ.tp.params.map(Name.Literal(_, false, None)).toList
case _ => List()
})
}
.getOrElse(List())
params
case _ => Nil
}
val mapped =
d.mapExpressions(resolveExpression(typeParams, bindingsMap, _))

View File

@ -400,6 +400,25 @@ public class SignatureTest extends TestBase {
assertEquals("Can read ten", 10, some.getMember("unwrap").asInt());
}
@Test
public void ascribedWithAParameterAndMethod() throws Exception {
final URI uri = new URI("memory://getter.enso");
final Source src = Source.newBuilder("enso", """
type Maybe a
Nothing
Some unwrap:a
get : a
get self = self.unwrap
""",uri.getAuthority())
.uri(uri)
.buildLiteral();
var module = ctx.eval(src);
var some = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "Maybe.Some 10");
assertEquals("Can get ten", 10, some.invokeMember("get").asInt());
}
@Test
public void suspendedAscribedParameter() throws Exception {
final URI uri = new URI("memory://suspended.enso");

View File

@ -77,9 +77,9 @@ class BindingAnalysisTest extends CompilerTest {
val metadata = ir.unsafeGetMetadata(BindingAnalysis, "Should exist.")
metadata.definedEntities should contain theSameElementsAs List(
Type("Foo", List(Cons("Mk_Foo", 3, false)), false),
Type("Bar", List(), false),
Type("Baz", List(), false),
Type("Foo", List(), List(Cons("Mk_Foo", 3, false)), false),
Type("Bar", List(), List(), false),
Type("Baz", List("x", "y"), List(), false),
PolyglotSymbol("MyClass"),
PolyglotSymbol("Renamed_Class"),
ModuleMethod("foo")

View File

@ -81,7 +81,7 @@ class MethodDefinitionsTest extends CompilerTest {
BindingsMap.Resolution(
BindingsMap.ResolvedType(
ctx.moduleReference(),
Type("Foo", List(), false)
Type("Foo", List("a", "b", "c"), List(), false)
)
)
)
@ -116,7 +116,7 @@ class MethodDefinitionsTest extends CompilerTest {
BindingsMap.Resolution(
BindingsMap.ResolvedType(
ctx.moduleReference(),
Type("Foo", List(), false)
Type("Foo", List("a", "b", "c"), List(), false)
)
)
)
@ -124,7 +124,7 @@ class MethodDefinitionsTest extends CompilerTest {
BindingsMap.Resolution(
BindingsMap.ResolvedType(
ctx.moduleReference(),
Type("Bar", List(), false)
Type("Bar", List(), List(), false)
)
)
)
@ -138,7 +138,7 @@ class MethodDefinitionsTest extends CompilerTest {
BindingsMap.Resolution(
BindingsMap.ResolvedType(
ctx.moduleReference(),
Type("Bar", List(), false)
Type("Bar", List(), List(), false)
)
)
)
@ -152,7 +152,7 @@ class MethodDefinitionsTest extends CompilerTest {
BindingsMap.Resolution(
BindingsMap.ResolvedType(
ctx.moduleReference(),
Type("Foo", List(), false)
Type("Foo", List("a", "b", "c"), List(), false)
)
)
)