Check number of type arguments in object creation expression, patch by
authorJuerg Billeter <j@bitron.ch>
Thu, 8 May 2008 19:01:40 +0000 (19:01 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Thu, 8 May 2008 19:01:40 +0000 (19:01 +0000)
2008-05-08  Juerg Billeter  <j@bitron.ch>

* vala/valasemanticanalyzer.vala:
* vala/valastruct.vala:

Check number of type arguments in object creation expression,
patch by Andreas Brauchli, fixes bug 528833

svn path=/trunk/; revision=1341

ChangeLog
vala/valasemanticanalyzer.vala
vala/valastruct.vala

index cfee637..cdccbaa 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2008-05-08  Jürg Billeter  <j@bitron.ch>
+
+       * vala/valasemanticanalyzer.vala:
+       * vala/valastruct.vala:
+
+       Check number of type arguments in object creation expression,
+       patch by Andreas Brauchli, fixes bug 528833
+
 2008-05-08  Jaap A. Haitsma  <jaap@haitsma.org>
 
        reviewed by: Jürg Billeter
index 91f1cf5..e594fda 100644 (file)
@@ -2313,9 +2313,14 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                expr.static_type = expr.type_reference.copy ();
                expr.static_type.transfers_ownership = true;
 
+               int given_num_type_args = expr.type_reference.get_type_arguments ().size;
+               int expected_num_type_args = 0;
+
                if (type is Class) {
                        var cl = (Class) type;
 
+                       expected_num_type_args = cl.get_type_parameters ().size;
+
                        if (expr.struct_creation) {
                                expr.error = true;
                                Report.error (expr.source_reference, "syntax error, use `new' to create new objects");
@@ -2344,6 +2349,8 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                } else if (type is Struct) {
                        var st = (Struct) type;
 
+                       expected_num_type_args = st.get_type_parameters ().size;
+
                        if (!expr.struct_creation) {
                                Report.warning (expr.source_reference, "deprecated syntax, don't use `new' to initialize structs");
                        }
@@ -2355,6 +2362,16 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                        }
                }
 
+               if (expected_num_type_args > given_num_type_args) {
+                       expr.error = true;
+                       Report.error (expr.source_reference, "too few type arguments");
+                       return;
+               } else if (expected_num_type_args < given_num_type_args) {
+                       expr.error = true;
+                       Report.error (expr.source_reference, "too many type arguments");
+                       return;
+               }
+
                if (expr.symbol_reference == null && expr.get_argument_list ().size != 0) {
                        expr.static_type = null;
                        expr.error = true;
index 8b13fb9..bd97bd0 100644 (file)
@@ -80,6 +80,15 @@ public class Vala.Struct : Typesymbol {
        }
        
        /**
+        * Returns a copy of the type parameter list.
+        *
+        * @return list of type parameters
+        */
+       public Collection<TypeParameter> get_type_parameters () {
+               return new ReadOnlyCollection<TypeParameter> (type_parameters);
+       }
+
+       /**
         * Adds the specified constant as a member to this struct.
         *
         * @param c a constant