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

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

Issue 1434983006: tweak FunctionTypeImpl to support generic methods (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: format & sort Created 5 years, 1 month 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 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 // Future 194 // Future
195 ClassElementImpl futureElement = 195 ClassElementImpl futureElement =
196 ElementFactory.classElement2("Future", ["T"]); 196 ElementFactory.classElement2("Future", ["T"]);
197 // factory Future.value([value]) 197 // factory Future.value([value])
198 ConstructorElementImpl futureConstructor = 198 ConstructorElementImpl futureConstructor =
199 ElementFactory.constructorElement2(futureElement, "value"); 199 ElementFactory.constructorElement2(futureElement, "value");
200 futureConstructor.parameters = <ParameterElement>[ 200 futureConstructor.parameters = <ParameterElement>[
201 ElementFactory.positionalParameter2("value", provider.dynamicType) 201 ElementFactory.positionalParameter2("value", provider.dynamicType)
202 ]; 202 ];
203 futureConstructor.factory = true; 203 futureConstructor.factory = true;
204 (futureConstructor.type as FunctionTypeImpl).typeArguments =
205 futureElement.type.typeArguments;
206 futureElement.constructors = <ConstructorElement>[futureConstructor]; 204 futureElement.constructors = <ConstructorElement>[futureConstructor];
207 // Future then(onValue(T value), { Function onError }); 205 // Future then(onValue(T value), { Function onError });
208 List<ParameterElement> parameters = <ParameterElement>[ 206 List<ParameterElement> parameters = <ParameterElement>[
209 ElementFactory.requiredParameter2( 207 ElementFactory.requiredParameter2(
210 "value", futureElement.typeParameters[0].type) 208 "value", futureElement.typeParameters[0].type)
211 ]; 209 ];
212 FunctionTypeAliasElementImpl aliasElement = 210 FunctionTypeAliasElementImpl aliasElement =
213 new FunctionTypeAliasElementImpl.forNode(null); 211 new FunctionTypeAliasElementImpl.forNode(null);
214 aliasElement.synthetic = true; 212 aliasElement.synthetic = true;
215 aliasElement.parameters = parameters; 213 aliasElement.parameters = parameters;
216 aliasElement.returnType = provider.dynamicType; 214 aliasElement.returnType = provider.dynamicType;
217 aliasElement.enclosingElement = asyncUnit; 215 aliasElement.enclosingElement = asyncUnit;
216 aliasElement.shareTypeParameters(futureElement.typeParameters);
218 FunctionTypeImpl aliasType = new FunctionTypeImpl.forTypedef(aliasElement); 217 FunctionTypeImpl aliasType = new FunctionTypeImpl.forTypedef(aliasElement);
219 aliasElement.shareTypeParameters(futureElement.typeParameters); 218 aliasElement.type = aliasType;
220 aliasType.typeArguments = futureElement.type.typeArguments;
221 DartType futureDynamicType = 219 DartType futureDynamicType =
222 futureElement.type.substitute4([provider.dynamicType]); 220 futureElement.type.substitute4([provider.dynamicType]);
223 MethodElement thenMethod = ElementFactory.methodElementWithParameters( 221 MethodElement thenMethod = ElementFactory.methodElementWithParameters(
224 "then", futureElement.type.typeArguments, futureDynamicType, [ 222 futureElement, "then", futureDynamicType, [
225 ElementFactory.requiredParameter2("onValue", aliasType), 223 ElementFactory.requiredParameter2("onValue", aliasType),
226 ElementFactory.namedParameter2("onError", provider.functionType) 224 ElementFactory.namedParameter2("onError", provider.functionType)
227 ]); 225 ]);
228 futureElement.methods = <MethodElement>[thenMethod]; 226 futureElement.methods = <MethodElement>[thenMethod];
229 // Completer 227 // Completer
230 ClassElementImpl completerElement = 228 ClassElementImpl completerElement =
231 ElementFactory.classElement2("Completer", ["T"]); 229 ElementFactory.classElement2("Completer", ["T"]);
232 ConstructorElementImpl completerConstructor = 230 ConstructorElementImpl completerConstructor =
233 ElementFactory.constructorElement2(completerElement, null); 231 ElementFactory.constructorElement2(completerElement, null);
234 (completerConstructor.type as FunctionTypeImpl).typeArguments =
235 completerElement.type.typeArguments;
236 completerElement.constructors = <ConstructorElement>[completerConstructor]; 232 completerElement.constructors = <ConstructorElement>[completerConstructor];
237 // StreamSubscription 233 // StreamSubscription
238 ClassElementImpl streamSubscriptionElement = 234 ClassElementImpl streamSubscriptionElement =
239 ElementFactory.classElement2("StreamSubscription", ["T"]); 235 ElementFactory.classElement2("StreamSubscription", ["T"]);
240 // Stream 236 // Stream
241 ClassElementImpl streamElement = 237 ClassElementImpl streamElement =
242 ElementFactory.classElement2("Stream", ["T"]); 238 ElementFactory.classElement2("Stream", ["T"]);
243 streamElement.constructors = <ConstructorElement>[ 239 streamElement.constructors = <ConstructorElement>[
244 ElementFactory.constructorElement2(streamElement, null) 240 ElementFactory.constructorElement2(streamElement, null)
245 ]; 241 ];
246 DartType returnType = streamSubscriptionElement.type 242 DartType returnType = streamSubscriptionElement.type
247 .substitute4(streamElement.type.typeArguments); 243 .substitute4(streamElement.type.typeArguments);
248 List<DartType> parameterTypes = <DartType>[ 244 List<DartType> parameterTypes = <DartType>[
249 ElementFactory 245 ElementFactory
250 .functionElement3('onData', VoidTypeImpl.instance.element, 246 .functionElement3('onData', VoidTypeImpl.instance.element,
251 <TypeDefiningElement>[streamElement.typeParameters[0]], null) 247 <TypeDefiningElement>[streamElement.typeParameters[0]], null)
252 .type, 248 .type,
253 ]; 249 ];
254 // TODO(brianwilkerson) This is missing the optional parameters. 250 // TODO(brianwilkerson) This is missing the optional parameters.
255 MethodElementImpl listenMethod = 251 MethodElementImpl listenMethod =
256 ElementFactory.methodElement('listen', returnType, parameterTypes); 252 ElementFactory.methodElement('listen', returnType, parameterTypes);
257 (listenMethod.type as FunctionTypeImpl).typeArguments =
258 streamElement.type.typeArguments;
259 (parameterTypes[0] as FunctionTypeImpl).typeArguments =
260 streamElement.type.typeArguments;
261 (parameterTypes[0].element as FunctionElementImpl).enclosingElement =
262 listenMethod;
263 streamElement.methods = <MethodElement>[listenMethod]; 253 streamElement.methods = <MethodElement>[listenMethod];
254 listenMethod.type = new FunctionTypeImpl(listenMethod);
255
256 FunctionElementImpl listenParamFunction = parameterTypes[0].element;
257 listenParamFunction.enclosingElement = listenMethod;
258 listenParamFunction.type = new FunctionTypeImpl(listenParamFunction);
259 ParameterElementImpl listenParam = listenMethod.parameters[0];
260 listenParam.type = listenParamFunction.type;
264 261
265 asyncUnit.types = <ClassElement>[ 262 asyncUnit.types = <ClassElement>[
266 completerElement, 263 completerElement,
267 futureElement, 264 futureElement,
268 streamElement 265 streamElement
269 ]; 266 ];
270 LibraryElementImpl asyncLibrary = new LibraryElementImpl.forNode( 267 LibraryElementImpl asyncLibrary = new LibraryElementImpl.forNode(
271 coreContext, AstFactory.libraryIdentifier2(["dart", "async"])); 268 coreContext, AstFactory.libraryIdentifier2(["dart", "async"]));
272 asyncLibrary.definingCompilationUnit = asyncUnit; 269 asyncLibrary.definingCompilationUnit = asyncUnit;
273 // 270 //
(...skipping 11894 matching lines...) Expand 10 before | Expand all | Expand 10 after
12168 '''; 12165 ''';
12169 _resolveTestUnit(code); 12166 _resolveTestUnit(code);
12170 12167
12171 SimpleIdentifier identifier = _findIdentifier('foo'); 12168 SimpleIdentifier identifier = _findIdentifier('foo');
12172 VariableDeclaration declaration = 12169 VariableDeclaration declaration =
12173 identifier.getAncestor((node) => node is VariableDeclaration); 12170 identifier.getAncestor((node) => node is VariableDeclaration);
12174 expect(declaration.initializer.staticType.name, 'String'); 12171 expect(declaration.initializer.staticType.name, 'String');
12175 expect(declaration.initializer.propagatedType, isNull); 12172 expect(declaration.initializer.propagatedType, isNull);
12176 } 12173 }
12177 12174
12175 void test_genericFunction() {
12176 // TODO(jmesserly): we're missing a case in the parser for the return type,
12177 // so "dynamic" must be used to workaround this.
12178 _resolveTestUnit(r'''
12179 dynamic/*=T*/ f/*<T>*/(/*=T*/ x) => null;
12180 ''');
12181 SimpleIdentifier f = _findIdentifier('f');
12182 FunctionElementImpl e = f.staticElement;
12183 expect(e.typeParameters.toString(), '[T]');
12184 expect(e.type.typeParameters.toString(), '[T]');
12185 expect(e.type.typeParameters[0].type, e.type.typeArguments[0]);
12186 expect(e.type.toString(), '(T) → T');
12187
12188 // Substitute for T
12189 DartType t = e.typeParameters[0].type;
12190 FunctionType ft = e.type.substitute2([typeProvider.stringType], [t]);
12191 expect(ft.toString(), '(String) → String');
12192 }
12193
12194 void test_genericMethod() {
12195 _resolveTestUnit(r'''
12196 class C<E> {
12197 List/*<T>*/ f/*<T>*/(E e) => null;
12198 }
12199 main() {
12200 C<String> cOfString;
12201 }
12202 ''');
12203 SimpleIdentifier f = _findIdentifier('f');
12204 MethodElementImpl e = f.staticElement;
12205 expect(e.typeParameters.toString(), '[T]');
12206 expect(e.type.typeParameters.toString(), '[T, E]');
12207 expect(e.type.typeArguments.toString(), '[T, E]');
12208 expect(e.type.toString(), '(E) → List<T>');
12209
12210 SimpleIdentifier c = _findIdentifier('cOfString');
12211 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type;
12212 expect(ft.toString(), '(String) → List<T>');
12213 DartType t = e.typeParameters[0].type;
12214 ft = ft.substitute2([typeProvider.intType], [t]);
12215 expect(ft.toString(), '(String) → List<int>');
12216 }
12217
12178 void test_pseudoGeneric_max_doubleDouble() { 12218 void test_pseudoGeneric_max_doubleDouble() {
12179 String code = r''' 12219 String code = r'''
12180 import 'dart:math'; 12220 import 'dart:math';
12181 main() { 12221 main() {
12182 var foo = max(1.0, 2.0); 12222 var foo = max(1.0, 2.0);
12183 } 12223 }
12184 '''; 12224 ''';
12185 _resolveTestUnit(code); 12225 _resolveTestUnit(code);
12186 12226
12187 SimpleIdentifier identifier = _findIdentifier('foo'); 12227 SimpleIdentifier identifier = _findIdentifier('foo');
(...skipping 2935 matching lines...) Expand 10 before | Expand all | Expand 10 after
15123 15163
15124 void _resolveTestUnit(String code) { 15164 void _resolveTestUnit(String code) {
15125 testCode = code; 15165 testCode = code;
15126 testSource = addSource(testCode); 15166 testSource = addSource(testCode);
15127 LibraryElement library = resolve2(testSource); 15167 LibraryElement library = resolve2(testSource);
15128 assertNoErrors(testSource); 15168 assertNoErrors(testSource);
15129 verify([testSource]); 15169 verify([testSource]);
15130 testUnit = resolveCompilationUnit(testSource, library); 15170 testUnit = resolveCompilationUnit(testSource, library);
15131 } 15171 }
15132 } 15172 }
OLDNEW
« pkg/analyzer/lib/src/task/strong_mode.dart ('K') | « pkg/analyzer/test/generated/element_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698