Skip to content

Commit 84b763f

Browse files
committed
Fixed error on non-primitive type in closure
1 parent c32fd11 commit 84b763f

14 files changed

+131
-22
lines changed

src/tests/encore/forward/forwardArgInClosure.enc

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ end
66

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

1213
def duplicate() : Fut[int]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
active class Crop
2+
var v : Maybe[int]
3+
def init(v : Maybe[int]) : unit
4+
this.v = v
5+
end
6+
def collect() : Maybe[int]
7+
this.v
8+
end
9+
end
10+
active class Pepper
11+
def green(arg : Fut[Maybe[int]]) : Maybe[int]
12+
get(arg ~~> fun(x : Maybe[int]) : unit => forward((new Crop(x)) ! collect()))
13+
Nothing : Maybe[int]
14+
end
15+
end
16+
active class Main
17+
def main() : unit
18+
val arg = (new Crop((Just(42)))) ! collect()
19+
val tem = (new Pepper) ! green(arg)
20+
println("{}", get(tem))
21+
end
22+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Just 42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
read class Mellon
2+
val v : int
3+
def init(v : int) : unit
4+
this. v = v
5+
end
6+
def grow() : int
7+
this.v
8+
end
9+
end
10+
active class Crop
11+
var v : Mellon
12+
def init(v : Mellon) : unit
13+
this.v = v
14+
end
15+
def collect() : Mellon
16+
this.v
17+
end
18+
end
19+
active class Pepper
20+
def green(arg : Fut[Mellon]) : Mellon
21+
(arg ~~> fun(x : Mellon) : unit => forward((new Crop(x)) ! collect()))
22+
new Mellon(42000000) -- Will be disregarded since it is used to bypass the typechecker.
23+
end
24+
end
25+
active class Main
26+
def main() : unit
27+
val mellon = new Mellon(42)
28+
val arg = (new Crop(mellon)) ! collect()
29+
println("{}", (get(arg)).grow())
30+
end
31+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
active class Crop
2+
var v : String
3+
def init(v : String) : unit
4+
this.v = v
5+
end
6+
def collect() : String
7+
this.v
8+
end
9+
end
10+
active class Pepper
11+
def green(arg : Fut[String]) : String
12+
(arg ~~> fun(x : String) : unit => forward((new Crop(x)) ! collect()))
13+
"Will be disregarded since it is used to bypass the typechecker."
14+
end
15+
end
16+
17+
active class Main
18+
def main() : unit
19+
val arg = (new Crop("42")) ! collect()
20+
val tem = (new Pepper) ! green(arg)
21+
println("{}", get(tem))
22+
end
23+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
typedef Tuple = (int, real)
2+
3+
active class Crop
4+
var v : Tuple
5+
def init(v : Tuple) : unit
6+
this.v = v
7+
end
8+
def collect() : Tuple
9+
this.v
10+
end
11+
end
12+
active class Pepper
13+
def green(arg : Fut[Tuple]) : Tuple
14+
(arg ~~> fun(x : Tuple) : unit => forward((new Crop(x)) ! collect()))
15+
(0, 0.0) -- Will be disregarded since it is used to bypass the typechecker.
16+
end
17+
end
18+
active class Main
19+
def main() : unit
20+
val arg = (new Crop((42, 42.0))) ! collect()
21+
val tem = (new Pepper) ! green(arg)
22+
println("{}", get(tem))
23+
end
24+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(42, 42.000000)

src/tests/encore/forward/forwardInClosure.enc

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
active class Main
22
def main() : unit
3-
val fres = (this!foo() ~~> fun (x : int) : int
4-
val tem = this ! inc(x)
5-
await(tem)
6-
forward(tem)
7-
end)
8-
await(fres)
9-
println("{}", get(fres))
3+
val foo = this ! foo()
4+
await(foo)
5+
val bar = this ! bar(foo)
6+
await(bar)
7+
println("{}", get(bar))
8+
end
9+
def bar(foo : Fut[int]) : int
10+
(foo ~~> fun (x : int) : unit
11+
val inc = this ! inc(x)
12+
await(inc)
13+
forward(inc)
14+
end)
15+
0 -- Will be disregarded since it is used to bypass the typechecker.
1016
end
11-
1217
def foo() : int
1318
42
1419
end
15-
1620
def inc(x : int) : int
1721
x + 1
1822
end

src/tests/encore/forward/forwardInClosurePolyType.enc

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ active class Base[t]
99
end
1010
end
1111
active class Foo[sharable ty]
12-
def foo(arg : Fut[ty]) : ty
13-
get(arg ~~> fun(x : ty) : ty => forward((new Base[ty](x)) ! base()))
12+
def foo(arg : Fut[ty], arg2 : Fut[ty]) : ty
13+
(arg ~~> fun(x : ty) : unit => forward((new Base[ty](x)) ! base()))
14+
get(arg2) -- Will be disregarded since it is used to bypass the typechecker.
1415
end
1516
end
16-
1717
active class Main
1818
def main() : unit
1919
val arg = (new Base[int](42)) ! base()
20-
println("{}", get((new Foo[int]) ! foo(arg)))
20+
val arg2 = (new Base[int](123456)) ! base()
21+
println("{}", get((new Foo[int]) ! foo(arg, arg2)))
2122
end
2223
end

src/tests/encore/forward/forwardMethodCallInClosureBody.enc

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ active class Base
33
42
44
end
55
end
6-
76
active class Foo
87
def foo(arg : Fut[int]) : int
9-
forward(arg ~~> fun(x : int) : int => forward((new Base) ! base()))
8+
(arg ~~> fun(x : int) : unit => forward((new Base) ! base()))
9+
0
1010
end
1111
end
12-
1312
active class Main
1413
def main() : unit
1514
val arg = (new Base) ! base()

src/types/Typechecker/TypeError.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,8 @@ instance Show Error where
726726
"the result type of the containing method %s")
727727
(show retType) (show ty)
728728
show (ForwardTypeClosError retType ty) =
729-
printf ("Returned type %s of forward should match with " ++
730-
"the result type of the closure %s")
729+
printf ("Result type %s of the closure should match with " ++
730+
"the return type %s of the forward")
731731
(show retType) (show ty)
732732
show (ForwardInPassiveContext cname) =
733733
printf "Forward can not be used in passive class '%s'"

src/types/Typechecker/Typechecker.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1268,9 +1268,9 @@ instance Checkable Expr where
12681268
return $ setType (getResultType ty) forward {forwardExpr = eExpr}
12691269
ClosureContext (Just mty) -> do
12701270
mty' <- resolveType mty
1271-
unlessM (getResultType ty `subtypeOf` mty') $
1272-
pushError eExpr $ ForwardTypeClosError mty' ty
1273-
return $ setType (getResultType ty) forward {forwardExpr = eExpr}
1271+
unless (isUnitType mty') $
1272+
pushError eExpr $ ForwardTypeClosError mty' unitType
1273+
return $ setType (unitType) forward {forwardExpr = eExpr}
12741274
ClosureContext Nothing -> tcError ClosureForwardError
12751275
_ -> pushError eExpr ForwardInFunction
12761276

0 commit comments

Comments
 (0)