[vala/0.48] codegen: Detect usage of static type-parameter in runtime context



commit 903b29c6819a817f6135e039f7a3c675e7dc8408
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue May 31 10:19:46 2022 +0200

    codegen: Detect usage of static type-parameter in runtime context
    
    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                     |  6 ++++++
 tests/Makefile.am                                    |  1 +
 tests/generics/type-parameter-static-in-runtime.test | 14 ++++++++++++++
 3 files changed, 21 insertions(+)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 4c7e0f73c..19fcd52a9 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -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) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0e4500b57..6ac21af77 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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
index 000000000..f6b6317fe
--- /dev/null
+++ b/tests/generics/type-parameter-static-in-runtime.test
@@ -0,0 +1,14 @@
+Invalid Code
+
+[Compact]
+class Foo<T> {
+       public Bar<T> bar () {
+               return new Bar<T> ();
+       }
+}
+
+class Bar<T> {
+}
+
+void main () {
+}


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