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

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: add more comments 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 test_covarianceChecks_returnFunction() async {
513 var source = addSource(r'''
514 typedef F<T>(T t);
515 typedef T R<T>();
516 class C<T> {
517 F<T> f;
518 F<T> get g => null;
519 F<T> m1() => null;
520 R<F<T>> m2() => null;
521
522 casts(C<T> other, T t, F<T> fT, R<F<T>> rfT) {
523 f;
524 g;
525 m1();
526 m2();
527
528 f(t);
529 g(t);
530 (f)(t);
531 (g)(t);
532 m1;
533 m2;
534
535 this.f;
536 this.g;
537 this.m1();
538 this.m2();
539 this.m1;
540 this.m2;
541 (this.m1)();
542 (this.m2)();
543 this.f(t);
544 this.g(t);
545 (this.f)(t);
546 (this.g)(t);
547
548 other.f;
549 other.g;
550 other.m1;
551 other.m2;
552 }
553 }
554 class D extends C<int> {}
555
556 D d;
557 C<Object> c;
558 F<Object> f;
559 F<Null> fN;
560 R<F<Object>> rf;
561 R<F<Null>> rfN;
562 R<R<F<Object>>> rrf;
563 R<R<F<Null>>> rrfN;
564 Object obj;
565 F<int> fi;
566 R<F<int>> rfi;
567 R<R<F<int>>> rrfi;
568
569 casts() {
570 c.f;
571 c.g;
572 c.m1;
573 c.m1();
574 c.m2();
575
576 fN = c.f;
577 fN = c.g;
578 rfN = c.m1;
579 rrfN = c.m2;
580 fN = c.m1();
581 rfN = c.m2();
582
583 f = c.f;
584 f = c.g;
585 rf = c.m1;
586 rrf = c.m2;
587 f = c.m1();
588 rf = c.m2();
589 c.m2()();
590
591 c.f(obj);
592 c.g(obj);
593 (c.f)(obj);
594 (c.g)(obj);
595 (c.m1)();
596 c.m1()(obj);
597 (c.m2)();
598 }
599
600 noCasts() {
601 fi = d.f;
602 fi = d.g;
603 rfi = d.m1;
604 fi = d.m1();
605 rrfi = d.m2;
606 rfi = d.m2();
607 d.f(42);
608 d.g(42);
609 (d.f)(42);
610 (d.g)(42);
611 d.m1()(42);
612 d.m2()()(42);
613 }
614 ''');
615 var unit = (await computeAnalysisResult(source)).unit;
616 assertNoErrors(source);
617
618 void expectCast(Statement statement, bool hasCast) {
619 var value = (statement as ExpressionStatement).expression;
620 if (value is AssignmentExpression) {
621 value = (value as AssignmentExpression).rightHandSide;
622 }
623 while (value is FunctionExpressionInvocation) {
624 value = (value as FunctionExpressionInvocation).function;
625 }
626 while (value is ParenthesizedExpression) {
627 value = (value as ParenthesizedExpression).expression;
628 }
629 var isCallingGetter =
630 value is MethodInvocation && !value.methodName.name.startsWith('m');
631 var cast = isCallingGetter
632 ? getImplicitOperationCast(value)
633 : getImplicitCast(value);
634 expect(cast, hasCast ? isNotNull : isNull,
635 reason: '`$statement` should ' +
636 (hasCast ? '' : 'not ') +
637 'have a cast on `$value`.');
638 }
639
640 for (var s in AstFinder.getStatementsInMethod(unit, 'C', 'casts')) {
641 expectCast(s, true);
642 }
643 for (var s in AstFinder.getStatementsInTopLevelFunction(unit, 'noCasts')) {
644 expectCast(s, false);
645 }
646 for (var s in AstFinder.getStatementsInTopLevelFunction(unit, 'casts')) {
647 expectCast(s, true);
648 }
649 }
650
423 test_factoryConstructor_propagation() async { 651 test_factoryConstructor_propagation() async {
424 String code = r''' 652 String code = r'''
425 class A<T> { 653 class A<T> {
426 factory A() { return new B(); } 654 factory A() { return new B(); }
427 } 655 }
428 class B<S> extends A<S> {} 656 class B<S> extends A<S> {}
429 '''; 657 ''';
430 CompilationUnit unit = await resolveSource(code); 658 CompilationUnit unit = await resolveSource(code);
431 659
432 ConstructorDeclaration constructor = 660 ConstructorDeclaration constructor =
(...skipping 3490 matching lines...) Expand 10 before | Expand all | Expand 10 after
3923 var v = x; 4151 var v = x;
3924 v; // marker 4152 v; // marker
3925 } 4153 }
3926 int x = 3; 4154 int x = 3;
3927 '''; 4155 ''';
3928 CompilationUnit unit = await resolveSource(code); 4156 CompilationUnit unit = await resolveSource(code);
3929 assertPropagatedAssignedType(code, unit, typeProvider.intType, null); 4157 assertPropagatedAssignedType(code, unit, typeProvider.intType, null);
3930 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null); 4158 assertTypeOfMarkedExpression(code, unit, typeProvider.intType, null);
3931 } 4159 }
3932 } 4160 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698