]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Check for assignment to constant
authorSimon Werbeck <simon.werbeck@gmail.com>
Sun, 18 Sep 2016 16:30:02 +0000 (18:30 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 18 Sep 2016 19:24:10 +0000 (21:24 +0200)
This simply checks if the left-hand side is a constant and if so raises
an error.

Fixes bug 771626

tests/Makefile.am
tests/basic-types/bug771626.test [new file with mode: 0644]
vala/valaassignment.vala

index 0832a41f4d8a0cf5e8f8b54a969dd1149cfd3e90..b2caa77679b3afd447abede81c0ddbdbdbc76933 100644 (file)
@@ -41,6 +41,7 @@ TESTS = \
        basic-types/bug729907.vala \
        basic-types/bug731017.vala \
        basic-types/bug761307.vala \
+       basic-types/bug771626.test \
        namespaces.vala \
        methods/lambda.vala \
        methods/closures.vala \
diff --git a/tests/basic-types/bug771626.test b/tests/basic-types/bug771626.test
new file mode 100644 (file)
index 0000000..64a5da8
--- /dev/null
@@ -0,0 +1,7 @@
+Invalid Code
+
+const int FOO = 0;
+
+void main () {
+       FOO = 1;
+}
index a6b73b913b6467fa45e60bbe44d65cc1dc620400..fc27acf6304d0ffd7c9e293c5de8f4f2aeb728a2 100644 (file)
@@ -145,6 +145,12 @@ public class Vala.Assignment : Expression {
                if (left is MemberAccess) {
                        var ma = (MemberAccess) left;
 
+                       if (ma.symbol_reference is Constant) {
+                               error = true;
+                               Report.error (source_reference, "Assignment to constant after initialization");
+                               return false;
+                       }
+
                        if ((!(ma.symbol_reference is Signal || ma.symbol_reference is DynamicProperty) && ma.value_type == null) ||
                            (ma.inner == null && ma.member_name == "this" && context.analyzer.is_in_instance_method ())) {
                                error = true;