]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Report invalid instance member access to property
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 31 Dec 2018 13:48:08 +0000 (14:48 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 28 Feb 2019 16:24:14 +0000 (17:24 +0100)
See https://gitlab.gnome.org/GNOME/vala/issues/605

codegen/valaccodememberaccessmodule.vala
tests/Makefile.am
tests/control-flow/bug790903-2.test [new file with mode: 0644]
vala/Makefile.am
vala/valabinaryexpression.vala
vala/valalocalvariable.vala
vala/valamemberaccess.vala
vala/valapropertyprototype.vala [new file with mode: 0644]
vala/valaunaryexpression.vala

index 49c1535f8fb93948bc12aa5fcdc0b4ad997c508b..b8859b63f121ac6b29c5ddd62edd1cdababb7ba0 100644 (file)
@@ -166,6 +166,13 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
                                }
                        }
 
+                       if (pub_inst == null && prop.binding == MemberBinding.INSTANCE) {
+                               // FIXME Report this with proper source-reference on the vala side!
+                               Report.error (prop.source_reference, "Invalid access to instance member `%s'".printf (prop.get_full_name ()));
+                               set_cvalue (expr, new CCodeInvalidExpression ());
+                               return;
+                       }
+
                        if (expr.inner is BaseAccess) {
                                var base_prop = prop;
                                if (prop.base_property != null) {
@@ -304,6 +311,7 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
                                ccode.add_expression (ccall);
                                set_cvalue (expr, ctemp);
                        }
+
                        expr.target_value.value_type = expr.value_type;
                        expr.target_value = store_temp_value (expr.target_value, expr);
                } else if (expr.symbol_reference is LocalVariable) {
index 0695ac7cb2256e6c032c046da6b06d17e4d131b1..19514bb690d541f96a72f1a2c0dbc2fd72dd59fc 100644 (file)
@@ -147,6 +147,7 @@ TESTS = \
        control-flow/bug736774-1.vala \
        control-flow/bug736774-2.vala \
        control-flow/bug790903.test \
+       control-flow/bug790903-2.test \
        enums/enum_only.vala \
        enums/enums.vala \
        enums/flags.vala \
diff --git a/tests/control-flow/bug790903-2.test b/tests/control-flow/bug790903-2.test
new file mode 100644 (file)
index 0000000..5935f15
--- /dev/null
@@ -0,0 +1,9 @@
+Invalid Code
+
+class Foo {
+       public string prop { get; set; }
+}
+
+void main () {
+       var foo = (string) Foo.prop;
+}
index 397594cdcb178fafe0b9cca714edff88f1f38fb1..8b85e4b7b9833655ec40f276560b07bed021f64a 100644 (file)
@@ -117,6 +117,7 @@ libvalacore_la_VALASOURCES = \
        valaprofile.vala \
        valapropertyaccessor.vala \
        valaproperty.vala \
+       valapropertyprototype.vala \
        valarealliteral.vala \
        valareferencetransferexpression.vala \
        valareferencetype.vala \
index c32f629b419decf01be3360e6e2d8ec79326f2ec..3d574645f651f17efaf13db0435d839e9f450d5b 100644 (file)
@@ -300,12 +300,12 @@ public class Vala.BinaryExpression : Expression {
                        return false;
                }
 
-               if (left.value_type is FieldPrototype) {
+               if (left.value_type is FieldPrototype || left.value_type is PropertyPrototype) {
                        error = true;
                        Report.error (left.source_reference, "Access to instance member `%s' denied".printf (left.symbol_reference.get_full_name ()));
                        return false;
                }
-               if (right.value_type is FieldPrototype) {
+               if (right.value_type is FieldPrototype || right.value_type is PropertyPrototype) {
                        error = true;
                        Report.error (right.source_reference, "Access to instance member `%s' denied".printf (right.symbol_reference.get_full_name ()));
                        return false;
index c11380700038bfaee329bf8cccb91294a7059fd2..337688f1514ff97e2fff6f8103eb403c16597ee6 100644 (file)
@@ -116,7 +116,7 @@ public class Vala.LocalVariable : Variable {
                                Report.error (source_reference, "var declaration not allowed with non-typed initializer");
                                return false;
                        }
-                       if (initializer.value_type is FieldPrototype) {
+                       if (initializer.value_type is FieldPrototype || initializer.value_type is PropertyPrototype) {
                                error = true;
                                Report.error (initializer.source_reference, "Access to instance member `%s' denied".printf (initializer.symbol_reference.get_full_name ()));
                                return false;
index 09e7ea16870285533c5525c64612ec0441fe699a..913fa05aef4db3018fa1a1ab4af540994eec5365 100644 (file)
@@ -771,6 +771,8 @@ public class Vala.MemberAccess : Expression {
                                value_type = context.analyzer.get_value_type_for_symbol (symbol_reference, lvalue);
                        } else if (symbol_reference is Field) {
                                value_type = new FieldPrototype ((Field) symbol_reference);
+                       } else if (symbol_reference is Property) {
+                               value_type = new PropertyPrototype ((Property) symbol_reference);
                        } else {
                                value_type = new InvalidType ();
                        }
diff --git a/vala/valapropertyprototype.vala b/vala/valapropertyprototype.vala
new file mode 100644 (file)
index 0000000..62306cd
--- /dev/null
@@ -0,0 +1,41 @@
+/* valafieldprototype.vala
+ *
+ * Copyright (C) 2018  Rico Tzschichholz
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Rico Tzschichholz <ricotz@ubuntu.com>
+ */
+
+/**
+ * A reference to an instance property without a specific instance.
+ */
+public class Vala.PropertyPrototype : DataType {
+       public weak Property property_symbol { get; set; }
+
+       public PropertyPrototype (Property property_symbol) {
+               this.property_symbol = property_symbol;
+       }
+
+       public override DataType copy () {
+               var result = new PropertyPrototype (property_symbol);
+               return result;
+       }
+
+       public override string to_qualified_string (Scope? scope) {
+               return property_symbol.get_full_name ();
+       }
+}
index 95b0d4fc48c27f689ba142aaf165dbdf85842dda..3adab0b2b8b9465f238950e0188b357e5a8ef2b8 100644 (file)
@@ -170,7 +170,7 @@ public class Vala.UnaryExpression : Expression {
                        return false;
                }
 
-               if (inner.value_type is FieldPrototype) {
+               if (inner.value_type is FieldPrototype || inner.value_type is PropertyPrototype) {
                        error = true;
                        Report.error (inner.source_reference, "Access to instance member `%s' denied".printf (inner.symbol_reference.get_full_name ()));
                        return false;