Index: pkg/compiler/lib/src/dart_types.dart |
diff --git a/pkg/compiler/lib/src/dart_types.dart b/pkg/compiler/lib/src/dart_types.dart |
index 685077d0b6380f328b351f118f8e5157f45b29d2..cc7d9cf45fd41fbdd441ad79e3cc5d769f06046e 100644 |
--- a/pkg/compiler/lib/src/dart_types.dart |
+++ b/pkg/compiler/lib/src/dart_types.dart |
@@ -164,6 +164,14 @@ abstract class DartType { |
type.accept(visitor, argument); |
} |
} |
+ |
+ /// Returns a [DartType] which corresponds to [this] except that any |
+ /// contained [MethodTypeVariableType] is replaced by a [DynamicType]. |
+ /// Temporary: Only used to support '--generic-method-syntax'. |
+ DartType get eraseMethodTypeVariableType => this; |
+ |
+ /// Returns true iff [this] is or contains a [MethodTypeVariableType]. |
+ bool get containsMethodTypeVariableType => false; |
} |
/** |
@@ -234,6 +242,22 @@ class TypeVariableType extends DartType { |
String toString() => name; |
} |
+/// Provides a thin model of method type variables: They are treated as if |
+/// their value were `dynamic` when used in a type annotation, and as a |
+/// malformed type when used in an `as` or `is` expression. |
+class MethodTypeVariableType extends TypeVariableType { |
+ MethodTypeVariableType(TypeVariableElement element) : super(element); |
+ |
+ @override |
+ bool get treatAsDynamic => true; |
+ |
+ @override |
+ DartType get eraseMethodTypeVariableType => const DynamicType(); |
+ |
+ @override |
+ get containsMethodTypeVariableType => true; |
+} |
+ |
/// Internal type representing the result of analyzing a statement. |
class StatementType extends DartType { |
Element get element => null; |
@@ -405,6 +429,12 @@ abstract class GenericType extends DartType { |
return sb.toString(); |
} |
+ @override |
+ bool get containsMethodTypeVariableType { |
+ return typeArguments.any( |
+ (DartType type) => type.containsMethodTypeVariableType); |
+ } |
+ |
int get hashCode { |
int hash = element.hashCode; |
for (DartType argument in typeArguments) { |
@@ -496,6 +526,14 @@ class InterfaceType extends GenericType { |
return member; |
} |
+ @override |
+ DartType get eraseMethodTypeVariableType { |
Johnni Winther
2016/05/17 10:27:05
Move this to GenericType and use [createInstantiat
eernst
2016/05/17 14:48:54
Done.
|
+ if (!containsMethodTypeVariableType) return this; |
+ List<DartType> newTypeArguments = typeArguments.map( |
+ (DartType type) => type.eraseMethodTypeVariableType).toList(); |
+ return new InterfaceType(element, newTypeArguments); |
+ } |
+ |
int get hashCode => _hashCode ??= super.hashCode; |
InterfaceType asRaw() => super.asRaw(); |
@@ -718,6 +756,34 @@ class FunctionType extends DartType { |
int computeArity() => parameterTypes.length; |
+ @override |
+ DartType get eraseMethodTypeVariableType { |
+ if (!containsMethodTypeVariableType) return this; |
+ DartType eraseIt(DartType type) => type.eraseMethodTypeVariableType; |
+ DartType newReturnType = returnType.eraseMethodTypeVariableType; |
+ List<DartType> newParameterTypes = parameterTypes.map(eraseIt).toList(); |
+ List<DartType> newOptionalParameterTypes = |
+ optionalParameterTypes.map(eraseIt).toList(); |
+ List<DartType> newNamedParameterTypes = |
+ namedParameterTypes.map(eraseIt).toList(); |
+ return new FunctionType.internal( |
+ element, |
+ newReturnType, |
+ newParameterTypes, |
+ newOptionalParameterTypes, |
+ namedParameters, |
+ newNamedParameterTypes); |
+ } |
+ |
+ @override |
+ bool get containsMethodTypeVariableType { |
+ bool containsIt(DartType type) => type.containsMethodTypeVariableType; |
+ return returnType.containsMethodTypeVariableType || |
+ parameterTypes.any(containsIt) || |
+ optionalParameterTypes.any(containsIt) || |
+ namedParameterTypes.any(containsIt); |
+ } |
+ |
int get hashCode { |
int hash = 3 * returnType.hashCode; |
for (DartType parameter in parameterTypes) { |
@@ -1347,6 +1413,9 @@ class Types implements DartTypes { |
static ClassElement getClassContext(DartType type) { |
TypeVariableType typeVariable = type.typeVariableOccurrence; |
if (typeVariable == null) return null; |
+ // TODO(eernst): When generic method support is complete enough to include |
+ // a runtime value for method type variables, this may need to be updated. |
+ if (typeVariable.element.typeDeclaration is! ClassElement) return null; |
return typeVariable.element.typeDeclaration; |
} |