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

Side by Side Diff: pkg/analyzer/lib/src/task/strong/checker.dart

Issue 2801683004: Reify implicit casts for FutureOr (Closed)
Patch Set: Add back FutureOr case Created 3 years, 8 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 | « no previous file | pkg/analyzer/test/src/task/strong/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 // TODO(jmesserly): this was ported from package:dev_compiler, and needs to be 5 // TODO(jmesserly): this was ported from package:dev_compiler, and needs to be
6 // refactored to fit into analyzer. 6 // refactored to fit into analyzer.
7 library analyzer.src.task.strong.checker; 7 library analyzer.src.task.strong.checker;
8 8
9 import 'package:analyzer/analyzer.dart'; 9 import 'package:analyzer/analyzer.dart';
10 import 'package:analyzer/dart/ast/ast.dart'; 10 import 'package:analyzer/dart/ast/ast.dart';
(...skipping 724 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 /// needed, and if so records it. 735 /// needed, and if so records it.
736 /// 736 ///
737 /// If [from] is omitted, uses the static type of [expr]. 737 /// If [from] is omitted, uses the static type of [expr].
738 /// 738 ///
739 /// If [expr] does not require an implicit cast because it is not related to 739 /// If [expr] does not require an implicit cast because it is not related to
740 /// [to] or is already a subtype of it, does nothing. 740 /// [to] or is already a subtype of it, does nothing.
741 void _checkImplicitCast(Expression expr, DartType to, 741 void _checkImplicitCast(Expression expr, DartType to,
742 {DartType from, bool opAssign: false}) { 742 {DartType from, bool opAssign: false}) {
743 from ??= _getDefiniteType(expr); 743 from ??= _getDefiniteType(expr);
744 744
745 if (_needsImplicitCast(expr, to, from: from)) { 745 if (_needsImplicitCast(expr, to, from: from) == true) {
746 _recordImplicitCast(expr, to, from: from, opAssign: opAssign); 746 _recordImplicitCast(expr, to, from: from, opAssign: opAssign);
747 } 747 }
748 } 748 }
749 749
750 /// Checks if the assignment is valid with respect to non-nullable types. 750 /// Checks if the assignment is valid with respect to non-nullable types.
751 /// Returns `false` if a nullable expression is assigned to a variable of 751 /// Returns `false` if a nullable expression is assigned to a variable of
752 /// non-nullable type and `true` otherwise. 752 /// non-nullable type and `true` otherwise.
753 bool _checkNonNullAssignment( 753 bool _checkNonNullAssignment(
754 Expression expression, DartType to, DartType from) { 754 Expression expression, DartType to, DartType from) {
755 if (rules.isNonNullableType(to) && rules.isNullableType(from)) { 755 if (rules.isNonNullableType(to) && rules.isNullableType(from)) {
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
834 } 834 }
835 835
836 var type = functionType.returnType; 836 var type = functionType.returnType;
837 837
838 InterfaceType expectedType = null; 838 InterfaceType expectedType = null;
839 if (body.isAsynchronous) { 839 if (body.isAsynchronous) {
840 if (body.isGenerator) { 840 if (body.isGenerator) {
841 // Stream<T> -> T 841 // Stream<T> -> T
842 expectedType = typeProvider.streamType; 842 expectedType = typeProvider.streamType;
843 } else { 843 } else {
844 // Don't validate return type of async methods. 844 // Future<T> -> FutureOr<T>
845 // They're handled by the runtime implementation. 845 var typeArg = (type.element == typeProvider.futureType.element)
846 return null; 846 ? type.typeArguments[0]
847 : typeProvider.dynamicType;
848 return typeProvider.futureOrType.instantiate([typeArg]);
847 } 849 }
848 } else { 850 } else {
849 if (body.isGenerator) { 851 if (body.isGenerator) {
850 // Iterable<T> -> T 852 // Iterable<T> -> T
851 expectedType = typeProvider.iterableType; 853 expectedType = typeProvider.iterableType;
852 } else { 854 } else {
853 // T -> T 855 // T -> T
854 return type; 856 return type;
855 } 857 }
856 } 858 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
900 // a dynamic parameter type requires a dynamic call in general. 902 // a dynamic parameter type requires a dynamic call in general.
901 // However, as an optimization, if we have an original definition, we know 903 // However, as an optimization, if we have an original definition, we know
902 // dynamic is reified as Object - in this case a regular call is fine. 904 // dynamic is reified as Object - in this case a regular call is fine.
903 if (hasStrictArrow(call.function)) { 905 if (hasStrictArrow(call.function)) {
904 return false; 906 return false;
905 } 907 }
906 return rules.anyParameterType(ft, (pt) => pt.isDynamic); 908 return rules.anyParameterType(ft, (pt) => pt.isDynamic);
907 } 909 }
908 910
909 /// Returns true if we need an implicit cast of [expr] from [from] type to 911 /// Returns true if we need an implicit cast of [expr] from [from] type to
910 /// [to] type, otherwise returns false. 912 /// [to] type, returns false if no cast is needed, and returns null if the
913 /// types are statically incompatible.
911 /// 914 ///
912 /// If [from] is omitted, uses the static type of [expr]. 915 /// If [from] is omitted, uses the static type of [expr]
913 bool _needsImplicitCast(Expression expr, DartType to, {DartType from}) { 916 bool _needsImplicitCast(Expression expr, DartType to, {DartType from}) {
914 from ??= _getDefiniteType(expr); 917 from ??= _getDefiniteType(expr);
915 918
916 if (!_checkNonNullAssignment(expr, to, from)) return false; 919 if (!_checkNonNullAssignment(expr, to, from)) return false;
917 920
918 // We can use anything as void. 921 // We can use anything as void.
919 if (to.isVoid) return false; 922 if (to.isVoid) return false;
920 923
921 // fromT <: toT, no coercion needed. 924 // fromT <: toT, no coercion needed.
922 if (rules.isSubtypeOf(from, to)) return false; 925 if (rules.isSubtypeOf(from, to)) return false;
923 926
924 // Note: a function type is never assignable to a class per the Dart 927 // Down cast or legal sideways cast, coercion needed.
925 // spec - even if it has a compatible call method. We disallow as 928 if (rules.isAssignableTo(from, to)) return true;
926 // well for consistency.
927 if (from is FunctionType && rules.getCallMethodType(to) != null) {
928 return false;
929 }
930 929
931 // Downcast if toT <: fromT 930 // Special case for FutureOr to handle returned values from async functions.
932 if (rules.isSubtypeOf(to, from)) { 931 // In this case, we're more permissive than assignability.
933 return true; 932 if (to.element == typeProvider.futureOrType.element) {
933 var to1 = to.typeArguments[0];
934 var to2 = typeProvider.futureType.instantiate([to1]);
935 return _needsImplicitCast(expr, to1, from: from) == true ||
936 _needsImplicitCast(expr, to2, from: from) == true;
934 } 937 }
935 938
936 // Anything else is an illegal sideways cast. 939 // Anything else is an illegal sideways cast.
937 // However, these will have been reported already in error_verifier, so we 940 // However, these will have been reported already in error_verifier, so we
938 // don't need to report them again. 941 // don't need to report them again.
939 return false; 942 return null;
940 } 943 }
941 944
942 void _recordDynamicInvoke(AstNode node, Expression target) { 945 void _recordDynamicInvoke(AstNode node, Expression target) {
943 _recordMessage(node, StrongModeCode.DYNAMIC_INVOKE, [node]); 946 _recordMessage(node, StrongModeCode.DYNAMIC_INVOKE, [node]);
944 // TODO(jmesserly): we may eventually want to record if the whole operation 947 // TODO(jmesserly): we may eventually want to record if the whole operation
945 // (node) was dynamic, rather than the target, but this is an easier fit 948 // (node) was dynamic, rather than the target, but this is an easier fit
946 // with what we used to do. 949 // with what we used to do.
947 if (target != null) setIsDynamicInvoke(target, true); 950 if (target != null) setIsDynamicInvoke(target, true);
948 } 951 }
949 952
950 /// Records an implicit cast for the [expr] from [from] to [to]. 953 /// Records an implicit cast for the [expr] from [from] to [to].
951 /// 954 ///
952 /// This will emit the appropriate error/warning/hint message as well as mark 955 /// This will emit the appropriate error/warning/hint message as well as mark
953 /// the AST node. 956 /// the AST node.
954 void _recordImplicitCast(Expression expr, DartType to, 957 void _recordImplicitCast(Expression expr, DartType to,
955 {DartType from, bool opAssign: false}) { 958 {DartType from, bool opAssign: false}) {
956 assert(rules.isSubtypeOf(to, from));
957
958 // Inference "casts": 959 // Inference "casts":
959 if (expr is Literal) { 960 if (expr is Literal) {
960 // fromT should be an exact type - this will almost certainly fail at 961 // fromT should be an exact type - this will almost certainly fail at
961 // runtime. 962 // runtime.
962 if (expr is ListLiteral) { 963 if (expr is ListLiteral) {
963 _recordMessage( 964 _recordMessage(
964 expr, StrongModeCode.INVALID_CAST_LITERAL_LIST, [from, to]); 965 expr, StrongModeCode.INVALID_CAST_LITERAL_LIST, [from, to]);
965 } else if (expr is MapLiteral) { 966 } else if (expr is MapLiteral) {
966 _recordMessage( 967 _recordMessage(
967 expr, StrongModeCode.INVALID_CAST_LITERAL_MAP, [from, to]); 968 expr, StrongModeCode.INVALID_CAST_LITERAL_MAP, [from, to]);
(...skipping 595 matching lines...) Expand 10 before | Expand all | Expand 10 after
1563 var visited = new Set<InterfaceType>(); 1564 var visited = new Set<InterfaceType>();
1564 do { 1565 do {
1565 visited.add(current); 1566 visited.add(current);
1566 current.mixins.reversed.forEach( 1567 current.mixins.reversed.forEach(
1567 (m) => _checkIndividualOverridesFromClass(node, m, seen, true)); 1568 (m) => _checkIndividualOverridesFromClass(node, m, seen, true));
1568 _checkIndividualOverridesFromClass(node, current.superclass, seen, true); 1569 _checkIndividualOverridesFromClass(node, current.superclass, seen, true);
1569 current = current.superclass; 1570 current = current.superclass;
1570 } while (!current.isObject && !visited.contains(current)); 1571 } while (!current.isObject && !visited.contains(current));
1571 } 1572 }
1572 } 1573 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/src/task/strong/checker_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698