[vala/staging] vala: Check accessibility of initializer for constant, enum value and property
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/staging] vala: Check accessibility of initializer for constant, enum value and property
- Date: Sat, 6 Nov 2021 17:56:13 +0000 (UTC)
commit 2d943cfc38546879f462e0b0abd24ce5ed4eb867
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Sat Nov 6 18:55:38 2021 +0100
vala: Check accessibility of initializer for constant, enum value and property
tests/Makefile.am | 3 +++
tests/semantic/constant-value-less-accessible.test | 8 ++++++++
tests/semantic/enum-value-less-accessible.test | 10 ++++++++++
tests/semantic/property-initializer-less-accessible.test | 10 ++++++++++
vala/valaconstant.vala | 7 +++++++
vala/valaenumvalue.vala | 7 +++++++
vala/valaproperty.vala | 7 +++++++
7 files changed, 52 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index eadf61b84..417cc7dbb 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1014,6 +1014,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 \
@@ -1032,6 +1033,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 \
@@ -1147,6 +1149,7 @@ TESTS = \
semantic/property-abstract-derived-compact.test \
semantic/property-accessibility.test \
semantic/property-construct.test \
+ semantic/property-initializer-less-accessible.test \
semantic/property-initializer-type.test \
semantic/property-method-returns-modified-pointer.test \
semantic/property-override.test \
diff --git a/tests/semantic/constant-value-less-accessible.test
b/tests/semantic/constant-value-less-accessible.test
new file mode 100644
index 000000000..ea909d6cf
--- /dev/null
+++ b/tests/semantic/constant-value-less-accessible.test
@@ -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
index 000000000..bbe131bce
--- /dev/null
+++ b/tests/semantic/enum-value-less-accessible.test
@@ -0,0 +1,10 @@
+Invalid Code
+
+const int BAR = 42;
+
+public enum Foo {
+ FOO = BAR
+}
+
+void main () {
+}
diff --git a/tests/semantic/property-initializer-less-accessible.test
b/tests/semantic/property-initializer-less-accessible.test
new file mode 100644
index 000000000..e480d9f25
--- /dev/null
+++ b/tests/semantic/property-initializer-less-accessible.test
@@ -0,0 +1,10 @@
+Invalid Code
+
+const int BAR = 42;
+
+public class Bar {
+ public int foo { get; set; default = BAR; }
+}
+
+void main () {
+}
diff --git a/vala/valaconstant.vala b/vala/valaconstant.vala
index 868d8e46b..6b77492d4 100644
--- a/vala/valaconstant.vala
+++ b/vala/valaconstant.vala
@@ -169,6 +169,13 @@ 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 ());
+ return false;
+ }
}
} else {
if (value != null) {
diff --git a/vala/valaenumvalue.vala b/vala/valaenumvalue.vala
index 7a98afb67..70f635e8c 100644
--- a/vala/valaenumvalue.vala
+++ b/vala/valaenumvalue.vala
@@ -73,6 +73,13 @@ 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 false;
+ }
}
return !error;
diff --git a/vala/valaproperty.vala b/vala/valaproperty.vala
index fac24d53d..f1a939252 100644
--- a/vala/valaproperty.vala
+++ b/vala/valaproperty.vala
@@ -546,6 +546,13 @@ public class Vala.Property : Symbol, Lockable {
Report.error (initializer.source_reference, "Expected initializer of type `%s' but
got `%s'", property_type.to_string (), initializer.value_type.to_string ());
}
+ // check whether initializer is at least as accessible as the property
+ if (initializer != null && !initializer.is_accessible (this)) {
+ error = true;
+ Report.error (initializer.source_reference, "initializer is less accessible than
property `%s'", get_full_name ());
+ return false;
+ }
+
context.analyzer.current_source_file = old_source_file;
context.analyzer.current_symbol = old_symbol;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]