[vala/wip/issue/327: 35/43] Covered all semantic checks plus tests



commit 52be2ac4ca8be6bc8dc64564539d5a30a7fec205
Author: Nick Schrader <nick schrader mailbox org>
Date:   Thu Apr 2 19:36:54 2020 -0300

    Covered all semantic checks plus tests

 tests/Makefile.am                                  | 12 +++++++-
 tests/objects/with-expression.vala                 | 15 ++++++----
 tests/semantic/with-array.test                     |  6 ++++
 tests/semantic/with-buildin.vala                   |  5 ++++
 tests/semantic/with-compact.vala                   | 13 ++++++++
 tests/semantic/with-dereferenced-pointer.vala      | 14 +++++++++
 tests/semantic/with-enum-member.vala               |  7 +++++
 tests/semantic/with-enum.test                      |  9 ++++++
 tests/semantic/with-error-member.test              | 10 +++++++
 tests/semantic/with-error.test                     |  9 ++++++
 tests/semantic/with-pointer.test                   |  6 ++++
 tests/semantic/with-string.vala                    |  5 ++++
 .../with-value.vala}                               |  0
 vala/valamemberaccess.vala                         |  8 ++++-
 vala/valawithstatement.vala                        | 35 +++++++++++++++++++---
 15 files changed, 143 insertions(+), 11 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 8317468cb..99fff01dc 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -40,7 +40,6 @@ TESTS = \
        basic-types/gvariants.vala \
        basic-types/gvariants-unboxing-safe.vala \
        basic-types/null.vala \
-       basic-types/with-value-type.vala \
        basic-types/bug570846.test \
        basic-types/bug571486.vala \
        basic-types/bug591552.vala \
@@ -911,10 +910,21 @@ TESTS = \
        semantic/unary-unsupported-increment.test \
        semantic/unary-unsupported-minus.test \
        semantic/unary-unsupported-negation.test \
+       semantic/with-array.test \
+       semantic/with-buildin.vala \
        semantic/with-class.test \
+       semantic/with-compact.vala \
+       semantic/with-dereferenced-pointer.vala \
+       semantic/with-enum.test \
+       semantic/with-enum-member.vala \
+       semantic/with-error.test \
+       semantic/with-error-member.test \
+       semantic/with-pointer.test \
        semantic/with-namespace.test \
        semantic/with-no-such-member.test \
        semantic/with-no-such-with-member.test \
+       semantic/with-string.vala \
+       semantic/with-value.vala \
        semantic/yield-call-requires-async-context.test \
        semantic/yield-call-requires-async-method.test \
        semantic/yield-creation-requires-async-context.test \
diff --git a/tests/objects/with-expression.vala b/tests/objects/with-expression.vala
index 07d1f27b3..decffa17e 100644
--- a/tests/objects/with-expression.vala
+++ b/tests/objects/with-expression.vala
@@ -13,19 +13,24 @@ class Foo {
 
 void test() {
     var foo = new Foo();
-    with(foo)
+    with (foo)
         method();
     
-    with(new Foo())
+    with (new Foo())
         method();
     
-    with(Foo.factory()) {
+    with (Foo.factory()) {
         method();
         method();
     }
 
-    assert(Foo.method_called == 4);
-    assert(Foo.factory_called == 1);
+    Foo[] arr = {foo, foo};
+    with (arr[0]) {
+        method();
+    }
+
+    assert (Foo.method_called == 5);
+    assert (Foo.factory_called == 1);
 }
 
 void main() {
diff --git a/tests/semantic/with-array.test b/tests/semantic/with-array.test
new file mode 100644
index 000000000..fd70019f4
--- /dev/null
+++ b/tests/semantic/with-array.test
@@ -0,0 +1,6 @@
+Invalid Code
+
+void main() {
+    int[] arr = {1, 2, 3};
+    with(arr) { }
+}
diff --git a/tests/semantic/with-buildin.vala b/tests/semantic/with-buildin.vala
new file mode 100644
index 000000000..a77fda8db
--- /dev/null
+++ b/tests/semantic/with-buildin.vala
@@ -0,0 +1,5 @@
+void main() {
+    with (stdout) {
+        assert (!eof ());
+    }
+}
diff --git a/tests/semantic/with-compact.vala b/tests/semantic/with-compact.vala
new file mode 100644
index 000000000..21ef4fafb
--- /dev/null
+++ b/tests/semantic/with-compact.vala
@@ -0,0 +1,13 @@
+[Compact]
+class Cmpct {
+    public int i;
+}
+
+void main() {
+    var c = new Cmpct();
+    with (c) {
+        i = 13;
+    }
+
+    assert (c.i == 13);
+}
diff --git a/tests/semantic/with-dereferenced-pointer.vala b/tests/semantic/with-dereferenced-pointer.vala
new file mode 100644
index 000000000..30217f0e1
--- /dev/null
+++ b/tests/semantic/with-dereferenced-pointer.vala
@@ -0,0 +1,14 @@
+class Foo { 
+    public int i;
+}
+
+void main() {
+    var f = new Foo();
+    var p = &f;
+
+    with (*p) { 
+        i = 13;
+    }
+
+    assert (f.i == 13);
+}
diff --git a/tests/semantic/with-enum-member.vala b/tests/semantic/with-enum-member.vala
new file mode 100644
index 000000000..8db9f2886
--- /dev/null
+++ b/tests/semantic/with-enum-member.vala
@@ -0,0 +1,7 @@
+enum Enumeration {
+    FIRST
+}
+
+void main() {
+    with (Enumeration.FIRST) { }
+}
diff --git a/tests/semantic/with-enum.test b/tests/semantic/with-enum.test
new file mode 100644
index 000000000..dea687a72
--- /dev/null
+++ b/tests/semantic/with-enum.test
@@ -0,0 +1,9 @@
+Invalid Code
+
+enum Enumeration { 
+    FIRST
+}
+
+void main() {
+    with (Enumeration) { }
+}
diff --git a/tests/semantic/with-error-member.test b/tests/semantic/with-error-member.test
new file mode 100644
index 000000000..907e45f83
--- /dev/null
+++ b/tests/semantic/with-error-member.test
@@ -0,0 +1,10 @@
+Invalid Code
+
+errordomain Err {
+    Error
+}
+
+void main() {
+    var e = new Err.Error ("msg");
+    with (e) { }
+}
diff --git a/tests/semantic/with-error.test b/tests/semantic/with-error.test
new file mode 100644
index 000000000..128c0c950
--- /dev/null
+++ b/tests/semantic/with-error.test
@@ -0,0 +1,9 @@
+Invalid Code
+
+errordomain Err { 
+    ERROR
+}
+
+void main() {
+    with (Err) { }
+}
diff --git a/tests/semantic/with-pointer.test b/tests/semantic/with-pointer.test
new file mode 100644
index 000000000..2293904ef
--- /dev/null
+++ b/tests/semantic/with-pointer.test
@@ -0,0 +1,6 @@
+Invalid Code
+
+void main() {
+    int i = 0;
+    with(&i) { }
+}
diff --git a/tests/semantic/with-string.vala b/tests/semantic/with-string.vala
new file mode 100644
index 000000000..262c786fc
--- /dev/null
+++ b/tests/semantic/with-string.vala
@@ -0,0 +1,5 @@
+void main() {
+    with ("string ") {
+        assert (chomp () == "string");
+    }
+}
diff --git a/tests/basic-types/with-value-type.vala b/tests/semantic/with-value.vala
similarity index 100%
rename from tests/basic-types/with-value-type.vala
rename to tests/semantic/with-value.vala
diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala
index abc960c1e..ab1c8b7cd 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -280,7 +280,13 @@ public class Vala.MemberAccess : Expression {
 
                                if (symbol_reference == null && sym is WithStatement) {
                                        unowned WithStatement w = (WithStatement) sym;
-                                       symbol_reference = w.with_variable.variable_type.get_member 
(member_name);
+
+                                       var variable_type = w.with_variable.variable_type;
+                                       if (variable_type is PointerType) {
+                                               variable_type = ((PointerType) variable_type).base_type;
+                                       }
+                                       
+                                       symbol_reference = variable_type.get_member (member_name);
                                        if (symbol_reference != null) {
                                                inner = new MemberAccess (null, w.with_variable.name, 
source_reference);
                                                inner.check (context);
diff --git a/vala/valawithstatement.vala b/vala/valawithstatement.vala
index edacf93ca..e28fb7999 100644
--- a/vala/valawithstatement.vala
+++ b/vala/valawithstatement.vala
@@ -82,15 +82,42 @@ public class Vala.WithStatement : Block {
                }
        }
 
-       public override bool check (CodeContext context) {
-               expression.check (context);
+       private bool is_object_or_value_type(DataType? type) {
+               if (type == null) {
+                       return false;
+               } else if (type is PointerType) {
+                       var pointer_type = (PointerType) type;
+                       return is_object_or_value_type(pointer_type.base_type) && expression is 
PointerIndirection;                     
+               } else {
+                       return type is ObjectType || type is ValueType;
+               }
+       }
 
+       private LocalVariable insert_local_variable_if_necessary() {
                LocalVariable local_var = expression.symbol_reference as LocalVariable;
                if (local_var == null) {
                        local_var = new LocalVariable (expression.value_type, "_with_local%d_".printf 
(next_with_id++), expression, source_reference);
                        body.insert_statement (0, new DeclarationStatement (local_var, source_reference));
                }
-               with_variable = local_var;
+               return local_var;
+       } 
+
+       public override bool check (CodeContext context) {
+               if (checked) {
+                       return !error;
+               }
+
+               checked = true;
+               
+               expression.check (context);
+
+               if (!is_object_or_value_type (expression.value_type)) {
+                       error = true;
+                       Report.error (expression.source_reference, "Expression must be of an object or basic 
type");
+                       return false;
+               }
+
+               with_variable = insert_local_variable_if_necessary ();
 
                var old_symbol = context.analyzer.current_symbol;
                owner = context.analyzer.current_symbol.scope;
@@ -100,7 +127,7 @@ public class Vala.WithStatement : Block {
 
                context.analyzer.current_symbol = old_symbol;
 
-               return true;
+               return !error;
        }
 
        public override void emit (CodeGenerator codegen) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]