[go: nahoru, domu]

Skip to content

Commit

Permalink
fix: lambda locations (enso-org#1522)
Browse files Browse the repository at this point in the history
Reattach original locations during
the lambda desugaring
  • Loading branch information
4e6 authored Feb 26, 2021
1 parent 3bf566f commit 0c5a362
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4249,7 +4249,7 @@ object IR {

override def toString: String =
s"""
|IR.Application.Operator.Section.Centre(
|IR.Application.Operator.Section.Sides(
|operator = $operator,
|location = $location,
|passData = ${this.showPassData},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ case object LambdaShorthandToLambda extends IRPass {
IR.Function.Lambda(
List(
IR.DefinitionArgument.Specified(
IR.Name.Literal(
name = IR.Name.Literal(
newName.name,
isReferent = false,
isMethod = false,
None
),
None,
suspended = false,
None
defaultValue = None,
suspended = false,
location = None
)
),
newName,
Expand Down Expand Up @@ -218,7 +218,7 @@ case object LambdaShorthandToLambda extends IRPass {
)

// If the function is shorthand, do the same
if (functionIsShorthand) {
val resultExpr = if (functionIsShorthand) {
IR.Function.Lambda(
List(
IR.DefinitionArgument.Specified(
Expand All @@ -238,6 +238,11 @@ case object LambdaShorthandToLambda extends IRPass {
None
)
} else appResult

resultExpr match {
case lam: IR.Function.Lambda => lam.copy(location = p.location)
case result => result
}
case f @ IR.Application.Force(tgt, _, _, _) =>
f.copy(target = desugarExpression(tgt, freshNameSupply))
case vector @ IR.Application.Literal.Sequence(items, _, _, _) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ case object SectionsToBinOp extends IRPass {
None
)
val opCall = IR.Application.Prefix(
op,
List(leftCallArg, rightCallArg),
function = op,
arguments = List(leftCallArg, rightCallArg),
hasDefaultsSuspended = false,
loc,
location = None,
passData,
diagnostics
)
Expand All @@ -132,23 +132,23 @@ case object SectionsToBinOp extends IRPass {
IR.Function.Lambda(
List(leftDefArg),
rightLam,
None
loc
)

} else {
val opCall = IR.Application.Prefix(
op,
List(arg, rightCallArg),
function = op,
arguments = List(arg, rightCallArg),
hasDefaultsSuspended = false,
loc,
location = None,
passData,
diagnostics
)

IR.Function.Lambda(
List(rightDefArg),
opCall,
None
loc
)
}
case Section.Sides(op, loc, passData, diagnostics) =>
Expand All @@ -173,10 +173,10 @@ case object SectionsToBinOp extends IRPass {
)

val opCall = IR.Application.Prefix(
op,
List(leftCallArg, rightCallArg),
function = op,
arguments = List(leftCallArg, rightCallArg),
hasDefaultsSuspended = false,
loc,
location = None,
passData,
diagnostics
)
Expand All @@ -190,7 +190,7 @@ case object SectionsToBinOp extends IRPass {
IR.Function.Lambda(
List(leftDefArg),
rightLambda,
None
loc
)

/* Note [Blanks in Sections]
Expand Down Expand Up @@ -237,10 +237,10 @@ case object SectionsToBinOp extends IRPass {
)

val opCall = IR.Application.Prefix(
op,
List(leftCallArg, rightCallArg),
function = op,
arguments = List(leftCallArg, rightCallArg),
hasDefaultsSuspended = false,
loc,
location = None,
passData,
diagnostics
)
Expand All @@ -254,22 +254,22 @@ case object SectionsToBinOp extends IRPass {
IR.Function.Lambda(
List(rightDefArg),
leftLam,
None
loc
)
} else {
val opCall = IR.Application.Prefix(
op,
List(leftCallArg, arg),
function = op,
arguments = List(leftCallArg, arg),
hasDefaultsSuspended = false,
loc,
location = None,
passData,
diagnostics
)

IR.Function.Lambda(
List(leftDefArg),
opCall,
None
loc
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class AstToIrTest extends CompilerTest with Inside {
|""".stripMargin.toIrExpression.get

ir shouldBe an[IR.Application.Operator.Section.Left]
ir.location shouldBe defined
}

"work properly for sides sections" in {
Expand All @@ -64,6 +65,8 @@ class AstToIrTest extends CompilerTest with Inside {
|""".stripMargin.toIrExpression.get

ir shouldBe an[IR.Application.Operator.Section.Sides]
// TODO[DB] Section.Sides location is not parsed
//ir.location shouldBe defined
}

"work properly for right sections" in {
Expand All @@ -73,6 +76,7 @@ class AstToIrTest extends CompilerTest with Inside {
|""".stripMargin.toIrExpression.get

ir shouldBe an[IR.Application.Operator.Section.Right]
ir.location shouldBe defined
}

"disallow sections with named arguments" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class LambdaShorthandToLambdaTest extends CompilerTest {
|""".stripMargin.preprocessExpression.get.desugar

ir shouldBe an[IR.Function.Lambda]
ir.location shouldBe defined
val irFn = ir.asInstanceOf[IR.Function.Lambda]
val irFnArgName =
irFn.arguments.head.asInstanceOf[IR.DefinitionArgument.Specified].name
Expand Down Expand Up @@ -98,6 +99,7 @@ class LambdaShorthandToLambdaTest extends CompilerTest {
|""".stripMargin.preprocessExpression.get.desugar

ir shouldBe an[IR.Function.Lambda]
ir.location shouldBe defined
val irFn = ir.asInstanceOf[IR.Function.Lambda]
val irFnArgName =
irFn.arguments.head.asInstanceOf[IR.DefinitionArgument.Specified].name
Expand Down Expand Up @@ -136,6 +138,7 @@ class LambdaShorthandToLambdaTest extends CompilerTest {
|""".stripMargin.preprocessExpression.get.desugar

ir shouldBe an[IR.Function.Lambda]
ir.location shouldBe defined
val irFn = ir.asInstanceOf[IR.Function.Lambda]
val irFnArgName =
irFn.arguments.head.asInstanceOf[IR.DefinitionArgument.Specified].name
Expand All @@ -157,6 +160,7 @@ class LambdaShorthandToLambdaTest extends CompilerTest {
|""".stripMargin.preprocessExpression.get.desugar

ir shouldBe an[IR.Function.Lambda]
ir.location shouldBe defined
val irFn = ir.asInstanceOf[IR.Function.Lambda]
val irFnArgName =
irFn.arguments.head.asInstanceOf[IR.DefinitionArgument.Specified].name
Expand All @@ -181,6 +185,7 @@ class LambdaShorthandToLambdaTest extends CompilerTest {
|""".stripMargin.preprocessExpression.get.desugar

ir shouldBe an[IR.Function.Lambda]
ir.location shouldBe defined

val irLam = ir.asInstanceOf[IR.Function.Lambda]
irLam.arguments.length shouldEqual 1
Expand All @@ -205,6 +210,7 @@ class LambdaShorthandToLambdaTest extends CompilerTest {
|""".stripMargin.preprocessExpression.get.desugar

ir shouldBe an[IR.Function.Lambda]
ir.location shouldBe defined
val irFn = ir.asInstanceOf[IR.Function.Lambda]

val argName =
Expand All @@ -225,6 +231,7 @@ class LambdaShorthandToLambdaTest extends CompilerTest {
|""".stripMargin.preprocessExpression.get.desugar

ir shouldBe an[IR.Function.Lambda]
ir.location shouldBe defined
val underscoreFn = ir.asInstanceOf[IR.Function.Lambda]
val underscoreArgName = underscoreFn.arguments.head.name

Expand Down Expand Up @@ -253,6 +260,7 @@ class LambdaShorthandToLambdaTest extends CompilerTest {
|""".stripMargin.preprocessExpression.get.desugar

ir shouldBe an[IR.Function.Lambda]
ir.location shouldBe defined
val irFn = ir.asInstanceOf[IR.Function.Lambda]
val arg1Name = irFn.arguments.head.name

Expand All @@ -278,6 +286,7 @@ class LambdaShorthandToLambdaTest extends CompilerTest {
|""".stripMargin.preprocessExpression.get.desugar

ir shouldBe an[IR.Function.Lambda]
ir.location shouldBe defined
val irFn = ir.asInstanceOf[IR.Function.Lambda]
val rightArgName = irFn.arguments.head.name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class SectionsToBinOpTest extends CompilerTest {
|""".stripMargin.preprocessExpression.get.desugar

ir shouldBe an[IR.Function.Lambda]
ir.location shouldBe defined

val irLam = ir.asInstanceOf[IR.Function.Lambda]
irLam.arguments.length shouldEqual 1
Expand Down Expand Up @@ -86,6 +87,8 @@ class SectionsToBinOpTest extends CompilerTest {
|""".stripMargin.preprocessExpression.get.desugar

ir shouldBe an[IR.Function.Lambda]
// TODO[DB] Section.Sides location is not parsed
//ir.location shouldBe defined

val leftLam = ir.asInstanceOf[IR.Function.Lambda]
leftLam.arguments.length shouldEqual 1
Expand Down Expand Up @@ -127,6 +130,7 @@ class SectionsToBinOpTest extends CompilerTest {
|""".stripMargin.preprocessExpression.get.desugar

ir shouldBe an[IR.Function.Lambda]
ir.location shouldBe defined

val irLam = ir.asInstanceOf[IR.Function.Lambda]
irLam.arguments.length shouldEqual 1
Expand Down Expand Up @@ -175,6 +179,7 @@ class SectionsToBinOpTest extends CompilerTest {
|""".stripMargin.preprocessExpression.get.desugar

ir shouldBe an[IR.Function.Lambda]
ir.location shouldBe defined
val irFn = ir.asInstanceOf[IR.Function.Lambda]

val rightArgName =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2229,6 +2229,59 @@ class RuntimeServerTest
)
}

it should "send updates for a lambda" in {
val contextId = UUID.randomUUID()
val requestId = UUID.randomUUID()
val moduleName = "Test.Main"
val metadata = new Metadata

val xId = metadata.addItem(41, 5)
val mainRes = metadata.addItem(51, 12)

val code =
"""from Builtins import all
|
|main =
| x = _ + 1
| IO.println x
|""".stripMargin.linesIterator.mkString("\n")
val contents = metadata.appendToCode(code)
val mainFile = context.writeMain(contents)

// create context
context.send(Api.Request(requestId, Api.CreateContextRequest(contextId)))
context.receive shouldEqual Some(
Api.Response(requestId, Api.CreateContextResponse(contextId))
)

// Open the new file
context.send(
Api.Request(Api.OpenFileNotification(mainFile, contents, true))
)
context.receiveNone shouldEqual None

// push main
context.send(
Api.Request(
requestId,
Api.PushContextRequest(
contextId,
Api.StackItem.ExplicitCall(
Api.MethodPointer(moduleName, "Test.Main", "main"),
None,
Vector()
)
)
)
)
context.receive(5) should contain allOf (
Api.Response(requestId, Api.PushContextResponse(contextId)),
TestMessages.update(contextId, xId, Constants.FUNCTION),
TestMessages.update(contextId, mainRes, Constants.NOTHING),
context.executionComplete(contextId)
)
}

it should "support file modification operations without attached ids" in {
val contextId = UUID.randomUUID()
val requestId = UUID.randomUUID()
Expand Down

0 comments on commit 0c5a362

Please sign in to comment.