| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 97 |
| 98 /// Checks the body of functions and properties. | 98 /// Checks the body of functions and properties. |
| 99 class CodeChecker extends RecursiveAstVisitor { | 99 class CodeChecker extends RecursiveAstVisitor { |
| 100 final StrongTypeSystemImpl rules; | 100 final StrongTypeSystemImpl rules; |
| 101 final TypeProvider typeProvider; | 101 final TypeProvider typeProvider; |
| 102 final AnalysisErrorListener reporter; | 102 final AnalysisErrorListener reporter; |
| 103 final _OverrideChecker _overrideChecker; | 103 final _OverrideChecker _overrideChecker; |
| 104 final bool _hints; | 104 final bool _hints; |
| 105 | 105 |
| 106 bool _failure = false; | 106 bool _failure = false; |
| 107 CodeChecker(this.typeProvider, StrongTypeSystemImpl rules, | 107 CodeChecker(TypeProvider typeProvider, StrongTypeSystemImpl rules, |
| 108 AnalysisErrorListener reporter, | 108 AnalysisErrorListener reporter, |
| 109 {bool hints: false}) | 109 {bool hints: false}) |
| 110 : rules = rules, | 110 : typeProvider = typeProvider, |
| 111 rules = rules, |
| 111 reporter = reporter, | 112 reporter = reporter, |
| 112 _hints = hints, | 113 _hints = hints, |
| 113 _overrideChecker = new _OverrideChecker(rules, reporter); | 114 _overrideChecker = new _OverrideChecker(typeProvider, rules, reporter); |
| 114 | 115 |
| 115 bool get failure => _failure || _overrideChecker._failure; | 116 bool get failure => _failure || _overrideChecker._failure; |
| 116 | 117 |
| 117 void checkArgument(Expression arg, DartType expectedType) { | 118 void checkArgument(Expression arg, DartType expectedType) { |
| 118 // Preserve named argument structure, so their immediate parent is the | 119 // Preserve named argument structure, so their immediate parent is the |
| 119 // method invocation. | 120 // method invocation. |
| 120 Expression baseExpression = arg is NamedExpression ? arg.expression : arg; | 121 Expression baseExpression = arg is NamedExpression ? arg.expression : arg; |
| 121 checkAssignment(baseExpression, expectedType); | 122 checkAssignment(baseExpression, expectedType); |
| 122 } | 123 } |
| 123 | 124 |
| (...skipping 825 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 949 return normalReturnType; | 950 return normalReturnType; |
| 950 } | 951 } |
| 951 } | 952 } |
| 952 | 953 |
| 953 /// Checks for overriding declarations of fields and methods. This is used to | 954 /// Checks for overriding declarations of fields and methods. This is used to |
| 954 /// check overrides between classes and superclasses, interfaces, and mixin | 955 /// check overrides between classes and superclasses, interfaces, and mixin |
| 955 /// applications. | 956 /// applications. |
| 956 class _OverrideChecker { | 957 class _OverrideChecker { |
| 957 bool _failure = false; | 958 bool _failure = false; |
| 958 final StrongTypeSystemImpl rules; | 959 final StrongTypeSystemImpl rules; |
| 960 final TypeProvider _typeProvider; |
| 959 final AnalysisErrorListener _reporter; | 961 final AnalysisErrorListener _reporter; |
| 960 | 962 |
| 961 _OverrideChecker(this.rules, this._reporter); | 963 _OverrideChecker(this._typeProvider, this.rules, this._reporter); |
| 962 | 964 |
| 963 void check(ClassDeclaration node) { | 965 void check(ClassDeclaration node) { |
| 964 if (node.element.type.isObject) return; | 966 if (node.element.type.isObject) return; |
| 965 _checkSuperOverrides(node); | 967 _checkSuperOverrides(node); |
| 966 _checkMixinApplicationOverrides(node); | 968 _checkMixinApplicationOverrides(node); |
| 967 _checkAllInterfaceOverrides(node); | 969 _checkAllInterfaceOverrides(node); |
| 968 } | 970 } |
| 969 | 971 |
| 970 /// Checks that implementations correctly override all reachable interfaces. | 972 /// Checks that implementations correctly override all reachable interfaces. |
| 971 /// In particular, we need to check these overrides for the definitions in | 973 /// In particular, we need to check these overrides for the definitions in |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1218 | 1220 |
| 1219 if (isSubclass && element is PropertyAccessorElement) { | 1221 if (isSubclass && element is PropertyAccessorElement) { |
| 1220 // Disallow any overriding if the base class defines this member | 1222 // Disallow any overriding if the base class defines this member |
| 1221 // as a field. We effectively treat fields as final / non-virtual. | 1223 // as a field. We effectively treat fields as final / non-virtual. |
| 1222 PropertyInducingElement field = _getMemberField(type, element); | 1224 PropertyInducingElement field = _getMemberField(type, element); |
| 1223 if (field != null) { | 1225 if (field != null) { |
| 1224 _recordMessage(new InvalidFieldOverride( | 1226 _recordMessage(new InvalidFieldOverride( |
| 1225 errorLocation, element, type, subType, baseType)); | 1227 errorLocation, element, type, subType, baseType)); |
| 1226 } | 1228 } |
| 1227 } | 1229 } |
| 1228 if (!rules.isSubtypeOf(subType, baseType)) { | 1230 FunctionType concreteSubType = subType; |
| 1231 FunctionType concreteBaseType = baseType; |
| 1232 if (element is MethodElement) { |
| 1233 if (concreteSubType.typeFormals.isNotEmpty) { |
| 1234 if (concreteBaseType.typeFormals.isEmpty) { |
| 1235 concreteSubType = rules.instantiateToBounds(concreteSubType); |
| 1236 } |
| 1237 } |
| 1238 concreteSubType = |
| 1239 rules.typeToConcreteType(_typeProvider, concreteSubType); |
| 1240 concreteBaseType = |
| 1241 rules.typeToConcreteType(_typeProvider, concreteBaseType); |
| 1242 } |
| 1243 if (!rules.isSubtypeOf(concreteSubType, concreteBaseType)) { |
| 1229 // See whether non-subtype cases fit one of our common patterns: | 1244 // See whether non-subtype cases fit one of our common patterns: |
| 1230 // | 1245 // |
| 1231 // Common pattern 1: Inferable return type (on getters and methods) | 1246 // Common pattern 1: Inferable return type (on getters and methods) |
| 1232 // class A { | 1247 // class A { |
| 1233 // int get foo => ...; | 1248 // int get foo => ...; |
| 1234 // String toString() { ... } | 1249 // String toString() { ... } |
| 1235 // } | 1250 // } |
| 1236 // class B extends A { | 1251 // class B extends A { |
| 1237 // get foo => e; // no type specified. | 1252 // get foo => e; // no type specified. |
| 1238 // toString() { ... } // no return type specified. | 1253 // toString() { ... } // no return type specified. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1278 } while (!current.isObject && !visited.contains(current)); | 1293 } while (!current.isObject && !visited.contains(current)); |
| 1279 } | 1294 } |
| 1280 | 1295 |
| 1281 void _recordMessage(StaticInfo info) { | 1296 void _recordMessage(StaticInfo info) { |
| 1282 if (info == null) return; | 1297 if (info == null) return; |
| 1283 var error = info.toAnalysisError(); | 1298 var error = info.toAnalysisError(); |
| 1284 if (error.errorCode.errorSeverity == ErrorSeverity.ERROR) _failure = true; | 1299 if (error.errorCode.errorSeverity == ErrorSeverity.ERROR) _failure = true; |
| 1285 _reporter.onError(error); | 1300 _reporter.onError(error); |
| 1286 } | 1301 } |
| 1287 } | 1302 } |
| OLD | NEW |