GNOME Bugzilla – Bug 611223
Null coalescing operator (??) incorrect return type
Last modified: 2017-04-17 10:46:43 UTC
Created attachment 154788 [details] A sample program that illustrates the bug The null coalescing operator (??) should return a non-nullable type. The operator should take a nullable type on the left hand side, a non-nullable type on the right hand side, and return a non-nullable type.
> The operator should take a nullable type on the left hand side, > a non-nullable type on the right hand side, and return a non-nullable type. Not exactly. It should accept both nullable and non-nullable on the right hand side, but the whole expression's type should mirror it.
That makes sense now that I think of it
Created attachment 155902 [details] [review] Make right type dominate in ?? operator
Thanks for the patch. However, this is not completely correct as the type of the left and the type of the right expression may differ in more than just the nullable flag. If the right expression is compatible to the type of the left expression, the type of the left expression should be used (as in master) with the exception that the nullable flag needs to be copied from the type of the right expression. Otherwise, the type of the right expression should be used (as in your patch).
(In reply to comment #4) > Thanks for the patch. However, this is not completely correct as the type of > the left and the type of the right expression may differ in more than just the > nullable flag. > > If the right expression is compatible to the type of the left expression, the we can assume it is, since the created assignment is checked. > type of the left expression should be used (as in master) with the exception > that the nullable flag needs to be copied from the type of the right > expression. Otherwise, the type of the right expression should be used (as in > your patch). why should left type dominate over right type? I assumed that would should change since right and left are comparable (again previously created and checked expression) That's why I just decided to do this simple patch, and I believe it is still enough, although the error message might not be clear: 611223.vala:12.28-12.29: error: Assignment: Cannot convert from `Obj?' to `Foo?' Obj non_nullable = foo ?? nn;
Still exists in valac 0.9.1
Still exists in valac 0.10
*** Bug 729488 has been marked as a duplicate of this bug. ***
Created attachment 349694 [details] [review] Fix + test-case Note: to be able to add a test-case for this bug i added the possibility to use some valac flags for a test group. I am not very used to invoking tests from automake, so you might want to revise that (IMHO the test code is not very pretty anyway)
commit 50748111d26d839cb8244e1bfc0c6924305a90b1 Author: Marvin W <git@larma.de> Date: Tue Apr 11 17:19:58 2017 +0200 vala: Handle non-null in coalescing expression commit 2637bc5499279f8a49a47a7f2a3bf41e55db16a3 Author: Marvin W <git@larma.de> Date: Tue Apr 11 17:19:58 2017 +0200 testrunner: Respect given VALAFLAGS