GNOME Bugzilla – Bug 659778
Lambda in enum produces non-compiling C code
Last modified: 2013-12-21 09:46:32 UTC
The lambda code generator assumes that pointer can be assigned to self. This is not true in case of: enum MyEnum { V1, V2, ...; public void f() { Idle.add (() => { ... }); } } Producing: static gboolean ___lambda25_ (MyEnum self) { ... } static gboolean ____lambda25__gsource_func (gpointer self) { gboolean result; result = ___lambda25_ (self); // This should be: result = ___lambda25_ ((MyEnum)self); return result; }
*** Bug 706243 has been marked as a duplicate of this bug. ***
Created attachment 252944 [details] [review] Handle the integers and enums in generic closure wrapper Handle edge-case when the one of generic parameters of delegate is an integer in closure or method. This does not handle (yet) the original problem as enum as this is not supported. The full test case would be as follows: delegate G DoSomething<G>(G g); void do_something<G> (DoSomething<G> f) {} enum TE { T; public void f() { do_something<TE> ((x) => { switch (this) { case T: return T; default: assert_not_reached (); } }); } public void g(int i) { do_something<TE> ((x) => { switch (this) { case T: i++; return T; default: assert_not_reached (); } }); } } class Test { public void f() { do_something<TE> (g); do_something<int> (h); } [CCode (instance_pos = -1)] private TE g(TE i) { return i; } [CCode (instance_pos = -1)] private int h(int i) { return i; } } int main() { TE t = TE.T; //t.f (); t.g (0); Test t2 = new Test (); t2.f (); return 0; }
Created attachment 252955 [details] [review] Allow using lambdas within enums, fixes bug #659778 The missing part - allows creation from enums. This patch requires the previous one. Probably is_signed_integer_type_argument || is_unsigned_integer_type_argument should be refactored into something like is_integer_type_argument.
Review of attachment 252944 [details] [review]: Looks fine thanks.
Review of attachment 252955 [details] [review]: Looks fine thanks, however can we generalize the checks as described below? ::: codegen/valaccodebasemodule.vala @@ +1860,3 @@ if (get_this_type () != null) { + if (is_signed_integer_type_argument (get_this_type ()) + || is_unsigned_integer_type_argument (get_this_type ())) { What about checking for SimpleType structs in general? @@ +2128,3 @@ } else { if (get_this_type () != null) { + if (!(is_signed_integer_type_argument (get_this_type ()) What about checking if the type can be destroyed in general?
Created attachment 264225 [details] [review] Allow using lambdas within enums, fixes bug #659778 > Review of attachment 252955 [details] [review]: > > Looks fine thanks, however can we generalize the checks as described below? > > ::: codegen/valaccodebasemodule.vala > @@ +1860,3 @@ > if (get_this_type () != null) { > + if (is_signed_integer_type_argument (get_this_type ()) > + || is_unsigned_integer_type_argument (get_this_type > ())) { > > What about checking for SimpleType structs in general? > (IIRC) Because enums are not SimpleType struct and checking for SimpleType in codegen would be more complicated at no added benefit (SimpleType does not work in Vala code). Unless I'm missing a function which would do it. > @@ +2128,3 @@ > } else { > if (get_this_type () != null) { > + if (!(is_signed_integer_type_argument (get_this_type ()) > > What about checking if the type can be destroyed in general? Done
(In reply to comment #6) > (IIRC) Because enums are not SimpleType struct and checking for SimpleType in > codegen would be more complicated at no added benefit (SimpleType does not work > in Vala code). Unless I'm missing a function which would do it. That's right. So, a better approach then would be to use get_ccode_name (get_data_type_for_symbol (current_type_symbol)). However if you're tired of the patch, no problem it's ok as is for now :-)
Created attachment 264621 [details] [review] Allow using lambdas within enums, fixes bug #659778 (In reply to comment #7) > (In reply to comment #6) > > (IIRC) Because enums are not SimpleType struct and checking for SimpleType in > > codegen would be more complicated at no added benefit (SimpleType does not work > > in Vala code). Unless I'm missing a function which would do it. > > That's right. So, a better approach then would be to use get_ccode_name > (get_data_type_for_symbol (current_type_symbol)). However if you're tired of > the patch, no problem it's ok as is for now :-) Ok. Done with better approach (sorry for delay).
commit fe9beb82b68090ce8f7557c05029fb50ecccb02b Author: Maciej Piechotka <uzytkownik2@gmail.com> Date: Fri Aug 23 22:54:42 2013 +0200 Allow using lambdas within enums. Fixes bug 659778 I've removed the bool -> protected bool change. Thanks :-) This problem has been fixed in the development version. The fix will be available in the next major software release. Thank you for your bug report.