typing: ANew distinguish the type of the class and the returned type
authorJean Privat <jean@pryen.org>
Wed, 22 Oct 2014 19:00:47 +0000 (15:00 -0400)
committerJean Privat <jean@pryen.org>
Thu, 23 Oct 2014 15:39:44 +0000 (11:39 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/astbuilder.nit
src/compiler/abstract_compiler.nit
src/interpreter/naive_interpreter.nit
src/rapid_type_analysis.nit
src/semantize/typing.nit

index cf31299..620bf49 100644 (file)
@@ -275,6 +275,7 @@ redef class ANewExpr
                        n_args.n_exprs.add_all(args)
                end
                self.callsite = callsite
+               self.recvtype = callsite.recv.as(MClassType)
                if callsite.mproperty.is_new then
                        self.mtype = callsite.msignature.return_mtype
                else
index 50f3c8e..c785dd6 100644 (file)
@@ -2898,7 +2898,8 @@ end
 redef class ANewExpr
        redef fun expr(v)
        do
-               var mtype = self.mtype.as(MClassType)
+               var mtype = self.recvtype
+               assert mtype != null
                var recv
                var ctype = mtype.ctype
                if mtype.mclass.name == "NativeArray" then
index 6e4a356..12a54de 100644 (file)
@@ -1666,7 +1666,7 @@ end
 redef class ANewExpr
        redef fun expr(v)
        do
-               var mtype = v.unanchor_type(self.mtype.as(not null))
+               var mtype = v.unanchor_type(self.recvtype.as(not null))
                var recv: Instance = new MutableInstance(mtype)
                v.init_instance(recv)
                var args = v.varargize(callsite.mpropdef, recv, self.n_args.n_exprs)
index f63034c..e5e4438 100644 (file)
@@ -684,7 +684,7 @@ end
 redef class ANewExpr
        redef fun accept_rapid_type_visitor(v)
        do
-               var mtype = self.mtype.as(MClassType)
+               var mtype = self.recvtype.as(not null)
                v.add_type(mtype)
                v.add_callsite(callsite)
        end
index 3aa87a8..8c648a0 100644 (file)
@@ -1680,11 +1680,13 @@ redef class ANewExpr
        # The constructor invoked by the new.
        var callsite: nullable CallSite
 
+       # The designated type
+       var recvtype: nullable MClassType
+
        redef fun accept_typing(v)
        do
                var recvtype = v.resolve_mtype(self.n_type)
                if recvtype == null then return
-               self.mtype = recvtype
 
                if not recvtype isa MClassType then
                        if recvtype isa MNullableType then
@@ -1704,6 +1706,8 @@ redef class ANewExpr
                        end
                end
 
+               self.recvtype = recvtype
+
                var name: String
                var nid = self.n_id
                if nid != null then
@@ -1714,6 +1718,13 @@ redef class ANewExpr
                var callsite = v.get_method(self, recvtype, name, false)
                if callsite == null then return
 
+               if not callsite.mproperty.is_new then
+                       self.mtype = recvtype
+               else
+                       self.mtype = callsite.msignature.return_mtype
+                       assert self.mtype != null
+               end
+
                self.callsite = callsite
 
                if not callsite.mproperty.is_init_for(recvtype.mclass) then