| 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 | |
| 777 if (_needsImplicitCast(expr, to, | 775 if (_needsImplicitCast(expr, to, |
| 778 from: from, isDeclarationCast: isDeclarationCast) == | 776 from: from, isDeclarationCast: isDeclarationCast) == |
| 779 true) { | 777 true) { |
| 780 _recordImplicitCast(expr, to, from: from, opAssign: opAssign); | 778 _recordImplicitCast(expr, to, from: from, opAssign: opAssign); |
| 781 } | 779 } |
| 782 } | 780 } |
| 783 | 781 |
| 784 /// Checks if the assignment is valid with respect to non-nullable types. | 782 /// Checks if the assignment is valid with respect to non-nullable types. |
| 785 /// Returns `false` if a nullable expression is assigned to a variable of | 783 /// Returns `false` if a nullable expression is assigned to a variable of |
| 786 /// non-nullable type and `true` otherwise. | 784 /// non-nullable type and `true` otherwise. |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1070 // Dynamic as the parameter type is treated as bottom. A function with | 1068 // Dynamic as the parameter type is treated as bottom. A function with |
| 1071 // a dynamic parameter type requires a dynamic call in general. | 1069 // a dynamic parameter type requires a dynamic call in general. |
| 1072 // However, as an optimization, if we have an original definition, we know | 1070 // However, as an optimization, if we have an original definition, we know |
| 1073 // dynamic is reified as Object - in this case a regular call is fine. | 1071 // dynamic is reified as Object - in this case a regular call is fine. |
| 1074 if (hasStrictArrow(call.function)) { | 1072 if (hasStrictArrow(call.function)) { |
| 1075 return false; | 1073 return false; |
| 1076 } | 1074 } |
| 1077 return rules.anyParameterType(ft, (pt) => pt.isDynamic); | 1075 return rules.anyParameterType(ft, (pt) => pt.isDynamic); |
| 1078 } | 1076 } |
| 1079 | 1077 |
| 1080 void _hintOnFuzzyArrows(Expression expr, DartType to, {DartType from}) { | |
| 1081 from ??= _getDefiniteType(expr); | |
| 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)) { | |
| 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 | |
| 1097 /// Returns true if we need an implicit cast of [expr] from [from] type to | 1078 /// Returns true if we need an implicit cast of [expr] from [from] type to |
| 1098 /// [to] type, returns false if no cast is needed, and returns null if the | 1079 /// [to] type, returns false if no cast is needed, and returns null if the |
| 1099 /// types are statically incompatible. | 1080 /// types are statically incompatible. |
| 1100 /// | 1081 /// |
| 1101 /// If [from] is omitted, uses the static type of [expr] | 1082 /// If [from] is omitted, uses the static type of [expr] |
| 1102 bool _needsImplicitCast(Expression expr, DartType to, | 1083 bool _needsImplicitCast(Expression expr, DartType to, |
| 1103 {DartType from, bool isDeclarationCast: false}) { | 1084 {DartType from, bool isDeclarationCast: false}) { |
| 1104 from ??= _getDefiniteType(expr); | 1085 from ??= _getDefiniteType(expr); |
| 1105 | 1086 |
| 1106 if (!_checkNonNullAssignment(expr, to, from)) return false; | 1087 if (!_checkNonNullAssignment(expr, to, from)) return false; |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1236 var severity = | 1217 var severity = |
| 1237 (processor != null) ? processor.severity : errorCode.errorSeverity; | 1218 (processor != null) ? processor.severity : errorCode.errorSeverity; |
| 1238 | 1219 |
| 1239 if (severity == ErrorSeverity.ERROR) { | 1220 if (severity == ErrorSeverity.ERROR) { |
| 1240 _failure = true; | 1221 _failure = true; |
| 1241 } | 1222 } |
| 1242 if (errorCode.type == ErrorType.HINT && | 1223 if (errorCode.type == ErrorType.HINT && |
| 1243 errorCode.name.startsWith('STRONG_MODE_TOP_LEVEL_')) { | 1224 errorCode.name.startsWith('STRONG_MODE_TOP_LEVEL_')) { |
| 1244 severity = ErrorSeverity.ERROR; | 1225 severity = ErrorSeverity.ERROR; |
| 1245 } | 1226 } |
| 1246 if (severity != ErrorSeverity.INFO || | 1227 if (severity != ErrorSeverity.INFO || _options.strongModeHints) { |
| 1247 _options.strongModeHints || | |
| 1248 errorCode == HintCode.USES_DYNAMIC_AS_BOTTOM) { | |
| 1249 int begin = node is AnnotatedNode | 1228 int begin = node is AnnotatedNode |
| 1250 ? node.firstTokenAfterCommentAndMetadata.offset | 1229 ? node.firstTokenAfterCommentAndMetadata.offset |
| 1251 : node.offset; | 1230 : node.offset; |
| 1252 int length = node.end - begin; | 1231 int length = node.end - begin; |
| 1253 var source = resolutionMap | 1232 var source = resolutionMap |
| 1254 .elementDeclaredByCompilationUnit(node.root as CompilationUnit) | 1233 .elementDeclaredByCompilationUnit(node.root as CompilationUnit) |
| 1255 .source; | 1234 .source; |
| 1256 var error = | 1235 var error = |
| 1257 new AnalysisError(source, begin, length, errorCode, arguments); | 1236 new AnalysisError(source, begin, length, errorCode, arguments); |
| 1258 reporter.onError(error); | 1237 reporter.onError(error); |
| (...skipping 743 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2002 } | 1981 } |
| 2003 | 1982 |
| 2004 /// If node is a [ClassDeclaration] returns its members, otherwise if node is | 1983 /// If node is a [ClassDeclaration] returns its members, otherwise if node is |
| 2005 /// a [ClassTypeAlias] this returns an empty list. | 1984 /// a [ClassTypeAlias] this returns an empty list. |
| 2006 WithClause _withClause(Declaration node) { | 1985 WithClause _withClause(Declaration node) { |
| 2007 return node is ClassDeclaration | 1986 return node is ClassDeclaration |
| 2008 ? node.withClause | 1987 ? node.withClause |
| 2009 : (node as ClassTypeAlias).withClause; | 1988 : (node as ClassTypeAlias).withClause; |
| 2010 } | 1989 } |
| 2011 } | 1990 } |
| OLD | NEW |