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"): export someFunc
fail(msg: string)
func streamTry() -> i8: func a() -> string:
on HOST_PEER_ID: <- "srrt"
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)

View File

@ -1,6 +1,10 @@
service GetStr("multiret-test"): aqua Import declares someFunc
retStr(s: *string, a: *string)
service GetStr("multiret-test"):
retStr(s: []string, a: []string)
func someFunc() -> string:
<- "some func call"
func foo_wrapper(): func foo_wrapper():
GetStr.retStr(nil, nil) 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.foldable.*
import cats.syntax.functor.* import cats.syntax.functor.*
import cats.syntax.option.* import cats.syntax.option.*
import cats.syntax.semigroup.*
import cats.syntax.validated.* import cats.syntax.validated.*
import cats.{Comonad, Monoid} import cats.{Comonad, Monoid}
@ -30,31 +31,32 @@ class ModuleSem[S[_]: Comonad, C: Picker](expr: ModuleExpr[S])(using
name.value, name.value,
shouldDeclare shouldDeclare
), ),
(ctx, _) => (ctx, initCtx) =>
val sumCtx = ctx |+| initCtx
// When file is handled, check that all the declarations exists // When file is handled, check that all the declarations exists
if (declareAll.nonEmpty) if (declareAll.nonEmpty)
ctx.setModule(name.value, declares = ctx.all).validNec val allDeclared = ctx.all ++ initCtx.all
else sumCtx.setModule(name.value, declares = allDeclared).validNec
else {
// summarize contexts to allow redeclaration of imports
( (
declareNames.fproductLeft(_.value) ::: declareCustom.fproductLeft(_.value) declareNames.fproductLeft(_.value) ::: declareCustom.fproductLeft(_.value)
).map { case (n, t) => ).map { case (n, t) =>
ctx sumCtx
.pick(n, None, ctx.module.nonEmpty) .pick(n, None, sumCtx.module.nonEmpty)
.toValidNec( .toValidNec(
error( error(
t, t,
s"`$n` is expected to be declared, but declaration is not found in the file" s"`$n` is expected to be declared, but declaration is not found in the file"
) )
) ).void
.void
}.combineAll.as { }.combineAll.as {
val tokens = declareNames.map(n => n.value -> n) ++ declareCustom.map(a => a.value -> a) 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? // TODO: why module name and declares is lost? where is it lost?
ctxWithDeclaresLoc.setModule(name.value, declares = shouldDeclare) ctxWithDeclaresLoc.setModule(name.value, declares = shouldDeclare)
} }
}
) )
word.value.fold( word.value.fold(

View File

@ -729,7 +729,7 @@ class TypesInterpreter[S[_], X](using
report report
.error( .error(
token, token,
s"Name `${name}` was already defined here" s"Name `$name` was already defined here"
) )
.as(ifDefined) .as(ifDefined)
case None => ifNotDefined case None => ifNotDefined