]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Detect usage of static type-parameter in runtime context
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 31 May 2022 08:19:46 +0000 (10:19 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 31 May 2022 08:19:46 +0000 (10:19 +0200)
Compact classes doesn't carry runtime information of its type-parameters.
In case this information is required we need report an error.

Fixes https://gitlab.gnome.org/GNOME/vala/issues/1326

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/generics/type-parameter-static-in-runtime.test [new file with mode: 0644]

index 50dbbea8f2b239c78f0c58c445787c9df3a06682..12134437334c59fdbdd8194ecae3336f5a7c73a5 100644 (file)
@@ -3048,6 +3048,11 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
        public CCodeExpression get_type_id_expression (DataType type, bool is_chainup = false) {
                if (type is GenericType) {
                        var type_parameter = ((GenericType) type).type_parameter;
+                       unowned Symbol? parent = type_parameter.owner.owner;
+                       if (parent is Class && ((Class) parent).is_compact) {
+                               Report.error (type.source_reference, "static type-parameter `%s' can not be used in runtime context", type.type_symbol.get_full_name ());
+                               return new CCodeInvalidExpression();
+                       }
                        string identifier = get_ccode_type_id (type_parameter);
                        return get_generic_type_expression (identifier, (GenericType) type, is_chainup);
                } else {
index 0d0fca034fe1733a52d5c1bf079bb866b24e38e2..3dd2cad450755c65c8400638edddb556af571abb 100644 (file)
@@ -790,6 +790,7 @@ TESTS = \
        generics/string-literal-comparison.vala \
        generics/type-parameter-properties.vala \
        generics/type-parameter-property-clash.vala \
+       generics/type-parameter-static-in-runtime.test \
        generics/value-pointer-type-access.vala \
        generics/bug640330.vala \
        generics/bug640330-2.test \
diff --git a/tests/generics/type-parameter-static-in-runtime.test b/tests/generics/type-parameter-static-in-runtime.test
new file mode 100644 (file)
index 0000000..f6b6317
--- /dev/null
@@ -0,0 +1,14 @@
+Invalid Code
+
+[Compact]
+class Foo<T> {
+       public Bar<T> bar () {
+               return new Bar<T> ();
+       }
+}
+
+class Bar<T> {
+}
+
+void main () {
+}