model: infer the array types of attributes
authorAlexis Laferrière <alexis.laf@xymus.net>
Thu, 15 Feb 2018 16:18:27 +0000 (11:18 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Thu, 15 Feb 2018 18:28:09 +0000 (13:28 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

src/modelize/modelize_property.nit

index aabe809..4db4790 100644 (file)
@@ -1489,6 +1489,27 @@ redef class AAttrPropdef
                                abort
                        end
                        if cla != null then mtype = cla.mclass_type
+               else if nexpr isa AArrayExpr and nexpr.n_type == null and nexpr.n_exprs.not_empty then
+                       # Non-empty arrays without an explicit type
+
+                       var item_mtypes = new Set[MType]
+                       var fails = false
+                       for node in nexpr.n_exprs do
+                               var item_mtype = infer_static_type(modelbuilder, node, mclassdef, mmodule, mreadpropdef)
+                               if item_mtype == null then
+                                       fails = true
+                               else
+                                       item_mtypes.add item_mtype
+                               end
+                       end
+
+                       if fails then return null # Failed to infer some types
+
+                       if item_mtypes.length > 1 then
+                               modelbuilder.error(self, "Type Error: ambiguous array type {item_mtypes.join(" ")}")
+                       end
+
+                       mtype = mmodule.array_type(item_mtypes.first)
                else
                        modelbuilder.error(self, "Error: untyped attribute `{mreadpropdef}`. Implicit typing allowed only for literals and new.")
                end