OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 Loading... | |
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 // Note: a function type is never assignable to a class per the Dart |
925 // spec - even if it has a compatible call method. We disallow as | 928 // spec - even if it has a compatible call method. We disallow as |
926 // well for consistency. | 929 // well for consistency. |
927 if (from is FunctionType && rules.getCallMethodType(to) != null) { | 930 if (from is FunctionType && rules.getCallMethodType(to) != null) { |
928 return false; | 931 return false; |
929 } | 932 } |
930 | 933 |
931 // Downcast if toT <: fromT | 934 // Downcast if toT <: fromT |
932 if (rules.isSubtypeOf(to, from)) { | 935 if (rules.isSubtypeOf(to, from)) { |
933 return true; | 936 return true; |
934 } | 937 } |
935 | 938 |
939 // TODO(vsm): Resolve assignability / implicit cast around FutureOr. | |
940 // Unwrap FutureOr union to check for a cast. | |
Leaf
2017/04/05 20:47:27
This will solve the async issue, but will also gen
vsm
2017/04/07 22:23:08
I've cleaned this up to just use assignability.
| |
941 if (to.element == typeProvider.futureOrType.element) { | |
942 var to1 = to.typeArguments[0]; | |
943 var to2 = typeProvider.futureType.instantiate([to1]); | |
944 return _needsImplicitCast(expr, to1, from: from) == true || | |
945 _needsImplicitCast(expr, to2, from: from) == true; | |
946 } | |
947 | |
936 // Anything else is an illegal sideways cast. | 948 // Anything else is an illegal sideways cast. |
937 // However, these will have been reported already in error_verifier, so we | 949 // However, these will have been reported already in error_verifier, so we |
938 // don't need to report them again. | 950 // don't need to report them again. |
939 return false; | 951 return null; |
Leaf
2017/04/05 20:47:27
Is there any reason not to just generate the cast
vsm
2017/04/07 22:23:07
Injecting the cast here spams the error list with
| |
940 } | 952 } |
941 | 953 |
942 void _recordDynamicInvoke(AstNode node, Expression target) { | 954 void _recordDynamicInvoke(AstNode node, Expression target) { |
943 _recordMessage(node, StrongModeCode.DYNAMIC_INVOKE, [node]); | 955 _recordMessage(node, StrongModeCode.DYNAMIC_INVOKE, [node]); |
944 // TODO(jmesserly): we may eventually want to record if the whole operation | 956 // 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 | 957 // (node) was dynamic, rather than the target, but this is an easier fit |
946 // with what we used to do. | 958 // with what we used to do. |
947 if (target != null) setIsDynamicInvoke(target, true); | 959 if (target != null) setIsDynamicInvoke(target, true); |
948 } | 960 } |
949 | 961 |
950 /// Records an implicit cast for the [expr] from [from] to [to]. | 962 /// Records an implicit cast for the [expr] from [from] to [to]. |
951 /// | 963 /// |
952 /// This will emit the appropriate error/warning/hint message as well as mark | 964 /// This will emit the appropriate error/warning/hint message as well as mark |
953 /// the AST node. | 965 /// the AST node. |
954 void _recordImplicitCast(Expression expr, DartType to, | 966 void _recordImplicitCast(Expression expr, DartType to, |
955 {DartType from, bool opAssign: false}) { | 967 {DartType from, bool opAssign: false}) { |
956 assert(rules.isSubtypeOf(to, from)); | 968 // TODO(vsm): Resolve assignability / implicit cast around FutureOr. |
969 assert(rules.isSubtypeOf(to, from) || | |
970 to.element == typeProvider.futureOrType.element); | |
957 | 971 |
958 // Inference "casts": | 972 // Inference "casts": |
959 if (expr is Literal) { | 973 if (expr is Literal) { |
960 // fromT should be an exact type - this will almost certainly fail at | 974 // fromT should be an exact type - this will almost certainly fail at |
961 // runtime. | 975 // runtime. |
962 if (expr is ListLiteral) { | 976 if (expr is ListLiteral) { |
963 _recordMessage( | 977 _recordMessage( |
964 expr, StrongModeCode.INVALID_CAST_LITERAL_LIST, [from, to]); | 978 expr, StrongModeCode.INVALID_CAST_LITERAL_LIST, [from, to]); |
965 } else if (expr is MapLiteral) { | 979 } else if (expr is MapLiteral) { |
966 _recordMessage( | 980 _recordMessage( |
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1563 var visited = new Set<InterfaceType>(); | 1577 var visited = new Set<InterfaceType>(); |
1564 do { | 1578 do { |
1565 visited.add(current); | 1579 visited.add(current); |
1566 current.mixins.reversed.forEach( | 1580 current.mixins.reversed.forEach( |
1567 (m) => _checkIndividualOverridesFromClass(node, m, seen, true)); | 1581 (m) => _checkIndividualOverridesFromClass(node, m, seen, true)); |
1568 _checkIndividualOverridesFromClass(node, current.superclass, seen, true); | 1582 _checkIndividualOverridesFromClass(node, current.superclass, seen, true); |
1569 current = current.superclass; | 1583 current = current.superclass; |
1570 } while (!current.isObject && !visited.contains(current)); | 1584 } while (!current.isObject && !visited.contains(current)); |
1571 } | 1585 } |
1572 } | 1586 } |
OLD | NEW |