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 761267 - Vala lacks a null-safe member access operator
Vala lacks a null-safe member access operator
Status: RESOLVED OBSOLETE
Product: vala
Classification: Core
Component: Objects
unspecified
Other All
: Normal enhancement
: ---
Assigned To: Vala maintainers
Vala maintainers
: 725356 777345 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2016-01-28 22:20 UTC by Aurélien Rivière
Modified: 2018-05-22 15:30 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Patch that provides this capabilities (5.01 KB, patch)
2016-01-28 22:20 UTC, Aurélien Rivière
none Details | Review
Fix a segfault and ensures the type is nullable (921 bytes, patch)
2016-02-02 19:30 UTC, Aurélien Rivière
none Details | Review
improved patch (5.32 KB, patch)
2016-02-02 20:24 UTC, Ben
none Details | Review
Improved patch (5.35 KB, patch)
2016-02-18 16:45 UTC, Ben
none Details | Review
add support for null-conditional operators (14.15 KB, patch)
2017-01-17 00:52 UTC, Jeeyong Um
none Details | Review
Test for null-conditional operators (3.89 KB, text/plain)
2017-01-17 00:53 UTC, Jeeyong Um
  Details
Add support for null-conditional operators (14.73 KB, patch)
2017-01-17 04:01 UTC, Jeeyong Um
none Details | Review

Description Aurélien Rivière 2016-01-28 22:20:26 UTC
Created attachment 319973 [details] [review]
Patch that provides this capabilities

Vala has no null-safe member access operator, written ?. in C# and Ceylon.

The behavior of this operator is defined there : http://ceylon-lang.org/documentation/1.2/reference/operator/nullsafe-member/
Comment 1 Ben 2016-02-02 17:52:42 UTC
This code gives me errors and compiler segfault

class Hey : Object {
    public int x = 3;
}

void do_it (Hey h) {
    int? y = h?.x;
    if (y != null)
        print ("%d\n", y);
}

void main () {
    do_it (null);
    do_it (new Hey ());
}
Comment 2 Aurélien Rivière 2016-02-02 19:19:36 UTC
-void do_it (Hey h) {
+void do_it (Hey? h) {

Indeed, don't know how I managed to compile my test class without error (and I didn't kept its code).

The issue comes from error = check_nullsafe_member_access (context); as the expression is replaced : parent_node.replace_expression (this, temp_access);
So, replacing error = by return fixes it. 

This fixed, your test case now shows another error: Assignment: Cannot convert from `null' to `int'. However, if you replace int with string, it works, so primitive types marked nullable are not supported.
Comment 3 Aurélien Rivière 2016-02-02 19:30:08 UTC
Created attachment 320289 [details] [review]
Fix a segfault and ensures the type is nullable
Comment 4 Ben 2016-02-02 20:24:51 UTC
Created attachment 320293 [details] [review]
improved patch

I added a new patch:
- cleaned up whitespace
- used git format-patch
- support primitive types
- keep reference to replaced expression
Comment 5 Ben 2016-02-02 20:29:48 UTC
If you return true from check(), doesn't that signify that there was an error?
Comment 6 Aurélien Rivière 2016-02-02 20:45:09 UTC
If my previous code was right, then yes, I have omitted to invert the boolean value.
Comment 7 Ben 2016-02-02 21:02:53 UTC
The `error =` vs `return` doesn't fix the issue, my patch does with `context.analyzer.replaced_nodes.add (this);`
Comment 8 Ben 2016-02-18 16:45:59 UTC
Created attachment 321602 [details] [review]
Improved patch

Changed commit author
Comment 9 Rico Tzschichholz 2017-01-16 19:50:00 UTC
*** Bug 777345 has been marked as a duplicate of this bug. ***
Comment 10 Jeeyong Um 2017-01-17 00:52:59 UTC
Created attachment 343600 [details] [review]
add support for null-conditional operators

This patch has been improved since it was uploaded in bug 777345. (Some bugs were found in specific scenarios)
Comment 11 Jeeyong Um 2017-01-17 00:53:52 UTC
Created attachment 343601 [details]
Test for null-conditional operators
Comment 12 Jeeyong Um 2017-01-17 04:01:32 UTC
Created attachment 343609 [details] [review]
Add support for null-conditional operators

Not allow null-conditional expression to be used as lvalue and remove unnecessary execute-permission from file

I'm sorry if I annoyed you by uploading patches too frequently.
Comment 13 Michael 'Mickey' Lauer 2018-02-24 11:39:47 UTC
*** Bug 725356 has been marked as a duplicate of this bug. ***
Comment 14 GNOME Infrastructure Team 2018-05-22 15:30:44 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/522.