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): Push to stream in closures [LNG-365] #1157

Merged
merged 6 commits into from
Jun 18, 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
57 changes: 41 additions & 16 deletions aqua-src/antithesis.aqua
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
aqua StreamMapTest declares *

export testGetFunc, Srv

service Srv("baaa"):
g() -> string

func testGetFunc() -> []string, []string, []string, u32:
streamMap: %string
key = "key"
resEmpty = streamMap.get(key)
streamMap <<- key, "first value"
resFirst = streamMap.get(key)
streamMap <<- key, "second value"
resSecond = streamMap.get(key)
<- resEmpty, resFirst, resSecond, resSecond.length
aqua StreamMapAbilities

export streamMapAbilityTest

ability Streams:
stream: *string
map: %string

ability Adds:
addToStream(s: string)
addToMap(k: string, v: string)

func addToStreamClosure(str: *string) -> string -> ():
cl = func (s: string):
str <<- s
<- cl

func addToMapClosure(str: %string) -> string, string -> ():
cl = func (k: string, v: string):
str <<- k, v
<- cl

func addTo{Streams}() -> Adds:
addStream = addToStreamClosure(Streams.stream)
addMap = addToMapClosure(Streams.map)
adds = Adds(addToStream = addStream, addToMap = addMap)
<- adds

func add{Adds}(s: string, k: string):
Adds.addToStream(s)
Adds.addToMap(k, k)

func streamMapAbilityTest() -> []string, []string:
stream: *string
map: %string
ab = Streams(stream = stream, map = map)
adds <- addTo{ab}()
add{adds}("one", "1")
add{adds}("two", "2")
add{adds}("three", "3")
<- stream, map.keys()
40 changes: 39 additions & 1 deletion integration-tests/aqua/examples/closureReturnRename.aqua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
aqua ClosureReturnRename

export lng193Bug
export lng193Bug, lng365Bug

func getClosure(arg: u16, peer: string) -> u16 -> u16:
on peer:
Expand All @@ -16,3 +16,41 @@ func lng193Bug(peer: string, closurePeer: string) -> u16:
res1 = a(1) + a(2) -- Call two times for
res2 = b(3) + b(4) -- bug to appear
<- res1 + res2

ability Streams:
stream: *string
map: %string

ability Adds:
addToStream(s: string)
addToMap(k: string, v: string)

func addToStreamClosure(str: *string) -> string -> ():
cl = func (s: string):
str <<- s
<- cl

func addToMapClosure(str: %string) -> string, string -> ():
cl = func (k: string, v: string):
str <<- k, v
<- cl

func addTo{Streams}() -> Adds:
addStream = addToStreamClosure(Streams.stream)
addMap = addToMapClosure(Streams.map)
adds = Adds(addToStream = addStream, addToMap = addMap)
<- adds

func add{Adds}(s: string, k: string):
Adds.addToStream(s)
Adds.addToMap(k, k)

func lng365Bug() -> []string, []string:
stream: *string
map: %string
ab = Streams(stream = stream, map = map)
adds <- addTo{ab}()
add{adds}("one", "1")
add{adds}("two", "2")
add{adds}("three", "3")
<- stream, map.keys()
8 changes: 7 additions & 1 deletion integration-tests/src/__test__/examples.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ import { literalCall } from "../examples/returnLiteralCall.js";
import { multiReturnCall } from "../examples/multiReturnCall.js";
import { declareCall } from "../examples/declareCall.js";
import { genOptions, genOptionsEmptyString } from "../examples/optionsCall.js";
import { lng193BugCall } from "../examples/closureReturnRename.js";
import { lng193BugCall, lng365BugCall } from "../examples/closureReturnRename.js";
import {
closuresCall,
multipleClosuresLNG262BugCall,
Expand Down Expand Up @@ -1232,6 +1232,12 @@ describe("Testing examples", () => {
expect(result).toEqual(1 + 42 + (2 + 42) + (3 + 42) + (4 + 42));
}, 20000);

it("closureReturnRename.aqua bug LNG-365", async () => {
const [values, keys] = await lng365BugCall();
expect(values).toEqual(["one", "two", "three"]);
expect(keys).toEqual(["1", "2", "3"]);
});

it("closures.aqua", async () => {
const closuresResult = await closuresCall();
const res1 = config.externalAddressesRelay2;
Expand Down
6 changes: 5 additions & 1 deletion integration-tests/src/examples/closureReturnRename.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { lng193Bug } from "../compiled/examples/closureReturnRename.js";
import { lng193Bug, lng365Bug } from "../compiled/examples/closureReturnRename.js";
import { config } from "../config.js";

const relays = config.relays;

export async function lng193BugCall(): Promise<number> {
return lng193Bug(relays[4].peerId, relays[5].peerId);
}

export async function lng365BugCall() {
return lng365Bug();
}
5 changes: 4 additions & 1 deletion model/raw/src/main/scala/aqua/raw/ops/RawTag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,10 @@ case class ClosureTag(
override def renameExports(map: Map[String, String]): RawTag =
copy(func =
func.copy(
name = map.getOrElse(func.name, func.name)
name = map.getOrElse(func.name, func.name),
arrow = func.arrow.copy(
body = func.arrow.body.map(_.renameExports(map))
)
)
)

Expand Down
Loading