]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Improve error while method is incompatible with delegate
authorRico Tzschichholz <ricotz@ubuntu.com>
Sat, 26 Aug 2017 15:45:10 +0000 (17:45 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sat, 2 Sep 2017 18:37:03 +0000 (20:37 +0200)
Add virtual DataType.to_prototype_string() and use a common implementation
in CallableType for its descendents.

codegen/valagtkmodule.vala
vala/valaassignment.vala
vala/valacallabletype.vala
vala/valadatatype.vala
vala/valadelegate.vala
vala/valamethodtype.vala
vala/valasemanticanalyzer.vala

index 6ef3766c26514ebccb24828fb0c97c7592795c7a..5d3299d5fc8cfc8a3bce8ac52ea5f0dbf2cceeb5 100644 (file)
@@ -360,7 +360,7 @@ public class Vala.GtkModule : GSignalModule {
                        var signal_type = new SignalType (sig);
                        var delegate_type = signal_type.get_handler_type ();
                        if (!method_type.compatible (delegate_type)) {
-                               Report.error (m.source_reference, "method `%s' is incompatible with signal `%s', expected `%s'".printf (method_type.to_string (), delegate_type.to_string (), delegate_type.delegate_symbol.get_prototype_string (m.name)));
+                               Report.error (m.source_reference, "method `%s' is incompatible with signal `%s', expected `%s'".printf (method_type.to_string (), delegate_type.to_string (), delegate_type.to_prototype_string (m.name)));
                        } else {
                                var wrapper = generate_delegate_wrapper (m, signal_type.get_handler_type (), m);
 
index 64b2395a3dad9e23af16300eea5324ea72fc7d6b..761a02e1c0168eab0843cc83f7c67df91b36f29d 100644 (file)
@@ -298,7 +298,7 @@ public class Vala.Assignment : Expression {
                                var delegate_type = (DelegateType) right.target_type;
 
                                error = true;
-                               Report.error (right.source_reference, "method `%s' is incompatible with signal `%s', expected `%s'".printf (right.value_type.to_string (), right.target_type.to_string (), delegate_type.delegate_symbol.get_prototype_string (m.name)));
+                               Report.error (right.source_reference, "method `%s' is incompatible with signal `%s', expected `%s'".printf (right.value_type.to_string (), right.target_type.to_string (), delegate_type.to_prototype_string (m.name)));
                                return false;
                        } else if (right_ma != null && right_ma.prototype_access) {
                                error = true;
index 9ad71ff426facdf8079ea3fdd2e19b3156346089..1646b44360490b3021079f6014b92c8e0bdef2d8 100644 (file)
@@ -26,4 +26,77 @@ using GLib;
  * A callable type, i.e. a delegate, method, or signal type.
  */
 public abstract class Vala.CallableType : DataType {
+       public override string to_prototype_string (string? override_name = null) {
+               StringBuilder builder = new StringBuilder ();
+
+               // Append return-type
+               var return_type = get_return_type ();
+               if (return_type.is_weak ()) {
+                       builder.append ("unowned ");
+               }
+               builder.append (return_type.to_qualified_string ());
+
+               // Append name
+               builder.append_c (' ');
+               builder.append (override_name ?? this.to_string ());
+               builder.append_c (' ');
+
+               // Append parameter-list
+               builder.append_c ('(');
+               int i = 1;
+               foreach (Parameter param in get_parameters ()) {
+                       if (i > 1) {
+                               builder.append (", ");
+                       }
+
+                       if (param.ellipsis) {
+                               builder.append ("...");
+                               continue;
+                       }
+
+                       if (param.direction == ParameterDirection.IN) {
+                               if (param.variable_type.value_owned) {
+                                       builder.append ("owned ");
+                               }
+                       } else {
+                               if (param.direction == ParameterDirection.REF) {
+                                       builder.append ("ref ");
+                               } else if (param.direction == ParameterDirection.OUT) {
+                                       builder.append ("out ");
+                               }
+                               if (!param.variable_type.value_owned && param.variable_type is ReferenceType) {
+                                       builder.append ("weak ");
+                               }
+                       }
+
+                       builder.append (param.variable_type.to_qualified_string ());
+
+                       if (param.initializer != null) {
+                               builder.append (" = ");
+                               builder.append (param.initializer.to_string ());
+                       }
+
+                       i++;
+               }
+               builder.append_c (')');
+
+               // Append error-types
+               var error_types = get_error_types ();
+               if (error_types.size > 0) {
+                       builder.append (" throws ");
+
+                       bool first = true;
+                       foreach (DataType type in error_types) {
+                               if (!first) {
+                                       builder.append (", ");
+                               } else {
+                                       first = false;
+                               }
+
+                               builder.append (type.to_string ());
+                       }
+               }
+
+               return builder.str;
+       }
 }
index f3f9bf92a4fea649d8628a93cae7d23c792cbe01..83b918dbdc613e08edc24e35d6e79bdae468121c 100644 (file)
@@ -486,6 +486,16 @@ public abstract class Vala.DataType : CodeNode {
                return null;
        }
 
+       /**
+        * Returns a stringified representation used for detailed error output
+        *
+        * @param override_name used as name if given
+        * @return stringified representation
+        */
+       public virtual string to_prototype_string (string? override_name = null) {
+               return "%s%s".printf (is_weak () ? "unowned " : "", to_qualified_string ());
+       }
+
        public bool is_weak () {
                if (this.value_owned) {
                        return false;
index 6f4ae3b7cf3d580b7f291b039d9468f1cf1ed1f9..356e6e141c1836901f64056efcf7807539da57d7 100644 (file)
@@ -241,54 +241,6 @@ public class Vala.Delegate : TypeSymbol, Callable {
                }
        }
 
-       public string get_prototype_string (string name) {
-               return "%s %s %s".printf (get_return_type_string (), name, get_parameters_string ());
-       }
-
-       string get_return_type_string () {
-               string str = "";
-               if (!return_type.value_owned && return_type is ReferenceType) {
-                       str = "weak ";
-               }
-               str += return_type.to_string ();
-
-               return str;
-       }
-
-       string get_parameters_string () {
-               string str = "(";
-
-               int i = 1;
-               foreach (Parameter param in parameters) {
-                       if (i > 1) {
-                               str += ", ";
-                       }
-
-                       if (param.direction == ParameterDirection.IN) {
-                               if (param.variable_type.value_owned) {
-                                       str += "owned ";
-                               }
-                       } else {
-                               if (param.direction == ParameterDirection.REF) {
-                                       str += "ref ";
-                               } else if (param.direction == ParameterDirection.OUT) {
-                                       str += "out ";
-                               }
-                               if (!param.variable_type.value_owned && param.variable_type is ReferenceType) {
-                                       str += "weak ";
-                               }
-                       }
-
-                       str += param.variable_type.to_string ();
-
-                       i++;
-               }
-
-               str += ")";
-
-               return str;
-       }
-
        public override bool check (CodeContext context) {
                if (checked) {
                        return !error;
index 1cf9a5857a7d15591177cd6b1144420b27921e00..2f2894e85c7573b799056feb1dc35d2ece5473dc 100644 (file)
@@ -72,45 +72,4 @@ public class Vala.MethodType : CallableType {
                }
                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 (Parameter 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 44b75f968bd299cd6c91220ac5be2ebbd266f12c..32a8d2d28f7b89df73f1f22bdfe0bcddd1064be0 100644 (file)
@@ -552,12 +552,12 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                if (arg.target_type != null) {
                        if ((direction == ParameterDirection.IN || direction == ParameterDirection.REF)
                            && !arg.value_type.compatible (arg.target_type)) {
-                               Report.error (arg.source_reference, "Argument %d: Cannot convert from `%s' to `%s'".printf (i + 1, arg.value_type.to_string (), arg.target_type.to_string ()));
+                               Report.error (arg.source_reference, "Argument %d: Cannot convert from `%s' to `%s'".printf (i + 1, arg.value_type.to_prototype_string (), arg.target_type.to_prototype_string ()));
                                return false;
                        } else if ((direction == ParameterDirection.REF || direction == ParameterDirection.OUT)
                                && !arg.target_type.compatible (arg.value_type)
                                && !(arg is NullLiteral)) {
-                               Report.error (arg.source_reference, "Argument %d: Cannot convert from `%s' to `%s'".printf (i + 1, arg.target_type.to_string (), arg.value_type.to_string ()));
+                               Report.error (arg.source_reference, "Argument %d: Cannot convert from `%s' to `%s'".printf (i + 1, arg.target_type.to_prototype_string (), arg.value_type.to_prototype_string ()));
                                return false;
                        }
                }