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 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 13096 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
13107 Expression exp = invocation.argumentList.arguments[0]; | 13107 Expression exp = invocation.argumentList.arguments[0]; |
13108 _isListOf(_isString)(exp.staticType); | 13108 _isListOf(_isString)(exp.staticType); |
13109 } | 13109 } |
13110 } | 13110 } |
13111 | 13111 |
13112 /** | 13112 /** |
13113 * Strong mode static analyzer end to end tests | 13113 * Strong mode static analyzer end to end tests |
13114 */ | 13114 */ |
13115 @reflectiveTest | 13115 @reflectiveTest |
13116 class StrongModeStaticTypeAnalyzer2Test extends _StaticTypeAnalyzer2TestShared { | 13116 class StrongModeStaticTypeAnalyzer2Test extends _StaticTypeAnalyzer2TestShared { |
13117 void fail_genericMethod_nested() { | |
13118 // TODO(jmesserly): this test currently fails because we incorrectly capture | |
13119 // S in FunctionTypeImpl.substitute. We should probably rename free | |
13120 // variables during substitution to avoid it. | |
13121 _resolveTestUnit(r''' | |
13122 class C<T> { | |
13123 /*=T*/ f/*<S>*/(/*=S*/ x) { | |
13124 new C<S>().f/*<int>*/(3); | |
13125 return null; | |
13126 } | |
13127 } | |
13128 '''); | |
13129 SimpleIdentifier f = _findIdentifier('f/*<int>*/(3);'); | |
13130 expect(f.staticType.toString(), '(int) → S'); | |
13131 } | |
13132 | |
13133 void setUp() { | 13117 void setUp() { |
13134 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | 13118 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
13135 options.strongMode = true; | 13119 options.strongMode = true; |
13136 resetWithOptions(options); | 13120 resetWithOptions(options); |
13137 } | 13121 } |
13138 | 13122 |
13139 void test_dynamicObjectGetter_hashCode() { | 13123 void test_dynamicObjectGetter_hashCode() { |
13140 String code = r''' | 13124 String code = r''' |
13141 main() { | 13125 main() { |
13142 dynamic a = null; | 13126 dynamic a = null; |
(...skipping 26 matching lines...) Expand all Loading... |
13169 } | 13153 } |
13170 | 13154 |
13171 void test_genericFunction() { | 13155 void test_genericFunction() { |
13172 if (!AnalysisEngine.instance.useTaskModel) { | 13156 if (!AnalysisEngine.instance.useTaskModel) { |
13173 return; | 13157 return; |
13174 } | 13158 } |
13175 _resolveTestUnit(r'/*=T*/ f/*<T>*/(/*=T*/ x) => null;'); | 13159 _resolveTestUnit(r'/*=T*/ f/*<T>*/(/*=T*/ x) => null;'); |
13176 SimpleIdentifier f = _findIdentifier('f'); | 13160 SimpleIdentifier f = _findIdentifier('f'); |
13177 FunctionElementImpl e = f.staticElement; | 13161 FunctionElementImpl e = f.staticElement; |
13178 expect(e.typeParameters.toString(), '[T]'); | 13162 expect(e.typeParameters.toString(), '[T]'); |
13179 expect(e.type.typeParameters.toString(), '[T]'); | 13163 expect(e.type.boundTypeParameters.toString(), '[T]'); |
13180 expect(e.type.typeParameters[0].type, e.type.typeArguments[0]); | 13164 expect(e.type.typeParameters.toString(), '[]'); |
13181 expect(e.type.toString(), '(T) → T'); | 13165 expect(e.type.toString(), '<T>(T) → T'); |
13182 | 13166 |
13183 // Substitute for T | 13167 FunctionType ft = e.type.instantiate([typeProvider.stringType]); |
13184 DartType t = e.typeParameters[0].type; | |
13185 FunctionType ft = e.type.substitute2([typeProvider.stringType], [t]); | |
13186 expect(ft.toString(), '(String) → String'); | 13168 expect(ft.toString(), '(String) → String'); |
13187 } | 13169 } |
13188 | 13170 |
13189 void test_genericMethod() { | 13171 void test_genericMethod() { |
13190 if (!AnalysisEngine.instance.useTaskModel) { | 13172 if (!AnalysisEngine.instance.useTaskModel) { |
13191 return; | 13173 return; |
13192 } | 13174 } |
13193 _resolveTestUnit(r''' | 13175 _resolveTestUnit(r''' |
13194 class C<E> { | 13176 class C<E> { |
13195 List/*<T>*/ f/*<T>*/(E e) => null; | 13177 List/*<T>*/ f/*<T>*/(E e) => null; |
13196 } | 13178 } |
13197 main() { | 13179 main() { |
13198 C<String> cOfString; | 13180 C<String> cOfString; |
13199 } | 13181 } |
13200 '''); | 13182 '''); |
13201 SimpleIdentifier f = _findIdentifier('f'); | 13183 SimpleIdentifier f = _findIdentifier('f'); |
13202 MethodElementImpl e = f.staticElement; | 13184 MethodElementImpl e = f.staticElement; |
13203 expect(e.typeParameters.toString(), '[T]'); | 13185 expect(e.typeParameters.toString(), '[T]'); |
13204 expect(e.type.typeParameters.toString(), '[T, E]'); | 13186 expect(e.type.boundTypeParameters.toString(), '[T]'); |
13205 expect(e.type.typeArguments.toString(), '[T, E]'); | 13187 expect(e.type.typeParameters.toString(), '[E]'); |
13206 expect(e.type.toString(), '(E) → List<T>'); | 13188 expect(e.type.typeArguments.toString(), '[E]'); |
| 13189 expect(e.type.toString(), '<T>(E) → List<T>'); |
13207 | 13190 |
13208 SimpleIdentifier c = _findIdentifier('cOfString'); | 13191 SimpleIdentifier c = _findIdentifier('cOfString'); |
13209 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type; | 13192 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type; |
13210 expect(ft.toString(), '(String) → List<T>'); | 13193 expect(ft.toString(), '<T>(String) → List<T>'); |
13211 DartType t = e.typeParameters[0].type; | 13194 ft = ft.instantiate([typeProvider.intType]); |
13212 ft = ft.substitute2([typeProvider.intType], [t]); | |
13213 expect(ft.toString(), '(String) → List<int>'); | 13195 expect(ft.toString(), '(String) → List<int>'); |
| 13196 expect('${ft.typeArguments}/${ft.typeParameters}', '[String, int]/[E, T]'); |
13214 } | 13197 } |
13215 | 13198 |
13216 void test_genericMethod_functionTypedParameter() { | 13199 void test_genericMethod_functionTypedParameter() { |
13217 if (!AnalysisEngine.instance.useTaskModel) { | 13200 if (!AnalysisEngine.instance.useTaskModel) { |
13218 return; | 13201 return; |
13219 } | 13202 } |
13220 _resolveTestUnit(r''' | 13203 _resolveTestUnit(r''' |
13221 class C<E> { | 13204 class C<E> { |
13222 List/*<T>*/ f/*<T>*/(/*=T*/ f(E e)) => null; | 13205 List/*<T>*/ f/*<T>*/(/*=T*/ f(E e)) => null; |
13223 } | 13206 } |
13224 main() { | 13207 main() { |
13225 C<String> cOfString; | 13208 C<String> cOfString; |
13226 } | 13209 } |
13227 '''); | 13210 '''); |
13228 SimpleIdentifier f = _findIdentifier('f'); | 13211 SimpleIdentifier f = _findIdentifier('f'); |
13229 MethodElementImpl e = f.staticElement; | 13212 MethodElementImpl e = f.staticElement; |
13230 expect(e.typeParameters.toString(), '[T]'); | 13213 expect(e.typeParameters.toString(), '[T]'); |
13231 expect(e.type.typeParameters.toString(), '[T, E]'); | 13214 expect(e.type.boundTypeParameters.toString(), '[T]'); |
13232 expect(e.type.typeArguments.toString(), '[T, E]'); | 13215 expect(e.type.typeParameters.toString(), '[E]'); |
13233 expect(e.type.toString(), '((E) → T) → List<T>'); | 13216 expect(e.type.typeArguments.toString(), '[E]'); |
| 13217 expect(e.type.toString(), '<T>((E) → T) → List<T>'); |
13234 | 13218 |
13235 SimpleIdentifier c = _findIdentifier('cOfString'); | 13219 SimpleIdentifier c = _findIdentifier('cOfString'); |
13236 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type; | 13220 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type; |
13237 expect(ft.toString(), '((String) → T) → List<T>'); | 13221 expect(ft.toString(), '<T>((String) → T) → List<T>'); |
13238 DartType t = e.typeParameters[0].type; | 13222 ft = ft.instantiate([typeProvider.intType]); |
13239 ft = ft.substitute2([typeProvider.intType], [t]); | |
13240 expect(ft.toString(), '((String) → int) → List<int>'); | 13223 expect(ft.toString(), '((String) → int) → List<int>'); |
13241 } | 13224 } |
13242 | 13225 |
| 13226 void test_genericMethod_nestedCapture() { |
| 13227 _resolveTestUnit(r''' |
| 13228 class C<T> { |
| 13229 /*=T*/ f/*<S>*/(/*=S*/ x) { |
| 13230 new C<S>().f/*<int>*/(3); |
| 13231 new C<S>().f; // tear-off |
| 13232 return null; |
| 13233 } |
| 13234 } |
| 13235 '''); |
| 13236 SimpleIdentifier f = _findIdentifier('f/*<int>*/(3);'); |
| 13237 expect(f.staticType.toString(), '(int) → S'); |
| 13238 FunctionType ft = f.staticType; |
| 13239 expect('${ft.typeArguments}/${ft.typeParameters}', '[S, int]/[T, S]'); |
| 13240 |
| 13241 f = _findIdentifier('f;'); |
| 13242 expect(f.staticType.toString(), '<S₀>(S₀) → S'); |
| 13243 } |
| 13244 |
| 13245 void test_genericMethod_nestedFunctions() { |
| 13246 _resolveTestUnit(r''' |
| 13247 /*=S*/ f/*<S>*/(/*=S*/ x) { |
| 13248 g/*<S>*/(/*=S*/ x) => f; |
| 13249 return null; |
| 13250 } |
| 13251 '''); |
| 13252 SimpleIdentifier g = _findIdentifier('f'); |
| 13253 expect(g.staticType.toString(), '<S>(S) → S'); |
| 13254 SimpleIdentifier f = _findIdentifier('g'); |
| 13255 expect(f.staticType.toString(), '<S>(S) → dynamic'); |
| 13256 } |
| 13257 |
13243 void test_pseudoGeneric_max_doubleDouble() { | 13258 void test_pseudoGeneric_max_doubleDouble() { |
13244 String code = r''' | 13259 String code = r''' |
13245 import 'dart:math'; | 13260 import 'dart:math'; |
13246 main() { | 13261 main() { |
13247 var foo = max(1.0, 2.0); | 13262 var foo = max(1.0, 2.0); |
13248 } | 13263 } |
13249 '''; | 13264 '''; |
13250 _resolveTestUnit(code); | 13265 _resolveTestUnit(code); |
13251 | 13266 |
13252 SimpleIdentifier identifier = _findIdentifier('foo'); | 13267 SimpleIdentifier identifier = _findIdentifier('foo'); |
(...skipping 3003 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
16256 | 16271 |
16257 void _resolveTestUnit(String code) { | 16272 void _resolveTestUnit(String code) { |
16258 testCode = code; | 16273 testCode = code; |
16259 testSource = addSource(testCode); | 16274 testSource = addSource(testCode); |
16260 LibraryElement library = resolve2(testSource); | 16275 LibraryElement library = resolve2(testSource); |
16261 assertNoErrors(testSource); | 16276 assertNoErrors(testSource); |
16262 verify([testSource]); | 16277 verify([testSource]); |
16263 testUnit = resolveCompilationUnit(testSource, library); | 16278 testUnit = resolveCompilationUnit(testSource, library); |
16264 } | 16279 } |
16265 } | 16280 } |
OLD | NEW |