typing: adapt for typed literal arrays.
authorJean Privat <jean@pryen.org>
Wed, 24 Sep 2014 20:46:43 +0000 (16:46 -0400)
committerJean Privat <jean@pryen.org>
Thu, 25 Sep 2014 17:56:47 +0000 (13:56 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/semantize/typing.nit

index 8851413..7015efd 100644 (file)
@@ -1097,19 +1097,37 @@ redef class AArrayExpr
 
        redef fun accept_typing(v)
        do
+               var mtype: nullable MType = null
+               var ntype = self.n_type
+               if ntype != null then
+                       mtype = v.resolve_mtype(ntype)
+                       if mtype == null then return # Skip error
+               end
                var mtypes = new Array[nullable MType]
+               var useless = false
                for e in self.n_exprs.n_exprs do
                        var t = v.visit_expr(e)
                        if t == null then
                                return # Skip error
                        end
-                       mtypes.add(t)
+                       if mtype != null then
+                               if v.check_subtype(e, t, mtype) == null then return # Skip error
+                               if t == mtype then useless = true
+                       else
+                               mtypes.add(t)
+                       end
+               end
+               if mtype == null then
+                       mtype = v.merge_types(self, mtypes)
                end
-               var mtype = v.merge_types(self, mtypes)
                if mtype == null then
                        v.error(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
                        return
                end
+               if useless then
+                       assert ntype != null
+                       v.modelbuilder.warning(ntype, "useless-type", "Warning: useless type declaration `{mtype}` in literal Array since it can be inferred from the elements type.")
+               end
                var mclass = v.get_mclass(self, "Array")
                if mclass == null then return # Forward error
                var array_mtype = mclass.get_mtype([mtype])