[vala/bug615830] Fix type checking when using generics in combination with subtyping



commit 82c9d4fc8ffa43726e85b06e99821ddb90177870
Author: Matthias Berndt <matthias_berndt gmx de>
Date:   Mon Aug 8 19:41:33 2016 +0200

    Fix type checking when using generics in combination with subtyping
    
    Fixes bug 615830.

 tests/Makefile.am              |    2 ++
 tests/objects/bug615830.test   |   12 ++++++++++++
 tests/objects/generics.vala    |    7 +++++++
 vala/valadatatype.vala         |   19 +++++++------------
 vala/valasemanticanalyzer.vala |    2 +-
 5 files changed, 29 insertions(+), 13 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ca2a52c..2d0a66e 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -126,6 +126,7 @@ TESTS = \
        delegates/bug703804.vala \
        objects/chainup.vala \
        objects/classes.vala \
+       objects/generics.vala \
        objects/fields.vala \
        objects/interfaces.vala \
        objects/methods.vala \
@@ -169,6 +170,7 @@ TESTS = \
        objects/bug751338.vala \
        objects/bug767092.test \
        objects/bug768823.test \
+       objects/bug615830.test \
        errors/errors.vala \
        errors/bug567181.vala \
        errors/bug579101.vala \
diff --git a/tests/objects/bug615830.test b/tests/objects/bug615830.test
new file mode 100644
index 0000000..04742b3
--- /dev/null
+++ b/tests/objects/bug615830.test
@@ -0,0 +1,12 @@
+Invalid Code
+
+class Foo<T> {
+}
+
+class Bar<T> : Foo<T> {
+}
+
+int main (string[] args) {
+       Foo<int> f = new Bar<string> ();
+       return 0;
+}
diff --git a/tests/objects/generics.vala b/tests/objects/generics.vala
new file mode 100644
index 0000000..8b39db9
--- /dev/null
+++ b/tests/objects/generics.vala
@@ -0,0 +1,7 @@
+void main() {
+       // Support generic types without type arguments for non-generic
+       // functions that are agnostic with regard to the actual type arguments.
+
+       GLib.HashTable<int, string> h = (GLib.HashTable) null;
+       (void) h;
+}
diff --git a/vala/valadatatype.vala b/vala/valadatatype.vala
index 02b4cf1..2e799b3 100644
--- a/vala/valadatatype.vala
+++ b/vala/valadatatype.vala
@@ -303,20 +303,19 @@ public abstract class Vala.DataType : CodeNode {
                        return true;
                }
 
-               if (data_type == target_type.data_type) {
+               if (data_type != null && target_type.data_type != null && data_type.is_subtype_of 
(target_type.data_type)) {
+                       var base_type = SemanticAnalyzer.get_instance_base_type_for_member(this, 
target_type.data_type, this);
                        // check compatibility of generic type arguments
-                       if (type_argument_list != null
-                           && type_argument_list.size > 0
-                           && type_argument_list.size == target_type.get_type_arguments ().size) {
-                               for (int i = 0; i < type_argument_list.size; i++) {
-                                       var type_arg = type_argument_list[i];
-                                       var target_type_arg = target_type.get_type_arguments ()[i];
+                       var base_type_args = base_type.get_type_arguments();
+                       var target_type_args = target_type.get_type_arguments();
+                       if (base_type_args.size == target_type_args.size) {
+                               for (int i = 0; i < base_type_args.size; i++) {
                                        // mutable generic types require type argument equality,
                                        // not just one way compatibility
                                        // as we do not currently have immutable generic container types,
                                        // the additional check would be very inconvenient, so we
                                        // skip the additional check for now
-                                       if (!type_arg.compatible (target_type_arg)) {
+                                       if (!base_type_args[i].compatible (target_type_args[i])) {
                                                return false;
                                        }
                                }
@@ -341,10 +340,6 @@ public abstract class Vala.DataType : CodeNode {
                        }
                }
 
-               if (data_type != null && target_type.data_type != null && data_type.is_subtype_of 
(target_type.data_type)) {
-                       return true;
-               }
-
                return false;
        }
 
diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala
index cb1a575..3b43d36 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -735,7 +735,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                return instance_base_type;
        }
 
-       static DataType? get_instance_base_type_for_member (DataType derived_instance_type, TypeSymbol 
type_symbol, CodeNode node_reference) {
+       public static DataType? get_instance_base_type_for_member (DataType derived_instance_type, TypeSymbol 
type_symbol, CodeNode node_reference) {
                DataType instance_type = derived_instance_type;
 
                while (instance_type is PointerType) {


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