From 87df737a2db334a8d7e60c411fae2a7669f5e0a5 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Wed, 22 Oct 2014 15:00:47 -0400 Subject: [PATCH] typing: ANew distinguish the type of the class and the returned type Signed-off-by: Jean Privat --- src/astbuilder.nit | 1 + src/compiler/abstract_compiler.nit | 3 ++- src/interpreter/naive_interpreter.nit | 2 +- src/rapid_type_analysis.nit | 2 +- src/semantize/typing.nit | 13 ++++++++++++- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/astbuilder.nit b/src/astbuilder.nit index cf31299..620bf49 100644 --- a/src/astbuilder.nit +++ b/src/astbuilder.nit @@ -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 diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit index 50f3c8e..c785dd6 100644 --- a/src/compiler/abstract_compiler.nit +++ b/src/compiler/abstract_compiler.nit @@ -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 diff --git a/src/interpreter/naive_interpreter.nit b/src/interpreter/naive_interpreter.nit index 6e4a356..12a54de 100644 --- a/src/interpreter/naive_interpreter.nit +++ b/src/interpreter/naive_interpreter.nit @@ -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) diff --git a/src/rapid_type_analysis.nit b/src/rapid_type_analysis.nit index f63034c..e5e4438 100644 --- a/src/rapid_type_analysis.nit +++ b/src/rapid_type_analysis.nit @@ -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 diff --git a/src/semantize/typing.nit b/src/semantize/typing.nit index 3aa87a8..8c648a0 100644 --- a/src/semantize/typing.nit +++ b/src/semantize/typing.nit @@ -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 -- 1.7.9.5