]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Distinguish between Enum and Flags for their to_string()
authorRico Tzschichholz <ricotz@ubuntu.com>
Fri, 16 Sep 2016 16:37:25 +0000 (18:37 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Fri, 16 Sep 2016 16:37:25 +0000 (18:37 +0200)
This fixes a GLib-critical while using g_enum_get_value() on a GFlagsClass.
Therefore use g_flags_get_first_value() if needed.

Patch by therebedragons111

https://bugzilla.gnome.org/show_bug.cgi?id=763831

codegen/valagtypemodule.vala
tests/Makefile.am
tests/enums/bug763831.vala [new file with mode: 0644]

index f4d3ea2f8e3668caa873235ebc688caccfe8a5d0..b7328860ebc64f14fb93366d79a00862da8fdc31 100644 (file)
@@ -2185,13 +2185,15 @@ public class Vala.GTypeModule : GErrorModule {
                }
                // to_string() on a gtype enum
 
+               bool is_flags = ((Enum) ((EnumValueType) ma.inner.value_type).type_symbol).is_flags;
+
                push_line (expr.source_reference);
-               var temp_var = get_temp_variable (new CType ("GEnumValue*"), false, expr, false);
+               var temp_var = get_temp_variable (new CType (is_flags ? "GFlagsValue*" : "GEnumValue*"), false, expr, false);
                emit_temp_var (temp_var);
 
                var class_ref = new CCodeFunctionCall (new CCodeIdentifier ("g_type_class_ref"));
                class_ref.add_argument (new CCodeIdentifier (get_ccode_type_id (ma.inner.value_type)));
-               var get_value = new CCodeFunctionCall (new CCodeIdentifier ("g_enum_get_value"));
+               var get_value = new CCodeFunctionCall (new CCodeIdentifier (is_flags ? "g_flags_get_first_value" : "g_enum_get_value"));
                get_value.add_argument (class_ref);
                get_value.add_argument ((CCodeExpression) get_ccodenode (((MemberAccess) expr.call).inner));
 
index 58104cb0a1899e154c73a3ba6e8e2538b2d0a57e..ca2a52cf6822f3979bbfffdfd43dc6e6fa711564 100644 (file)
@@ -86,6 +86,7 @@ TESTS = \
        enums/enums.vala \
        enums/flags.vala \
        enums/bug673879.vala \
+       enums/bug763831.vala \
        structs/structs.vala \
        structs/gvalue.vala \
        structs/bug530605.vala \
diff --git a/tests/enums/bug763831.vala b/tests/enums/bug763831.vala
new file mode 100644 (file)
index 0000000..519fdf3
--- /dev/null
@@ -0,0 +1,13 @@
+[Flags]
+enum Foo {
+       TEST = 1 << 0;
+}
+
+enum Bar {
+       TEST = 1 << 0;
+}
+
+void main() {
+       Foo.TEST.to_string ();
+       Bar.TEST.to_string ();
+}