]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Better error message for invalid number of arguments
authorMarc-André Lureau <marcandre.lureau@gmail.com>
Wed, 27 Jan 2010 00:31:09 +0000 (01:31 +0100)
committerJürg Billeter <j@bitron.ch>
Wed, 4 Aug 2010 14:53:16 +0000 (16:53 +0200)
Fixes bug 608187.

vala/valacodewriter.vala
vala/valadatatype.vala
vala/valamethodtype.vala
vala/valasemanticanalyzer.vala

index 6adb5bc2180f04203247f96df5e79bfb43617406..e3ca81b148fcf2683589f09ec24fd668ac4ec3db 100644 (file)
@@ -741,7 +741,7 @@ public class Vala.CodeWriter : CodeVisitor {
                        write_string ("class ");
                }
 
-               if (is_weak (f.variable_type)) {
+               if (f.variable_type.is_weak ()) {
                        write_string ("weak ");
                }
 
@@ -836,7 +836,7 @@ public class Vala.CodeWriter : CodeVisitor {
                                } else if (param.direction == ParameterDirection.OUT) {
                                        write_string ("out ");
                                }
-                               if (is_weak (param.variable_type)) {
+                               if (param.variable_type.is_weak ()) {
                                        write_string ("unowned ");
                                }
                        }
@@ -1823,31 +1823,13 @@ public class Vala.CodeWriter : CodeVisitor {
        }
 
        private void write_return_type (DataType type) {
-               if (is_weak (type)) {
+               if (type.is_weak ()) {
                        write_string ("unowned ");
                }
 
                write_type (type);
        }
 
-       private bool is_weak (DataType type) {
-               if (type.value_owned) {
-                       return false;
-               } else if (type is VoidType || type is PointerType) {
-                       return false;
-               } else if (type is ValueType) {
-                       if (type.nullable) {
-                               // nullable structs are heap allocated
-                               return true;
-                       }
-
-                       // TODO return true for structs with destroy
-                       return false;
-               }
-
-               return true;
-       }
-
        private void write_type (DataType type) {
                write_string (type.to_qualified_string (current_scope));
        }
index 2e964f368fbd4c18760fdab44c16b6a00dac0e6b..67bba3245a969a6101860052adc8c83e8a6d4a19 100644 (file)
@@ -520,4 +520,22 @@ public abstract class Vala.DataType : CodeNode {
 
                return result;
        }
+
+       public bool is_weak () {
+               if (this.value_owned) {
+                       return false;
+               } else if (this is VoidType || this is PointerType) {
+                       return false;
+               } else if (this is ValueType) {
+                       if (this.nullable) {
+                               // nullable structs are heap allocated
+                               return true;
+                       }
+
+                       // TODO return true for structs with destroy
+                       return false;
+               }
+
+               return true;
+       }
 }
index a2ad6d4c097ff37de429ba434aa2324780a95bd3..87ec555b92effb55c4e319ca374c13ce222825ed 100644 (file)
@@ -76,4 +76,45 @@ public class Vala.MethodType : DataType {
                }
                return null;
        }
+
+       public string to_prototype_string (bool with_type_parameters = false) {
+               var proto = "%s %s (".printf (get_return_type ().to_string (), this.to_string ());
+
+               int i = 1;
+               foreach (FormalParameter param in get_parameters ()) {
+                       if (i > 1) {
+                               proto += ", ";
+                       }
+
+                       if (param.ellipsis) {
+                               proto += "...";
+                               continue;
+                       }
+
+                       if (param.direction == ParameterDirection.IN) {
+                               if (param.variable_type.value_owned) {
+                                       proto += "owned ";
+                               }
+                       } else {
+                               if (param.direction == ParameterDirection.REF) {
+                                       proto += "ref ";
+                               } else if (param.direction == ParameterDirection.OUT) {
+                                       proto += "out ";
+                               }
+                               if (param.variable_type.is_weak ()) {
+                                       proto += "unowned ";
+                               }
+                       }
+
+                       proto = "%s%s %s".printf (proto, param.variable_type.to_qualified_string (), param.name);
+
+                       if (param.initializer != null) {
+                               proto = "%s = %s".printf (proto, param.initializer.to_string ());
+                       }
+
+                       i++;
+               }
+
+               return proto + ")";
+       }
 }
index 8226aed188285a63aad34a56df2a8c4eaa31ef05..e710912c50329546d95a9785de84d2fe9b623234 100644 (file)
@@ -418,8 +418,9 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
 
                        if (arg_it == null || !arg_it.next ()) {
                                if (param.initializer == null) {
+                                       var m = (MethodType) mtype;
                                        expr.error = true;
-                                       Report.error (expr.source_reference, "Too few arguments, method `%s' does not take %d arguments".printf (mtype.to_string (), args.size));
+                                       Report.error (expr.source_reference, "%d missing arguments for `%s'".printf (m.get_parameters ().size - args.size, m.to_prototype_string ()));
                                        return false;
                                } else {
                                        var invocation_expr = expr as MethodCall;
@@ -474,8 +475,9 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                                i++;
                        }
                } else if (!ellipsis && arg_it != null && arg_it.next ()) {
+                       var m = (MethodType) mtype;
                        expr.error = true;
-                       Report.error (expr.source_reference, "Too many arguments, method `%s' does not take %d arguments".printf (mtype.to_string (), args.size));
+                       Report.error (expr.source_reference, "%d extra arguments for `%s'".printf (args.size - m.get_parameters ().size, m.to_prototype_string ()));
                        return false;
                }