modelize_properties: attributes inherit the static type of the getter
authorJean Privat <jean@pryen.org>
Tue, 15 Jul 2014 19:50:17 +0000 (15:50 -0400)
committerJean Privat <jean@pryen.org>
Tue, 15 Jul 2014 19:50:17 +0000 (15:50 -0400)
so, one could write

~~~
interface A
  fun foo: String
end

class B
  super A
  redef var foo # var foo is a String!
end
~~~

Signed-off-by: Jean Privat <jean@pryen.org>

src/modelize_property.nit

index 159523b..9a331e5 100644 (file)
@@ -742,12 +742,21 @@ redef class AAttrPropdef
                var mmodule = mclassdef.mmodule
                var mtype: nullable MType = null
 
+               var mreadpropdef = self.mreadpropdef
+
                var ntype = self.n_type
                if ntype != null then
                        mtype = modelbuilder.resolve_mtype(mmodule, mclassdef, ntype)
                        if mtype == null then return
                end
 
+               # Inherit the type from the getter (usually an abstact getter)
+               if mtype == null and mreadpropdef != null and not mreadpropdef.is_intro then
+                       var msignature = mreadpropdef.mproperty.intro.msignature
+                       if msignature == null then return # Error, thus skiped
+                       mtype = msignature.return_mtype
+               end
+
                var nexpr = self.n_expr
                if mtype == null then
                        if nexpr != null then
@@ -775,11 +784,9 @@ redef class AAttrPropdef
                                        modelbuilder.error(self, "Error: Untyped attribute {mpropdef}. Implicit typing allowed only for literals and new.")
                                end
 
-                       else
-                               modelbuilder.error(self, "Error: Untyped attribute {mpropdef}")
+                               if mtype == null then return
                        end
-               else
-                       assert ntype != null
+               else if ntype != null then
                        if nexpr isa ANewExpr then
                                var xmtype = modelbuilder.resolve_mtype(mmodule, mclassdef, nexpr.n_type)
                                if xmtype == mtype and modelbuilder.toolcontext.opt_warn.value >= 2 then
@@ -788,11 +795,13 @@ redef class AAttrPropdef
                        end
                end
 
-               if mtype == null then return
+               if mtype == null then
+                       modelbuilder.error(self, "Error: Untyped attribute {mpropdef}")
+                       return
+               end
 
                mpropdef.static_mtype = mtype
 
-               var mreadpropdef = self.mreadpropdef
                if mreadpropdef != null then
                        var msignature = new MSignature(new Array[MParameter], mtype)
                        mreadpropdef.msignature = msignature