|
| 1 | +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:_foreign_helper' show JS; |
| 6 | +import 'dart:_runtime' as dart; |
| 7 | +import 'dart:async'; |
| 8 | + |
| 9 | +import 'package:expect/expect.dart'; |
| 10 | + |
| 11 | +class A {} |
| 12 | + |
| 13 | +class B extends A {} |
| 14 | + |
| 15 | +class C extends B {} |
| 16 | + |
| 17 | +class D<T extends B> {} |
| 18 | + |
| 19 | +class E<T, S> {} |
| 20 | + |
| 21 | +class F extends E<B, B> {} |
| 22 | + |
| 23 | +// Returns sWrapped<tWrapped> as a wrapped type. |
| 24 | +Object generic1(Type sWrapped, Type tWrapped) { |
| 25 | + var s = dart.unwrapType(sWrapped); |
| 26 | + var t = dart.unwrapType(tWrapped); |
| 27 | + var sGeneric = dart.getGenericClass(s); |
| 28 | + return dart.wrapType(JS('', '#(#)', sGeneric, t)); |
| 29 | +} |
| 30 | + |
| 31 | +// Returns sWrapped<tWrapped, rWrapped> as a wrapped type. |
| 32 | +Object generic2(Type sWrapped, Type tWrapped, Type rWrapped) { |
| 33 | + var s = dart.unwrapType(sWrapped); |
| 34 | + var t = dart.unwrapType(tWrapped); |
| 35 | + var r = dart.unwrapType(rWrapped); |
| 36 | + var sGeneric = dart.getGenericClass(s); |
| 37 | + return dart.wrapType(JS('', '#(#, #)', sGeneric, t, r)); |
| 38 | +} |
| 39 | + |
| 40 | +// Returns a function type of argWrapped -> returnWrapped as a wrapped type. |
| 41 | +Object function1(Type returnWrapped, Type argWrapped) { |
| 42 | + var returnType = dart.unwrapType(returnWrapped); |
| 43 | + var argType = dart.unwrapType(argWrapped); |
| 44 | + var fun = dart.fnType(returnType, [argType]); |
| 45 | + return dart.wrapType(fun); |
| 46 | +} |
| 47 | + |
| 48 | +// Returns a function type with a bounded type argument that takes no argument |
| 49 | +// and returns void as a wrapped type. |
| 50 | +Object genericFunction(Type boundWrapped) => dart.wrapType(dart.gFnType( |
| 51 | + (T) => [dart.VoidType, []], (T) => [dart.unwrapType(boundWrapped)])); |
| 52 | + |
| 53 | +// Returns a function type with a bounded generic return type of |
| 54 | +// <T extends typeBoud> argWrapped -> T as a wrapped type. |
| 55 | +Object functionGenericReturn(Type boundWrapped, Type argWrapped) => |
| 56 | + dart.wrapType(dart.gFnType( |
| 57 | + (T) => [ |
| 58 | + T, |
| 59 | + [dart.unwrapType(argWrapped)] |
| 60 | + ], |
| 61 | + (T) => [dart.unwrapType(boundWrapped)])); |
| 62 | + |
| 63 | +// Returns a function with a bounded generic argument type of |
| 64 | +// <T extends typeBoud> T -> returnWrapped as a wrapped type. |
| 65 | +Object functionGenericArg(Type boundWrapped, Type returnWrapped) => |
| 66 | + dart.wrapType(dart.gFnType( |
| 67 | + (T) => [ |
| 68 | + dart.unwrapType(returnWrapped), |
| 69 | + [T] |
| 70 | + ], |
| 71 | + (T) => [dart.unwrapType(boundWrapped)])); |
| 72 | + |
| 73 | +void checkSubtype(Type sWrapped, Type tWrapped) { |
| 74 | + var s = dart.unwrapType(sWrapped); |
| 75 | + var t = dart.unwrapType(tWrapped); |
| 76 | + Expect.isTrue(dart.isSubtypeOf(s, t), '$s should be subtype of $t.'); |
| 77 | +} |
| 78 | + |
| 79 | +void checkProperSubtype(Type sWrapped, Type tWrapped) { |
| 80 | + var s = dart.unwrapType(sWrapped); |
| 81 | + var t = dart.unwrapType(tWrapped); |
| 82 | + Expect.isTrue(dart.isSubtypeOf(s, t), '$s should be subtype of $t.'); |
| 83 | + Expect.isFalse(dart.isSubtypeOf(t, s), '$t should not be subtype of $s.'); |
| 84 | +} |
| 85 | + |
| 86 | +void main() { |
| 87 | + // A <: dynamic |
| 88 | + checkProperSubtype(A, dynamic); |
| 89 | + // A <: Object |
| 90 | + checkProperSubtype(A, Object); |
| 91 | + // TODO(nshahan) Test void as top? A <: void |
| 92 | + |
| 93 | + // Null <: A |
| 94 | + checkProperSubtype(Null, A); |
| 95 | + |
| 96 | + // FutureOr<Null> <: Future<Null> |
| 97 | + checkSubtype(generic1(FutureOr, Null), generic1(Future, Null)); |
| 98 | + // Future<B> <: FutureOr<A> |
| 99 | + checkProperSubtype(generic1(Future, B), generic1(FutureOr, A)); |
| 100 | + // B <: <: FutureOr<A> |
| 101 | + checkProperSubtype(B, generic1(FutureOr, A)); |
| 102 | + // Future<B> <: Future<A> |
| 103 | + checkProperSubtype(generic1(Future, B), generic1(Future, A)); |
| 104 | + // B <: A |
| 105 | + checkProperSubtype(B, A); |
| 106 | + |
| 107 | + // A <: A |
| 108 | + checkSubtype(A, A); |
| 109 | + // C <: B |
| 110 | + checkProperSubtype(C, B); |
| 111 | + // C <: A |
| 112 | + checkProperSubtype(C, A); |
| 113 | + |
| 114 | + // A -> B <: Function |
| 115 | + checkProperSubtype(function1(B, A), Function); |
| 116 | + |
| 117 | + // A -> B <: A -> B |
| 118 | + checkSubtype(function1(B, A), function1(B, A)); |
| 119 | + |
| 120 | + // A -> B <: B -> B |
| 121 | + checkProperSubtype(function1(B, A), function1(B, B)); |
| 122 | + // TODO(nshahan) Subtype check with covariant keyword? |
| 123 | + |
| 124 | + // A -> B <: A -> A |
| 125 | + checkSubtype(function1(B, A), function1(A, A)); |
| 126 | + |
| 127 | + // Generic Function Subtypes. |
| 128 | + // Bound is a built in type. |
| 129 | + // <T extends int> void -> void <: <T extends int> void -> void |
| 130 | + checkSubtype(genericFunction(int), genericFunction(int)); |
| 131 | + |
| 132 | + // <T extends String> A -> T <: <T extends String> B -> T |
| 133 | + checkProperSubtype( |
| 134 | + functionGenericReturn(String, A), functionGenericReturn(String, B)); |
| 135 | + |
| 136 | + // <T extends double> T -> B <: <T extends double> T -> A |
| 137 | + checkProperSubtype( |
| 138 | + functionGenericArg(double, B), functionGenericArg(double, A)); |
| 139 | + |
| 140 | + // Bound is a function type. |
| 141 | + // <T extends A -> B> void -> void <: <T extends A -> B> void -> void |
| 142 | + checkSubtype( |
| 143 | + genericFunction(function1(B, A)), genericFunction(function1(B, A))); |
| 144 | + |
| 145 | + // <T extends A -> B> A -> T <: <T extends A -> B> B -> T |
| 146 | + checkProperSubtype(functionGenericReturn(function1(B, A), A), |
| 147 | + functionGenericReturn(function1(B, A), B)); |
| 148 | + |
| 149 | + // <T extends A -> B> T -> B <: <T extends A -> B> T -> A |
| 150 | + checkProperSubtype(functionGenericArg(function1(B, A), B), |
| 151 | + functionGenericArg(function1(B, A), A)); |
| 152 | + |
| 153 | + // Bound is a user defined class. |
| 154 | + // <T extends B> void -> void <: <T extends B> void -> void |
| 155 | + checkSubtype(genericFunction(B), genericFunction(B)); |
| 156 | + |
| 157 | + // <T extends B> A -> T <: <T extends B> B -> T |
| 158 | + checkProperSubtype(functionGenericReturn(B, A), functionGenericReturn(B, B)); |
| 159 | + |
| 160 | + // <T extends B> T -> B <: <T extends B> T -> A |
| 161 | + checkProperSubtype(functionGenericArg(B, B), functionGenericArg(B, A)); |
| 162 | + |
| 163 | + // Bound is a Future. |
| 164 | + // <T extends Future<B>> void -> void <: <T extends Future<B>> void -> void |
| 165 | + checkSubtype(genericFunction(generic1(Future, B)), |
| 166 | + genericFunction(generic1(Future, B))); |
| 167 | + |
| 168 | + // <T extends Future<B>> A -> T <: <T extends Future<B>> B -> T |
| 169 | + checkProperSubtype(functionGenericReturn(generic1(Future, B), A), |
| 170 | + functionGenericReturn(generic1(Future, B), B)); |
| 171 | + |
| 172 | + // <T extends Future<B>> T -> B <: <T extends Future<B>> T -> A |
| 173 | + checkProperSubtype(functionGenericArg(generic1(Future, B), B), |
| 174 | + functionGenericArg(generic1(Future, B), A)); |
| 175 | + |
| 176 | + // Bound is a FutureOr. |
| 177 | + // <T extends FutureOr<B>> void -> void <: |
| 178 | + // <T extends FutureOr<B>> void -> void |
| 179 | + checkSubtype(genericFunction(generic1(FutureOr, B)), |
| 180 | + genericFunction(generic1(FutureOr, B))); |
| 181 | + |
| 182 | + // <T extends FutureOr<B>> A -> T <: <T extends FutureOr<B>> B -> T |
| 183 | + checkProperSubtype(functionGenericReturn(generic1(FutureOr, B), A), |
| 184 | + functionGenericReturn(generic1(FutureOr, B), B)); |
| 185 | + |
| 186 | + // <T extends FutureOr<B>> T -> B <: <T extends FutureOr<B>> T -> A |
| 187 | + checkProperSubtype(functionGenericArg(generic1(FutureOr, B), B), |
| 188 | + functionGenericArg(generic1(FutureOr, B), A)); |
| 189 | + |
| 190 | + // D <: D<B> |
| 191 | + checkSubtype(D, generic1(D, B)); |
| 192 | + // D<B> <: D |
| 193 | + checkSubtype(generic1(D, B), D); |
| 194 | + // D<C> <: D<B> |
| 195 | + checkProperSubtype(generic1(D, C), generic1(D, B)); |
| 196 | + |
| 197 | + // F <: E |
| 198 | + checkProperSubtype(F, E); |
| 199 | + // F <: E<A, A> |
| 200 | + checkProperSubtype(F, generic2(E, A, A)); |
| 201 | + // // E<B, B> <: E<A, A> |
| 202 | + checkProperSubtype(generic2(E, B, B), E); |
| 203 | + // // E<B, B> <: E<A, A> |
| 204 | + checkProperSubtype(generic2(E, B, B), generic2(E, A, A)); |
| 205 | +} |
0 commit comments