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

Side by Side Diff: lib/src/checker/rules.dart

Issue 1121993004: Inference through conditional expressions (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Rebase Created 5 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 | « lib/runtime/dart/core.js ('k') | test/checker/checker_test.dart » ('j') | 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 dev_compiler.src.checker.rules; 5 library dev_compiler.src.checker.rules;
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/resolver.dart'; 9 import 'package:analyzer/src/generated/resolver.dart';
10 10
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 603
604 /// Downward inference 604 /// Downward inference
605 bool inferExpression(Expression e, DartType t, List<String> errors) { 605 bool inferExpression(Expression e, DartType t, List<String> errors) {
606 // Don't cast top level expressions, only sub-expressions 606 // Don't cast top level expressions, only sub-expressions
607 return _inferExpression(e, t, errors, cast: false); 607 return _inferExpression(e, t, errors, cast: false);
608 } 608 }
609 609
610 /// Downward inference 610 /// Downward inference
611 bool _inferExpression(Expression e, DartType t, List<String> errors, 611 bool _inferExpression(Expression e, DartType t, List<String> errors,
612 {cast: true}) { 612 {cast: true}) {
613 if (e is ConditionalExpression) {
614 return _inferConditionalExpression(e, t, errors);
615 }
613 if (e is Conversion) return _inferExpression(e.node, t, errors); 616 if (e is Conversion) return _inferExpression(e.node, t, errors);
614 if (rules.isSubTypeOf(rules.getStaticType(e), t)) return true; 617 if (rules.isSubTypeOf(rules.getStaticType(e), t)) return true;
615 if (cast && rules.getStaticType(e).isDynamic) { 618 if (cast && rules.getStaticType(e).isDynamic) {
616 annotateCastFromDynamic(e, t); 619 annotateCastFromDynamic(e, t);
617 return true; 620 return true;
618 } 621 }
619 if (e is FunctionExpression) return _inferFunctionExpression(e, t, errors); 622 if (e is FunctionExpression) return _inferFunctionExpression(e, t, errors);
620 if (e is ListLiteral) return _inferListLiteral(e, t, errors); 623 if (e is ListLiteral) return _inferListLiteral(e, t, errors);
621 if (e is MapLiteral) return _inferMapLiteral(e, t, errors); 624 if (e is MapLiteral) return _inferMapLiteral(e, t, errors);
622 if (e is NamedExpression) return _inferNamedExpression(e, t, errors); 625 if (e is NamedExpression) return _inferNamedExpression(e, t, errors);
623 if (e is InstanceCreationExpression) return _inferInstanceCreationExpression ( 626 if (e is InstanceCreationExpression) {
624 e, t, errors); 627 return _inferInstanceCreationExpression(e, t, errors);
628 }
625 errors.add("$e cannot be typed as $t"); 629 errors.add("$e cannot be typed as $t");
626 return false; 630 return false;
627 } 631 }
628 632
629 /// If t1 = I<dynamic, ..., dynamic>, then look for a supertype 633 /// If t1 = I<dynamic, ..., dynamic>, then look for a supertype
630 /// of t1 of the form K<S0, ..., Sm> where t2 = K<S0', ..., Sm'> 634 /// of t1 of the form K<S0, ..., Sm> where t2 = K<S0', ..., Sm'>
631 /// If the supertype exists, use the constraints S0 <: S0', ... Sm <: Sm' 635 /// If the supertype exists, use the constraints S0 <: S0', ... Sm <: Sm'
632 /// to derive a concrete instantation for I of the form <T0, ..., Tn>, 636 /// to derive a concrete instantation for I of the form <T0, ..., Tn>,
633 /// such that I<T0, .., Tn> <: t2 637 /// such that I<T0, .., Tn> <: t2
634 List<DartType> _matchTypes(InterfaceType t1, InterfaceType t2) { 638 List<DartType> _matchTypes(InterfaceType t1, InterfaceType t2) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 // t1.element.type will be of the form T1<X0, ..., Xn> 704 // t1.element.type will be of the form T1<X0, ..., Xn>
701 if (!match(t1.element.type)) return null; 705 if (!match(t1.element.type)) return null;
702 var newT1 = t1.element.type.substitute4(actuals); 706 var newT1 = t1.element.type.substitute4(actuals);
703 // If we found a solution, return it. 707 // If we found a solution, return it.
704 if (rules.isSubTypeOf(newT1, t2)) return actuals; 708 if (rules.isSubTypeOf(newT1, t2)) return actuals;
705 return null; 709 return null;
706 } 710 }
707 711
708 /// These assume that e is not already a subtype of t 712 /// These assume that e is not already a subtype of t
709 713
714 bool _inferConditionalExpression(
715 ConditionalExpression e, DartType t, errors) {
716 return _inferExpression(e.thenExpression, t, errors) &&
717 _inferExpression(e.elseExpression, t, errors);
718 }
719
710 bool _inferInstanceCreationExpression( 720 bool _inferInstanceCreationExpression(
711 InstanceCreationExpression e, DartType t, errors) { 721 InstanceCreationExpression e, DartType t, errors) {
712 var arguments = e.argumentList.arguments; 722 var arguments = e.argumentList.arguments;
713 var rawType = rules.getStaticType(e); 723 var rawType = rules.getStaticType(e);
714 // rawType is the instantiated type of the instance 724 // rawType is the instantiated type of the instance
715 if (rawType is! InterfaceType) return false; 725 if (rawType is! InterfaceType) return false;
716 var type = (rawType as InterfaceType); 726 var type = (rawType as InterfaceType);
717 if (type.typeParameters == null || 727 if (type.typeParameters == null ||
718 type.typeParameters.length == 0) return false; 728 type.typeParameters.length == 0) return false;
719 if (e.constructorName.type == null) return false; 729 if (e.constructorName.type == null) return false;
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
844 var entries = e.entries; 854 var entries = e.entries;
845 bool inferEntry(MapLiteralEntry entry) { 855 bool inferEntry(MapLiteralEntry entry) {
846 return _inferExpression(entry.key, kType, errors) && 856 return _inferExpression(entry.key, kType, errors) &&
847 _inferExpression(entry.value, vType, errors); 857 _inferExpression(entry.value, vType, errors);
848 } 858 }
849 var b = entries.every(inferEntry); 859 var b = entries.every(inferEntry);
850 if (b) annotateMapLiteral(e, targs); 860 if (b) annotateMapLiteral(e, targs);
851 return b; 861 return b;
852 } 862 }
853 } 863 }
OLDNEW
« no previous file with comments | « lib/runtime/dart/core.js ('k') | test/checker/checker_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698