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

Side by Side Diff: pkg/analyzer/test/generated/resolver_test.dart

Issue 1586813002: fix #25425, more inference of generic methods (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: rebase Created 4 years, 11 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
« no previous file with comments | « pkg/analyzer/lib/src/generated/static_type_analyzer.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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.generated.resolver_test; 5 library analyzer.test.generated.resolver_test;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analyzer/dart/element/element.dart'; 9 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/dart/element/type.dart'; 10 import 'package:analyzer/dart/element/type.dart';
(...skipping 13016 matching lines...) Expand 10 before | Expand all | Expand 10 after
13027 check("f2", _isListOf(_isInt)); 13027 check("f2", _isListOf(_isInt));
13028 check("f3", _isListOf(_isListOf(_isInt))); 13028 check("f3", _isListOf(_isListOf(_isInt)));
13029 } 13029 }
13030 } 13030 }
13031 13031
13032 /** 13032 /**
13033 * Strong mode static analyzer end to end tests 13033 * Strong mode static analyzer end to end tests
13034 */ 13034 */
13035 @reflectiveTest 13035 @reflectiveTest
13036 class StrongModeStaticTypeAnalyzer2Test extends _StaticTypeAnalyzer2TestShared { 13036 class StrongModeStaticTypeAnalyzer2Test extends _StaticTypeAnalyzer2TestShared {
13037 void fail_genericMethod_functionExpressionInvocation_inferred() {
13038 _resolveTestUnit(r'''
13039 class C<E> {
13040 /*=T*/ f/*<T>*/(/*=T*/ e) => null;
13041 static /*=T*/ g/*<T>*/(/*=T*/ e) => null;
13042 static final h = g;
13043 }
13044
13045 /*=T*/ topF/*<T>*/(/*=T*/ e) => null;
13046 var topG = topF;
13047 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) {
13048 var c = new C<int>();
13049 /*=T*/ lf/*<T>*/(/*=T*/ e) => null;
13050
13051 var lambdaCall = (/*<E>*/(/*=E*/ e) => e)(3);
13052 var methodCall = (c.f)(3);
13053 var staticCall = (C.g)(3);
13054 var staticFieldCall = (C.h)(3);
13055 var topFunCall = (topF)(3);
13056 var topFieldCall = (topG)(3);
13057 var localCall = (lf)(3);
13058 var paramCall = (pf)(3);
13059 }
13060 ''');
13061 expect(_findIdentifier('methodCall').staticType.toString(), "int");
13062 expect(_findIdentifier('staticCall').staticType.toString(), "int");
13063 expect(_findIdentifier('staticFieldCall').staticType.toString(), "int");
13064 expect(_findIdentifier('topFunCall').staticType.toString(), "int");
13065 expect(_findIdentifier('topFieldCall').staticType.toString(), "int");
13066 expect(_findIdentifier('localCall').staticType.toString(), "int");
13067 expect(_findIdentifier('paramCall').staticType.toString(), "int");
13068 expect(_findIdentifier('lambdaCall').staticType.toString(), "int");
13069 }
13070
13071 void fail_genericMethod_functionInvocation_inferred() {
13072 _resolveTestUnit(r'''
13073 class C<E> {
13074 /*=T*/ f/*<T>*/(/*=T*/ e) => null;
13075 static /*=T*/ g/*<T>*/(/*=T*/ e) => null;
13076 static final h = g;
13077 }
13078
13079 /*=T*/ topF/*<T>*/(/*=T*/ e) => null;
13080 var topG = topF;
13081 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) {
13082 var c = new C<int>();
13083 /*=T*/ lf/*<T>*/(/*=T*/ e) => null;
13084 var methodCall = c.f(3);
13085 var staticCall = C.g(3);
13086 var staticFieldCall = C.h(3);
13087 var topFunCall = topF(3);
13088 var topFieldCall = topG(3);
13089 var localCall = lf(3);
13090 var paramCall = pf(3);
13091 }
13092 ''');
13093 expect(_findIdentifier('methodCall').staticType.toString(), "int");
13094 expect(_findIdentifier('staticCall').staticType.toString(), "int");
13095 expect(_findIdentifier('staticFieldCall').staticType.toString(), "int");
13096 expect(_findIdentifier('topFunCall').staticType.toString(), "int");
13097 expect(_findIdentifier('topFieldCall').staticType.toString(), "int");
13098 expect(_findIdentifier('localCall').staticType.toString(), "int");
13099 expect(_findIdentifier('paramCall').staticType.toString(), "int");
13100 }
13101
13102 void fail_genericMethod_tearoff_instantiated() { 13037 void fail_genericMethod_tearoff_instantiated() {
13103 _resolveTestUnit(r''' 13038 _resolveTestUnit(r'''
13104 class C<E> { 13039 class C<E> {
13105 /*=T*/ f/*<T>*/(E e) => null; 13040 /*=T*/ f/*<T>*/(E e) => null;
13106 static /*=T*/ g/*<T>*/(/*=T*/ e) => null; 13041 static /*=T*/ g/*<T>*/(/*=T*/ e) => null;
13107 static final h = g; 13042 static final h = g;
13108 } 13043 }
13109 13044
13110 /*=T*/ topF/*<T>*/(/*=T*/ e) => null; 13045 /*=T*/ topF/*<T>*/(/*=T*/ e) => null;
13111 var topG = topF; 13046 var topG = topF;
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
13381 expect(_findIdentifier('methodCall').staticType.toString(), "int"); 13316 expect(_findIdentifier('methodCall').staticType.toString(), "int");
13382 expect(_findIdentifier('staticCall').staticType.toString(), "int"); 13317 expect(_findIdentifier('staticCall').staticType.toString(), "int");
13383 expect(_findIdentifier('staticFieldCall').staticType.toString(), "int"); 13318 expect(_findIdentifier('staticFieldCall').staticType.toString(), "int");
13384 expect(_findIdentifier('topFunCall').staticType.toString(), "int"); 13319 expect(_findIdentifier('topFunCall').staticType.toString(), "int");
13385 expect(_findIdentifier('topFieldCall').staticType.toString(), "int"); 13320 expect(_findIdentifier('topFieldCall').staticType.toString(), "int");
13386 expect(_findIdentifier('localCall').staticType.toString(), "int"); 13321 expect(_findIdentifier('localCall').staticType.toString(), "int");
13387 expect(_findIdentifier('paramCall').staticType.toString(), "int"); 13322 expect(_findIdentifier('paramCall').staticType.toString(), "int");
13388 expect(_findIdentifier('lambdaCall').staticType.toString(), "int"); 13323 expect(_findIdentifier('lambdaCall').staticType.toString(), "int");
13389 } 13324 }
13390 13325
13326 void test_genericMethod_functionExpressionInvocation_inferred() {
13327 _resolveTestUnit(r'''
13328 class C<E> {
13329 /*=T*/ f/*<T>*/(/*=T*/ e) => null;
13330 static /*=T*/ g/*<T>*/(/*=T*/ e) => null;
13331 static final h = g;
13332 }
13333
13334 /*=T*/ topF/*<T>*/(/*=T*/ e) => null;
13335 var topG = topF;
13336 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) {
13337 var c = new C<int>();
13338 /*=T*/ lf/*<T>*/(/*=T*/ e) => null;
13339
13340 var lambdaCall = (/*<E>*/(/*=E*/ e) => e)(3);
13341 var methodCall = (c.f)(3);
13342 var staticCall = (C.g)(3);
13343 var staticFieldCall = (C.h)(3);
13344 var topFunCall = (topF)(3);
13345 var topFieldCall = (topG)(3);
13346 var localCall = (lf)(3);
13347 var paramCall = (pf)(3);
13348 }
13349 ''');
13350 expect(_findIdentifier('methodCall').staticType.toString(), "int");
13351 expect(_findIdentifier('staticCall').staticType.toString(), "int");
13352 expect(_findIdentifier('staticFieldCall').staticType.toString(), "int");
13353 expect(_findIdentifier('topFunCall').staticType.toString(), "int");
13354 expect(_findIdentifier('topFieldCall').staticType.toString(), "int");
13355 expect(_findIdentifier('localCall').staticType.toString(), "int");
13356 expect(_findIdentifier('paramCall').staticType.toString(), "int");
13357 expect(_findIdentifier('lambdaCall').staticType.toString(), "int");
13358 }
13359
13391 void test_genericMethod_functionInvocation_explicit() { 13360 void test_genericMethod_functionInvocation_explicit() {
13392 _resolveTestUnit(r''' 13361 _resolveTestUnit(r'''
13393 class C<E> { 13362 class C<E> {
13394 /*=T*/ f/*<T>*/(/*=T*/ e) => null; 13363 /*=T*/ f/*<T>*/(/*=T*/ e) => null;
13395 static /*=T*/ g/*<T>*/(/*=T*/ e) => null; 13364 static /*=T*/ g/*<T>*/(/*=T*/ e) => null;
13396 static final h = g; 13365 static final h = g;
13397 } 13366 }
13398 13367
13399 /*=T*/ topF/*<T>*/(/*=T*/ e) => null; 13368 /*=T*/ topF/*<T>*/(/*=T*/ e) => null;
13400 var topG = topF; 13369 var topG = topF;
(...skipping 11 matching lines...) Expand all
13412 '''); 13381 ''');
13413 expect(_findIdentifier('methodCall').staticType.toString(), "int"); 13382 expect(_findIdentifier('methodCall').staticType.toString(), "int");
13414 expect(_findIdentifier('staticCall').staticType.toString(), "int"); 13383 expect(_findIdentifier('staticCall').staticType.toString(), "int");
13415 expect(_findIdentifier('staticFieldCall').staticType.toString(), "int"); 13384 expect(_findIdentifier('staticFieldCall').staticType.toString(), "int");
13416 expect(_findIdentifier('topFunCall').staticType.toString(), "int"); 13385 expect(_findIdentifier('topFunCall').staticType.toString(), "int");
13417 expect(_findIdentifier('topFieldCall').staticType.toString(), "int"); 13386 expect(_findIdentifier('topFieldCall').staticType.toString(), "int");
13418 expect(_findIdentifier('localCall').staticType.toString(), "int"); 13387 expect(_findIdentifier('localCall').staticType.toString(), "int");
13419 expect(_findIdentifier('paramCall').staticType.toString(), "int"); 13388 expect(_findIdentifier('paramCall').staticType.toString(), "int");
13420 } 13389 }
13421 13390
13391 void test_genericMethod_functionInvocation_inferred() {
13392 _resolveTestUnit(r'''
13393 class C<E> {
13394 /*=T*/ f/*<T>*/(/*=T*/ e) => null;
13395 static /*=T*/ g/*<T>*/(/*=T*/ e) => null;
13396 static final h = g;
13397 }
13398
13399 /*=T*/ topF/*<T>*/(/*=T*/ e) => null;
13400 var topG = topF;
13401 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) {
13402 var c = new C<int>();
13403 /*=T*/ lf/*<T>*/(/*=T*/ e) => null;
13404 var methodCall = c.f(3);
13405 var staticCall = C.g(3);
13406 var staticFieldCall = C.h(3);
13407 var topFunCall = topF(3);
13408 var topFieldCall = topG(3);
13409 var localCall = lf(3);
13410 var paramCall = pf(3);
13411 }
13412 ''');
13413 expect(_findIdentifier('methodCall').staticType.toString(), "int");
13414 expect(_findIdentifier('staticCall').staticType.toString(), "int");
13415 expect(_findIdentifier('staticFieldCall').staticType.toString(), "int");
13416 expect(_findIdentifier('topFunCall').staticType.toString(), "int");
13417 expect(_findIdentifier('topFieldCall').staticType.toString(), "int");
13418 expect(_findIdentifier('localCall').staticType.toString(), "int");
13419 expect(_findIdentifier('paramCall').staticType.toString(), "int");
13420 }
13421
13422 void test_genericMethod_functionTypedParameter() { 13422 void test_genericMethod_functionTypedParameter() {
13423 _resolveTestUnit(r''' 13423 _resolveTestUnit(r'''
13424 class C<E> { 13424 class C<E> {
13425 List/*<T>*/ f/*<T>*/(/*=T*/ f(E e)) => null; 13425 List/*<T>*/ f/*<T>*/(/*=T*/ f(E e)) => null;
13426 } 13426 }
13427 main() { 13427 main() {
13428 C<String> cOfString; 13428 C<String> cOfString;
13429 } 13429 }
13430 '''); 13430 ''');
13431 SimpleIdentifier f = _findIdentifier('f'); 13431 SimpleIdentifier f = _findIdentifier('f');
(...skipping 21 matching lines...) Expand all
13453 } 13453 }
13454 void foo() { 13454 void foo() {
13455 List list = null; 13455 List list = null;
13456 list.map((e) => e); 13456 list.map((e) => e);
13457 list.map((e) => 3); 13457 list.map((e) => 3);
13458 }'''); 13458 }''');
13459 13459
13460 SimpleIdentifier map1 = _findIdentifier('map((e) => e);'); 13460 SimpleIdentifier map1 = _findIdentifier('map((e) => e);');
13461 MethodInvocation m1 = map1.parent; 13461 MethodInvocation m1 = map1.parent;
13462 expect(m1.staticInvokeType.toString(), '((dynamic) → dynamic) → dynamic'); 13462 expect(m1.staticInvokeType.toString(), '((dynamic) → dynamic) → dynamic');
13463 expect(map1.staticType, isNull); 13463 expect(map1.staticType.toString(), '<T>((dynamic) → T) → T');
13464 expect(map1.propagatedType, isNull); 13464 expect(map1.propagatedType, isNull);
13465 SimpleIdentifier map2 = _findIdentifier('map((e) => 3);'); 13465 SimpleIdentifier map2 = _findIdentifier('map((e) => 3);');
13466 MethodInvocation m2 = map2.parent; 13466 MethodInvocation m2 = map2.parent;
13467 expect(m2.staticInvokeType.toString(), '((dynamic) → int) → int'); 13467 expect(m2.staticInvokeType.toString(), '((dynamic) → int) → int');
13468 expect(map2.staticType, isNull); 13468 expect(map2.staticType.toString(), '<T>((dynamic) → T) → T');
13469 expect(map2.propagatedType, isNull); 13469 expect(map2.propagatedType, isNull);
13470 } 13470 }
13471 13471
13472 void test_genericMethod_nestedCapture() { 13472 void test_genericMethod_nestedCapture() {
13473 _resolveTestUnit(r''' 13473 _resolveTestUnit(r'''
13474 class C<T> { 13474 class C<T> {
13475 /*=T*/ f/*<S>*/(/*=S*/ x) { 13475 /*=T*/ f/*<S>*/(/*=S*/ x) {
13476 new C<S>().f/*<int>*/(3); 13476 new C<S>().f/*<int>*/(3);
13477 new C<S>().f; // tear-off 13477 new C<S>().f; // tear-off
13478 return null; 13478 return null;
(...skipping 2284 matching lines...) Expand 10 before | Expand all | Expand 10 after
15763 library helper; 15763 library helper;
15764 dynamic $name = (int x) => x + 42'); 15764 dynamic $name = (int x) => x + 42');
15765 '''); 15765 ''');
15766 String code = ''' 15766 String code = '''
15767 import 'helper.dart' as helper; 15767 import 'helper.dart' as helper;
15768 main() { 15768 main() {
15769 helper.$name(); // marker 15769 helper.$name(); // marker
15770 }'''; 15770 }''';
15771 SimpleIdentifier methodName = _findMarkedIdentifier(code, "(); // marker"); 15771 SimpleIdentifier methodName = _findMarkedIdentifier(code, "(); // marker");
15772 MethodInvocation methodInvoke = methodName.parent; 15772 MethodInvocation methodInvoke = methodName.parent;
15773 expect(methodName.staticType, null, reason: 'library prefix has no type'); 15773 expect(methodName.staticType, typeProvider.dynamicType);
15774 expect(methodInvoke.staticType, typeProvider.dynamicType); 15774 expect(methodInvoke.staticType, typeProvider.dynamicType);
15775 } 15775 }
15776 15776
15777 void test_objectMethodInference_disabled_for_local_function() { 15777 void test_objectMethodInference_disabled_for_local_function() {
15778 String name = 'toString'; 15778 String name = 'toString';
15779 String code = ''' 15779 String code = '''
15780 main() { 15780 main() {
15781 dynamic $name = () => null; 15781 dynamic $name = () => null;
15782 $name(); // marker 15782 $name(); // marker
15783 }'''; 15783 }''';
(...skipping 925 matching lines...) Expand 10 before | Expand all | Expand 10 after
16709 16709
16710 void _resolveTestUnit(String code) { 16710 void _resolveTestUnit(String code) {
16711 testCode = code; 16711 testCode = code;
16712 testSource = addSource(testCode); 16712 testSource = addSource(testCode);
16713 LibraryElement library = resolve2(testSource); 16713 LibraryElement library = resolve2(testSource);
16714 assertNoErrors(testSource); 16714 assertNoErrors(testSource);
16715 verify([testSource]); 16715 verify([testSource]);
16716 testUnit = resolveCompilationUnit(testSource, library); 16716 testUnit = resolveCompilationUnit(testSource, library);
16717 } 16717 }
16718 } 16718 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/static_type_analyzer.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698