Skip to content

Commit

Permalink
fix(compiler): Redeclare imports [LNG-344] (#1109)
Browse files Browse the repository at this point in the history
  • Loading branch information
DieMyst authored Apr 2, 2024
1 parent cc69e69 commit 8f06ac1
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 48 deletions.
41 changes: 5 additions & 36 deletions aqua-src/antithesis.aqua
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
aqua A
aqua Main

export streamTry, streamFor
import someFunc from "import.aqua"

service FailureSrv("failure"):
fail(msg: string)
export someFunc

func streamTry() -> i8:
on HOST_PEER_ID:
try:
stream: *i8
anotherStream = stream
stream <<- 1
anotherStream <<- 1
FailureSrv.fail("try")
catch e:
stream = *[88,88,88]
stream <<- 2
FailureSrv.fail("catch")
otherwise:
stream: *i8
stream <<- 3

stream: *i8
stream <<- 4

<- stream!

service StreamService("test-service"):
store(numbers: []u32, n: u32)

func callService(stream: *u32, n: u32):
stream <<- 1
StreamService.store(stream, n)

func streamFor():
arr = [1,2,3,4,5]
for a <- arr:
callService(*[], a)
func a() -> string:
<- "srrt"
6 changes: 5 additions & 1 deletion aqua-src/import.aqua
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
aqua Import declares someFunc

service GetStr("multiret-test"):
retStr(s: *string, a: *string)
retStr(s: []string, a: []string)

func someFunc() -> string:
<- "some func call"

func foo_wrapper():
GetStr.retStr(nil, nil)
Expand Down
4 changes: 4 additions & 0 deletions integration-tests/aqua/examples/redeclare/import.aqua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
aqua Import declares someFunc

func someFunc() -> string:
<- "some func"
3 changes: 3 additions & 0 deletions integration-tests/aqua/examples/redeclare/redeclare.aqua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
aqua Redeclare declares someFunc

import someFunc from "import.aqua"
5 changes: 5 additions & 0 deletions integration-tests/aqua/examples/redeclare/useDeclare.aqua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
aqua UseDeclare declares someFunc

import someFunc from "redeclare.aqua"

export someFunc
22 changes: 12 additions & 10 deletions semantics/src/main/scala/aqua/semantics/header/ModuleSem.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import cats.kernel.Semigroup
import cats.syntax.foldable.*
import cats.syntax.functor.*
import cats.syntax.option.*
import cats.syntax.semigroup.*
import cats.syntax.validated.*
import cats.{Comonad, Monoid}

Expand All @@ -30,31 +31,32 @@ class ModuleSem[S[_]: Comonad, C: Picker](expr: ModuleExpr[S])(using
name.value,
shouldDeclare
),
(ctx, _) =>
(ctx, initCtx) =>
val sumCtx = ctx |+| initCtx
// When file is handled, check that all the declarations exists
if (declareAll.nonEmpty)
ctx.setModule(name.value, declares = ctx.all).validNec
else
val allDeclared = ctx.all ++ initCtx.all
sumCtx.setModule(name.value, declares = allDeclared).validNec
else {
// summarize contexts to allow redeclaration of imports
(
declareNames.fproductLeft(_.value) ::: declareCustom.fproductLeft(_.value)
).map { case (n, t) =>
ctx
.pick(n, None, ctx.module.nonEmpty)
sumCtx
.pick(n, None, sumCtx.module.nonEmpty)
.toValidNec(
error(
t,
s"`$n` is expected to be declared, but declaration is not found in the file"
)
)
.void
).void
}.combineAll.as {
val tokens = declareNames.map(n => n.value -> n) ++ declareCustom.map(a => a.value -> a)
val ctxWithDeclaresLoc = ctx.addOccurences(tokens)
val ctxWithDeclaresLoc = sumCtx.addOccurences(tokens)
// TODO: why module name and declares is lost? where is it lost?
ctxWithDeclaresLoc.setModule(name.value, declares = shouldDeclare)
}


}
)

word.value.fold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ class TypesInterpreter[S[_], X](using
report
.error(
token,
s"Name `${name}` was already defined here"
s"Name `$name` was already defined here"
)
.as(ifDefined)
case None => ifNotDefined
Expand Down

0 comments on commit 8f06ac1

Please sign in to comment.