From: Rico Tzschichholz Date: Sat, 12 Jan 2019 14:20:34 +0000 (+0100) Subject: vala: Allow direct access to the integer constants of an error-domain X-Git-Tag: 0.43.5~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f48208b4cab9f5a7da469067c4cae096082c39ba;p=thirdparty%2Fvala.git vala: Allow direct access to the integer constants of an error-domain Fixes https://gitlab.gnome.org/GNOME/vala/issues/732 --- 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;