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

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

Issue 2954523002: fix #27259, implement covariance checking for strong mode and DDC (Closed)
Patch Set: rebase Created 3 years, 5 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) 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 library analyzer.test.generated.strong_mode_test; 5 library analyzer.test.generated.strong_mode_test;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; 10 import 'package:analyzer/dart/ast/standard_resolution_map.dart';
11 import 'package:analyzer/dart/element/element.dart'; 11 import 'package:analyzer/dart/element/element.dart';
12 import 'package:analyzer/dart/element/type.dart'; 12 import 'package:analyzer/dart/element/type.dart';
13 import 'package:analyzer/src/dart/element/element.dart'; 13 import 'package:analyzer/src/dart/element/element.dart';
14 import 'package:analyzer/src/error/codes.dart'; 14 import 'package:analyzer/src/error/codes.dart';
15 import 'package:analyzer/src/generated/engine.dart'; 15 import 'package:analyzer/src/generated/engine.dart';
16 import 'package:analyzer/src/generated/source_io.dart'; 16 import 'package:analyzer/src/generated/source_io.dart';
17 import 'package:analyzer/src/task/strong/ast_properties.dart';
17 import 'package:front_end/src/base/errors.dart'; 18 import 'package:front_end/src/base/errors.dart';
18 import 'package:test/test.dart'; 19 import 'package:test/test.dart';
19 import 'package:test_reflective_loader/test_reflective_loader.dart'; 20 import 'package:test_reflective_loader/test_reflective_loader.dart';
20 21
21 import '../utils.dart'; 22 import '../utils.dart';
22 import 'resolver_test_case.dart'; 23 import 'resolver_test_case.dart';
23 24
24 main() { 25 main() {
25 defineReflectiveSuite(() { 26 defineReflectiveSuite(() {
26 defineReflectiveTests(StrongModeLocalInferenceTest); 27 defineReflectiveTests(StrongModeLocalInferenceTest);
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 } 414 }
414 '''; 415 ''';
415 CompilationUnit unit = await resolveSource(code); 416 CompilationUnit unit = await resolveSource(code);
416 ConstructorDeclaration constructor = 417 ConstructorDeclaration constructor =
417 AstFinder.getConstructorInClass(unit, "A", null); 418 AstFinder.getConstructorInClass(unit, "A", null);
418 ConstructorFieldInitializer assignment = constructor.initializers[0]; 419 ConstructorFieldInitializer assignment = constructor.initializers[0];
419 Expression exp = assignment.expression; 420 Expression exp = assignment.expression;
420 _isListOf(_isString)(exp.staticType); 421 _isListOf(_isString)(exp.staticType);
421 } 422 }
422 423
424 test_covarianceChecks() async {
425 var source = addSource(r'''
426 class C<T> {
427 add(T t) {}
428 forEach(void f(T t)) {}
429 }
430 class D extends C<int> {
431 add(int t) {}
432 forEach(void f(int t)) {}
433 }
434 class E extends C<int> {
435 add(Object t) {}
436 forEach(void f(Null t)) {}
437 }
438 ''');
439 var unit = (await computeAnalysisResult(source)).unit;
440 assertNoErrors(source);
441 var cAdd = AstFinder.getMethodInClass(unit, "C", "add");
442 var covariantC = getClassCovariantParameters(AstFinder.getClass(unit, "C"));
443 expect(covariantC.toList(), [cAdd.element.parameters[0]]);
444
445 var dAdd = AstFinder.getMethodInClass(unit, "D", "add");
446 var covariantD = getClassCovariantParameters(AstFinder.getClass(unit, "D"));
447 expect(covariantD.toList(), [dAdd.element.parameters[0]]);
448
449 var covariantE = getClassCovariantParameters(AstFinder.getClass(unit, "E"));
450 expect(covariantE.toList(), []);
451 }
452
453 test_covarianceChecks_genericMethods() async {
454 var source = addSource(r'''
455 class C<T> {
456 add<S>(T t) {}
457 forEach<S>(S f(T t)) {}
458 }
459 class D extends C<int> {
460 add<S>(int t) {}
461 forEach<S>(S f(int t)) {}
462 }
463 class E extends C<int> {
464 add<S>(Object t) {}
465 forEach<S>(S f(Null t)) {}
466 }
467 ''');
468 var unit = (await computeAnalysisResult(source)).unit;
469 assertNoErrors(source);
470
471 var cAdd = AstFinder.getMethodInClass(unit, "C", "add");
472 var covariantC = getClassCovariantParameters(AstFinder.getClass(unit, "C"));
473 expect(covariantC.toList(), [cAdd.element.parameters[0]]);
474
475 var dAdd = AstFinder.getMethodInClass(unit, "D", "add");
476 var covariantD = getClassCovariantParameters(AstFinder.getClass(unit, "D"));
477 expect(covariantD.toList(), [dAdd.element.parameters[0]]);
478
479 var covariantE = getClassCovariantParameters(AstFinder.getClass(unit, "E"));
480 expect(covariantE.toList(), []);
481 }
482
483 test_covarianceChecks_superclass() async {
484 var source = addSource(r'''
485 class C<T> {
486 add(T t) {}
487 forEach(void f(T t)) {}
488 }
489 class D {
490 add(int t) {}
491 forEach(void f(int t)) {}
492 }
493 class E extends D implements C<int> {}
494 ''');
495 var unit = (await computeAnalysisResult(source)).unit;
496 assertNoErrors(source);
497 var cAdd = AstFinder.getMethodInClass(unit, "C", "add");
498 var covariantC = getClassCovariantParameters(AstFinder.getClass(unit, "C"));
499 expect(covariantC.toList(), [cAdd.element.parameters[0]]);
500
501 var dAdd = AstFinder.getMethodInClass(unit, "D", "add");
502 var covariantD = getClassCovariantParameters(AstFinder.getClass(unit, "D"));
503 expect(covariantD, null);
504
505 var classE = AstFinder.getClass(unit, "E");
506 var covariantE = getClassCovariantParameters(classE);
507 var superCovariantE = getSuperclassCovariantParameters(classE);
508 expect(covariantE.toList(), []);
509 expect(superCovariantE.toList(), [dAdd.element.parameters[0]]);
510 }
511
512 @soloTest
Leaf 2017/06/28 18:12:29 remove?
Jennifer Messerly 2017/07/05 20:11:20 Done.
513 test_covarianceChecks_returnFunction() async {
514 var source = addSource(r'''
515
516 typedef F<T>(T t);
517 typedef T R<T>();
518 class C<T> {
519 F<T> f;
520 F<T> get g => null;
521 F<T> m1() => null;
522 R<F<T>> m2() => null;
523
524 casts(C<T> other, T t, F<T> fT, R<F<T>> rfT) {
525 fT = f;
526 fT = g;
527 fT = m1();
528 rfT = m2();
529
530 f(t);
531 g(t);
532 (f)(t);
533 (g)(t);
534 m1()(t);
535 m2()()(t);
536
537 fT = this.f;
538 fT = this.g;
539 fT = this.m1();
540 rfT = this.m2();
541
542 this.f(t);
543 this.g(t);
544 (this.f)(t);
545 (this.g)(t);
546 this.m1()(t);
547 this.m2()()(t);
548
549 fT = other.f;
550 fT = other.g;
551 fT = other.m1();
552 rfT = other.m2();
553
554 other.f(t);
555 other.g(t);
556 (other.f)(t);
557 (other.g)(t);
558 other.m1()(t);
559 other.m2()()(t);
560 }
561 }
562 class D extends C<int> {}
563
564 D d;
565 C<Object> c;
566 F<Object> f;
567 F<Null> fN;
568 R<F<Object>> rf;
569 R<F<Null>> rfN;
570 R<R<F<Object>>> rrf;
571 R<R<F<Null>>> rrfN;
572 Object obj;
573 F<int> fi;
574 R<F<int>> rfi;
575 R<R<F<int>>> rrfi;
576
577 casts() {
578 f = c.f;
579 f = c.g;
580 rf = c.m1;
581 rrf = c.m2;
582 f = c.m1();
583 rf = c.m2();
584 c.m2()();
585
586 c.f(obj);
587 c.g(obj);
588 (c.f)(obj);
589 (c.g)(obj);
590 (c.m1)();
591 c.m1()(obj);
592 (c.m2)();
593 }
594
595 noCasts() {
596 c.f;
597 c.g;
598 c.m1;
599 c.m1();
600 c.m2();
601
602 fN = c.f;
603 fN = c.g;
604 rfN = c.m1;
605 rrfN = c.m2;
606 fN = c.m1();
607 rfN = c.m2();
608 fN = c.m2()();
609
610 fi = d.f;
611 fi = d.g;
612 rfi = d.m1;
613 fi = d.m1();
614 rrfi = d.m2;
615 rfi = d.m2();
616 fi = d.m2()();
617 d.f(42);
618 d.g(42);
619 (d.f)(42);
620 (d.g)(42);
621 d.m1()(42);
622 d.m2()()(42);
623 }
624 ''');
625 var unit = (await computeAnalysisResult(source)).unit;
626 assertNoErrors(source);
627
628 void expectCast(Statement statement, bool hasCast) {
629 var expr = (statement as ExpressionStatement).expression;
630 Expression value;
631 if (expr is AssignmentExpression) {
632 value = expr.rightHandSide;
633 } else {
634 value = expr;
635 while (value is InvocationExpression) {
636 value = (value as InvocationExpression).function;
637 }
638 // We don't put the cast on the method itself
639 if (value is SimpleIdentifier && value.name.startsWith('m')) {
640 value = value.parent;
641 }
642 }
643 while (value is ParenthesizedExpression) {
644 value = (value as ParenthesizedExpression).expression;
645 }
646 var parent = value.parent;
647 var cast = parent is MethodInvocation && value == parent.methodName
648 ? getImplicitOperationCast(value)
649 : getImplicitCast(value);
650 expect(cast, hasCast ? isNotNull : isNull,
651 reason: '`$expr` should ' +
652 (hasCast ? '' : 'not ') +
653 'have a cast on `$value`.');
654 }
655
656 for (var s in AstFinder.getStatementsInMethod(unit, 'C', 'casts')) {
657 expectCast(s, true);
658 }
659 for (var s in AstFinder.getStatementsInTopLevelFunction(unit, 'noCasts')) {
660 expectCast(s, false);
661 }
662 for (var s in AstFinder.getStatementsInTopLevelFunction(unit, 'casts')) {
663 expectCast(s, true);
664 }
665 }
666
423 test_factoryConstructor_propagation() async { 667 test_factoryConstructor_propagation() async {
424 String code = r''' 668 String code = r'''
425 class A<T> { 669 class A<T> {
426 factory A() { return new B(); } 670 factory A() { return new B(); }
427 } 671 }
428 class B<S> extends A<S> {} 672 class B<S> extends A<S> {}
429 '''; 673 ''';
430 CompilationUnit unit = await resolveSource(code); 674 CompilationUnit unit = await resolveSource(code);
431 675
432 ConstructorDeclaration constructor = 676 ConstructorDeclaration constructor =
(...skipping 3490 matching lines...) Expand 10 before | Expand all | Expand 10 after
3923 var v = x; 4167 var v = x;
3924 v; // marker 4168 v; // marker
3925 } 4169 }
3926 int x = 3; 4170 int x = 3;
3927 '''; 4171 ''';
3928 CompilationUnit unit = await resolveSource(code); 4172 CompilationUnit unit = await resolveSource(code);
3929 assertPropagatedAssignedType(code, unit, typeProvider.intType, null); 4173 assertPropagatedAssignedType(code, unit, typeProvider.intType, null);
3930 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null); 4174 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null);
3931 } 4175 }
3932 } 4176 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698