Skip to content

Commit

Permalink
fix(compiler): Fix incorrect service method renaming [fixes LNG-199] (#…
Browse files Browse the repository at this point in the history
…757)

* Rename only arrows, not service calls

* Add tests

* Fix comment
  • Loading branch information
InversionSpaces committed Jun 16, 2023
1 parent c73a98b commit e22fff7
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
108 changes: 108 additions & 0 deletions model/inline/src/test/scala/aqua/model/inline/ArrowInlinerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import aqua.raw.ops.*
import aqua.raw.value.{ApplyPropertyRaw, FunctorRaw, IntoFieldRaw, IntoIndexRaw, LiteralRaw, VarRaw}
import aqua.types.*
import cats.syntax.show.*
import cats.syntax.option.*
import cats.data.{Chain, NonEmptyList, NonEmptyMap}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
Expand Down Expand Up @@ -1261,6 +1262,113 @@ class ArrowInlinerSpec extends AnyFlatSpec with Matchers {
model.equalsOrShowDiff(expected) shouldEqual true
}

/**
* service Test("test-service"):
* method(method: string) -> string
*
* func test(method: string) -> string:
* res <- Test.method(method)
* <- res
*
* func main():
* method = "method"
* test(method)
*/
"arrow inliner" should "not rename service call [bug LNG-199]" in {
val testName = "test"
val argMethodName = "method"
val serviceName = "Test"
val serviceId = LiteralRaw("test-service", LiteralType.string)
val res = VarRaw("res", ScalarType.string)

val testType = ArrowType(
domain = ProductType.labelled(List(argMethodName -> ScalarType.string)),
codomain = ProductType(ScalarType.string :: Nil)
)

val testBody = SeqTag.wrap(
CallArrowRawTag
.service(
serviceId = serviceId,
fnName = argMethodName,
call = Call(
args = VarRaw(argMethodName, ScalarType.string) :: Nil,
exportTo = Call.Export(res.name, res.`type`) :: Nil
),
name = serviceName,
arrowType = ArrowType(
domain = ProductType.labelled(List(argMethodName -> ScalarType.string)),
codomain = ProductType(ScalarType.string :: Nil)
)
)
.leaf,
ReturnTag(
NonEmptyList.one(res)
).leaf
)

val testFunc = FuncArrow(
funcName = testName,
body = testBody,
arrowType = testType,
ret = Nil,
capturedArrows = Map.empty,
capturedValues = Map.empty,
capturedTopology = None
)

val mainType = ArrowType(
domain = ProductType(Nil),
codomain = ProductType(Nil)
)

val mainBody = SeqTag.wrap(
AssignmentTag(
LiteralRaw.quote(argMethodName),
argMethodName
).leaf,
CallArrowRawTag
.func(
fnName = testName,
call = Call(args = VarRaw(argMethodName, LiteralType.string) :: Nil, Nil)
)
.leaf
)

val mainFunc = FuncArrow(
funcName = "main",
body = mainBody,
arrowType = ArrowType(ProductType(Nil), ProductType(Nil)),
ret = Nil,
capturedArrows = Map(testName -> testFunc),
capturedValues = Map.empty,
capturedTopology = None
)

val model = ArrowInliner
.callArrow[InliningState](
mainFunc,
CallModel(Nil, Nil)
)
.runA(InliningState())
.value

val expected = MetaModel
.CallArrowModel(testName)
.wrap(
CallServiceModel(
serviceId = ValueModel.fromRaw(serviceId),
funcName = argMethodName,
call = CallModel(
args = LiteralModel.quote(argMethodName) :: Nil,
exportTo = CallModel.Export(res.name, res.`type`) :: Nil
)
).leaf
)

model.equalsOrShowDiff(expected) shouldEqual true
}

/*
data Prod:
value: string
Expand Down
6 changes: 5 additions & 1 deletion model/raw/src/main/scala/aqua/raw/value/ValueRaw.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ case class CallArrowRaw(

override def renameVars(map: Map[String, String]): ValueRaw =
copy(
name = map.getOrElse(name, name),
name = map
.get(name)
// Rename only if it is **not** a service or ability call, see [bug LNG-199]
.filterNot(_ => ability.isDefined)
.getOrElse(name),
arguments = arguments.map(_.renameVars(map)),
serviceId = serviceId.map(_.renameVars(map))
)
Expand Down

0 comments on commit e22fff7

Please sign in to comment.