Index: lib/src/codegen/reify_coercions.dart |
diff --git a/lib/src/codegen/reify_coercions.dart b/lib/src/codegen/reify_coercions.dart |
index 7e91a9cefe8df28e261e903ce92496bc62c5f568..36c965e3fbd46383b701590f411c41eb3a075a78 100644 |
--- a/lib/src/codegen/reify_coercions.dart |
+++ b/lib/src/codegen/reify_coercions.dart |
@@ -7,7 +7,8 @@ library dev_compiler.src.codegen.reify_coercions; |
import 'package:analyzer/analyzer.dart' as analyzer; |
import 'package:analyzer/src/generated/ast.dart'; |
import 'package:analyzer/src/generated/element.dart'; |
-import 'package:analyzer/src/task/strong/rules.dart'; |
+import 'package:analyzer/src/generated/type_system.dart' |
+ show StrongTypeSystemImpl; |
import 'package:logging/logging.dart' as logger; |
import '../info.dart'; |
@@ -38,55 +39,6 @@ class NewTypeIdDesc { |
NewTypeIdDesc({this.fromCurrent, this.importedFrom, this.synthetic}); |
} |
-class _Inference extends DownwardsInference { |
- TypeManager _tm; |
- |
- _Inference(TypeRules rules, this._tm) : super(rules); |
- |
- @override |
- void annotateCastFromDynamic(Expression e, DartType t) { |
- var cast = Coercion.cast(e.staticType, t); |
- var info = new DynamicCast(rules, e, cast); |
- CoercionInfo.set(e, info); |
- } |
- |
- @override |
- void annotateListLiteral(ListLiteral e, List<DartType> targs) { |
- var tNames = targs.map(_tm.typeNameFromDartType).toList(); |
- e.typeArguments = AstBuilder.typeArgumentList(tNames); |
- var listT = rules.provider.listType.substitute4(targs); |
- e.staticType = listT; |
- } |
- |
- @override |
- void annotateMapLiteral(MapLiteral e, List<DartType> targs) { |
- var tNames = targs.map(_tm.typeNameFromDartType).toList(); |
- e.typeArguments = AstBuilder.typeArgumentList(tNames); |
- var mapT = rules.provider.mapType.substitute4(targs); |
- e.staticType = mapT; |
- } |
- |
- @override |
- void annotateInstanceCreationExpression( |
- InstanceCreationExpression e, List<DartType> targs) { |
- var tNames = targs.map(_tm.typeNameFromDartType).toList(); |
- var cName = e.constructorName; |
- var id = cName.type.name; |
- var typeName = AstBuilder.typeName(id, tNames); |
- cName.type = typeName; |
- var newType = |
- (e.staticType.element as ClassElement).type.substitute4(targs); |
- e.staticType = newType; |
- typeName.type = newType; |
- } |
- |
- @override |
- void annotateFunctionExpression(FunctionExpression e, DartType returnType) { |
- // Implicitly changes e.staticType |
- (e.element as ExecutableElementImpl).returnType = returnType; |
- } |
-} |
- |
// This class implements a pass which modifies (in place) the ast replacing |
// abstract coercion nodes with their dart implementations. |
class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> { |
@@ -94,17 +46,17 @@ class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> { |
final TypeManager _tm; |
final VariableManager _vm; |
final LibraryUnit _library; |
- final _Inference _inferrer; |
+ final StrongTypeSystemImpl _typeSystem; |
CoercionReifier._( |
- this._cm, this._tm, this._vm, this._library, this._inferrer); |
+ this._cm, this._tm, this._vm, this._library, this._typeSystem); |
- factory CoercionReifier(LibraryUnit library, TypeRules rules) { |
+ factory CoercionReifier( |
+ LibraryUnit library, StrongTypeSystemImpl typeSystem) { |
var vm = new VariableManager(); |
var tm = new TypeManager(library.library.element.enclosingElement, vm); |
var cm = new CoercionManager(vm, tm); |
- var inferrer = new _Inference(rules, tm); |
- return new CoercionReifier._(cm, tm, vm, library, inferrer); |
+ return new CoercionReifier._(cm, tm, vm, library, typeSystem); |
} |
// This should be the entry point for this class. Entering via the |
@@ -130,8 +82,14 @@ class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> { |
///////////////// Private ////////////////////////////////// |
Object _visitInferredTypeBase(InferredTypeBase node, Expression expr) { |
- var success = _inferrer.inferExpression(expr, node.type, <String>[]); |
- assert(success); |
+ DartType t = node.type; |
+ if (!_typeSystem.isSubtypeOf(_getStaticType(expr), t)) { |
+ if (_getStaticType(expr).isDynamic) { |
+ var cast = Coercion.cast(expr.staticType, t); |
+ var info = new DynamicCast(_typeSystem, expr, cast); |
+ CoercionInfo.set(expr, info); |
+ } |
+ } |
expr.visitChildren(this); |
return null; |
} |
@@ -155,6 +113,10 @@ class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> { |
_cm.exitCompilationUnit(unit); |
return ret; |
} |
+ |
+ DartType _getStaticType(Expression expr) { |
+ return expr.staticType ?? DynamicTypeImpl.instance; |
+ } |
} |
// This provides a placeholder variable manager. Currently it simply |