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

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

Issue 1647253002: Add the ability to summarize inferred types based on function-typed parameters. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 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
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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.src.summary.summary_common; 5 library analyzer.test.src.summary.summary_common;
6 6
7 import 'package:analyzer/analyzer.dart'; 7 import 'package:analyzer/analyzer.dart';
8 import 'package:analyzer/dart/ast/ast.dart'; 8 import 'package:analyzer/dart/ast/ast.dart';
9 import 'package:analyzer/dart/element/element.dart'; 9 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/src/generated/engine.dart'; 10 import 'package:analyzer/src/generated/engine.dart';
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 UnlinkedUnit unlinkedSourceUnit, 368 UnlinkedUnit unlinkedSourceUnit,
369 int numTypeParameters: 0}) { 369 int numTypeParameters: 0}) {
370 linkedSourceUnit ??= definingUnit; 370 linkedSourceUnit ??= definingUnit;
371 expect(typeRef, isNotNull, 371 expect(typeRef, isNotNull,
372 reason: 'No entry in linkedSourceUnit.types matching slotId'); 372 reason: 'No entry in linkedSourceUnit.types matching slotId');
373 expect(typeRef.paramReference, 0); 373 expect(typeRef.paramReference, 0);
374 int index = typeRef.reference; 374 int index = typeRef.reference;
375 if (!allowTypeParameters) { 375 if (!allowTypeParameters) {
376 expect(typeRef.typeArguments, isEmpty); 376 expect(typeRef.typeArguments, isEmpty);
377 } 377 }
378 checkTypeRefCommonElements( 378 checkReferenceIndex(index, absoluteUri, relativeUri, expectedName,
379 index, 379 expectedKind: expectedKind,
380 absoluteUri, 380 expectedTargetUnit: expectedTargetUnit,
381 relativeUri, 381 linkedSourceUnit: linkedSourceUnit,
382 expectedName, 382 unlinkedSourceUnit: unlinkedSourceUnit,
383 expectedKind, 383 numTypeParameters: numTypeParameters);
384 expectedTargetUnit,
385 linkedSourceUnit,
386 unlinkedSourceUnit,
387 numTypeParameters);
388 } 384 }
389 385
390 /** 386 /**
391 * Verify that the given [slotId] represents a reference to a type declared 387 * Verify that the given [slotId] represents a reference to a type declared
392 * in a file reachable via [absoluteUri] and [relativeUri], having name 388 * in a file reachable via [absoluteUri] and [relativeUri], having name
393 * [expectedName]. If [allowTypeParameters] is true, allow the type 389 * [expectedName]. If [allowTypeParameters] is true, allow the type
394 * reference to supply type parameters. [expectedKind] is the kind of object 390 * reference to supply type parameters. [expectedKind] is the kind of object
395 * referenced. [linkedSourceUnit] and [unlinkedSourceUnit] refer to the 391 * referenced. [linkedSourceUnit] and [unlinkedSourceUnit] refer to the
396 * compilation unit within which the [typeRef] appears; if not specified they 392 * compilation unit within which the [typeRef] appears; if not specified they
397 * are assumed to refer to the defining compilation unit. 393 * are assumed to refer to the defining compilation unit.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 void checkPrefix(int prefixReference, String name) { 441 void checkPrefix(int prefixReference, String name) {
446 expect(prefixReference, isNot(0)); 442 expect(prefixReference, isNot(0));
447 expect(unlinkedUnits[0].references[prefixReference].prefixReference, 0); 443 expect(unlinkedUnits[0].references[prefixReference].prefixReference, 0);
448 expect(unlinkedUnits[0].references[prefixReference].name, name); 444 expect(unlinkedUnits[0].references[prefixReference].name, name);
449 expect(definingUnit.references[prefixReference].dependency, 0); 445 expect(definingUnit.references[prefixReference].dependency, 0);
450 expect(definingUnit.references[prefixReference].kind, ReferenceKind.prefix); 446 expect(definingUnit.references[prefixReference].kind, ReferenceKind.prefix);
451 expect(definingUnit.references[prefixReference].unit, 0); 447 expect(definingUnit.references[prefixReference].unit, 0);
452 } 448 }
453 449
454 /** 450 /**
451 * Check the data structures that are reachable from an index in the
452 * references table.. If the reference in question is an explicit
453 * reference, return the [UnlinkedReference] that is used to make the
454 * explicit reference. If the type reference in question is an implicit
455 * reference, return `null`.
456 */
457 UnlinkedReference checkReferenceIndex(int referenceIndex, String absoluteUri,
458 String relativeUri, String expectedName,
459 {ReferenceKind expectedKind: ReferenceKind.classOrEnum,
460 int expectedTargetUnit: 0,
461 LinkedUnit linkedSourceUnit,
462 UnlinkedUnit unlinkedSourceUnit,
463 int numTypeParameters: 0}) {
464 linkedSourceUnit ??= definingUnit;
465 unlinkedSourceUnit ??= unlinkedUnits[0];
466 LinkedReference referenceResolution =
467 linkedSourceUnit.references[referenceIndex];
468 String name;
469 UnlinkedReference reference;
470 if (referenceIndex < unlinkedSourceUnit.references.length) {
471 // This is an explicit reference, so its name and prefix should be in
472 // [UnlinkedUnit.references].
473 expect(referenceResolution.name, isEmpty);
474 reference = unlinkedSourceUnit.references[referenceIndex];
475 name = reference.name;
476 if (reference.prefixReference != 0) {
477 // Prefixes should appear in the references table before any reference
478 // that uses them.
479 expect(reference.prefixReference, lessThan(referenceIndex));
480 }
481 } else {
482 // This is an implicit reference, so its name should be in
483 // [LinkedUnit.references].
484 name = referenceResolution.name;
485 }
486 // Index 0 is reserved.
487 expect(referenceIndex, isNot(0));
488 if (absoluteUri == null) {
489 expect(referenceResolution.dependency, 0);
490 } else {
491 checkDependency(referenceResolution.dependency, absoluteUri, relativeUri);
492 }
493 if (expectedKind == ReferenceKind.unresolved && !checkAstDerivedData) {
494 // summarize_elements.dart isn't yet able to record the name of
495 // unresolved references. TODO(paulberry): fix this.
496 expect(name, '*unresolved*');
497 } else {
498 if (expectedName == null) {
499 expect(name, isEmpty);
500 } else {
501 expect(name, expectedName);
502 }
503 }
504 expect(referenceResolution.kind, expectedKind);
505 expect(referenceResolution.unit, expectedTargetUnit);
506 expect(referenceResolution.numTypeParameters, numTypeParameters);
507 return reference;
508 }
509
510 /**
455 * Verify that the given [typeRef] represents a reference to a type declared 511 * Verify that the given [typeRef] represents a reference to a type declared
456 * in a file reachable via [absoluteUri] and [relativeUri], having name 512 * in a file reachable via [absoluteUri] and [relativeUri], having name
457 * [expectedName]. If [expectedPrefix] is supplied, verify that the type is 513 * [expectedName]. If [expectedPrefix] is supplied, verify that the type is
458 * reached via the given prefix. If [allowTypeParameters] is true, allow the 514 * reached via the given prefix. If [allowTypeParameters] is true, allow the
459 * type reference to supply type parameters. [expectedKind] is the kind of 515 * type reference to supply type parameters. [expectedKind] is the kind of
460 * object referenced. [linkedSourceUnit] and [unlinkedSourceUnit] refer 516 * object referenced. [linkedSourceUnit] and [unlinkedSourceUnit] refer
461 * to the compilation unit within which the [typeRef] appears; if not 517 * to the compilation unit within which the [typeRef] appears; if not
462 * specified they are assumed to refer to the defining compilation unit. 518 * specified they are assumed to refer to the defining compilation unit.
463 * [expectedTargetUnit] is the index of the compilation unit in which the 519 * [expectedTargetUnit] is the index of the compilation unit in which the
464 * target of the [typeRef] is expected to appear; if not specified it is 520 * target of the [typeRef] is expected to appear; if not specified it is
(...skipping 10 matching lines...) Expand all
475 LinkedUnit linkedSourceUnit, 531 LinkedUnit linkedSourceUnit,
476 UnlinkedUnit unlinkedSourceUnit, 532 UnlinkedUnit unlinkedSourceUnit,
477 int numTypeParameters: 0}) { 533 int numTypeParameters: 0}) {
478 linkedSourceUnit ??= definingUnit; 534 linkedSourceUnit ??= definingUnit;
479 expect(typeRef, new isInstanceOf<EntityRef>()); 535 expect(typeRef, new isInstanceOf<EntityRef>());
480 expect(typeRef.paramReference, 0); 536 expect(typeRef.paramReference, 0);
481 int index = typeRef.reference; 537 int index = typeRef.reference;
482 if (!allowTypeParameters) { 538 if (!allowTypeParameters) {
483 expect(typeRef.typeArguments, isEmpty); 539 expect(typeRef.typeArguments, isEmpty);
484 } 540 }
485 UnlinkedReference reference = checkTypeRefCommonElements( 541 UnlinkedReference reference = checkReferenceIndex(
486 index, 542 index, absoluteUri, relativeUri, expectedName,
487 absoluteUri, 543 expectedKind: expectedKind,
488 relativeUri, 544 expectedTargetUnit: expectedTargetUnit,
489 expectedName, 545 linkedSourceUnit: linkedSourceUnit,
490 expectedKind, 546 unlinkedSourceUnit: unlinkedSourceUnit,
491 expectedTargetUnit, 547 numTypeParameters: numTypeParameters);
492 linkedSourceUnit,
493 unlinkedSourceUnit,
494 numTypeParameters);
495 expect(reference, isNotNull, 548 expect(reference, isNotNull,
496 reason: 'Unlinked type refs must refer to an explicit reference'); 549 reason: 'Unlinked type refs must refer to an explicit reference');
497 if (expectedKind == ReferenceKind.unresolved && !checkAstDerivedData) { 550 if (expectedKind == ReferenceKind.unresolved && !checkAstDerivedData) {
498 // summarize_elements.dart isn't yet able to record the prefix of 551 // summarize_elements.dart isn't yet able to record the prefix of
499 // unresolved references. TODO(paulberry): fix this. 552 // unresolved references. TODO(paulberry): fix this.
500 expect(reference.prefixReference, 0); 553 expect(reference.prefixReference, 0);
501 } else if (expectedPrefix != null) { 554 } else if (expectedPrefix != null) {
502 checkPrefix(reference.prefixReference, expectedPrefix); 555 checkPrefix(reference.prefixReference, expectedPrefix);
503 } else if (prefixExpectations != null) { 556 } else if (prefixExpectations != null) {
504 for (_PrefixExpectation expectation in prefixExpectations) { 557 for (_PrefixExpectation expectation in prefixExpectations) {
505 expect(reference.prefixReference, isNot(0)); 558 expect(reference.prefixReference, isNot(0));
506 reference = checkTypeRefCommonElements( 559 reference = checkReferenceIndex(
507 reference.prefixReference, 560 reference.prefixReference,
508 expectation.inLibraryDefiningUnit 561 expectation.inLibraryDefiningUnit
509 ? null 562 ? null
510 : expectation.absoluteUri ?? absoluteUri, 563 : expectation.absoluteUri ?? absoluteUri,
511 expectation.inLibraryDefiningUnit 564 expectation.inLibraryDefiningUnit
512 ? null 565 ? null
513 : expectation.relativeUri ?? relativeUri, 566 : expectation.relativeUri ?? relativeUri,
514 expectation.name, 567 expectation.name,
515 expectation.kind, 568 expectedKind: expectation.kind,
516 expectedTargetUnit, 569 expectedTargetUnit: expectedTargetUnit,
517 linkedSourceUnit, 570 linkedSourceUnit: linkedSourceUnit,
518 unlinkedSourceUnit, 571 unlinkedSourceUnit: unlinkedSourceUnit,
519 expectation.numTypeParameters); 572 numTypeParameters: expectation.numTypeParameters);
520 } 573 }
521 expect(reference.prefixReference, 0); 574 expect(reference.prefixReference, 0);
522 } else { 575 } else {
523 expect(reference.prefixReference, 0); 576 expect(reference.prefixReference, 0);
524 } 577 }
525 } 578 }
526 579
527 /** 580 /**
528 * Check the data structures that are common between [checkTypeRef] and
529 * [checkLinkedTypeRef]. If the type reference in question is an explicit
530 * reference, return the [UnlinkedReference] that is used to make the
531 * explicit reference. If the type reference in question is an implicit
532 * reference, return `null`.
533 */
534 UnlinkedReference checkTypeRefCommonElements(
535 int referenceIndex,
536 String absoluteUri,
537 String relativeUri,
538 String expectedName,
539 ReferenceKind expectedKind,
540 int expectedTargetUnit,
541 LinkedUnit linkedSourceUnit,
542 UnlinkedUnit unlinkedSourceUnit,
543 int numTypeParameters) {
544 unlinkedSourceUnit ??= unlinkedUnits[0];
545 LinkedReference referenceResolution =
546 linkedSourceUnit.references[referenceIndex];
547 String name;
548 UnlinkedReference reference;
549 if (referenceIndex < unlinkedSourceUnit.references.length) {
550 // This is an explicit reference, so its name and prefix should be in
551 // [UnlinkedUnit.references].
552 expect(referenceResolution.name, isEmpty);
553 reference = unlinkedSourceUnit.references[referenceIndex];
554 name = reference.name;
555 if (reference.prefixReference != 0) {
556 // Prefixes should appear in the references table before any reference
557 // that uses them.
558 expect(reference.prefixReference, lessThan(referenceIndex));
559 }
560 } else {
561 // This is an implicit reference, so its name should be in
562 // [LinkedUnit.references].
563 name = referenceResolution.name;
564 }
565 // Index 0 is reserved.
566 expect(referenceIndex, isNot(0));
567 if (absoluteUri == null) {
568 expect(referenceResolution.dependency, 0);
569 } else {
570 checkDependency(referenceResolution.dependency, absoluteUri, relativeUri);
571 }
572 if (expectedKind == ReferenceKind.unresolved && !checkAstDerivedData) {
573 // summarize_elements.dart isn't yet able to record the name of
574 // unresolved references. TODO(paulberry): fix this.
575 expect(name, '*unresolved*');
576 } else {
577 if (expectedName == null) {
578 expect(name, isEmpty);
579 } else {
580 expect(name, expectedName);
581 }
582 }
583 expect(referenceResolution.kind, expectedKind);
584 expect(referenceResolution.unit, expectedTargetUnit);
585 expect(referenceResolution.numTypeParameters, numTypeParameters);
586 return reference;
587 }
588
589 /**
590 * Verify that the given [typeRef] represents a reference to an unresolved 581 * Verify that the given [typeRef] represents a reference to an unresolved
591 * type. 582 * type.
592 */ 583 */
593 void checkUnresolvedTypeRef( 584 void checkUnresolvedTypeRef(
594 EntityRef typeRef, String expectedPrefix, String expectedName, 585 EntityRef typeRef, String expectedPrefix, String expectedName,
595 {LinkedUnit linkedSourceUnit, UnlinkedUnit unlinkedSourceUnit}) { 586 {LinkedUnit linkedSourceUnit, UnlinkedUnit unlinkedSourceUnit}) {
596 // When serializing from the element model, unresolved type refs lose their 587 // When serializing from the element model, unresolved type refs lose their
597 // name. 588 // name.
598 checkTypeRef(typeRef, null, null, checkAstDerivedData ? expectedName : null, 589 checkTypeRef(typeRef, null, null, checkAstDerivedData ? expectedName : null,
599 expectedPrefix: expectedPrefix, 590 expectedPrefix: expectedPrefix,
(...skipping 3394 matching lines...) Expand 10 before | Expand all | Expand 10 after
3994 ' abstract class D<U, V> { Map<V, U> get v; }', 3985 ' abstract class D<U, V> { Map<V, U> get v; }',
3995 className: 'C'); 3986 className: 'C');
3996 EntityRef type = getTypeRefForSlot(cls.fields[0].inferredTypeSlot); 3987 EntityRef type = getTypeRefForSlot(cls.fields[0].inferredTypeSlot);
3997 // Check that v has inferred type Map<T, int>. 3988 // Check that v has inferred type Map<T, int>.
3998 checkLinkedTypeRef(type, 'dart:core', 'dart:core', 'Map', 3989 checkLinkedTypeRef(type, 'dart:core', 'dart:core', 'Map',
3999 allowTypeParameters: true, numTypeParameters: 2); 3990 allowTypeParameters: true, numTypeParameters: 2);
4000 checkParamTypeRef(type.typeArguments[0], 1); 3991 checkParamTypeRef(type.typeArguments[0], 1);
4001 checkLinkedTypeRef(type.typeArguments[1], 'dart:core', 'dart:core', 'int'); 3992 checkLinkedTypeRef(type.typeArguments[1], 'dart:core', 'dart:core', 'int');
4002 } 3993 }
4003 3994
3995 test_inferred_type_refers_to_method_function_typed_parameter_type() {
3996 if (!strongMode || skipFullyLinkedData) {
3997 return;
3998 }
3999 UnlinkedClass cls = serializeClassText(
4000 'class C extends D { void f(int x, g) {} }'
4001 ' abstract class D { void f(int x, int g(String s)); }',
4002 className: 'C');
4003 EntityRef type =
4004 getTypeRefForSlot(cls.executables[0].parameters[1].inferredTypeSlot);
4005 // Check that parameter g's inferred type is the type implied by D.f's 1st
4006 // (zero-based) parameter.
4007 expect(type.implicitFunctionTypeIndices, hasLength(1));
4008 expect(type.implicitFunctionTypeIndices[0], 1);
scheglov 2016/01/29 20:56:56 I think just equality would work fine and replace
Paul Berry 2016/01/29 21:13:18 Whoops, I accidentally just landed the CL without
4009 expect(type.paramReference, 0);
4010 expect(type.typeArguments, isEmpty);
4011 expect(type.reference,
4012 greaterThanOrEqualTo(unlinkedUnits[0].references.length));
4013 LinkedReference linkedReference =
4014 linked.units[0].references[type.reference];
4015 expect(linkedReference.dependency, 0);
4016 expect(linkedReference.kind, ReferenceKind.method);
4017 expect(linkedReference.name, 'f');
4018 expect(linkedReference.numTypeParameters, 0);
4019 expect(linkedReference.unit, 0);
4020 expect(linkedReference.containingReference, isNot(0));
4021 expect(linkedReference.containingReference, lessThan(type.reference));
4022 checkReferenceIndex(linkedReference.containingReference, null, null, 'D');
4023 }
4024
4025 test_inferred_type_refers_to_setter_function_typed_parameter_type() {
4026 if (!strongMode || skipFullyLinkedData) {
4027 return;
4028 }
4029 UnlinkedClass cls = serializeClassText(
4030 'class C extends D { void set f(g) {} }'
4031 ' abstract class D { void set f(int g(String s)); }',
4032 className: 'C');
4033 EntityRef type =
4034 getTypeRefForSlot(cls.executables[0].parameters[0].inferredTypeSlot);
4035 // Check that parameter g's inferred type is the type implied by D.f's 1st
4036 // (zero-based) parameter.
4037 expect(type.implicitFunctionTypeIndices, hasLength(1));
4038 expect(type.implicitFunctionTypeIndices[0], 0);
4039 expect(type.paramReference, 0);
4040 expect(type.typeArguments, isEmpty);
4041 expect(type.reference,
4042 greaterThanOrEqualTo(unlinkedUnits[0].references.length));
4043 LinkedReference linkedReference =
4044 linked.units[0].references[type.reference];
4045 expect(linkedReference.dependency, 0);
4046 expect(linkedReference.kind, ReferenceKind.propertyAccessor);
4047 expect(linkedReference.name, 'f=');
4048 expect(linkedReference.numTypeParameters, 0);
4049 expect(linkedReference.unit, 0);
4050 expect(linkedReference.containingReference, isNot(0));
4051 expect(linkedReference.containingReference, lessThan(type.reference));
4052 checkReferenceIndex(linkedReference.containingReference, null, null, 'D');
4053 }
4054
4004 test_invalid_prefix_dynamic() { 4055 test_invalid_prefix_dynamic() {
4005 if (checkAstDerivedData) { 4056 if (checkAstDerivedData) {
4006 // TODO(paulberry): get this to work properly. 4057 // TODO(paulberry): get this to work properly.
4007 return; 4058 return;
4008 } 4059 }
4009 checkUnresolvedTypeRef( 4060 checkUnresolvedTypeRef(
4010 serializeTypeText('dynamic.T', allowErrors: true), 'dynamic', 'T'); 4061 serializeTypeText('dynamic.T', allowErrors: true), 'dynamic', 'T');
4011 } 4062 }
4012 4063
4013 test_invalid_prefix_type_parameter() { 4064 test_invalid_prefix_type_parameter() {
(...skipping 955 matching lines...) Expand 10 before | Expand all | Expand 10 after
4969 final String absoluteUri; 5020 final String absoluteUri;
4970 final String relativeUri; 5021 final String relativeUri;
4971 final int numTypeParameters; 5022 final int numTypeParameters;
4972 5023
4973 _PrefixExpectation(this.kind, this.name, 5024 _PrefixExpectation(this.kind, this.name,
4974 {this.inLibraryDefiningUnit: false, 5025 {this.inLibraryDefiningUnit: false,
4975 this.absoluteUri, 5026 this.absoluteUri,
4976 this.relativeUri, 5027 this.relativeUri,
4977 this.numTypeParameters: 0}); 5028 this.numTypeParameters: 0});
4978 } 5029 }
OLDNEW
« no previous file with comments | « pkg/analyzer/test/src/summary/resynthesize_test.dart ('k') | pkg/analyzer/tool/summary/idl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698