]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Check accessibility of initializer for constant and enum value
authorRico Tzschichholz <ricotz@ubuntu.com>
Sat, 6 Nov 2021 17:55:38 +0000 (18:55 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 30 Nov 2021 13:16:00 +0000 (14:16 +0100)
tests/Makefile.am
tests/semantic/constant-value-less-accessible.test [new file with mode: 0644]
tests/semantic/enum-value-less-accessible.test [new file with mode: 0644]
vala/valaconstant.vala
vala/valaenumvalue.vala

index 190628152771dc0797824148abe9de1209a29667..329fb8795e2b9c1ec2811c2fae971ca3d8598932 100644 (file)
@@ -1029,6 +1029,7 @@ TESTS = \
        semantic/constant-reassignment-member.test \
        semantic/constant-type-less-accessible.test \
        semantic/constant-value.test \
+       semantic/constant-value-less-accessible.test \
        semantic/constant-value-missing.test \
        semantic/constant-value-type.test \
        semantic/constant-void.test \
@@ -1047,6 +1048,7 @@ TESTS = \
        semantic/delete-unsupported.test \
        semantic/element-access-index-invalid.test \
        semantic/enum-empty.test \
+       semantic/enum-value-less-accessible.test \
        semantic/errordomain-empty.test \
        semantic/field-accessibility.test \
        semantic/field-compact-static.test \
diff --git a/tests/semantic/constant-value-less-accessible.test b/tests/semantic/constant-value-less-accessible.test
new file mode 100644 (file)
index 0000000..ea909d6
--- /dev/null
@@ -0,0 +1,8 @@
+Invalid Code
+
+const int BAR = 42;
+
+public const int FOO = BAR;
+
+void main () {
+}
diff --git a/tests/semantic/enum-value-less-accessible.test b/tests/semantic/enum-value-less-accessible.test
new file mode 100644 (file)
index 0000000..bbe131b
--- /dev/null
@@ -0,0 +1,10 @@
+Invalid Code
+
+const int BAR = 42;
+
+public enum Foo {
+       FOO = BAR
+}
+
+void main () {
+}
index 92591026c5872fef2c64e011472ba067fa9e4102..c9b1bfbfd038a7ce786e7bf5a6001d2cc9f97542 100644 (file)
@@ -168,6 +168,12 @@ public class Vala.Constant : Symbol {
                                        Report.error (value.source_reference, "Value must be constant");
                                        return false;
                                }
+
+                               // check whether initializer is at least as accessible as the constant
+                               if (!value.is_accessible (this)) {
+                                       error = true;
+                                       Report.error (value.source_reference, "value is less accessible than constant `%s'", get_full_name ());
+                               }
                        }
                } else {
                        if (value != null) {
index 7a98afb678df7e7d573b7bc526f5ecae74e15a42..be3fcc52ea65c98bd087888a2eb24f123d14dd00 100644 (file)
@@ -73,6 +73,12 @@ public class Vala.EnumValue : Constant {
 
                if (value != null) {
                        value.check (context);
+
+                       // check whether initializer is at least as accessible as the enum value
+                       if (!value.is_accessible (this)) {
+                               error = true;
+                               Report.error (value.source_reference, "value is less accessible than enum `%s'", parent_symbol.get_full_name ());
+                       }
                }
 
                return !error;