Use enum as type tags instead of integer constants.
This is done to enable exhaustiveness in switch statements using the
tags, as they are required to cover all enum values, unlike ints.
Additionally, enum values better demonstrate intent.
Change-Id: I8d6de7a12fce059069cf74fe11c381cb70beb8a7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/437580
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
diff --git a/pkg/analyzer/lib/src/dart/analysis/driver.dart b/pkg/analyzer/lib/src/dart/analysis/driver.dart
index c6af85f..ac68e8e 100644
--- a/pkg/analyzer/lib/src/dart/analysis/driver.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/driver.dart
@@ -110,7 +110,7 @@
// TODO(scheglov): Clean up the list of implicitly analyzed files.
class AnalysisDriver {
/// The version of data format, should be incremented on every format change.
- static const int DATA_VERSION = 482;
+ static const int DATA_VERSION = 483;
/// The number of exception contexts allowed to write. Once this field is
/// zero, we stop writing any new exception contexts in this process.
diff --git a/pkg/analyzer/lib/src/summary2/ast_binary_tag.dart b/pkg/analyzer/lib/src/summary2/ast_binary_tag.dart
index 77028a4..0d90ee3 100644
--- a/pkg/analyzer/lib/src/summary2/ast_binary_tag.dart
+++ b/pkg/analyzer/lib/src/summary2/ast_binary_tag.dart
@@ -129,18 +129,6 @@
static const int ParameterKindOptionalPositional = 2;
static const int ParameterKindRequiredNamed = 3;
static const int ParameterKindOptionalNamed = 4;
-
- static const int NullType = 2;
- static const int DynamicType = 3;
- static const int FunctionType = 4;
- static const int InvalidType = 5;
- static const int NeverType = 6;
- static const int InterfaceType = 7;
- static const int InterfaceType_noTypeArguments_none = 8;
- static const int InterfaceType_noTypeArguments_question = 9;
- static const int RecordType = 10;
- static const int TypeParameterType = 11;
- static const int VoidType = 12;
}
enum TypeParameterVarianceTag {
@@ -150,3 +138,17 @@
contravariant,
invariant,
}
+
+enum TypeTag {
+ NullType,
+ DynamicType,
+ FunctionType,
+ InvalidType,
+ NeverType,
+ InterfaceType,
+ InterfaceType_noTypeArguments_none,
+ InterfaceType_noTypeArguments_question,
+ RecordType,
+ TypeParameterType,
+ VoidType,
+}
diff --git a/pkg/analyzer/lib/src/summary2/bundle_reader.dart b/pkg/analyzer/lib/src/summary2/bundle_reader.dart
index f5997b4..4c15f88 100644
--- a/pkg/analyzer/lib/src/summary2/bundle_reader.dart
+++ b/pkg/analyzer/lib/src/summary2/bundle_reader.dart
@@ -1631,58 +1631,57 @@
}
TypeImpl? readType() {
- var tag = _reader.readByte();
- if (tag == Tag.NullType) {
- return null;
- } else if (tag == Tag.DynamicType) {
- var type = DynamicTypeImpl.instance;
- return _readAliasElementArguments(type);
- } else if (tag == Tag.FunctionType) {
- var type = _readFunctionType();
- return _readAliasElementArguments(type);
- } else if (tag == Tag.InterfaceType) {
- var element = readElement() as InterfaceElementImpl;
- var typeArguments = _readTypeList();
- var nullability = _readNullability();
- var type = element.instantiateImpl(
- typeArguments: typeArguments,
- nullabilitySuffix: nullability,
- );
- return _readAliasElementArguments(type);
- } else if (tag == Tag.InterfaceType_noTypeArguments_none) {
- var element = readElement() as InterfaceElementImpl;
- var type = element.instantiateImpl(
- typeArguments: const [],
- nullabilitySuffix: NullabilitySuffix.none,
- );
- return _readAliasElementArguments(type);
- } else if (tag == Tag.InterfaceType_noTypeArguments_question) {
- var element = readElement() as InterfaceElementImpl;
- var type = element.instantiateImpl(
- typeArguments: const [],
- nullabilitySuffix: NullabilitySuffix.question,
- );
- return _readAliasElementArguments(type);
- } else if (tag == Tag.InvalidType) {
- var type = InvalidTypeImpl.instance;
- return _readAliasElementArguments(type);
- } else if (tag == Tag.NeverType) {
- var nullability = _readNullability();
- var type = NeverTypeImpl.instance.withNullability(nullability);
- return _readAliasElementArguments(type);
- } else if (tag == Tag.RecordType) {
- var type = _readRecordType();
- return _readAliasElementArguments(type);
- } else if (tag == Tag.TypeParameterType) {
- var element = readElement() as TypeParameterElementImpl;
- var nullability = _readNullability();
- var type = element.instantiate(nullabilitySuffix: nullability);
- return _readAliasElementArguments(type);
- } else if (tag == Tag.VoidType) {
- var type = VoidTypeImpl.instance;
- return _readAliasElementArguments(type);
- } else {
- throw UnimplementedError('$tag');
+ var tag = readEnum(TypeTag.values);
+ switch (tag) {
+ case TypeTag.NullType:
+ return null;
+ case TypeTag.DynamicType:
+ var type = DynamicTypeImpl.instance;
+ return _readAliasElementArguments(type);
+ case TypeTag.FunctionType:
+ var type = _readFunctionType();
+ return _readAliasElementArguments(type);
+ case TypeTag.InterfaceType:
+ var element = readElement() as InterfaceElementImpl;
+ var typeArguments = _readTypeList();
+ var nullability = _readNullability();
+ var type = element.instantiateImpl(
+ typeArguments: typeArguments,
+ nullabilitySuffix: nullability,
+ );
+ return _readAliasElementArguments(type);
+ case TypeTag.InterfaceType_noTypeArguments_none:
+ var element = readElement() as InterfaceElementImpl;
+ var type = element.instantiateImpl(
+ typeArguments: const [],
+ nullabilitySuffix: NullabilitySuffix.none,
+ );
+ return _readAliasElementArguments(type);
+ case TypeTag.InterfaceType_noTypeArguments_question:
+ var element = readElement() as InterfaceElementImpl;
+ var type = element.instantiateImpl(
+ typeArguments: const [],
+ nullabilitySuffix: NullabilitySuffix.question,
+ );
+ return _readAliasElementArguments(type);
+ case TypeTag.InvalidType:
+ var type = InvalidTypeImpl.instance;
+ return _readAliasElementArguments(type);
+ case TypeTag.NeverType:
+ var nullability = _readNullability();
+ var type = NeverTypeImpl.instance.withNullability(nullability);
+ return _readAliasElementArguments(type);
+ case TypeTag.RecordType:
+ var type = _readRecordType();
+ return _readAliasElementArguments(type);
+ case TypeTag.TypeParameterType:
+ var element = readElement() as TypeParameterElementImpl;
+ var nullability = _readNullability();
+ var type = element.instantiate(nullabilitySuffix: nullability);
+ return _readAliasElementArguments(type);
+ case TypeTag.VoidType:
+ var type = VoidTypeImpl.instance;
+ return _readAliasElementArguments(type);
}
}
diff --git a/pkg/analyzer/lib/src/summary2/bundle_writer.dart b/pkg/analyzer/lib/src/summary2/bundle_writer.dart
index bffcfb6..145df80 100644
--- a/pkg/analyzer/lib/src/summary2/bundle_writer.dart
+++ b/pkg/analyzer/lib/src/summary2/bundle_writer.dart
@@ -922,9 +922,9 @@
void writeType(DartType? type) {
if (type == null) {
- writeByte(Tag.NullType);
+ writeEnum(TypeTag.NullType);
} else if (type is DynamicTypeImpl) {
- writeByte(Tag.DynamicType);
+ writeEnum(TypeTag.DynamicType);
_writeTypeAliasElementArguments(type);
} else if (type is FunctionTypeImpl) {
_writeFunctionType(type);
@@ -934,14 +934,14 @@
var nullabilitySuffix = type.nullabilitySuffix;
if (typeArguments.isEmpty) {
if (nullabilitySuffix == NullabilitySuffix.none) {
- writeByte(Tag.InterfaceType_noTypeArguments_none);
+ writeEnum(TypeTag.InterfaceType_noTypeArguments_none);
} else if (nullabilitySuffix == NullabilitySuffix.question) {
- writeByte(Tag.InterfaceType_noTypeArguments_question);
+ writeEnum(TypeTag.InterfaceType_noTypeArguments_question);
}
// TODO(scheglov): Write raw
writeElement(type.element);
} else {
- writeByte(Tag.InterfaceType);
+ writeEnum(TypeTag.InterfaceType);
// TODO(scheglov): Write raw
writeElement(type.element);
writeUInt30(typeArguments.length);
@@ -952,22 +952,22 @@
}
_writeTypeAliasElementArguments(type);
} else if (type is InvalidTypeImpl) {
- writeByte(Tag.InvalidType);
+ writeEnum(TypeTag.InvalidType);
_writeTypeAliasElementArguments(type);
} else if (type is NeverTypeImpl) {
- writeByte(Tag.NeverType);
+ writeEnum(TypeTag.NeverType);
_writeNullabilitySuffix(type.nullabilitySuffix);
_writeTypeAliasElementArguments(type);
} else if (type is RecordTypeImpl) {
_writeRecordType(type);
_writeTypeAliasElementArguments(type);
} else if (type is TypeParameterTypeImpl) {
- writeByte(Tag.TypeParameterType);
+ writeEnum(TypeTag.TypeParameterType);
writeElement(type.element);
_writeNullabilitySuffix(type.nullabilitySuffix);
_writeTypeAliasElementArguments(type);
} else if (type is VoidTypeImpl) {
- writeByte(Tag.VoidType);
+ writeEnum(TypeTag.VoidType);
_writeTypeAliasElementArguments(type);
} else {
throw UnimplementedError('${type.runtimeType}');
@@ -1026,7 +1026,7 @@
void _writeFunctionType(FunctionTypeImpl type) {
type = _toSyntheticFunctionType(type);
- writeByte(Tag.FunctionType);
+ writeEnum(TypeTag.FunctionType);
_writeTypeParameters(type.typeFormals, () {
writeType(type.returnType);
@@ -1070,7 +1070,7 @@
}
void _writeRecordType(RecordTypeImpl type) {
- writeByte(Tag.RecordType);
+ writeEnum(TypeTag.RecordType);
writeList<RecordTypePositionalField>(type.positionalFields, (field) {
writeType(field.type);