Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(322)

Side by Side Diff: pkg/analyzer/test/src/task/strong/checker_test.dart

Issue 2093523002: fix #25573, add option to disable implicit dynamic (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Merge remote-tracking branch 'origin/master' into no_implicit_dynamic Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 library analyzer.test.src.task.strong.checker_test; 5 library analyzer.test.src.task.strong.checker_test;
6 6
7 import '../../../reflective_tests.dart'; 7 import '../../../reflective_tests.dart';
8 import 'strong_test_helper.dart'; 8 import 'strong_test_helper.dart';
9 9
10 void main() { 10 void main() {
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 a[b] += d; 308 a[b] += d;
309 a[/*info:DYNAMIC_CAST*/c] += d; 309 a[/*info:DYNAMIC_CAST*/c] += d;
310 a[/*warning:ARGUMENT_TYPE_NOT_ASSIGNABLE*/z] += d; 310 a[/*warning:ARGUMENT_TYPE_NOT_ASSIGNABLE*/z] += d;
311 a[b] += /*info:DYNAMIC_CAST*/c; 311 a[b] += /*info:DYNAMIC_CAST*/c;
312 a[b] += /*warning:ARGUMENT_TYPE_NOT_ASSIGNABLE*/z; 312 a[b] += /*warning:ARGUMENT_TYPE_NOT_ASSIGNABLE*/z;
313 /*info:DYNAMIC_INVOKE,info:DYNAMIC_INVOKE*/c[b] += d; 313 /*info:DYNAMIC_INVOKE,info:DYNAMIC_INVOKE*/c[b] += d;
314 } 314 }
315 '''); 315 ''');
316 } 316 }
317 317
318 void test_constructorInvalid() {
319 // Regression test for https://github.com/dart-lang/sdk/issues/26695
320 checkFile('''
321 class A {
322 B({ /*error:FIELD_INITIALIZER_OUTSIDE_CONSTRUCTOR*/this.test: 1.0 }) {}
323 final double test = 0.0;
324 }
325 ''');
326 }
327
318 void test_constructors() { 328 void test_constructors() {
319 checkFile(''' 329 checkFile('''
320 const num z = 25; 330 const num z = 25;
321 Object obj = "world"; 331 Object obj = "world";
322 332
323 class A { 333 class A {
324 int x; 334 int x;
325 String y; 335 String y;
326 336
327 A(this.x) : this.y = /*warning:FIELD_INITIALIZER_NOT_ASSIGNABLE*/42; 337 A(this.x) : this.y = /*warning:FIELD_INITIALIZER_NOT_ASSIGNABLE*/42;
(...skipping 14 matching lines...) Expand all
342 B.c3(num x, Object y) : super.c3(x, /*info:DOWN_CAST_IMPLICIT*/y); 352 B.c3(num x, Object y) : super.c3(x, /*info:DOWN_CAST_IMPLICIT*/y);
343 } 353 }
344 354
345 void main() { 355 void main() {
346 A a = new A.c2(/*info:DOWN_CAST_IMPLICIT*/z, /*warning:ARGUMENT_TYPE_NOT_ASSI GNABLE*/z); 356 A a = new A.c2(/*info:DOWN_CAST_IMPLICIT*/z, /*warning:ARGUMENT_TYPE_NOT_ASSI GNABLE*/z);
347 var b = new B.c2(/*warning:ARGUMENT_TYPE_NOT_ASSIGNABLE*/"hello", /*info:DOWN _CAST_IMPLICIT*/obj); 357 var b = new B.c2(/*warning:ARGUMENT_TYPE_NOT_ASSIGNABLE*/"hello", /*info:DOWN _CAST_IMPLICIT*/obj);
348 } 358 }
349 '''); 359 ''');
350 } 360 }
351 361
352 void test_constructorInvalid() {
353 // Regression test for https://github.com/dart-lang/sdk/issues/26695
354 checkFile('''
355 class A {
356 B({ /*error:FIELD_INITIALIZER_OUTSIDE_CONSTRUCTOR*/this.test: 1.0 }) {}
357 final double test = 0.0;
358 }
359 ''');
360 }
361
362 void test_conversionAndDynamicInvoke() { 362 void test_conversionAndDynamicInvoke() {
363 addFile( 363 addFile(
364 ''' 364 '''
365 dynamic toString = (int x) => x + 42; 365 dynamic toString = (int x) => x + 42;
366 dynamic hashCode = "hello"; 366 dynamic hashCode = "hello";
367 ''', 367 ''',
368 name: '/helper.dart'); 368 name: '/helper.dart');
369 checkFile(''' 369 checkFile('''
370 import 'helper.dart' as helper; 370 import 'helper.dart' as helper;
371 371
(...skipping 1532 matching lines...) Expand 10 before | Expand all | Expand 10 after
1904 for (;/*info:DOWN_CAST_IMPLICIT*/obj;) {} 1904 for (;/*info:DOWN_CAST_IMPLICIT*/obj;) {}
1905 for (;/*error:NON_BOOL_CONDITION*/i;) {} 1905 for (;/*error:NON_BOOL_CONDITION*/i;) {}
1906 } 1906 }
1907 '''); 1907 ''');
1908 } 1908 }
1909 1909
1910 void test_implicitCasts() { 1910 void test_implicitCasts() {
1911 addFile('num n; int i = /*info:ASSIGNMENT_CAST*/n;'); 1911 addFile('num n; int i = /*info:ASSIGNMENT_CAST*/n;');
1912 check(); 1912 check();
1913 // TODO(jmesserly): should not be emitting the hint as well as the error. 1913 // TODO(jmesserly): should not be emitting the hint as well as the error.
1914 // It is a "strong mode hint" however, so it will not be user visible.
1914 addFile( 1915 addFile(
1915 'num n; int i = /*info:ASSIGNMENT_CAST,error:INVALID_ASSIGNMENT*/n;'); 1916 'num n; int i = /*info:ASSIGNMENT_CAST,error:INVALID_ASSIGNMENT*/n;');
1916 check(implicitCasts: false); 1917 check(implicitCasts: false);
1917 } 1918 }
1918 1919
1920 void test_implicitDynamic_field() {
1921 addFile(r'''
1922 class C {
1923 var /*error:IMPLICIT_DYNAMIC_FIELD*/x0;
1924 var /*error:IMPLICIT_DYNAMIC_FIELD*/x1 = (<dynamic>[])[0];
1925 var /*error:IMPLICIT_DYNAMIC_FIELD*/x2,
1926 x3 = 42,
1927 /*error:IMPLICIT_DYNAMIC_FIELD*/x4;
1928 dynamic y0;
1929 dynamic y1 = (<dynamic>[])[0];
1930 }
1931 ''');
1932 check(implicitDynamic: false);
1933 }
1934
1935 void test_implicitDynamic_function() {
1936 addFile(r'''
1937 /*=T*/ a/*<T>*/(/*=T*/ t) => t;
1938 /*=T*/ b/*<T>*/() => null;
1939
1940 void main/*<S>*/() {
1941 dynamic d;
1942 int i;
1943 /*error:IMPLICIT_DYNAMIC_FUNCTION*/a(d);
1944 a(42);
1945 /*error:IMPLICIT_DYNAMIC_FUNCTION*/b();
1946 d = /*error:IMPLICIT_DYNAMIC_FUNCTION*/b();
1947 i = b();
1948
1949 void f/*<T>*/(/*=T*/ t) {};
1950 /*=T*/ g/*<T>*/() => null;
1951
1952 /*error:IMPLICIT_DYNAMIC_FUNCTION*/f(d);
1953 f(42);
1954 /*error:IMPLICIT_DYNAMIC_FUNCTION*/g();
1955 d = /*error:IMPLICIT_DYNAMIC_FUNCTION*/g();
1956 i = g();
1957
1958 /*error:IMPLICIT_DYNAMIC_INVOKE*/(/*<T>*/(/*=T*/ t) => t)(d);
1959 (/*<T>*/(/*=T*/ t) => t)(42);
1960 (/*<T>*/() => null as dynamic/*=T*/)/*<int>*/();
1961 }
1962 ''');
1963 check(implicitDynamic: false);
1964 }
1965 void test_implicitDynamic_listLiteral() {
1966 addFile(r'''
1967
1968 var l0 = /*error:IMPLICIT_DYNAMIC_LIST_LITERAL*/[];
1969 List l1 = /*error:IMPLICIT_DYNAMIC_LIST_LITERAL*/[];
1970 List<dynamic> l2 = /*error:IMPLICIT_DYNAMIC_LIST_LITERAL*/[];
1971 dynamic d = 42;
1972 var l3 = /*error:IMPLICIT_DYNAMIC_LIST_LITERAL*/[d, d];
1973
1974 var l4 = <dynamic>[];
1975 var l5 = <int>[];
1976 List<int> l6 = /*info:INFERRED_TYPE_LITERAL*/[];
1977 var l7 = /*info:INFERRED_TYPE_LITERAL*/[42];
1978 ''');
1979 check(implicitDynamic: false);
1980 }
1981
1982 void test_implicitDynamic_mapLiteral() {
1983 addFile(r'''
1984 var m0 = /*error:IMPLICIT_DYNAMIC_MAP_LITERAL*/{};
1985 Map m1 = /*error:IMPLICIT_DYNAMIC_MAP_LITERAL*/{};
1986 Map<dynamic, dynamic> m2 = /*error:IMPLICIT_DYNAMIC_MAP_LITERAL*/{};
1987 dynamic d = 42;
1988 var m3 = /*error:IMPLICIT_DYNAMIC_MAP_LITERAL*/{d: d};
1989 var m4 = /*info:INFERRED_TYPE_LITERAL,error:IMPLICIT_DYNAMIC_MAP_LITERAL*/{'x': d, 'y': d};
1990 var m5 = /*info:INFERRED_TYPE_LITERAL,error:IMPLICIT_DYNAMIC_MAP_LITERAL*/{d: 'x '};
1991
1992 var m6 = <dynamic, dynamic>{};
1993 var m7 = <String, String>{};
1994 Map<String, String> m8 = /*info:INFERRED_TYPE_LITERAL*/{};
1995 var m9 = /*info:INFERRED_TYPE_LITERAL*/{'hi': 'there'};
1996 ''');
1997 check(implicitDynamic: false);
1998 }
1999
2000 void test_implicitDynamic_method() {
2001 addFile(r'''
2002 class C {
2003 /*=T*/ m/*<T>*/(/*=T*/ s) => s;
2004 /*=T*/ n/*<T>*/() => null;
2005 }
2006 class D<E> {
2007 /*=T*/ m/*<T>*/(/*=T*/ s) => s;
2008 /*=T*/ n/*<T>*/() => null;
2009 }
2010 void f() {
2011 dynamic d;
2012 int i;
2013 new C()./*error:IMPLICIT_DYNAMIC_METHOD*/m(d);
2014 new C().m(42);
2015 new C()./*error:IMPLICIT_DYNAMIC_METHOD*/n();
2016 d = new C()./*error:IMPLICIT_DYNAMIC_METHOD*/n();
2017 i = new C().n();
2018
2019 new D<int>()./*error:IMPLICIT_DYNAMIC_METHOD*/m(d);
2020 new D<int>().m(42);
2021 new D<int>()./*error:IMPLICIT_DYNAMIC_METHOD*/n();
2022 d = new D<int>()./*error:IMPLICIT_DYNAMIC_METHOD*/n();
2023 i = new D<int>().n();
2024 }
2025 ''');
2026 check(implicitDynamic: false);
2027 }
2028
2029 void test_implicitDynamic_parameter() {
2030 addFile(r'''
2031 const dynamic DYNAMIC_VALUE = 42;
2032
2033 // simple formal
2034 void f0(/*error:IMPLICIT_DYNAMIC_PARAMETER*/x) {}
2035 void f1(dynamic x) {}
2036
2037 // default formal
2038 void df0([/*error:IMPLICIT_DYNAMIC_PARAMETER*/x = DYNAMIC_VALUE]) {}
2039 void df1([dynamic x = DYNAMIC_VALUE]) {}
2040 void df2([/*info:INFERRED_TYPE*/x = 42]) {}
2041
2042 // default formal (named)
2043 void nf0({/*error:IMPLICIT_DYNAMIC_PARAMETER*/x: DYNAMIC_VALUE}) {}
2044 void nf1({dynamic x: DYNAMIC_VALUE}) {}
2045 void nf2({/*info:INFERRED_TYPE*/x: 42}) {}
2046
2047 // field formal
2048 class C {
2049 var /*error:IMPLICIT_DYNAMIC_FIELD*/x;
2050 C(this.x);
2051 }
2052
2053 // function typed formal
2054 void ftf0(void x(/*error:IMPLICIT_DYNAMIC_PARAMETER*/y)) {}
2055 void ftf1(void x(int y)) {}
2056 ''');
2057 check(implicitDynamic: false);
2058 }
2059
2060 void test_implicitDynamic_return() {
2061 addFile(r'''
2062 // function
2063 /*error:IMPLICIT_DYNAMIC_RETURN*/f0() {}
2064 dynamic f1() { return 42; }
2065
2066 // nested function
2067 void main() {
2068 /*error:IMPLICIT_DYNAMIC_RETURN*/g0() {}
2069 dynamic g1() { return 42; }
2070 }
2071
2072 // methods
2073 class B {
2074 int m1() => 42;
2075 }
2076 class C extends B {
2077 /*error:IMPLICIT_DYNAMIC_RETURN*/m0() => 123;
2078 m1() => 123;
2079 dynamic m2() => 'hi';
2080 }
2081
2082 // accessors
2083 set x(int value) {}
2084 /*error:IMPLICIT_DYNAMIC_RETURN*/get y0 => 42;
2085 dynamic get y1 => 42;
2086
2087 // function typed formals
2088 void ftf0(/*error:IMPLICIT_DYNAMIC_RETURN*/f(int x)) {}
2089 void ftf1(dynamic f(int x)) {}
2090
2091 // function expressions
2092 var fe0 = (int x) => x as dynamic;
2093 var fe1 = (int x) => x;
2094 ''');
2095 check(implicitDynamic: false);
2096 }
2097
2098 void test_implicitDynamic_type() {
2099 addFile(r'''
2100 class C<T> {}
2101 class M1<T extends /*error:IMPLICIT_DYNAMIC_TYPE*/List> {}
2102 class M2<T> {}
2103 class I<T> {}
2104 class D<T, S> extends /*error:IMPLICIT_DYNAMIC_TYPE*/C
2105 with M1, /*error:IMPLICIT_DYNAMIC_TYPE*/M2
2106 implements /*error:IMPLICIT_DYNAMIC_TYPE*/I {}
2107
2108 C f(D d) {
2109 D x = new /*error:IMPLICIT_DYNAMIC_TYPE*/D();
2110 D<int, dynamic> y = /*info:INFERRED_TYPE_ALLOCATION*/new /*error:IMPLICIT_DYNA MIC_TYPE*/D();
2111 D<dynamic, int> z = /*info:INFERRED_TYPE_ALLOCATION*/new /*error:IMPLICIT_DYNA MIC_TYPE*/D();
2112 return new /*error:IMPLICIT_DYNAMIC_TYPE*/C();
2113 }
2114
2115 class A<T extends num> {}
2116 class N1<T extends List<int>> {}
2117 class N2<T extends Object> {}
2118 class J<T extends Object> {}
2119 class B<T extends Object> extends A with N1, N2 implements J {}
2120 A g(B b) {
2121 B y = /*info:INFERRED_TYPE_ALLOCATION*/new B();
2122 return /*info:INFERRED_TYPE_ALLOCATION*/new A();
2123 }
2124 ''');
2125 check(implicitDynamic: false);
2126 }
2127
2128 void test_implicitDynamic_variable() {
2129 addFile(r'''
2130 var /*error:IMPLICIT_DYNAMIC_VARIABLE*/x0;
2131 var /*error:IMPLICIT_DYNAMIC_VARIABLE*/x1 = (<dynamic>[])[0];
2132 var /*error:IMPLICIT_DYNAMIC_VARIABLE*/x2,
2133 x3 = 42,
2134 /*error:IMPLICIT_DYNAMIC_VARIABLE*/x4;
2135 dynamic y0;
2136 dynamic y1 = (<dynamic>[])[0];
2137 ''');
2138 check(implicitDynamic: false);
2139 }
2140
1919 void test_invalidOverrides_baseClassOverrideToChildInterface() { 2141 void test_invalidOverrides_baseClassOverrideToChildInterface() {
1920 checkFile(''' 2142 checkFile('''
1921 class A {} 2143 class A {}
1922 class B {} 2144 class B {}
1923 2145
1924 abstract class I { 2146 abstract class I {
1925 m(A a); 2147 m(A a);
1926 } 2148 }
1927 2149
1928 class Base { 2150 class Base {
(...skipping 1380 matching lines...) Expand 10 before | Expand all | Expand 10 after
3309 // Regression test for https://github.com/dart-lang/sdk/issues/25069 3531 // Regression test for https://github.com/dart-lang/sdk/issues/25069
3310 checkFile(''' 3532 checkFile('''
3311 typedef int Foo(); 3533 typedef int Foo();
3312 void foo() {} 3534 void foo() {}
3313 void main () { 3535 void main () {
3314 Foo x = /*error:INVALID_ASSIGNMENT,info:USE_OF_VOID_RESULT*/foo(); 3536 Foo x = /*error:INVALID_ASSIGNMENT,info:USE_OF_VOID_RESULT*/foo();
3315 } 3537 }
3316 '''); 3538 ''');
3317 } 3539 }
3318 } 3540 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/error_verifier.dart ('k') | pkg/analyzer/test/src/task/strong/strong_test_helper.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698