| 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 /// Defines static information collected by the type checker and used later by | 5 /// Defines static information collected by the type checker and used later by |
| 6 /// emitters to generate code. | 6 /// emitters to generate code. |
| 7 // TODO(jmesserly): this was ported from package:dev_compiler, and needs to be | 7 // TODO(jmesserly): this was ported from package:dev_compiler, and needs to be |
| 8 // refactored to fit into analyzer. | 8 // refactored to fit into analyzer. |
| 9 library analyzer.src.task.strong.info; | 9 library analyzer.src.task.strong.info; |
| 10 | 10 |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 | 131 |
| 132 if (expression is InstanceCreationExpression) { | 132 if (expression is InstanceCreationExpression) { |
| 133 ConstructorElement e = expression.staticElement; | 133 ConstructorElement e = expression.staticElement; |
| 134 if (e == null || !e.isFactory) { | 134 if (e == null || !e.isFactory) { |
| 135 // fromT should be an exact type - this will almost certainly fail at | 135 // fromT should be an exact type - this will almost certainly fail at |
| 136 // runtime. | 136 // runtime. |
| 137 return new StaticTypeError(rules, expression, toT); | 137 return new StaticTypeError(rules, expression, toT); |
| 138 } | 138 } |
| 139 } | 139 } |
| 140 | 140 |
| 141 Element element = null; | 141 if (StaticInfo.isKnownFunction(expression)) { |
| 142 if (expression is PropertyAccess) { | |
| 143 element = expression.propertyName.staticElement; | |
| 144 } else if (expression is Identifier) { | |
| 145 element = expression.staticElement; | |
| 146 } | |
| 147 // First class functions and static methods, where we know the original | |
| 148 // declaration, will have an exact type, so we know a downcast will fail. | |
| 149 if (element is FunctionElement || | |
| 150 element is MethodElement && element.isStatic) { | |
| 151 return new StaticTypeError(rules, expression, toT); | 142 return new StaticTypeError(rules, expression, toT); |
| 152 } | 143 } |
| 153 | 144 |
| 154 // TODO(vsm): Change this to an assert when we have generic methods and | 145 // TODO(vsm): Change this to an assert when we have generic methods and |
| 155 // fix TypeRules._coerceTo to disallow implicit sideways casts. | 146 // fix TypeRules._coerceTo to disallow implicit sideways casts. |
| 156 if (!rules.isSubtypeOf(toT, fromT)) { | 147 if (!rules.isSubtypeOf(toT, fromT)) { |
| 157 assert(toT.isSubtypeOf(fromT) || fromT.isAssignableTo(toT)); | 148 assert(toT.isSubtypeOf(fromT) || fromT.isAssignableTo(toT)); |
| 158 return new DownCastComposite(rules, expression, cast); | 149 return new DownCastComposite(rules, expression, cast); |
| 159 } | 150 } |
| 160 | 151 |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 ? (node as AnnotatedNode).firstTokenAfterCommentAndMetadata.offset | 535 ? (node as AnnotatedNode).firstTokenAfterCommentAndMetadata.offset |
| 545 : node.offset; | 536 : node.offset; |
| 546 int length = node.end - begin; | 537 int length = node.end - begin; |
| 547 var source = (node.root as CompilationUnit).element.source; | 538 var source = (node.root as CompilationUnit).element.source; |
| 548 return new AnalysisError(source, begin, length, toErrorCode(), arguments); | 539 return new AnalysisError(source, begin, length, toErrorCode(), arguments); |
| 549 } | 540 } |
| 550 | 541 |
| 551 // TODO(jmesserly): review the usage of error codes. We probably want our own, | 542 // TODO(jmesserly): review the usage of error codes. We probably want our own, |
| 552 // as well as some DDC specific [ErrorType]s. | 543 // as well as some DDC specific [ErrorType]s. |
| 553 ErrorCode toErrorCode(); | 544 ErrorCode toErrorCode(); |
| 545 |
| 546 static bool isKnownFunction(Expression expression) { |
| 547 Element element = null; |
| 548 if (expression is PropertyAccess) { |
| 549 element = expression.propertyName.staticElement; |
| 550 } else if (expression is Identifier) { |
| 551 element = expression.staticElement; |
| 552 } |
| 553 // First class functions and static methods, where we know the original |
| 554 // declaration, will have an exact type, so we know a downcast will fail. |
| 555 return element is FunctionElement || |
| 556 element is MethodElement && element.isStatic; |
| 557 } |
| 554 } | 558 } |
| 555 | 559 |
| 556 class StaticTypeError extends StaticError { | 560 class StaticTypeError extends StaticError { |
| 557 final DartType baseType; | 561 final DartType baseType; |
| 558 final DartType expectedType; | 562 final DartType expectedType; |
| 559 | 563 |
| 560 StaticTypeError(TypeSystem rules, Expression expression, this.expectedType) | 564 StaticTypeError(TypeSystem rules, Expression expression, this.expectedType) |
| 561 : baseType = expression.staticType ?? DynamicTypeImpl.instance, | 565 : baseType = expression.staticType ?? DynamicTypeImpl.instance, |
| 562 super(expression); | 566 super(expression); |
| 563 | 567 |
| 564 @override | 568 @override |
| 565 List<Object> get arguments => [node, baseType, expectedType]; | 569 List<Object> get arguments => [node, baseType, expectedType]; |
| 566 @override | 570 @override |
| 567 String get message => 'Type check failed: {0} ({1}) is not of type {2}'; | 571 String get message => 'Type check failed: {0} ({1}) is not of type {2}'; |
| 568 | 572 |
| 569 @override | 573 @override |
| 570 String get name => 'STRONG_MODE_STATIC_TYPE_ERROR'; | 574 String get name => 'STRONG_MODE_STATIC_TYPE_ERROR'; |
| 571 } | 575 } |
| OLD | NEW |