From e9e23e2849cbe590416d79d70b8ecc83f04f43bf Mon Sep 17 00:00:00 2001 From: Elias Castegren Date: Thu, 1 Jun 2017 21:58:29 +0200 Subject: [PATCH 1/2] Allow closures to use method type parameters This commit fixes a bug where closures did not properly capture type parameters of methods. A test has been added. Fixes #809. --- src/back/CodeGen/MethodDecl.hs | 2 +- .../encore/parametric/methodPolyClosure.enc | 18 ++++++++++++++++++ .../encore/parametric/methodPolyClosure.out | 1 + 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/tests/encore/parametric/methodPolyClosure.enc create mode 100644 src/tests/encore/parametric/methodPolyClosure.out diff --git a/src/back/CodeGen/MethodDecl.hs b/src/back/CodeGen/MethodDecl.hs index 7a50c77b0..411633723 100644 --- a/src/back/CodeGen/MethodDecl.hs +++ b/src/back/CodeGen/MethodDecl.hs @@ -122,7 +122,7 @@ translateGeneral mdecl@(A.Method {A.mbody, A.mlocals}) (Deref $ Cast (Ptr . AsType $ classTypeName cname) thisVar) `Dot` name - closures = map (\clos -> translateClosure clos typeVars newTable) + closures = map (\clos -> translateClosure clos (typeVars ++ mTypeVars) newTable) (reverse (Util.filter A.isClosure mbody)) localize cls prefix fun = diff --git a/src/tests/encore/parametric/methodPolyClosure.enc b/src/tests/encore/parametric/methodPolyClosure.enc new file mode 100644 index 000000000..a990de830 --- /dev/null +++ b/src/tests/encore/parametric/methodPolyClosure.enc @@ -0,0 +1,18 @@ +read class Bar[t] + def bar() : unit + println("In Bar") + end +end + +read class Foo + def foo[ty]() : () -> unit + fun() => (new Bar[ty]()).bar() + end +end + +active class Main + def main() : unit + val f = (new Foo()).foo[int]() + f() + end +end diff --git a/src/tests/encore/parametric/methodPolyClosure.out b/src/tests/encore/parametric/methodPolyClosure.out new file mode 100644 index 000000000..b3834d927 --- /dev/null +++ b/src/tests/encore/parametric/methodPolyClosure.out @@ -0,0 +1 @@ +In Bar From eb504816349cf0ff342c0d4158963dca1a5ed056 Mon Sep 17 00:00:00 2001 From: EliasC Date: Fri, 2 Jun 2017 09:21:58 +0200 Subject: [PATCH 2/2] Added missing type variables when building closure environment --- src/back/CodeGen/MethodDecl.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/back/CodeGen/MethodDecl.hs b/src/back/CodeGen/MethodDecl.hs index 411633723..a543095fc 100644 --- a/src/back/CodeGen/MethodDecl.hs +++ b/src/back/CodeGen/MethodDecl.hs @@ -101,7 +101,7 @@ translateGeneral mdecl@(A.Method {A.mbody, A.mlocals}) argNames = map (AsLval . argName) encArgNames argTypes = map translate encArgTypes subst = [(ID.thisName, thisVar)] ++ - varSubFromTypeVars typeVars ++ + varSubFromTypeVars (typeVars ++ mTypeVars) ++ zip encArgNames argNames ctx = Ctx.setMtdCtx (Ctx.new subst newTable) mdecl forwardingCtx = Ctx.setMtdCtx(Ctx.newWithForwarding subst newTable) mdecl