After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 659778 - Lambda in enum produces non-compiling C code
Lambda in enum produces non-compiling C code
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Code Generator
0.21.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
: 706243 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2011-09-22 01:22 UTC by Maciej (Matthew) Piechotka
Modified: 2013-12-21 09:46 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Handle the integers and enums in generic closure wrapper (3.72 KB, patch)
2013-08-23 19:49 UTC, Maciej (Matthew) Piechotka
committed Details | Review
Allow using lambdas within enums, fixes bug #659778 (6.31 KB, patch)
2013-08-23 20:57 UTC, Maciej (Matthew) Piechotka
needs-work Details | Review
Allow using lambdas within enums, fixes bug #659778 (6.32 KB, patch)
2013-12-15 15:41 UTC, Maciej (Matthew) Piechotka
none Details | Review
Allow using lambdas within enums, fixes bug #659778 (5.14 KB, patch)
2013-12-20 15:35 UTC, Maciej (Matthew) Piechotka
none Details | Review

Description Maciej (Matthew) Piechotka 2011-09-22 01:22:44 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;
}
Comment 1 Maciej (Matthew) Piechotka 2013-08-20 11:27:44 UTC
*** Bug 706243 has been marked as a duplicate of this bug. ***
Comment 2 Maciej (Matthew) Piechotka 2013-08-23 19:49:50 UTC
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;
}
Comment 3 Maciej (Matthew) Piechotka 2013-08-23 20:57:05 UTC
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.
Comment 4 Luca Bruno 2013-12-14 13:09:49 UTC
Review of attachment 252944 [details] [review]:

Looks fine thanks.
Comment 5 Luca Bruno 2013-12-14 13:11:16 UTC
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?
Comment 6 Maciej (Matthew) Piechotka 2013-12-15 15:41:11 UTC
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
Comment 7 Luca Bruno 2013-12-16 08:26:02 UTC
(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 :-)
Comment 8 Maciej (Matthew) Piechotka 2013-12-20 15:35:09 UTC
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).
Comment 9 Luca Bruno 2013-12-21 09:46:32 UTC
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.