[vala/staging] vala: Allow direct access to the integer constants of an error-domain



commit f48208b4cab9f5a7da469067c4cae096082c39ba
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Jan 12 15:20:34 2019 +0100

    vala: Allow direct access to the integer constants of an error-domain
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/732

 tests/Makefile.am           |  1 +
 tests/errors/errorcode.vala | 32 ++++++++++++++++++++++++++++++++
 vala/valaerrorcode.vala     | 20 ++++++++++++++++++++
 vala/valamemberaccess.vala  |  3 +++
 4 files changed, 56 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3594ebe3e..9ad4e4061 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -363,6 +363,7 @@ TESTS = \
        objects/bug795521.vala \
        errors/catch-error-code.vala \
        errors/errors.vala \
+       errors/errorcode.vala \
        errors/errordomain.vala \
        errors/invalid-type-check.test \
        errors/method-throws.vala \
diff --git a/tests/errors/errorcode.vala b/tests/errors/errorcode.vala
new file mode 100644
index 000000000..2cb96ec67
--- /dev/null
+++ b/tests/errors/errorcode.vala
@@ -0,0 +1,32 @@
+errordomain FooError {
+       REALLY_BAD,
+       NOT_SO_GOOD,
+       EVEN_WORSE = 23
+}
+
+void main () {
+       {
+               var error = new IOError.NO_SPACE ("foo");
+               assert (error.code == 12);
+       }
+       {
+               var code = IOError.NO_SPACE;
+               assert (code == 12);
+       }
+       {
+               var error = new FooError.NOT_SO_GOOD ("foo");
+               assert (error.code == 1);
+       }
+       {
+               var code = FooError.NOT_SO_GOOD;
+               assert (code == 1);
+       }
+       {
+               var error = new FooError.EVEN_WORSE ("foo");
+               assert (error.code == 23);
+       }
+       {
+               var code = FooError.EVEN_WORSE;
+               assert (code == 23);
+       }
+}
diff --git a/vala/valaerrorcode.vala b/vala/valaerrorcode.vala
index ef5f36410..3e3c57b37 100644
--- a/vala/valaerrorcode.vala
+++ b/vala/valaerrorcode.vala
@@ -39,7 +39,23 @@ public class Vala.ErrorCode : TypeSymbol {
                }
        }
 
+       /**
+        * Refers to the enum value of this error code for direct access.
+        */
+       public Constant code {
+               get {
+                       return _code;
+               }
+               private set {
+                       _code = value;
+                       if (_code != null) {
+                               _code.owner = owner;
+                       }
+               }
+       }
+
        private Expression _value;
+       private Constant _code;
 
        /**
         * Creates a new enum value.
@@ -84,6 +100,10 @@ public class Vala.ErrorCode : TypeSymbol {
                        value.check (context);
                }
 
+               code = new Constant (name, context.analyzer.int_type.copy (), null, source_reference, 
comment);
+               code.external = true;
+               code.check (context);
+
                return !error;
        }
 }
diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala
index 09faed946..4b131ae47 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -703,6 +703,9 @@ public class Vala.MemberAccess : Expression {
                } else if (member is Signal) {
                        instance = true;
                        access = member.access;
+               } else if (!creation_member && member is ErrorCode) {
+                       symbol_reference = ((ErrorCode) member).code;
+                       member = symbol_reference;
                }
 
                member.used = true;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]