OLD | NEW |
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 13045 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
13056 identifier.getAncestor((node) => node is VariableDeclaration); | 13056 identifier.getAncestor((node) => node is VariableDeclaration); |
13057 expect(declaration.initializer.staticType.name, 'String'); | 13057 expect(declaration.initializer.staticType.name, 'String'); |
13058 expect(declaration.initializer.propagatedType, isNull); | 13058 expect(declaration.initializer.propagatedType, isNull); |
13059 } | 13059 } |
13060 | 13060 |
13061 void test_genericFunction() { | 13061 void test_genericFunction() { |
13062 _resolveTestUnit(r'/*=T*/ f/*<T>*/(/*=T*/ x) => null;'); | 13062 _resolveTestUnit(r'/*=T*/ f/*<T>*/(/*=T*/ x) => null;'); |
13063 SimpleIdentifier f = _findIdentifier('f'); | 13063 SimpleIdentifier f = _findIdentifier('f'); |
13064 FunctionElementImpl e = f.staticElement; | 13064 FunctionElementImpl e = f.staticElement; |
13065 expect(e.typeParameters.toString(), '[T]'); | 13065 expect(e.typeParameters.toString(), '[T]'); |
13066 expect(e.type.boundTypeParameters.toString(), '[T]'); | 13066 expect(e.type.typeFormals.toString(), '[T]'); |
13067 expect(e.type.typeParameters.toString(), '[]'); | 13067 expect(e.type.typeParameters.toString(), '[]'); |
13068 expect(e.type.toString(), '<T>(T) → T'); | 13068 expect(e.type.toString(), '<T>(T) → T'); |
13069 | 13069 |
13070 FunctionType ft = e.type.instantiate([typeProvider.stringType]); | 13070 FunctionType ft = e.type.instantiate([typeProvider.stringType]); |
13071 expect(ft.toString(), '(String) → String'); | 13071 expect(ft.toString(), '(String) → String'); |
13072 } | 13072 } |
13073 | 13073 |
13074 void test_genericFunction_bounds() { | 13074 void test_genericFunction_bounds() { |
13075 _resolveTestUnit(r'/*=T*/ f/*<T extends num>*/(/*=T*/ x) => null;'); | 13075 _resolveTestUnit(r'/*=T*/ f/*<T extends num>*/(/*=T*/ x) => null;'); |
13076 SimpleIdentifier f = _findIdentifier('f'); | 13076 SimpleIdentifier f = _findIdentifier('f'); |
13077 FunctionElementImpl e = f.staticElement; | 13077 FunctionElementImpl e = f.staticElement; |
13078 expect(e.typeParameters.toString(), '[T extends num]'); | 13078 expect(e.typeParameters.toString(), '[T extends num]'); |
13079 expect(e.type.boundTypeParameters.toString(), '[T extends num]'); | 13079 expect(e.type.typeFormals.toString(), '[T extends num]'); |
13080 expect(e.type.typeParameters.toString(), '[]'); | 13080 expect(e.type.typeParameters.toString(), '[]'); |
13081 expect(e.type.toString(), '<T extends num>(T) → T'); | 13081 expect(e.type.toString(), '<T extends num>(T) → T'); |
13082 } | 13082 } |
13083 | 13083 |
13084 void test_genericFunction_static() { | 13084 void test_genericFunction_static() { |
13085 _resolveTestUnit(r''' | 13085 _resolveTestUnit(r''' |
13086 class C<E> { | 13086 class C<E> { |
13087 static /*=T*/ f/*<T>*/(/*=T*/ x) => null; | 13087 static /*=T*/ f/*<T>*/(/*=T*/ x) => null; |
13088 } | 13088 } |
13089 '''); | 13089 '''); |
13090 SimpleIdentifier f = _findIdentifier('f'); | 13090 SimpleIdentifier f = _findIdentifier('f'); |
13091 MethodElementImpl e = f.staticElement; | 13091 MethodElementImpl e = f.staticElement; |
13092 expect(e.typeParameters.toString(), '[T]'); | 13092 expect(e.typeParameters.toString(), '[T]'); |
13093 expect(e.type.boundTypeParameters.toString(), '[T]'); | 13093 expect(e.type.typeFormals.toString(), '[T]'); |
13094 // TODO(jmesserly): we could get rid of this {E/E} substitution, but it's | 13094 // TODO(jmesserly): we could get rid of this {E/E} substitution, but it's |
13095 // probably harmless, as E won't be used in the function (error verifier | 13095 // probably harmless, as E won't be used in the function (error verifier |
13096 // checks this), and {E/E} is a no-op anyway. | 13096 // checks this), and {E/E} is a no-op anyway. |
13097 expect(e.type.typeParameters.toString(), '[E]'); | 13097 expect(e.type.typeParameters.toString(), '[E]'); |
13098 expect(e.type.typeArguments.toString(), '[E]'); | 13098 expect(e.type.typeArguments.toString(), '[E]'); |
13099 expect(e.type.toString(), '<T>(T) → T'); | 13099 expect(e.type.toString(), '<T>(T) → T'); |
13100 | 13100 |
13101 FunctionType ft = e.type.instantiate([typeProvider.stringType]); | 13101 FunctionType ft = e.type.instantiate([typeProvider.stringType]); |
13102 expect(ft.toString(), '(String) → String'); | 13102 expect(ft.toString(), '(String) → String'); |
13103 } | 13103 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
13182 class C<E> { | 13182 class C<E> { |
13183 List/*<T>*/ f/*<T>*/(E e) => null; | 13183 List/*<T>*/ f/*<T>*/(E e) => null; |
13184 } | 13184 } |
13185 main() { | 13185 main() { |
13186 C<String> cOfString; | 13186 C<String> cOfString; |
13187 } | 13187 } |
13188 '''); | 13188 '''); |
13189 SimpleIdentifier f = _findIdentifier('f'); | 13189 SimpleIdentifier f = _findIdentifier('f'); |
13190 MethodElementImpl e = f.staticElement; | 13190 MethodElementImpl e = f.staticElement; |
13191 expect(e.typeParameters.toString(), '[T]'); | 13191 expect(e.typeParameters.toString(), '[T]'); |
13192 expect(e.type.boundTypeParameters.toString(), '[T]'); | 13192 expect(e.type.typeFormals.toString(), '[T]'); |
13193 expect(e.type.typeParameters.toString(), '[E]'); | 13193 expect(e.type.typeParameters.toString(), '[E]'); |
13194 expect(e.type.typeArguments.toString(), '[E]'); | 13194 expect(e.type.typeArguments.toString(), '[E]'); |
13195 expect(e.type.toString(), '<T>(E) → List<T>'); | 13195 expect(e.type.toString(), '<T>(E) → List<T>'); |
13196 | 13196 |
13197 SimpleIdentifier c = _findIdentifier('cOfString'); | 13197 SimpleIdentifier c = _findIdentifier('cOfString'); |
13198 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type; | 13198 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type; |
13199 expect(ft.toString(), '<T>(String) → List<T>'); | 13199 expect(ft.toString(), '<T>(String) → List<T>'); |
13200 ft = ft.instantiate([typeProvider.intType]); | 13200 ft = ft.instantiate([typeProvider.intType]); |
13201 expect(ft.toString(), '(String) → List<int>'); | 13201 expect(ft.toString(), '(String) → List<int>'); |
13202 expect('${ft.typeArguments}/${ft.typeParameters}', '[String, int]/[E, T]'); | 13202 expect('${ft.typeArguments}/${ft.typeParameters}', '[String, int]/[E, T]'); |
13203 } | 13203 } |
13204 | 13204 |
13205 void test_genericMethod_explicitTypeParams() { | 13205 void test_genericMethod_explicitTypeParams() { |
13206 _resolveTestUnit(r''' | 13206 _resolveTestUnit(r''' |
13207 class C<E> { | 13207 class C<E> { |
13208 List/*<T>*/ f/*<T>*/(E e) => null; | 13208 List/*<T>*/ f/*<T>*/(E e) => null; |
13209 } | 13209 } |
13210 main() { | 13210 main() { |
13211 C<String> cOfString; | 13211 C<String> cOfString; |
13212 var x = cOfString.f/*<int>*/('hi'); | 13212 var x = cOfString.f/*<int>*/('hi'); |
13213 } | 13213 } |
13214 '''); | 13214 '''); |
13215 SimpleIdentifier f = _findIdentifier('f/*<int>*/'); | 13215 MethodInvocation f = _findIdentifier('f/*<int>*/').parent; |
13216 FunctionType ft = f.staticType; | 13216 FunctionType ft = f.staticInvokeType; |
13217 expect(ft.toString(), '(String) → List<int>'); | 13217 expect(ft.toString(), '(String) → List<int>'); |
13218 expect('${ft.typeArguments}/${ft.typeParameters}', '[String, int]/[E, T]'); | 13218 expect('${ft.typeArguments}/${ft.typeParameters}', '[String, int]/[E, T]'); |
13219 | 13219 |
13220 SimpleIdentifier x = _findIdentifier('x'); | 13220 SimpleIdentifier x = _findIdentifier('x'); |
13221 expect(x.staticType, | 13221 expect(x.staticType, |
13222 typeProvider.listType.substitute4([typeProvider.intType])); | 13222 typeProvider.listType.substitute4([typeProvider.intType])); |
13223 } | 13223 } |
13224 | 13224 |
13225 void test_genericMethod_functionTypedParameter() { | 13225 void test_genericMethod_functionTypedParameter() { |
13226 _resolveTestUnit(r''' | 13226 _resolveTestUnit(r''' |
13227 class C<E> { | 13227 class C<E> { |
13228 List/*<T>*/ f/*<T>*/(/*=T*/ f(E e)) => null; | 13228 List/*<T>*/ f/*<T>*/(/*=T*/ f(E e)) => null; |
13229 } | 13229 } |
13230 main() { | 13230 main() { |
13231 C<String> cOfString; | 13231 C<String> cOfString; |
13232 } | 13232 } |
13233 '''); | 13233 '''); |
13234 SimpleIdentifier f = _findIdentifier('f'); | 13234 SimpleIdentifier f = _findIdentifier('f'); |
13235 MethodElementImpl e = f.staticElement; | 13235 MethodElementImpl e = f.staticElement; |
13236 expect(e.typeParameters.toString(), '[T]'); | 13236 expect(e.typeParameters.toString(), '[T]'); |
13237 expect(e.type.boundTypeParameters.toString(), '[T]'); | 13237 expect(e.type.typeFormals.toString(), '[T]'); |
13238 expect(e.type.typeParameters.toString(), '[E]'); | 13238 expect(e.type.typeParameters.toString(), '[E]'); |
13239 expect(e.type.typeArguments.toString(), '[E]'); | 13239 expect(e.type.typeArguments.toString(), '[E]'); |
13240 expect(e.type.toString(), '<T>((E) → T) → List<T>'); | 13240 expect(e.type.toString(), '<T>((E) → T) → List<T>'); |
13241 | 13241 |
13242 SimpleIdentifier c = _findIdentifier('cOfString'); | 13242 SimpleIdentifier c = _findIdentifier('cOfString'); |
13243 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type; | 13243 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type; |
13244 expect(ft.toString(), '<T>((String) → T) → List<T>'); | 13244 expect(ft.toString(), '<T>((String) → T) → List<T>'); |
13245 ft = ft.instantiate([typeProvider.intType]); | 13245 ft = ft.instantiate([typeProvider.intType]); |
13246 expect(ft.toString(), '((String) → int) → List<int>'); | 13246 expect(ft.toString(), '((String) → int) → List<int>'); |
13247 } | 13247 } |
13248 | 13248 |
13249 void test_genericMethod_implicitDynamic() { | 13249 void test_genericMethod_implicitDynamic() { |
13250 // Regression test for: | 13250 // Regression test for: |
13251 // https://github.com/dart-lang/sdk/issues/25100#issuecomment-162047588 | 13251 // https://github.com/dart-lang/sdk/issues/25100#issuecomment-162047588 |
13252 // These should not cause any hints or warnings. | 13252 // These should not cause any hints or warnings. |
13253 _resolveTestUnit(r''' | 13253 _resolveTestUnit(r''' |
13254 class List<E> { | 13254 class List<E> { |
13255 /*=T*/ map/*<T>*/(/*=T*/ f(E e)) => null; | 13255 /*=T*/ map/*<T>*/(/*=T*/ f(E e)) => null; |
13256 } | 13256 } |
13257 void foo() { | 13257 void foo() { |
13258 List list = null; | 13258 List list = null; |
13259 list.map((e) => e); | 13259 list.map((e) => e); |
13260 list.map((e) => 3); | 13260 list.map((e) => 3); |
13261 }'''); | 13261 }'''); |
13262 | 13262 |
13263 SimpleIdentifier map1 = _findIdentifier('map((e) => e);'); | 13263 SimpleIdentifier map1 = _findIdentifier('map((e) => e);'); |
13264 expect(map1.staticType.toString(), '((dynamic) → dynamic) → dynamic'); | 13264 MethodInvocation m1 = map1.parent; |
| 13265 expect(m1.staticInvokeType.toString(), '((dynamic) → dynamic) → dynamic'); |
| 13266 expect(map1.staticType, isNull); |
13265 expect(map1.propagatedType, isNull); | 13267 expect(map1.propagatedType, isNull); |
13266 SimpleIdentifier map2 = _findIdentifier('map((e) => 3);'); | 13268 SimpleIdentifier map2 = _findIdentifier('map((e) => 3);'); |
13267 expect(map2.staticType.toString(), '((dynamic) → int) → int'); | 13269 MethodInvocation m2 = map2.parent; |
| 13270 expect(m2.staticInvokeType.toString(), '((dynamic) → int) → int'); |
| 13271 expect(map2.staticType, isNull); |
13268 expect(map2.propagatedType, isNull); | 13272 expect(map2.propagatedType, isNull); |
13269 } | 13273 } |
13270 | 13274 |
13271 void test_genericMethod_nestedCapture() { | 13275 void test_genericMethod_nestedCapture() { |
13272 _resolveTestUnit(r''' | 13276 _resolveTestUnit(r''' |
13273 class C<T> { | 13277 class C<T> { |
13274 /*=T*/ f/*<S>*/(/*=S*/ x) { | 13278 /*=T*/ f/*<S>*/(/*=S*/ x) { |
13275 new C<S>().f/*<int>*/(3); | 13279 new C<S>().f/*<int>*/(3); |
13276 new C<S>().f; // tear-off | 13280 new C<S>().f; // tear-off |
13277 return null; | 13281 return null; |
13278 } | 13282 } |
13279 } | 13283 } |
13280 '''); | 13284 '''); |
13281 SimpleIdentifier f = _findIdentifier('f/*<int>*/(3);'); | 13285 MethodInvocation f = _findIdentifier('f/*<int>*/(3);').parent; |
13282 expect(f.staticType.toString(), '(int) → S'); | 13286 expect(f.staticInvokeType.toString(), '(int) → S'); |
13283 FunctionType ft = f.staticType; | 13287 FunctionType ft = f.staticInvokeType; |
13284 expect('${ft.typeArguments}/${ft.typeParameters}', '[S, int]/[T, S]'); | 13288 expect('${ft.typeArguments}/${ft.typeParameters}', '[S, int]/[T, S]'); |
13285 | 13289 |
13286 f = _findIdentifier('f;'); | 13290 SimpleIdentifier f2 = _findIdentifier('f;'); |
13287 expect(f.staticType.toString(), '<S₀>(S₀) → S'); | 13291 expect(f2.staticType.toString(), '<S₀>(S₀) → S'); |
13288 } | 13292 } |
13289 | 13293 |
13290 void test_genericMethod_nestedFunctions() { | 13294 void test_genericMethod_nestedFunctions() { |
13291 _resolveTestUnit(r''' | 13295 _resolveTestUnit(r''' |
13292 /*=S*/ f/*<S>*/(/*=S*/ x) { | 13296 /*=S*/ f/*<S>*/(/*=S*/ x) { |
13293 g/*<S>*/(/*=S*/ x) => f; | 13297 g/*<S>*/(/*=S*/ x) => f; |
13294 return null; | 13298 return null; |
13295 } | 13299 } |
13296 '''); | 13300 '''); |
13297 SimpleIdentifier g = _findIdentifier('f'); | 13301 SimpleIdentifier g = _findIdentifier('f'); |
13298 expect(g.staticType.toString(), '<S>(S) → S'); | 13302 expect(g.staticType.toString(), '<S>(S) → S'); |
13299 SimpleIdentifier f = _findIdentifier('g'); | 13303 SimpleIdentifier f = _findIdentifier('g'); |
13300 expect(f.staticType.toString(), '<S>(S) → dynamic'); | 13304 expect(f.staticType.toString(), '<S>(S) → dynamic'); |
13301 } | 13305 } |
13302 | 13306 |
13303 void test_genericMethod_override() { | 13307 void test_genericMethod_override() { |
13304 _resolveTestUnit(r''' | 13308 _resolveTestUnit(r''' |
13305 class C { | 13309 class C { |
13306 /*=T*/ f/*<T>*/(/*=T*/ x) => null; | 13310 /*=T*/ f/*<T>*/(/*=T*/ x) => null; |
13307 } | 13311 } |
13308 class D extends C { | 13312 class D extends C { |
13309 /*=T*/ f/*<T>*/(/*=T*/ x) => null; // from D | 13313 /*=T*/ f/*<T>*/(/*=T*/ x) => null; // from D |
13310 } | 13314 } |
13311 '''); | 13315 '''); |
13312 SimpleIdentifier f = | 13316 SimpleIdentifier f = |
13313 _findIdentifier('f/*<T>*/(/*=T*/ x) => null; // from D'); | 13317 _findIdentifier('f/*<T>*/(/*=T*/ x) => null; // from D'); |
13314 MethodElementImpl e = f.staticElement; | 13318 MethodElementImpl e = f.staticElement; |
13315 expect(e.typeParameters.toString(), '[T]'); | 13319 expect(e.typeParameters.toString(), '[T]'); |
13316 expect(e.type.boundTypeParameters.toString(), '[T]'); | 13320 expect(e.type.typeFormals.toString(), '[T]'); |
13317 expect(e.type.toString(), '<T>(T) → T'); | 13321 expect(e.type.toString(), '<T>(T) → T'); |
13318 | 13322 |
13319 FunctionType ft = e.type.instantiate([typeProvider.stringType]); | 13323 FunctionType ft = e.type.instantiate([typeProvider.stringType]); |
13320 expect(ft.toString(), '(String) → String'); | 13324 expect(ft.toString(), '(String) → String'); |
13321 } | 13325 } |
13322 | 13326 |
13323 void test_genericMethod_override_bounds() { | 13327 void test_genericMethod_override_bounds() { |
13324 _resolveTestUnit(r''' | 13328 _resolveTestUnit(r''' |
13325 class A {} | 13329 class A {} |
13326 class B extends A {} | 13330 class B extends A {} |
(...skipping 3052 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
16379 | 16383 |
16380 void _resolveTestUnit(String code) { | 16384 void _resolveTestUnit(String code) { |
16381 testCode = code; | 16385 testCode = code; |
16382 testSource = addSource(testCode); | 16386 testSource = addSource(testCode); |
16383 LibraryElement library = resolve2(testSource); | 16387 LibraryElement library = resolve2(testSource); |
16384 assertNoErrors(testSource); | 16388 assertNoErrors(testSource); |
16385 verify([testSource]); | 16389 verify([testSource]); |
16386 testUnit = resolveCompilationUnit(testSource, library); | 16390 testUnit = resolveCompilationUnit(testSource, library); |
16387 } | 16391 } |
16388 } | 16392 } |
OLD | NEW |