| 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 library dev_compiler.src.checker.checker; | 5 library dev_compiler.src.checker.checker; |
| 6 | 6 |
| 7 import 'package:analyzer/analyzer.dart'; | 7 import 'package:analyzer/analyzer.dart'; |
| 8 import 'package:analyzer/src/generated/ast.dart'; | 8 import 'package:analyzer/src/generated/ast.dart'; |
| 9 import 'package:analyzer/src/generated/element.dart'; | 9 import 'package:analyzer/src/generated/element.dart'; |
| 10 import 'package:analyzer/src/generated/scanner.dart' show Token, TokenType; | 10 import 'package:analyzer/src/generated/scanner.dart' show Token, TokenType; |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 // | 288 // |
| 289 // Common pattern 1: Inferable return type (on getters and methods) | 289 // Common pattern 1: Inferable return type (on getters and methods) |
| 290 // class A { | 290 // class A { |
| 291 // int get foo => ...; | 291 // int get foo => ...; |
| 292 // String toString() { ... } | 292 // String toString() { ... } |
| 293 // } | 293 // } |
| 294 // class B extends A { | 294 // class B extends A { |
| 295 // get foo => e; // no type specified. | 295 // get foo => e; // no type specified. |
| 296 // toString() { ... } // no return type specified. | 296 // toString() { ... } // no return type specified. |
| 297 // } | 297 // } |
| 298 if (_isInferableOverride(element, node, subType, baseType)) { | 298 _recordMessage(new InvalidMethodOverride( |
| 299 _recordMessage(new InferableOverride(errorLocation, element, type, | 299 errorLocation, element, type, subType, baseType)); |
| 300 subType.returnType, baseType.returnType)); | |
| 301 } else { | |
| 302 _recordMessage(new InvalidMethodOverride( | |
| 303 errorLocation, element, type, subType, baseType)); | |
| 304 } | |
| 305 } | 300 } |
| 306 return true; | 301 return true; |
| 307 } | 302 } |
| 308 | 303 |
| 309 bool _isInferableOverride(ExecutableElement element, AstNode node, | |
| 310 FunctionType subType, FunctionType baseType) { | |
| 311 if (_inferFromOverrides || node == null) return false; | |
| 312 final isGetter = element is PropertyAccessorElement && element.isGetter; | |
| 313 if (isGetter && element.isSynthetic) { | |
| 314 var field = node.parent.parent; | |
| 315 return field is FieldDeclaration && field.fields.type == null; | |
| 316 } | |
| 317 | |
| 318 // node is a MethodDeclaration whenever getters and setters are | |
| 319 // declared explicitly. Setters declared from a field will have the | |
| 320 // correct return type, so we don't need to check that separately. | |
| 321 return node is MethodDeclaration && | |
| 322 node.returnType == null && | |
| 323 _rules.isFunctionSubTypeOf(subType, baseType, ignoreReturn: true); | |
| 324 } | |
| 325 | |
| 326 void _recordMessage(StaticInfo info) { | 304 void _recordMessage(StaticInfo info) { |
| 327 if (info == null) return; | 305 if (info == null) return; |
| 328 var error = info.toAnalysisError(); | 306 var error = info.toAnalysisError(); |
| 329 if (error.errorCode.errorSeverity == ErrorSeverity.ERROR) _failure = true; | 307 if (error.errorCode.errorSeverity == ErrorSeverity.ERROR) _failure = true; |
| 330 _reporter.onError(error); | 308 _reporter.onError(error); |
| 331 } | 309 } |
| 332 } | 310 } |
| 333 | 311 |
| 334 /// Checks the body of functions and properties. | 312 /// Checks the body of functions and properties. |
| 335 class CodeChecker extends RecursiveAstVisitor { | 313 class CodeChecker extends RecursiveAstVisitor { |
| (...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 940 if (info is CoercionInfo) { | 918 if (info is CoercionInfo) { |
| 941 // TODO(jmesserly): if we're run again on the same AST, we'll produce the | 919 // TODO(jmesserly): if we're run again on the same AST, we'll produce the |
| 942 // same annotations. This should be harmless. This might go away once | 920 // same annotations. This should be harmless. This might go away once |
| 943 // CodeChecker is integrated better with analyzer, as it will know that | 921 // CodeChecker is integrated better with analyzer, as it will know that |
| 944 // checking has already been performed. | 922 // checking has already been performed. |
| 945 // assert(CoercionInfo.get(info.node) == null); | 923 // assert(CoercionInfo.get(info.node) == null); |
| 946 CoercionInfo.set(info.node, info); | 924 CoercionInfo.set(info.node, info); |
| 947 } | 925 } |
| 948 } | 926 } |
| 949 } | 927 } |
| OLD | NEW |