Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): Add outside context to closures [LNG-317] #1038

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions aqua-src/antithesis.aqua
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
aqua M

export bugLng314
export bugLng317

service MyOp("op"):
identity(s: string) -> string

ability WorkerJob:
runOnSingleWorker(w: string) -> string
runOnSingleWorker(w: string) -> []string

func runJob(j: -> []string) -> []string:
<- j()

func disjoint_run{WorkerJob}() -> -> string:
run = func () -> string:
r <- WorkerJob.runOnSingleWorker()
func disjoint_run{WorkerJob}() -> -> []string:
run = func () -> []string:
r <- WorkerJob.runOnSingleWorker("a")
<- r
<- run

func runJob(j: -> string) -> string:
<- j()
func empty() -> string:
a = "empty"
<- a

func bugLng317() -> []string:

res: *string

outer = () -> string:
<- empty()

func bugLng314() -> string:
job2 = () -> string:
<- "strstrstr"
worker_job = WorkerJob(runOnSingleWorker = job2)
clos = () -> -> []string:
job2 = () -> []string:
res <- outer()
res <- MyOp.identity("identity")
<- res
<- job2
worker_job = WorkerJob(runOnSingleWorker = clos())
subnet_job <- disjoint_run{worker_job}()
res <- runJob(subnet_job)
<- res
finalRes <- runJob(subnet_job)
<- finalRes
38 changes: 36 additions & 2 deletions integration-tests/aqua/examples/closures.aqua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Closure declares *

import "@fluencelabs/aqua-lib/builtin.aqua"

export LocalSrv, closureIn, closureOut, closureBig, closureOut2, lng58Bug, multipleClosuresBugLNG262
export LocalSrv, closureIn, closureOut, closureBig, closureOut2, lng58Bug, multipleClosuresBugLNG262, lng317Bug

service MyOp("op"):
identity(s: string) -> string
Expand Down Expand Up @@ -80,4 +80,38 @@ func create(a: i8) -> -> i8:
func multipleClosuresBugLNG262() -> i8, i8:
arr1 <- create(1)
arr2 <- create(2)
<- arr1(), arr2()
<- arr1(), arr2()

ability WorkerJob:
runOnSingleWorker(w: string) -> []string

func runJob(j: -> []string) -> []string:
<- j()

func disjoint_run{WorkerJob}() -> -> []string:
run = func () -> []string:
r <- WorkerJob.runOnSingleWorker("a")
<- r
<- run

func empty() -> string:
a = "empty"
<- a

func lng317Bug() -> []string:

res: *string

outer = () -> string:
<- empty()

clos = () -> -> []string:
job2 = () -> []string:
res <- outer()
res <- MyOp.identity("identity")
<- res
<- job2
worker_job = WorkerJob(runOnSingleWorker = clos())
subnet_job <- disjoint_run{worker_job}()
finalRes <- runJob(subnet_job)
<- finalRes
6 changes: 6 additions & 0 deletions integration-tests/src/__test__/examples.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ import { lng193BugCall } from "../examples/closureReturnRename.js";
import {
closuresCall,
multipleClosuresLNG262BugCall,
lng317BugCall
} from "../examples/closures.js";
import { closureArrowCaptureCall } from "../examples/closureArrowCapture.js";
import {
Expand Down Expand Up @@ -1112,6 +1113,11 @@ describe("Testing examples", () => {
expect(result).toEqual([1, 2]);
});

it("closures.aqua bug LNG-317", async () => {
let result = await lng317BugCall();
expect(result).toEqual(["empty", "identity"]);
});

it("closureArrowCapture.aqua", async () => {
let result = await closureArrowCaptureCall("input");
expect(result).toEqual("call: ".repeat(4) + "input");
Expand Down
5 changes: 5 additions & 0 deletions integration-tests/src/examples/closures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
registerLocalSrv,
closureOut2,
lng58Bug,
lng317Bug,
multipleClosuresBugLNG262
} from "../compiled/examples/closures.js";
import { config } from "../config.js";
Expand Down Expand Up @@ -37,3 +38,7 @@ export async function lng58CBugCall(): Promise<string> {
export async function multipleClosuresLNG262BugCall(): Promise<[number, number]> {
return multipleClosuresBugLNG262();
}

export async function lng317BugCall(): Promise<string[]> {
return lng317Bug();
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ object ArrowInliner extends Logging {

// Rename arrows according to values
arrowsRenamed = Renamed(
valuesRenamed.renames.filterKeys(abilitiesArrows.keySet).toMap,
valuesRenamed.renames.view.filterKeys(abilitiesArrows.keySet).toMap,
abilitiesArrows.renamed(valuesRenamed.renames)
)

Expand Down
2 changes: 1 addition & 1 deletion model/raw/src/main/scala/aqua/raw/arrow/FuncRaw.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case class FuncRaw(

override def rawPartType: Type = arrow.`type`

def capturedVars: Set[String] = {
lazy val capturedVars: Set[String] = {
val freeBodyVars = arrow.body.usesVarNames.value
val argsNames = arrow.`type`.domain
.toLabelledList()
Expand Down
3 changes: 1 addition & 2 deletions model/raw/src/main/scala/aqua/raw/ops/RawTag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ case class ClosureTag(

override def exportsVarNames: Set[String] = Set(func.name)

// FIXME: Is it correct?
override def usesVarNames: Set[String] = Set.empty
override def usesVarNames: Set[String] = func.capturedVars

override def renameExports(map: Map[String, String]): RawTag =
copy(func =
Expand Down
Loading