| 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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 122 |
| 123 // Factory to create correct DownCast variant. | 123 // Factory to create correct DownCast variant. |
| 124 static StaticInfo create(TypeRules rules, Expression expression, Cast cast, | 124 static StaticInfo create(TypeRules rules, Expression expression, Cast cast, |
| 125 {String reason}) { | 125 {String reason}) { |
| 126 final fromT = cast.fromType; | 126 final fromT = cast.fromType; |
| 127 final toT = cast.toType; | 127 final toT = cast.toType; |
| 128 | 128 |
| 129 // toT <:_R fromT => to <: fromT | 129 // toT <:_R fromT => to <: fromT |
| 130 // NB: classes with call methods are subtypes of function | 130 // NB: classes with call methods are subtypes of function |
| 131 // types, but the function type is not assignable to the class | 131 // types, but the function type is not assignable to the class |
| 132 assert(toT.isSubtypeOf(fromT) || fromT.isAssignableTo(toT)); | |
| 133 | 132 |
| 134 // Handle null call specially. | 133 // Handle null call specially. |
| 135 if (expression is NullLiteral) { | 134 if (expression is NullLiteral) { |
| 136 // TODO(vsm): Create a NullCast for this once we revisit nonnullability. | 135 // TODO(vsm): Create a NullCast for this once we revisit nonnullability. |
| 137 return new DownCastImplicit(rules, expression, cast); | 136 return new DownCastImplicit(rules, expression, cast); |
| 138 } | 137 } |
| 139 | 138 |
| 140 // Inference "casts": | 139 // Inference "casts": |
| 141 if (expression is Literal) { | 140 if (expression is Literal) { |
| 142 // fromT should be an exact type - this will almost certainly fail at | 141 // fromT should be an exact type - this will almost certainly fail at |
| 143 // runtime. | 142 // runtime. |
| 144 return new StaticTypeError(rules, expression, toT, reason: reason); | 143 return new StaticTypeError(rules, expression, toT, reason: reason); |
| 145 } | 144 } |
| 146 if (expression is FunctionExpression) { | 145 if (expression is FunctionExpression) { |
| 147 // fromT should be an exact type - this will almost certainly fail at | 146 // fromT should be an exact type - this will almost certainly fail at |
| 148 // runtime. | 147 // runtime. |
| 149 return new UninferredClosure(rules, expression, cast); | 148 return new UninferredClosure(rules, expression, cast); |
| 150 } | 149 } |
| 151 if (expression is InstanceCreationExpression) { | 150 if (expression is InstanceCreationExpression) { |
| 152 // fromT should be an exact type - this will almost certainly fail at | 151 // fromT should be an exact type - this will almost certainly fail at |
| 153 // runtime. | 152 // runtime. |
| 154 return new StaticTypeError(rules, expression, toT, reason: reason); | 153 return new StaticTypeError(rules, expression, toT, reason: reason); |
| 155 } | 154 } |
| 156 | 155 |
| 156 // TODO(vsm): Change this to an assert when we have generic methods and |
| 157 // fix TypeRules._coerceTo to disallow implicit sideways casts. |
| 158 if (!rules.isSubTypeOf(toT, fromT)) { |
| 159 assert(toT.isSubtypeOf(fromT) || fromT.isAssignableTo(toT)); |
| 160 return new DownCastComposite(rules, expression, cast); |
| 161 } |
| 162 |
| 157 // Composite cast: these are more likely to fail. | 163 // Composite cast: these are more likely to fail. |
| 158 if (!rules.isGroundType(toT)) { | 164 if (!rules.isGroundType(toT)) { |
| 159 // This cast is (probably) due to our different treatment of dynamic. | 165 // This cast is (probably) due to our different treatment of dynamic. |
| 160 // It may be more likely to fail at runtime. | 166 // It may be more likely to fail at runtime. |
| 161 return new DownCastComposite(rules, expression, cast); | 167 if (fromT is InterfaceType) { |
| 168 // For class types, we'd like to allow non-generic down casts, e.g., |
| 169 // Iterable<T> to List<T>. The intuition here is that raw (generic) |
| 170 // casts are problematic, and we should complain about those. |
| 171 var typeArgs = fromT.typeArguments; |
| 172 if (typeArgs.isEmpty || typeArgs.any((t) => t.isDynamic)) { |
| 173 return new DownCastComposite(rules, expression, cast); |
| 174 } |
| 175 } else { |
| 176 return new DownCastComposite(rules, expression, cast); |
| 177 } |
| 162 } | 178 } |
| 163 | 179 |
| 164 // Dynamic cast | 180 // Dynamic cast |
| 165 if (fromT.isDynamic) { | 181 if (fromT.isDynamic) { |
| 166 return new DynamicCast(rules, expression, cast); | 182 return new DynamicCast(rules, expression, cast); |
| 167 } | 183 } |
| 168 | 184 |
| 169 // Assignment cast | 185 // Assignment cast |
| 170 var parent = expression.parent; | 186 var parent = expression.parent; |
| 171 if (parent is VariableDeclaration && (parent.initializer == expression)) { | 187 if (parent is VariableDeclaration && (parent.initializer == expression)) { |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 /// Better to have `super` at the end, as required by the Dart style guide: | 479 /// Better to have `super` at the end, as required by the Dart style guide: |
| 464 /// <http://goo.gl/q1T4BB> | 480 /// <http://goo.gl/q1T4BB> |
| 465 /// | 481 /// |
| 466 /// For now this is the only pattern we support. | 482 /// For now this is the only pattern we support. |
| 467 class InvalidSuperInvocation extends StaticError { | 483 class InvalidSuperInvocation extends StaticError { |
| 468 InvalidSuperInvocation(SuperConstructorInvocation node) : super(node); | 484 InvalidSuperInvocation(SuperConstructorInvocation node) : super(node); |
| 469 | 485 |
| 470 @override String get message => "super call must be last in an initializer " | 486 @override String get message => "super call must be last in an initializer " |
| 471 "list (see http://goo.gl/q1T4BB): {0}"; | 487 "list (see http://goo.gl/q1T4BB): {0}"; |
| 472 } | 488 } |
| OLD | NEW |