fix(compiler): Redeclare imports [LNG-344] (#1109)

This commit is contained in:
Dima 2024-04-02 12:04:01 +03:00 committed by GitHub
parent cc69e696dc
commit 8f06ac1cba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 36 additions and 49 deletions

View File

@ -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"

View File

@ -1,6 +1,10 @@
service GetStr("multiret-test"):
retStr(s: *string, a: *string)
aqua Import declares someFunc
service GetStr("multiret-test"):
retStr(s: []string, a: []string)
func someFunc() -> string:
<- "some func call"
func foo_wrapper():
GetStr.retStr(nil, nil)

View File

@ -0,0 +1,4 @@
aqua Import declares someFunc
func someFunc() -> string:
<- "some func"

View File

@ -0,0 +1,3 @@
aqua Redeclare declares someFunc
import someFunc from "import.aqua"

View File

@ -0,0 +1,5 @@
aqua UseDeclare declares someFunc
import someFunc from "redeclare.aqua"
export someFunc

View File

@ -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}
@ -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(

View File

@ -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