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

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

Issue 1488383004: Fixes incorrect generic function type capture (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years 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) 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 engine.resolver_test; 5 library engine.resolver_test;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analyzer/src/context/context.dart' as newContext; 9 import 'package:analyzer/src/context/context.dart' as newContext;
10 import 'package:analyzer/src/generated/ast.dart'; 10 import 'package:analyzer/src/generated/ast.dart';
(...skipping 12118 matching lines...) Expand 10 before | Expand all | Expand 10 after
12129 computeLibrarySourceErrors(source); 12129 computeLibrarySourceErrors(source);
12130 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_OPERATOR]); 12130 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_OPERATOR]);
12131 } 12131 }
12132 } 12132 }
12133 12133
12134 /** 12134 /**
12135 * Strong mode static analyzer end to end tests 12135 * Strong mode static analyzer end to end tests
12136 */ 12136 */
12137 @reflectiveTest 12137 @reflectiveTest
12138 class StrongModeStaticTypeAnalyzer2Test extends _StaticTypeAnalyzer2TestShared { 12138 class StrongModeStaticTypeAnalyzer2Test extends _StaticTypeAnalyzer2TestShared {
12139 void fail_genericMethod_nested() {
12140 // TODO(jmesserly): this test currently fails because we incorrectly capture
12141 // S in FunctionTypeImpl.substitute. We should probably rename free
12142 // variables during substitution to avoid it.
12143 _resolveTestUnit(r'''
12144 class C<T> {
12145 /*=T*/ f/*<S>*/(/*=S*/ x) {
12146 new C<S>().f/*<int>*/(3);
12147 return null;
12148 }
12149 }
12150 ''');
12151 SimpleIdentifier f = _findIdentifier('f/*<int>*/(3);');
12152 expect(f.staticType.toString(), '(int) → S');
12153 }
12154
12155 void setUp() { 12139 void setUp() {
12156 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); 12140 AnalysisOptionsImpl options = new AnalysisOptionsImpl();
12157 options.strongMode = true; 12141 options.strongMode = true;
12158 resetWithOptions(options); 12142 resetWithOptions(options);
12159 } 12143 }
12160 12144
12161 void test_dynamicObjectGetter_hashCode() { 12145 void test_dynamicObjectGetter_hashCode() {
12162 String code = r''' 12146 String code = r'''
12163 main() { 12147 main() {
12164 dynamic a = null; 12148 dynamic a = null;
(...skipping 26 matching lines...) Expand all
12191 } 12175 }
12192 12176
12193 void test_genericFunction() { 12177 void test_genericFunction() {
12194 if (!AnalysisEngine.instance.useTaskModel) { 12178 if (!AnalysisEngine.instance.useTaskModel) {
12195 return; 12179 return;
12196 } 12180 }
12197 _resolveTestUnit(r'/*=T*/ f/*<T>*/(/*=T*/ x) => null;'); 12181 _resolveTestUnit(r'/*=T*/ f/*<T>*/(/*=T*/ x) => null;');
12198 SimpleIdentifier f = _findIdentifier('f'); 12182 SimpleIdentifier f = _findIdentifier('f');
12199 FunctionElementImpl e = f.staticElement; 12183 FunctionElementImpl e = f.staticElement;
12200 expect(e.typeParameters.toString(), '[T]'); 12184 expect(e.typeParameters.toString(), '[T]');
12201 expect(e.type.typeParameters.toString(), '[T]'); 12185 expect(e.type.boundTypeParameters.toString(), '[T]');
12202 expect(e.type.typeParameters[0].type, e.type.typeArguments[0]); 12186 expect(e.type.typeParameters.toString(), '[]');
12203 expect(e.type.toString(), '(T) → T'); 12187 expect(e.type.toString(), '<T>(T) → T');
12204 12188
12205 // Substitute for T 12189 FunctionType ft = e.type.instantiate([typeProvider.stringType]);
12206 DartType t = e.typeParameters[0].type;
12207 FunctionType ft = e.type.substitute2([typeProvider.stringType], [t]);
12208 expect(ft.toString(), '(String) → String'); 12190 expect(ft.toString(), '(String) → String');
12209 } 12191 }
12210 12192
12211 void test_genericMethod() { 12193 void test_genericMethod() {
12212 if (!AnalysisEngine.instance.useTaskModel) { 12194 if (!AnalysisEngine.instance.useTaskModel) {
12213 return; 12195 return;
12214 } 12196 }
12215 _resolveTestUnit(r''' 12197 _resolveTestUnit(r'''
12216 class C<E> { 12198 class C<E> {
12217 List/*<T>*/ f/*<T>*/(E e) => null; 12199 List/*<T>*/ f/*<T>*/(E e) => null;
12218 } 12200 }
12219 main() { 12201 main() {
12220 C<String> cOfString; 12202 C<String> cOfString;
12221 } 12203 }
12222 '''); 12204 ''');
12223 SimpleIdentifier f = _findIdentifier('f'); 12205 SimpleIdentifier f = _findIdentifier('f');
12224 MethodElementImpl e = f.staticElement; 12206 MethodElementImpl e = f.staticElement;
12225 expect(e.typeParameters.toString(), '[T]'); 12207 expect(e.typeParameters.toString(), '[T]');
12226 expect(e.type.typeParameters.toString(), '[T, E]'); 12208 expect(e.type.boundTypeParameters.toString(), '[T]');
12227 expect(e.type.typeArguments.toString(), '[T, E]'); 12209 expect(e.type.typeParameters.toString(), '[E]');
12228 expect(e.type.toString(), '(E) → List<T>'); 12210 expect(e.type.typeArguments.toString(), '[E]');
12211 expect(e.type.toString(), '<T>(E) → List<T>');
12229 12212
12230 SimpleIdentifier c = _findIdentifier('cOfString'); 12213 SimpleIdentifier c = _findIdentifier('cOfString');
12231 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type; 12214 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type;
12232 expect(ft.toString(), '(String) → List<T>'); 12215 expect(ft.toString(), '<T>(String) → List<T>');
12233 DartType t = e.typeParameters[0].type; 12216 ft = ft.instantiate([typeProvider.intType]);
12234 ft = ft.substitute2([typeProvider.intType], [t]);
12235 expect(ft.toString(), '(String) → List<int>'); 12217 expect(ft.toString(), '(String) → List<int>');
12218 expect('${ft.typeArguments}/${ft.typeParameters}', '[String, int]/[E, T]');
12236 } 12219 }
12237 12220
12238 void test_genericMethod_functionTypedParameter() { 12221 void test_genericMethod_functionTypedParameter() {
12239 if (!AnalysisEngine.instance.useTaskModel) { 12222 if (!AnalysisEngine.instance.useTaskModel) {
12240 return; 12223 return;
12241 } 12224 }
12242 _resolveTestUnit(r''' 12225 _resolveTestUnit(r'''
12243 class C<E> { 12226 class C<E> {
12244 List/*<T>*/ f/*<T>*/(/*=T*/ f(E e)) => null; 12227 List/*<T>*/ f/*<T>*/(/*=T*/ f(E e)) => null;
12245 } 12228 }
12246 main() { 12229 main() {
12247 C<String> cOfString; 12230 C<String> cOfString;
12248 } 12231 }
12249 '''); 12232 ''');
12250 SimpleIdentifier f = _findIdentifier('f'); 12233 SimpleIdentifier f = _findIdentifier('f');
12251 MethodElementImpl e = f.staticElement; 12234 MethodElementImpl e = f.staticElement;
12252 expect(e.typeParameters.toString(), '[T]'); 12235 expect(e.typeParameters.toString(), '[T]');
12253 expect(e.type.typeParameters.toString(), '[T, E]'); 12236 expect(e.type.boundTypeParameters.toString(), '[T]');
12254 expect(e.type.typeArguments.toString(), '[T, E]'); 12237 expect(e.type.typeParameters.toString(), '[E]');
12255 expect(e.type.toString(), '((E) → T) → List<T>'); 12238 expect(e.type.typeArguments.toString(), '[E]');
12239 expect(e.type.toString(), '<T>((E) → T) → List<T>');
12256 12240
12257 SimpleIdentifier c = _findIdentifier('cOfString'); 12241 SimpleIdentifier c = _findIdentifier('cOfString');
12258 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type; 12242 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type;
12259 expect(ft.toString(), '((String) → T) → List<T>'); 12243 expect(ft.toString(), '<T>((String) → T) → List<T>');
12260 DartType t = e.typeParameters[0].type; 12244 ft = ft.instantiate([typeProvider.intType]);
12261 ft = ft.substitute2([typeProvider.intType], [t]);
12262 expect(ft.toString(), '((String) → int) → List<int>'); 12245 expect(ft.toString(), '((String) → int) → List<int>');
12263 } 12246 }
12264 12247
12248 void test_genericMethod_nestedCapture() {
12249 _resolveTestUnit(r'''
12250 class C<T> {
12251 /*=T*/ f/*<S>*/(/*=S*/ x) {
12252 new C<S>().f/*<int>*/(3);
12253 new C<S>().f; // tear-off
12254 return null;
12255 }
12256 }
12257 ''');
12258 SimpleIdentifier f = _findIdentifier('f/*<int>*/(3);');
12259 expect(f.staticType.toString(), '(int) → S');
12260 FunctionType ft = f.staticType;
12261 expect('${ft.typeArguments}/${ft.typeParameters}', '[S, int]/[T, S]');
12262
12263 f = _findIdentifier('f;');
12264 expect(f.staticType.toString(), '<S₀>(S₀) → S');
12265 }
12266
12267 void test_genericMethod_nestedFunctions() {
12268 _resolveTestUnit(r'''
12269 /*=S*/ f/*<S>*/(/*=S*/ x) {
12270 g/*<S>*/(/*=S*/ x) => f;
12271 return null;
12272 }
12273 ''');
12274 SimpleIdentifier g = _findIdentifier('f');
12275 expect(g.staticType.toString(), '<S>(S) → S');
12276 SimpleIdentifier f = _findIdentifier('g');
12277 expect(f.staticType.toString(), '<S₀>(S₀) → dynamic');
12278 }
12279
12265 void test_pseudoGeneric_max_doubleDouble() { 12280 void test_pseudoGeneric_max_doubleDouble() {
12266 String code = r''' 12281 String code = r'''
12267 import 'dart:math'; 12282 import 'dart:math';
12268 main() { 12283 main() {
12269 var foo = max(1.0, 2.0); 12284 var foo = max(1.0, 2.0);
12270 } 12285 }
12271 '''; 12286 ''';
12272 _resolveTestUnit(code); 12287 _resolveTestUnit(code);
12273 12288
12274 SimpleIdentifier identifier = _findIdentifier('foo'); 12289 SimpleIdentifier identifier = _findIdentifier('foo');
(...skipping 571 matching lines...) Expand 10 before | Expand all | Expand 10 after
12846 } 12861 }
12847 12862
12848 void test_getType_noScope() { 12863 void test_getType_noScope() {
12849 TypeOverrideManager manager = new TypeOverrideManager(); 12864 TypeOverrideManager manager = new TypeOverrideManager();
12850 expect(manager.getType(ElementFactory.localVariableElement2("v")), isNull); 12865 expect(manager.getType(ElementFactory.localVariableElement2("v")), isNull);
12851 } 12866 }
12852 } 12867 }
12853 12868
12854 @reflectiveTest 12869 @reflectiveTest
12855 class TypePropagationTest extends ResolverTestCase { 12870 class TypePropagationTest extends ResolverTestCase {
12856 void test_invocation_target_prefixed() {
12857 addNamedSource(
12858 '/helper.dart',
12859 '''
12860 library helper;
12861 int max(int x, int y) => 0;
12862 ''');
12863 String code = '''
12864 import 'helper.dart' as helper;
12865 main() {
12866 helper.max(10, 10); // marker
12867 }''';
12868 SimpleIdentifier methodName =
12869 _findMarkedIdentifier(code, "(10, 10); // marker");
12870 MethodInvocation methodInvoke = methodName.parent;
12871 expect(methodInvoke.methodName.staticElement, isNotNull);
12872 expect(methodInvoke.methodName.propagatedElement, isNull);
12873 }
12874
12875 void fail_mergePropagatedTypesAtJoinPoint_1() { 12871 void fail_mergePropagatedTypesAtJoinPoint_1() {
12876 // https://code.google.com/p/dart/issues/detail?id=19929 12872 // https://code.google.com/p/dart/issues/detail?id=19929
12877 _assertTypeOfMarkedExpression( 12873 _assertTypeOfMarkedExpression(
12878 r''' 12874 r'''
12879 f1(x) { 12875 f1(x) {
12880 var y = []; 12876 var y = [];
12881 if (x) { 12877 if (x) {
12882 y = 0; 12878 y = 0;
12883 } else { 12879 } else {
12884 y = ''; 12880 y = '';
(...skipping 799 matching lines...) Expand 10 before | Expand all | Expand 10 after
13684 expect(identifier.propagatedType, same(null)); 13680 expect(identifier.propagatedType, same(null));
13685 } 13681 }
13686 { 13682 {
13687 SimpleIdentifier identifier = EngineTestCase.findNode( 13683 SimpleIdentifier identifier = EngineTestCase.findNode(
13688 unit, code, "v; // marker", (node) => node is SimpleIdentifier); 13684 unit, code, "v; // marker", (node) => node is SimpleIdentifier);
13689 expect(identifier.staticType, same(typeProvider.intType)); 13685 expect(identifier.staticType, same(typeProvider.intType));
13690 expect(identifier.propagatedType, same(null)); 13686 expect(identifier.propagatedType, same(null));
13691 } 13687 }
13692 } 13688 }
13693 13689
13690 void test_invocation_target_prefixed() {
13691 addNamedSource(
13692 '/helper.dart',
13693 '''
13694 library helper;
13695 int max(int x, int y) => 0;
13696 ''');
13697 String code = '''
13698 import 'helper.dart' as helper;
13699 main() {
13700 helper.max(10, 10); // marker
13701 }''';
13702 SimpleIdentifier methodName =
13703 _findMarkedIdentifier(code, "(10, 10); // marker");
13704 MethodInvocation methodInvoke = methodName.parent;
13705 expect(methodInvoke.methodName.staticElement, isNotNull);
13706 expect(methodInvoke.methodName.propagatedElement, isNull);
13707 }
13708
13694 void test_is_conditional() { 13709 void test_is_conditional() {
13695 Source source = addSource(r''' 13710 Source source = addSource(r'''
13696 class A {} 13711 class A {}
13697 A f(var p) { 13712 A f(var p) {
13698 return (p is A) ? p : null; 13713 return (p is A) ? p : null;
13699 }'''); 13714 }''');
13700 LibraryElement library = resolve2(source); 13715 LibraryElement library = resolve2(source);
13701 assertNoErrors(source); 13716 assertNoErrors(source);
13702 verify([source]); 13717 verify([source]);
13703 CompilationUnit unit = resolveCompilationUnit(source, library); 13718 CompilationUnit unit = resolveCompilationUnit(source, library);
(...skipping 1574 matching lines...) Expand 10 before | Expand all | Expand 10 after
15278 15293
15279 void _resolveTestUnit(String code) { 15294 void _resolveTestUnit(String code) {
15280 testCode = code; 15295 testCode = code;
15281 testSource = addSource(testCode); 15296 testSource = addSource(testCode);
15282 LibraryElement library = resolve2(testSource); 15297 LibraryElement library = resolve2(testSource);
15283 assertNoErrors(testSource); 15298 assertNoErrors(testSource);
15284 verify([testSource]); 15299 verify([testSource]);
15285 testUnit = resolveCompilationUnit(testSource, library); 15300 testUnit = resolveCompilationUnit(testSource, library);
15286 } 15301 }
15287 } 15302 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698