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 'dart:collection'; | 9 import 'dart:collection'; |
10 import 'package:analyzer/analyzer.dart'; | 10 import 'package:analyzer/analyzer.dart'; |
(...skipping 754 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
765 /// needed, and if so records it. | 765 /// needed, and if so records it. |
766 /// | 766 /// |
767 /// If [from] is omitted, uses the static type of [expr]. | 767 /// If [from] is omitted, uses the static type of [expr]. |
768 /// | 768 /// |
769 /// If [expr] does not require an implicit cast because it is not related to | 769 /// If [expr] does not require an implicit cast because it is not related to |
770 /// [to] or is already a subtype of it, does nothing. | 770 /// [to] or is already a subtype of it, does nothing. |
771 void _checkImplicitCast(Expression expr, DartType to, | 771 void _checkImplicitCast(Expression expr, DartType to, |
772 {DartType from, bool opAssign: false, bool isDeclarationCast: false}) { | 772 {DartType from, bool opAssign: false, bool isDeclarationCast: false}) { |
773 from ??= _getDefiniteType(expr); | 773 from ??= _getDefiniteType(expr); |
774 | 774 |
775 _hintOnFuzzyArrows(expr, to, from: from); | |
776 | |
775 if (_needsImplicitCast(expr, to, | 777 if (_needsImplicitCast(expr, to, |
776 from: from, isDeclarationCast: isDeclarationCast) == | 778 from: from, isDeclarationCast: isDeclarationCast) == |
777 true) { | 779 true) { |
778 _recordImplicitCast(expr, to, from: from, opAssign: opAssign); | 780 _recordImplicitCast(expr, to, from: from, opAssign: opAssign); |
779 } | 781 } |
780 } | 782 } |
781 | 783 |
782 /// Checks if the assignment is valid with respect to non-nullable types. | 784 /// Checks if the assignment is valid with respect to non-nullable types. |
783 /// Returns `false` if a nullable expression is assigned to a variable of | 785 /// Returns `false` if a nullable expression is assigned to a variable of |
784 /// non-nullable type and `true` otherwise. | 786 /// non-nullable type and `true` otherwise. |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1068 // Dynamic as the parameter type is treated as bottom. A function with | 1070 // Dynamic as the parameter type is treated as bottom. A function with |
1069 // a dynamic parameter type requires a dynamic call in general. | 1071 // a dynamic parameter type requires a dynamic call in general. |
1070 // However, as an optimization, if we have an original definition, we know | 1072 // However, as an optimization, if we have an original definition, we know |
1071 // dynamic is reified as Object - in this case a regular call is fine. | 1073 // dynamic is reified as Object - in this case a regular call is fine. |
1072 if (hasStrictArrow(call.function)) { | 1074 if (hasStrictArrow(call.function)) { |
1073 return false; | 1075 return false; |
1074 } | 1076 } |
1075 return rules.anyParameterType(ft, (pt) => pt.isDynamic); | 1077 return rules.anyParameterType(ft, (pt) => pt.isDynamic); |
1076 } | 1078 } |
1077 | 1079 |
1080 void _hintOnFuzzyArrows(Expression expr, DartType to, {DartType from}) { | |
1081 from ??= _getDefiniteType(expr); | |
Jennifer Messerly
2017/08/30 23:29:48
this is not needed because `_checkImplicitCast` di
| |
1082 | |
1083 // If it is a subtype with fuzzy arrows on, | |
1084 // check to see if it still is with them off. | |
1085 if (rules.isSubtypeOf(from, to)) { | |
Jennifer Messerly
2017/08/30 23:29:48
alternate way to implement this method that may be
| |
1086 try { | |
1087 rules.allowDynamicAsBottom = false; | |
1088 // If still true, no warning needed | |
1089 if (rules.isSubtypeOf(from, to)) return; | |
1090 _recordMessage(expr, HintCode.USES_DYNAMIC_AS_BOTTOM, [from, to]); | |
1091 } finally { | |
1092 rules.allowDynamicAsBottom = true; | |
1093 } | |
1094 } | |
1095 } | |
1096 | |
1078 /// Returns true if we need an implicit cast of [expr] from [from] type to | 1097 /// Returns true if we need an implicit cast of [expr] from [from] type to |
1079 /// [to] type, returns false if no cast is needed, and returns null if the | 1098 /// [to] type, returns false if no cast is needed, and returns null if the |
1080 /// types are statically incompatible. | 1099 /// types are statically incompatible. |
1081 /// | 1100 /// |
1082 /// If [from] is omitted, uses the static type of [expr] | 1101 /// If [from] is omitted, uses the static type of [expr] |
1083 bool _needsImplicitCast(Expression expr, DartType to, | 1102 bool _needsImplicitCast(Expression expr, DartType to, |
1084 {DartType from, bool isDeclarationCast: false}) { | 1103 {DartType from, bool isDeclarationCast: false}) { |
1085 from ??= _getDefiniteType(expr); | 1104 from ??= _getDefiniteType(expr); |
1086 | 1105 |
1087 if (!_checkNonNullAssignment(expr, to, from)) return false; | 1106 if (!_checkNonNullAssignment(expr, to, from)) return false; |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1217 var severity = | 1236 var severity = |
1218 (processor != null) ? processor.severity : errorCode.errorSeverity; | 1237 (processor != null) ? processor.severity : errorCode.errorSeverity; |
1219 | 1238 |
1220 if (severity == ErrorSeverity.ERROR) { | 1239 if (severity == ErrorSeverity.ERROR) { |
1221 _failure = true; | 1240 _failure = true; |
1222 } | 1241 } |
1223 if (errorCode.type == ErrorType.HINT && | 1242 if (errorCode.type == ErrorType.HINT && |
1224 errorCode.name.startsWith('STRONG_MODE_TOP_LEVEL_')) { | 1243 errorCode.name.startsWith('STRONG_MODE_TOP_LEVEL_')) { |
1225 severity = ErrorSeverity.ERROR; | 1244 severity = ErrorSeverity.ERROR; |
1226 } | 1245 } |
1227 if (severity != ErrorSeverity.INFO || _options.strongModeHints) { | 1246 if (severity != ErrorSeverity.INFO || |
1247 _options.strongModeHints || | |
1248 errorCode == HintCode.USES_DYNAMIC_AS_BOTTOM) { | |
1228 int begin = node is AnnotatedNode | 1249 int begin = node is AnnotatedNode |
1229 ? node.firstTokenAfterCommentAndMetadata.offset | 1250 ? node.firstTokenAfterCommentAndMetadata.offset |
1230 : node.offset; | 1251 : node.offset; |
1231 int length = node.end - begin; | 1252 int length = node.end - begin; |
1232 var source = resolutionMap | 1253 var source = resolutionMap |
1233 .elementDeclaredByCompilationUnit(node.root as CompilationUnit) | 1254 .elementDeclaredByCompilationUnit(node.root as CompilationUnit) |
1234 .source; | 1255 .source; |
1235 var error = | 1256 var error = |
1236 new AnalysisError(source, begin, length, errorCode, arguments); | 1257 new AnalysisError(source, begin, length, errorCode, arguments); |
1237 reporter.onError(error); | 1258 reporter.onError(error); |
(...skipping 743 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1981 } | 2002 } |
1982 | 2003 |
1983 /// If node is a [ClassDeclaration] returns its members, otherwise if node is | 2004 /// If node is a [ClassDeclaration] returns its members, otherwise if node is |
1984 /// a [ClassTypeAlias] this returns an empty list. | 2005 /// a [ClassTypeAlias] this returns an empty list. |
1985 WithClause _withClause(Declaration node) { | 2006 WithClause _withClause(Declaration node) { |
1986 return node is ClassDeclaration | 2007 return node is ClassDeclaration |
1987 ? node.withClause | 2008 ? node.withClause |
1988 : (node as ClassTypeAlias).withClause; | 2009 : (node as ClassTypeAlias).withClause; |
1989 } | 2010 } |
1990 } | 2011 } |
OLD | NEW |