Skip to content

Commit

Permalink
Fixed error on non-primitive type in closure
Browse files Browse the repository at this point in the history
  • Loading branch information
PhucVH888 committed Jun 13, 2017
1 parent c32fd11 commit 84b763f
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/tests/encore/forward/forwardArgInClosure.enc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ end

active class Foo
def join(ff : Fut[Fut[int]]) : int
forward(ff ~~> (fun (f : Fut[int]) : int => forward(f)))
(ff ~~> (fun (f : Fut[int]) : unit => forward(f)))
0 -- Will be disregarded since it is used to bypass the typechecker.
end

def duplicate() : Fut[int]
Expand Down
22 changes: 22 additions & 0 deletions src/tests/encore/forward/forwardClosMaybe.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
active class Crop
var v : Maybe[int]
def init(v : Maybe[int]) : unit
this.v = v
end
def collect() : Maybe[int]
this.v
end
end
active class Pepper
def green(arg : Fut[Maybe[int]]) : Maybe[int]
get(arg ~~> fun(x : Maybe[int]) : unit => forward((new Crop(x)) ! collect()))
Nothing : Maybe[int]
end
end
active class Main
def main() : unit
val arg = (new Crop((Just(42)))) ! collect()
val tem = (new Pepper) ! green(arg)
println("{}", get(tem))
end
end
1 change: 1 addition & 0 deletions src/tests/encore/forward/forwardClosMaybe.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Just 42
31 changes: 31 additions & 0 deletions src/tests/encore/forward/forwardClosPassiveObject.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
read class Mellon
val v : int
def init(v : int) : unit
this. v = v
end
def grow() : int
this.v
end
end
active class Crop
var v : Mellon
def init(v : Mellon) : unit
this.v = v
end
def collect() : Mellon
this.v
end
end
active class Pepper
def green(arg : Fut[Mellon]) : Mellon
(arg ~~> fun(x : Mellon) : unit => forward((new Crop(x)) ! collect()))
new Mellon(42000000) -- Will be disregarded since it is used to bypass the typechecker.
end
end
active class Main
def main() : unit
val mellon = new Mellon(42)
val arg = (new Crop(mellon)) ! collect()
println("{}", (get(arg)).grow())
end
end
1 change: 1 addition & 0 deletions src/tests/encore/forward/forwardClosPassiveObject.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
23 changes: 23 additions & 0 deletions src/tests/encore/forward/forwardClosString.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
active class Crop
var v : String
def init(v : String) : unit
this.v = v
end
def collect() : String
this.v
end
end
active class Pepper
def green(arg : Fut[String]) : String
(arg ~~> fun(x : String) : unit => forward((new Crop(x)) ! collect()))
"Will be disregarded since it is used to bypass the typechecker."
end
end

active class Main
def main() : unit
val arg = (new Crop("42")) ! collect()
val tem = (new Pepper) ! green(arg)
println("{}", get(tem))
end
end
1 change: 1 addition & 0 deletions src/tests/encore/forward/forwardClosString.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
24 changes: 24 additions & 0 deletions src/tests/encore/forward/forwardClosTuple.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
typedef Tuple = (int, real)

active class Crop
var v : Tuple
def init(v : Tuple) : unit
this.v = v
end
def collect() : Tuple
this.v
end
end
active class Pepper
def green(arg : Fut[Tuple]) : Tuple
(arg ~~> fun(x : Tuple) : unit => forward((new Crop(x)) ! collect()))
(0, 0.0) -- Will be disregarded since it is used to bypass the typechecker.
end
end
active class Main
def main() : unit
val arg = (new Crop((42, 42.0))) ! collect()
val tem = (new Pepper) ! green(arg)
println("{}", get(tem))
end
end
1 change: 1 addition & 0 deletions src/tests/encore/forward/forwardClosTuple.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(42, 42.000000)
22 changes: 13 additions & 9 deletions src/tests/encore/forward/forwardInClosure.enc
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
active class Main
def main() : unit
val fres = (this!foo() ~~> fun (x : int) : int
val tem = this ! inc(x)
await(tem)
forward(tem)
end)
await(fres)
println("{}", get(fres))
val foo = this ! foo()
await(foo)
val bar = this ! bar(foo)
await(bar)
println("{}", get(bar))
end
def bar(foo : Fut[int]) : int
(foo ~~> fun (x : int) : unit
val inc = this ! inc(x)
await(inc)
forward(inc)
end)
0 -- Will be disregarded since it is used to bypass the typechecker.
end

def foo() : int
42
end

def inc(x : int) : int
x + 1
end
Expand Down
9 changes: 5 additions & 4 deletions src/tests/encore/forward/forwardInClosurePolyType.enc
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ active class Base[t]
end
end
active class Foo[sharable ty]
def foo(arg : Fut[ty]) : ty
get(arg ~~> fun(x : ty) : ty => forward((new Base[ty](x)) ! base()))
def foo(arg : Fut[ty], arg2 : Fut[ty]) : ty
(arg ~~> fun(x : ty) : unit => forward((new Base[ty](x)) ! base()))
get(arg2) -- Will be disregarded since it is used to bypass the typechecker.
end
end

active class Main
def main() : unit
val arg = (new Base[int](42)) ! base()
println("{}", get((new Foo[int]) ! foo(arg)))
val arg2 = (new Base[int](123456)) ! base()
println("{}", get((new Foo[int]) ! foo(arg, arg2)))
end
end
5 changes: 2 additions & 3 deletions src/tests/encore/forward/forwardMethodCallInClosureBody.enc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ active class Base
42
end
end

active class Foo
def foo(arg : Fut[int]) : int
forward(arg ~~> fun(x : int) : int => forward((new Base) ! base()))
(arg ~~> fun(x : int) : unit => forward((new Base) ! base()))
0
end
end

active class Main
def main() : unit
val arg = (new Base) ! base()
Expand Down
4 changes: 2 additions & 2 deletions src/types/Typechecker/TypeError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,8 @@ instance Show Error where
"the result type of the containing method %s")
(show retType) (show ty)
show (ForwardTypeClosError retType ty) =
printf ("Returned type %s of forward should match with " ++
"the result type of the closure %s")
printf ("Result type %s of the closure should match with " ++
"the return type %s of the forward")
(show retType) (show ty)
show (ForwardInPassiveContext cname) =
printf "Forward can not be used in passive class '%s'"
Expand Down
6 changes: 3 additions & 3 deletions src/types/Typechecker/Typechecker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,9 +1268,9 @@ instance Checkable Expr where
return $ setType (getResultType ty) forward {forwardExpr = eExpr}
ClosureContext (Just mty) -> do
mty' <- resolveType mty
unlessM (getResultType ty `subtypeOf` mty') $
pushError eExpr $ ForwardTypeClosError mty' ty
return $ setType (getResultType ty) forward {forwardExpr = eExpr}
unless (isUnitType mty') $
pushError eExpr $ ForwardTypeClosError mty' unitType
return $ setType (unitType) forward {forwardExpr = eExpr}
ClosureContext Nothing -> tcError ClosureForwardError
_ -> pushError eExpr ForwardInFunction

Expand Down

0 comments on commit 84b763f

Please sign in to comment.