OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// General type checking tests | 5 /// General type checking tests |
6 library dev_compiler.test.checker_test; | 6 library dev_compiler.test.checker_test; |
7 | 7 |
8 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
9 | 9 |
10 import 'package:dev_compiler/src/testing.dart'; | 10 import 'package:dev_compiler/src/testing.dart'; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 '/helper.dart': ''' | 101 '/helper.dart': ''' |
102 dynamic toString = (int x) => x + 42; | 102 dynamic toString = (int x) => x + 42; |
103 dynamic hashCode = "hello"; | 103 dynamic hashCode = "hello"; |
104 ''', | 104 ''', |
105 '/main.dart': ''' | 105 '/main.dart': ''' |
106 import 'helper.dart' as helper; | 106 import 'helper.dart' as helper; |
107 | 107 |
108 class A { | 108 class A { |
109 String x = "hello world"; | 109 String x = "hello world"; |
110 | 110 |
111 void baz1(y) => x + y; | 111 void baz1(y) => x + /*info:DynamicCast*/y; |
112 static baz2(y) => y + y; | 112 static baz2(y) => /*info:DynamicInvoke*/y + y; |
113 } | 113 } |
114 | 114 |
115 void foo(String str) { | 115 void foo(String str) { |
116 print(str); | 116 print(str); |
117 } | 117 } |
118 | 118 |
119 class B { | 119 class B { |
120 String toString([int arg]) => arg.toString(); | 120 String toString([int arg]) => arg.toString(); |
121 } | 121 } |
122 | 122 |
(...skipping 849 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
972 } | 972 } |
973 int i2i(int x) => x; | 973 int i2i(int x) => x; |
974 num n2n(num x) => x; | 974 num n2n(num x) => x; |
975 void main() { | 975 void main() { |
976 { | 976 { |
977 I2I f; | 977 I2I f; |
978 f = new A(); | 978 f = new A(); |
979 f = /*severe:StaticTypeError*/new B(); | 979 f = /*severe:StaticTypeError*/new B(); |
980 f = i2i; | 980 f = i2i; |
981 f = /*warning:DownCastComposite*/n2n; | 981 f = /*warning:DownCastComposite*/n2n; |
982 f = /*warning:DownCastComposite*/(i2i as Object); | 982 f = /*warning:DownCastComposite*/i2i as Object; |
983 f = /*warning:DownCastComposite*/(n2n as Function); | 983 f = /*warning:DownCastComposite*/n2n as Function; |
984 } | 984 } |
985 { | 985 { |
986 N2N f; | 986 N2N f; |
987 f = /*severe:StaticTypeError*/new A(); | 987 f = /*severe:StaticTypeError*/new A(); |
988 f = new B(); | 988 f = new B(); |
989 f = /*warning:DownCastComposite*/i2i; | 989 f = /*warning:DownCastComposite*/i2i; |
990 f = n2n; | 990 f = n2n; |
991 f = /*warning:DownCastComposite*/(i2i as Object); | 991 f = /*warning:DownCastComposite*/i2i as Object; |
992 f = /*warning:DownCastComposite*/(n2n as Function); | 992 f = /*warning:DownCastComposite*/n2n as Function; |
993 } | 993 } |
994 { | 994 { |
995 A f; | 995 A f; |
996 f = new A(); | 996 f = new A(); |
997 f = /*severe:StaticTypeError*/new B(); | 997 f = /*severe:StaticTypeError*/new B(); |
998 f = /*severe:StaticTypeError*/i2i; | 998 f = /*severe:StaticTypeError*/i2i; |
999 f = /*severe:StaticTypeError*/n2n; | 999 f = /*severe:StaticTypeError*/n2n; |
1000 f = /*warning:DownCastImplicit*/(i2i as Object); | 1000 f = /*warning:DownCastImplicit*/i2i as Object; |
1001 f = /*warning:DownCastImplicit*/(n2n as Function); | 1001 f = /*warning:DownCastImplicit*/n2n as Function; |
1002 } | 1002 } |
1003 { | 1003 { |
1004 B f; | 1004 B f; |
1005 f = /*severe:StaticTypeError*/new A(); | 1005 f = /*severe:StaticTypeError*/new A(); |
1006 f = new B(); | 1006 f = new B(); |
1007 f = /*severe:StaticTypeError*/i2i; | 1007 f = /*severe:StaticTypeError*/i2i; |
1008 f = /*severe:StaticTypeError*/n2n; | 1008 f = /*severe:StaticTypeError*/n2n; |
1009 f = /*warning:DownCastImplicit*/(i2i as Object); | 1009 f = /*warning:DownCastImplicit*/i2i as Object; |
1010 f = /*warning:DownCastImplicit*/(n2n as Function); | 1010 f = /*warning:DownCastImplicit*/n2n as Function; |
1011 } | 1011 } |
1012 { | 1012 { |
1013 Function f; | 1013 Function f; |
1014 f = new A(); | 1014 f = new A(); |
1015 f = new B(); | 1015 f = new B(); |
1016 f = i2i; | 1016 f = i2i; |
1017 f = n2n; | 1017 f = n2n; |
1018 f = /*warning:DownCastImplicit*/(i2i as Object); | 1018 f = /*warning:DownCastImplicit*/i2i as Object; |
1019 f = (n2n as Function); | 1019 f = (n2n as Function); |
1020 } | 1020 } |
1021 } | 1021 } |
1022 ''' | 1022 ''' |
1023 }); | 1023 }); |
1024 }); | 1024 }); |
1025 | 1025 |
1026 test('Function typing and subtyping: void', () { | 1026 test('Function typing and subtyping: void', () { |
1027 testChecker({ | 1027 testChecker({ |
1028 '/main.dart': ''' | 1028 '/main.dart': ''' |
1029 | 1029 |
1030 class A { | 1030 class A { |
1031 void bar() => null; | 1031 void bar() => null; |
1032 void foo() => bar; // allowed | 1032 void foo() => bar; // allowed |
1033 } | 1033 } |
1034 ''' | 1034 ''' |
1035 }); | 1035 }); |
1036 }); | 1036 }); |
1037 | 1037 |
1038 test('Closure wrapping of literals', () { | 1038 test('Closure wrapping of literals', () { |
1039 testChecker({ | 1039 testChecker({ |
1040 '/main.dart': ''' | 1040 '/main.dart': ''' |
1041 typedef T F<T>(T t1, T t2); | 1041 typedef T F<T>(T t1, T t2); |
1042 typedef dynamic D(t1, t2); | 1042 typedef dynamic D(t1, t2); |
1043 | 1043 |
1044 void main() { | 1044 void main() { |
1045 F f1 = (x, y) => x + y; | 1045 F f1 = (x, y) => /*info:DynamicInvoke*/x + y; |
1046 F<int> f2 = /*warning:ClosureWrapLiteral*/(x, y) => x + y; | 1046 F<int> f2 = /*warning:ClosureWrapLiteral*/(x, y) => /*info:DynamicInvoke
*/x + y; |
1047 D f3 = (x, y) => x + y; | 1047 D f3 = (x, y) => /*info:DynamicInvoke*/x + y; |
1048 Function f4 = (x, y) => x + y; | 1048 Function f4 = (x, y) => /*info:DynamicInvoke*/x + y; |
1049 f2 = /*warning:ClosureWrap*/f1; | 1049 f2 = /*warning:ClosureWrap*/f1; |
1050 f1 = (int x, int y) => x + y; | 1050 f1 = (int x, int y) => x + y; |
1051 f2 = /*severe:StaticTypeError*/(int x) => -x; | 1051 f2 = /*severe:StaticTypeError*/(int x) => -x; |
1052 } | 1052 } |
1053 ''' | 1053 ''' |
1054 }, wrapClosures: true, inferDownwards: false); | 1054 }, wrapClosures: true, inferDownwards: false); |
1055 }); | 1055 }); |
1056 | 1056 |
1057 test('Generic subtyping: invariance', () { | 1057 test('Generic subtyping: invariance', () { |
1058 testChecker({ | 1058 testChecker({ |
(...skipping 889 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1948 A operator -(B b) {} | 1948 A operator -(B b) {} |
1949 } | 1949 } |
1950 | 1950 |
1951 foo() => new A(); | 1951 foo() => new A(); |
1952 | 1952 |
1953 test() { | 1953 test() { |
1954 A a = new A(); | 1954 A a = new A(); |
1955 B b = new B(); | 1955 B b = new B(); |
1956 var c = foo(); | 1956 var c = foo(); |
1957 a = a * b; | 1957 a = a * b; |
1958 a = a * /*pass should be warning:DownCastImplicit*/c; | 1958 a = a * /*info:DynamicCast*/c; |
1959 a = a / b; | 1959 a = a / b; |
1960 a = a ~/ b; | 1960 a = a ~/ b; |
1961 a = a % b; | 1961 a = a % b; |
1962 a = a + b; | 1962 a = a + b; |
1963 a = a + /*pass should be severe:StaticTypeError*/a; | 1963 a = a + /*severe:StaticTypeError*/a; |
1964 a = a - b; | 1964 a = a - b; |
1965 b = /*severe:StaticTypeError*/b - b; | 1965 b = /*severe:StaticTypeError*/b - b; |
1966 a = a << b; | 1966 a = a << b; |
1967 a = a >> b; | 1967 a = a >> b; |
1968 a = a & b; | 1968 a = a & b; |
1969 a = a ^ b; | 1969 a = a ^ b; |
1970 a = a | b; | 1970 a = a | b; |
1971 c = (/*pass should be info:DynamicInvoke*/c + b); | 1971 c = (/*info:DynamicInvoke*/c + b); |
| 1972 |
| 1973 String x = 'hello'; |
| 1974 int y = 42; |
| 1975 x = x + x; |
| 1976 x = x + /*info:DynamicCast*/c; |
| 1977 x = x + /*severe:StaticTypeError*/y; |
| 1978 |
| 1979 bool p = true; |
| 1980 p = p && p; |
| 1981 p = p && /*info:DynamicCast*/c; |
| 1982 p = (/*info:DynamicCast*/c) && p; |
| 1983 p = (/*info:DynamicCast*/c) && /*info:DynamicCast*/c; |
| 1984 p = (/*severe:StaticTypeError*/y) && p; |
| 1985 p = c == y; |
1972 } | 1986 } |
1973 ''' | 1987 ''' |
1974 }); | 1988 }); |
1975 }); | 1989 }); |
1976 | 1990 |
1977 test('compound assignments', () { | 1991 test('compound assignments', () { |
1978 testChecker({ | 1992 testChecker({ |
1979 '/main.dart': ''' | 1993 '/main.dart': ''' |
1980 class A { | 1994 class A { |
1981 A operator *(B b) {} | 1995 A operator *(B b) {} |
(...skipping 933 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2915 f = bar as II2D; | 2929 f = bar as II2D; |
2916 f = bar as DD2I; | 2930 f = bar as DD2I; |
2917 f = bar as DI2D; | 2931 f = bar as DI2D; |
2918 f = bar as ID2D; | 2932 f = bar as ID2D; |
2919 f = bar as DD2D; | 2933 f = bar as DD2D; |
2920 } | 2934 } |
2921 ''' | 2935 ''' |
2922 }); | 2936 }); |
2923 }); | 2937 }); |
2924 } | 2938 } |
OLD | NEW |