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

Side by Side Diff: pkg/analyzer/test/src/summary/linker_test.dart

Issue 2014893004: In AST-based type inference, use function elements rather than variable elements. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 7 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/summary/link.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) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 import 'package:analyzer/dart/element/type.dart'; 5 import 'package:analyzer/dart/element/type.dart';
6 import 'package:analyzer/src/dart/element/element.dart'; 6 import 'package:analyzer/src/dart/element/element.dart';
7 import 'package:analyzer/src/summary/format.dart'; 7 import 'package:analyzer/src/summary/format.dart';
8 import 'package:analyzer/src/summary/idl.dart'; 8 import 'package:analyzer/src/summary/idl.dart';
9 import 'package:analyzer/src/summary/link.dart'; 9 import 'package:analyzer/src/summary/link.dart';
10 import 'package:unittest/unittest.dart'; 10 import 'package:unittest/unittest.dart';
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 ''' 187 '''
188 var x = () {}; 188 var x = () {};
189 ''', 189 ''',
190 path: '/a.dart'); 190 path: '/a.dart');
191 addBundle(bundle); 191 addBundle(bundle);
192 createLinker(''' 192 createLinker('''
193 import 'a.dart'; 193 import 'a.dart';
194 var y = x; 194 var y = x;
195 '''); 195 ''');
196 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); 196 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri);
197 expect( 197 expect(_getVariable(library.getContainedName('y')).inferredType.toString(),
198 library
199 .getContainedName('y')
200 .asTypeInferenceNode
201 .variableElement
202 .inferredType
203 .toString(),
204 '() → dynamic'); 198 '() → dynamic');
205 } 199 }
206 200
207 void test_inferredType_instanceField_dynamic() { 201 void test_inferredType_instanceField_dynamic() {
208 createLinker(''' 202 createLinker('''
209 var x; 203 var x;
210 class C { 204 class C {
211 var f = x; // Inferred type: dynamic 205 var f = x; // Inferred type: dynamic
212 } 206 }
213 '''); 207 ''');
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 } 264 }
271 265
272 void test_inferredType_staticField_dynamic() { 266 void test_inferredType_staticField_dynamic() {
273 createLinker(''' 267 createLinker('''
274 dynamic x = null; 268 dynamic x = null;
275 class C { 269 class C {
276 static var y = x; 270 static var y = x;
277 } 271 }
278 '''); 272 ''');
279 expect( 273 expect(
280 linker 274 _getVariable(linker
281 .getLibrary(linkerInputs.testDartUri) 275 .getLibrary(linkerInputs.testDartUri)
282 .getContainedName('C') 276 .getContainedName('C')
283 .getContainedName('y') 277 .getContainedName('y'))
284 .asTypeInferenceNode
285 .variableElement
286 .inferredType 278 .inferredType
287 .toString(), 279 .toString(),
288 'dynamic'); 280 'dynamic');
289 } 281 }
290 282
291 void test_inferredType_topLevelVariable_dynamic() { 283 void test_inferredType_topLevelVariable_dynamic() {
292 createLinker(''' 284 createLinker('''
293 dynamic x = null; 285 dynamic x = null;
294 var y = x; 286 var y = x;
295 '''); 287 ''');
296 expect( 288 expect(
297 linker 289 _getVariable(linker
298 .getLibrary(linkerInputs.testDartUri) 290 .getLibrary(linkerInputs.testDartUri)
299 .getContainedName('y') 291 .getContainedName('y'))
300 .asTypeInferenceNode
301 .variableElement
302 .inferredType 292 .inferredType
303 .toString(), 293 .toString(),
304 'dynamic'); 294 'dynamic');
305 } 295 }
306 296
307 void test_inferredTypeFromOutsideBuildUnit_dynamic() { 297 void test_inferredTypeFromOutsideBuildUnit_dynamic() {
308 var bundle = createPackageBundle( 298 var bundle = createPackageBundle(
309 ''' 299 '''
310 var x; 300 var x;
311 var y = x; // Inferred type: dynamic 301 var y = x; // Inferred type: dynamic
312 ''', 302 ''',
313 path: '/a.dart'); 303 path: '/a.dart');
314 addBundle(bundle); 304 addBundle(bundle);
315 createLinker(''' 305 createLinker('''
316 import 'a.dart'; 306 import 'a.dart';
317 var z = y; // Inferred type: dynamic 307 var z = y; // Inferred type: dynamic
318 '''); 308 ''');
319 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); 309 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri);
320 expect( 310 expect(_getVariable(library.getContainedName('z')).inferredType.toString(),
321 library
322 .getContainedName('z')
323 .asTypeInferenceNode
324 .variableElement
325 .inferredType
326 .toString(),
327 'dynamic'); 311 'dynamic');
328 } 312 }
329 313
330 void test_inferredTypeFromOutsideBuildUnit_instanceField() { 314 void test_inferredTypeFromOutsideBuildUnit_instanceField() {
331 var bundle = createPackageBundle( 315 var bundle = createPackageBundle(
332 ''' 316 '''
333 class C { 317 class C {
334 var f = 0; // Inferred type: int 318 var f = 0; // Inferred type: int
335 } 319 }
336 ''', 320 ''',
337 path: '/a.dart'); 321 path: '/a.dart');
338 addBundle(bundle); 322 addBundle(bundle);
339 createLinker(''' 323 createLinker('''
340 import 'a.dart'; 324 import 'a.dart';
341 var x = new C().f; // Inferred type: int 325 var x = new C().f; // Inferred type: int
342 '''); 326 ''');
343 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); 327 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri);
344 expect( 328 expect(_getVariable(library.getContainedName('x')).inferredType.toString(),
345 library
346 .getContainedName('x')
347 .asTypeInferenceNode
348 .variableElement
349 .inferredType
350 .toString(),
351 'int'); 329 'int');
352 } 330 }
353 331
354 void test_inferredTypeFromOutsideBuildUnit_instanceField_toInstanceField() { 332 void test_inferredTypeFromOutsideBuildUnit_instanceField_toInstanceField() {
355 var bundle = createPackageBundle( 333 var bundle = createPackageBundle(
356 ''' 334 '''
357 class C { 335 class C {
358 var f = 0; // Inferred type: int 336 var f = 0; // Inferred type: int
359 } 337 }
360 ''', 338 ''',
(...skipping 20 matching lines...) Expand all
381 f<T>(t) => t; // Inferred param type: T 359 f<T>(t) => t; // Inferred param type: T
382 } 360 }
383 ''', 361 ''',
384 path: '/a.dart'); 362 path: '/a.dart');
385 addBundle(bundle); 363 addBundle(bundle);
386 createLinker(''' 364 createLinker('''
387 import 'a.dart'; 365 import 'a.dart';
388 var x = new C().f(0); // Inferred type: int 366 var x = new C().f(0); // Inferred type: int
389 '''); 367 ''');
390 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); 368 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri);
391 expect( 369 expect(_getVariable(library.getContainedName('x')).inferredType.toString(),
392 library
393 .getContainedName('x')
394 .asTypeInferenceNode
395 .variableElement
396 .inferredType
397 .toString(),
398 'int'); 370 'int');
399 } 371 }
400 372
401 void test_inferredTypeFromOutsideBuildUnit_methodParamType_viaInheritance() { 373 void test_inferredTypeFromOutsideBuildUnit_methodParamType_viaInheritance() {
402 var bundle = createPackageBundle( 374 var bundle = createPackageBundle(
403 ''' 375 '''
404 class B { 376 class B {
405 void f(int i) {} 377 void f(int i) {}
406 } 378 }
407 class C extends B { 379 class C extends B {
(...skipping 27 matching lines...) Expand all
435 f() => 1; // Inferred return type: int 407 f() => 1; // Inferred return type: int
436 } 408 }
437 ''', 409 ''',
438 path: '/a.dart'); 410 path: '/a.dart');
439 addBundle(bundle); 411 addBundle(bundle);
440 createLinker(''' 412 createLinker('''
441 import 'a.dart'; 413 import 'a.dart';
442 var x = new C().f(); // Inferred type: int 414 var x = new C().f(); // Inferred type: int
443 '''); 415 ''');
444 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); 416 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri);
445 expect( 417 expect(_getVariable(library.getContainedName('x')).inferredType.toString(),
446 library
447 .getContainedName('x')
448 .asTypeInferenceNode
449 .variableElement
450 .inferredType
451 .toString(),
452 'int'); 418 'int');
453 } 419 }
454 420
455 void test_inferredTypeFromOutsideBuildUnit_methodReturnType_viaInheritance() { 421 void test_inferredTypeFromOutsideBuildUnit_methodReturnType_viaInheritance() {
456 var bundle = createPackageBundle( 422 var bundle = createPackageBundle(
457 ''' 423 '''
458 class B { 424 class B {
459 int f() => 0; 425 int f() => 0;
460 } 426 }
461 class C extends B { 427 class C extends B {
(...skipping 14 matching lines...) Expand all
476 expect(cls.methods, hasLength(1)); 442 expect(cls.methods, hasLength(1));
477 expect(cls.methods[0].returnType.toString(), 'int'); 443 expect(cls.methods[0].returnType.toString(), 'int');
478 } 444 }
479 445
480 void test_inferredTypeFromOutsideBuildUnit_staticField() { 446 void test_inferredTypeFromOutsideBuildUnit_staticField() {
481 var bundle = 447 var bundle =
482 createPackageBundle('class C { static var f = 0; }', path: '/a.dart'); 448 createPackageBundle('class C { static var f = 0; }', path: '/a.dart');
483 addBundle(bundle); 449 addBundle(bundle);
484 createLinker('import "a.dart"; var x = C.f;', path: '/b.dart'); 450 createLinker('import "a.dart"; var x = C.f;', path: '/b.dart');
485 expect( 451 expect(
486 linker 452 _getVariable(linker
487 .getLibrary(linkerInputs.testDartUri) 453 .getLibrary(linkerInputs.testDartUri)
488 .getContainedName('x') 454 .getContainedName('x'))
489 .asTypeInferenceNode
490 .variableElement
491 .inferredType 455 .inferredType
492 .toString(), 456 .toString(),
493 'int'); 457 'int');
494 } 458 }
495 459
496 void test_inferredTypeFromOutsideBuildUnit_topLevelVariable() { 460 void test_inferredTypeFromOutsideBuildUnit_topLevelVariable() {
497 var bundle = createPackageBundle('var a = 0;', path: '/a.dart'); 461 var bundle = createPackageBundle('var a = 0;', path: '/a.dart');
498 addBundle(bundle); 462 addBundle(bundle);
499 createLinker('import "a.dart"; var b = a;', path: '/b.dart'); 463 createLinker('import "a.dart"; var b = a;', path: '/b.dart');
500 expect( 464 expect(
501 linker 465 _getVariable(linker
502 .getLibrary(linkerInputs.testDartUri) 466 .getLibrary(linkerInputs.testDartUri)
503 .getContainedName('b') 467 .getContainedName('b'))
504 .asTypeInferenceNode
505 .variableElement
506 .inferredType 468 .inferredType
507 .toString(), 469 .toString(),
508 'int'); 470 'int');
509 } 471 }
510 472
511 void test_instantiate_param_of_param_to_bounds() { 473 void test_instantiate_param_of_param_to_bounds() {
512 createLinker(''' 474 createLinker('''
513 class C<T> {} 475 class C<T> {}
514 class D<T extends num> {} 476 class D<T extends num> {}
515 final x = new C<D>(); 477 final x = new C<D>();
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
667 if (ref.kind == ReferenceKind.function) { 629 if (ref.kind == ReferenceKind.function) {
668 ref.localIndex = 1234; 630 ref.localIndex = 1234;
669 } 631 }
670 } 632 }
671 addBundle(bundle); 633 addBundle(bundle);
672 createLinker(''' 634 createLinker('''
673 import 'a.dart'; 635 import 'a.dart';
674 var y = x; 636 var y = x;
675 '''); 637 ''');
676 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); 638 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri);
677 expect( 639 expect(_getVariable(library.getContainedName('y')).inferredType.toString(),
678 library
679 .getContainedName('y')
680 .asTypeInferenceNode
681 .variableElement
682 .inferredType
683 .toString(),
684 'dynamic'); 640 'dynamic');
685 } 641 }
686 642
687 void test_multiplyInheritedExecutable_differentSignatures() { 643 void test_multiplyInheritedExecutable_differentSignatures() {
688 createLinker(''' 644 createLinker('''
689 class B { 645 class B {
690 void f() {} 646 void f() {}
691 } 647 }
692 abstract class I { 648 abstract class I {
693 f(); 649 f();
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); 772 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri);
817 PropertyAccessorElementForLink_Variable c = library.getContainedName('c'); 773 PropertyAccessorElementForLink_Variable c = library.getContainedName('c');
818 expect(c.variable.initializer, isNotNull); 774 expect(c.variable.initializer, isNotNull);
819 PropertyAccessorElementForLink_Variable i = library.getContainedName('i'); 775 PropertyAccessorElementForLink_Variable i = library.getContainedName('i');
820 expect(i.variable.initializer, isNotNull); 776 expect(i.variable.initializer, isNotNull);
821 PropertyAccessorElementForLink_Variable j = library.getContainedName('j'); 777 PropertyAccessorElementForLink_Variable j = library.getContainedName('j');
822 expect(j.variable.initializer, isNull); 778 expect(j.variable.initializer, isNull);
823 PropertyAccessorElementForLink_Variable v = library.getContainedName('v'); 779 PropertyAccessorElementForLink_Variable v = library.getContainedName('v');
824 expect(v.variable.initializer, isNotNull); 780 expect(v.variable.initializer, isNotNull);
825 } 781 }
782
783 VariableElementForLink _getVariable(ReferenceableElementForLink element) {
784 return (element as PropertyAccessorElementForLink_Variable).variable;
785 }
826 } 786 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/summary/link.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698