[vala] Better error message for invalid number of arguments



commit 2707980d20bce1c71d2ca9f2cbf4c681225129b9
Author: Marc-André Lureau <marcandre lureau gmail com>
Date:   Wed Jan 27 01:31:09 2010 +0100

    Better error message for invalid number of arguments
    
    Fixes bug 608187.

 vala/valacodewriter.vala       |   24 ++--------------------
 vala/valadatatype.vala         |   18 +++++++++++++++++
 vala/valamethodtype.vala       |   41 ++++++++++++++++++++++++++++++++++++++++
 vala/valasemanticanalyzer.vala |    6 +++-
 4 files changed, 66 insertions(+), 23 deletions(-)
---
diff --git a/vala/valacodewriter.vala b/vala/valacodewriter.vala
index 6adb5bc..e3ca81b 100644
--- a/vala/valacodewriter.vala
+++ b/vala/valacodewriter.vala
@@ -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));
 	}
diff --git a/vala/valadatatype.vala b/vala/valadatatype.vala
index 2e964f3..67bba32 100644
--- a/vala/valadatatype.vala
+++ b/vala/valadatatype.vala
@@ -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;
+	}
 }
diff --git a/vala/valamethodtype.vala b/vala/valamethodtype.vala
index a2ad6d4..87ec555 100644
--- a/vala/valamethodtype.vala
+++ b/vala/valamethodtype.vala
@@ -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 + ")";
+	}
 }
diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala
index 8226aed..e710912 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -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;
 		}
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]