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

Side by Side Diff: pkg/analyzer/test/src/task/strong_mode_test.dart

Issue 1359243003: Infer setter parameter types in strong mode (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 2 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/task/strong_mode.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) 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 test.src.task.strong_mode_test; 5 library test.src.task.strong_mode_test;
6 6
7 import 'package:analyzer/src/generated/ast.dart'; 7 import 'package:analyzer/src/generated/ast.dart';
8 import 'package:analyzer/src/generated/element.dart'; 8 import 'package:analyzer/src/generated/element.dart';
9 import 'package:analyzer/src/generated/source.dart'; 9 import 'package:analyzer/src/generated/source.dart';
10 import 'package:analyzer/src/task/strong_mode.dart'; 10 import 'package:analyzer/src/task/strong_mode.dart';
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 String getterName = 'g'; 451 String getterName = 'g';
452 CompilationUnitElement unit = resolve(''' 452 CompilationUnitElement unit = resolve('''
453 class A { 453 class A {
454 int get $getterName => 0; 454 int get $getterName => 0;
455 set $getterName(String value) {} 455 set $getterName(String value) {}
456 } 456 }
457 class B extends A { 457 class B extends A {
458 var get $getterName => 1; 458 var get $getterName => 1;
459 } 459 }
460 '''); 460 ''');
461 ClassElement classA = unit.getType('A');
462 FieldElement fieldA = classA.getField(getterName);
463 PropertyAccessorElement getterA = classA.getGetter(getterName);
461 ClassElement classB = unit.getType('B'); 464 ClassElement classB = unit.getType('B');
462 FieldElement fieldB = classB.getField(getterName); 465 FieldElement fieldB = classB.getField(getterName);
463 PropertyAccessorElement getterB = classB.getGetter(getterName); 466 PropertyAccessorElement getterB = classB.getGetter(getterName);
464 expect(fieldB.type.isDynamic, isTrue); 467 expect(fieldB.type.isDynamic, isTrue);
465 expect(getterB.returnType.isDynamic, isTrue); 468 expect(getterB.returnType.isDynamic, isTrue);
466 469
467 inferrer.inferCompilationUnit(unit); 470 inferrer.inferCompilationUnit(unit);
468 471
472 // Expected behavior is that the getter is inferred: getters and setters
473 // are treated as independent methods.
474 expect(fieldB.type, fieldA.type);
475 expect(getterB.returnType, getterA.returnType);
476 }
477
478 void test_inferCompilationUnit_setter_single() {
479 InstanceMemberInferrer inferrer = createInferrer;
480 String setterName = 'g';
481 CompilationUnitElement unit = resolve('''
482 class A {
483 set $setterName(int x) {}
484 }
485 class B extends A {
486 set $setterName(x) {}
487 }
488 ''');
489 ClassElement classA = unit.getType('A');
490 FieldElement fieldA = classA.getField(setterName);
491 PropertyAccessorElement setterA = classA.getSetter(setterName);
492 ClassElement classB = unit.getType('B');
493 FieldElement fieldB = classB.getField(setterName);
494 PropertyAccessorElement setterB = classB.getSetter(setterName);
469 expect(fieldB.type.isDynamic, isTrue); 495 expect(fieldB.type.isDynamic, isTrue);
470 expect(getterB.returnType.isDynamic, isTrue); 496 expect(setterB.parameters[0].type.isDynamic, isTrue);
497
498 inferrer.inferCompilationUnit(unit);
499
500 expect(fieldB.type, fieldA.type);
501 expect(setterB.parameters[0].type, setterA.parameters[0].type);
502 }
503
504 void test_inferCompilationUnit_setter_single_generic() {
505 InstanceMemberInferrer inferrer = createInferrer;
506 String setterName = 'g';
507 CompilationUnitElement unit = resolve('''
508 class A<E> {
509 set $setterName(E x) {}
510 }
511 class B<E> extends A<E> {
512 set $setterName(x) {}
513 }
514 ''');
515 ClassElement classB = unit.getType('B');
516 DartType typeBE = classB.typeParameters[0].type;
517 FieldElement fieldB = classB.getField(setterName);
518 PropertyAccessorElement setterB = classB.getSetter(setterName);
519 expect(fieldB.type.isDynamic, isTrue);
520 expect(setterB.parameters[0].type.isDynamic, isTrue);
521
522 inferrer.inferCompilationUnit(unit);
523
524 expect(fieldB.type, typeBE);
525 expect(setterB.parameters[0].type, typeBE);
526 }
527
528 void test_inferCompilationUnit_setter_single_inconsistentAccessors() {
529 InstanceMemberInferrer inferrer = createInferrer;
530 String getterName = 'g';
531 CompilationUnitElement unit = resolve('''
532 class A {
533 int get $getterName => 0;
534 set $getterName(String value) {}
535 }
536 class B extends A {
537 set $getterName(x) {}
538 }
539 ''');
540 ClassElement classA = unit.getType('A');
541 FieldElement fieldA = classA.getField(getterName);
542 PropertyAccessorElement setterA = classA.getSetter(getterName);
543 ClassElement classB = unit.getType('B');
544 FieldElement fieldB = classB.getField(getterName);
545 PropertyAccessorElement setterB = classB.getSetter(getterName);
546 expect(fieldB.type.isDynamic, isTrue);
547 expect(setterB.parameters[0].type.isDynamic, isTrue);
548
549 inferrer.inferCompilationUnit(unit);
550
551 // Expected behavior is that the getter is inferred: getters and setters
552 // are treated as independent methods.
553 expect(setterB.parameters[0].type, setterA.parameters[0].type);
554
555 // Note that B's synthetic field type will be String. This matches what
556 // resolver would do if we explicitly typed the parameter as 'String'
557 expect(fieldB.type, setterB.parameters[0].type);
471 } 558 }
472 559
473 void test_inferCompilationUnit_invalid_inheritanceCycle() { 560 void test_inferCompilationUnit_invalid_inheritanceCycle() {
474 InstanceMemberInferrer inferrer = createInferrer; 561 InstanceMemberInferrer inferrer = createInferrer;
475 CompilationUnitElement unit = resolve(''' 562 CompilationUnitElement unit = resolve('''
476 class A extends C {} 563 class A extends C {}
477 class B extends A {} 564 class B extends A {}
478 class C extends B {} 565 class C extends B {}
479 '''); 566 ''');
480 inferrer.inferCompilationUnit(unit); 567 inferrer.inferCompilationUnit(unit);
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after
900 } 987 }
901 } 988 }
902 } 989 }
903 '''); 990 ''');
904 CompilationUnit unit = context.resolveCompilationUnit2(source, source); 991 CompilationUnit unit = context.resolveCompilationUnit2(source, source);
905 VariableGatherer gatherer = new VariableGatherer(filter); 992 VariableGatherer gatherer = new VariableGatherer(filter);
906 unit.accept(gatherer); 993 unit.accept(gatherer);
907 return gatherer.results; 994 return gatherer.results;
908 } 995 }
909 } 996 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/task/strong_mode.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698