[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: Tue, 30 Nov 2021 12:43:28 +0000 (UTC)
commit 56ef37232cd5d6d326aff34a6712f6fbbca26fff
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 | 6 ++++++
vala/valaenumvalue.vala | 6 ++++++
vala/valaproperty.vala | 6 ++++++
7 files changed, 49 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 190628152..757cb48f5 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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 \
@@ -1164,6 +1166,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 92591026c..c9b1bfbfd 100644
--- a/vala/valaconstant.vala
+++ b/vala/valaconstant.vala
@@ -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) {
diff --git a/vala/valaenumvalue.vala b/vala/valaenumvalue.vala
index 7a98afb67..be3fcc52e 100644
--- a/vala/valaenumvalue.vala
+++ b/vala/valaenumvalue.vala
@@ -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;
diff --git a/vala/valaproperty.vala b/vala/valaproperty.vala
index fac24d53d..956b72eb8 100644
--- a/vala/valaproperty.vala
+++ b/vala/valaproperty.vala
@@ -546,6 +546,12 @@ 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 ());
+ }
+
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]