]> 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>
Wed, 10 Aug 2022 17:10:59 +0000 (19:10 +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 4c7e0f73c60fff175af3733beaffb8896bd4e4c4..19fcd52a9d6af2b90b0b2ac4f4b7576e2cc83f1e 100644 (file)
@@ -2839,6 +2839,12 @@ 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".printf (type_parameter.get_full_name ()));
+                               return new CCodeInvalidExpression();
+                       }
+
                        string var_name = "%s_type".printf (type_parameter.name.ascii_down ());
 
                        if (type_parameter.parent_symbol is Interface) {
index 0e4500b572a7e0af00a7c97aac301da9020b3fea..6ac21af779a6d74771bdcf69f33525898c2872b8 100644 (file)
@@ -732,6 +732,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 () {
+}