Skip to content

Commit fba6ec3

Browse files
fshcheglovCommit Queue
authored andcommitted
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 <[email protected]> Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 25e7e41 commit fba6ec3

File tree

4 files changed

+77
-76
lines changed

4 files changed

+77
-76
lines changed

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ testFineAfterLibraryAnalyzerHook;
110110
// TODO(scheglov): Clean up the list of implicitly analyzed files.
111111
class AnalysisDriver {
112112
/// The version of data format, should be incremented on every format change.
113-
static const int DATA_VERSION = 482;
113+
static const int DATA_VERSION = 483;
114114

115115
/// The number of exception contexts allowed to write. Once this field is
116116
/// zero, we stop writing any new exception contexts in this process.

pkg/analyzer/lib/src/summary2/ast_binary_tag.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,6 @@ class Tag {
129129
static const int ParameterKindOptionalPositional = 2;
130130
static const int ParameterKindRequiredNamed = 3;
131131
static const int ParameterKindOptionalNamed = 4;
132-
133-
static const int NullType = 2;
134-
static const int DynamicType = 3;
135-
static const int FunctionType = 4;
136-
static const int InvalidType = 5;
137-
static const int NeverType = 6;
138-
static const int InterfaceType = 7;
139-
static const int InterfaceType_noTypeArguments_none = 8;
140-
static const int InterfaceType_noTypeArguments_question = 9;
141-
static const int RecordType = 10;
142-
static const int TypeParameterType = 11;
143-
static const int VoidType = 12;
144132
}
145133

146134
enum TypeParameterVarianceTag {
@@ -150,3 +138,17 @@ enum TypeParameterVarianceTag {
150138
contravariant,
151139
invariant,
152140
}
141+
142+
enum TypeTag {
143+
NullType,
144+
DynamicType,
145+
FunctionType,
146+
InvalidType,
147+
NeverType,
148+
InterfaceType,
149+
InterfaceType_noTypeArguments_none,
150+
InterfaceType_noTypeArguments_question,
151+
RecordType,
152+
TypeParameterType,
153+
VoidType,
154+
}

pkg/analyzer/lib/src/summary2/bundle_reader.dart

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,58 +1631,57 @@ class ResolutionReader {
16311631
}
16321632

16331633
TypeImpl? readType() {
1634-
var tag = _reader.readByte();
1635-
if (tag == Tag.NullType) {
1636-
return null;
1637-
} else if (tag == Tag.DynamicType) {
1638-
var type = DynamicTypeImpl.instance;
1639-
return _readAliasElementArguments(type);
1640-
} else if (tag == Tag.FunctionType) {
1641-
var type = _readFunctionType();
1642-
return _readAliasElementArguments(type);
1643-
} else if (tag == Tag.InterfaceType) {
1644-
var element = readElement() as InterfaceElementImpl;
1645-
var typeArguments = _readTypeList();
1646-
var nullability = _readNullability();
1647-
var type = element.instantiateImpl(
1648-
typeArguments: typeArguments,
1649-
nullabilitySuffix: nullability,
1650-
);
1651-
return _readAliasElementArguments(type);
1652-
} else if (tag == Tag.InterfaceType_noTypeArguments_none) {
1653-
var element = readElement() as InterfaceElementImpl;
1654-
var type = element.instantiateImpl(
1655-
typeArguments: const [],
1656-
nullabilitySuffix: NullabilitySuffix.none,
1657-
);
1658-
return _readAliasElementArguments(type);
1659-
} else if (tag == Tag.InterfaceType_noTypeArguments_question) {
1660-
var element = readElement() as InterfaceElementImpl;
1661-
var type = element.instantiateImpl(
1662-
typeArguments: const [],
1663-
nullabilitySuffix: NullabilitySuffix.question,
1664-
);
1665-
return _readAliasElementArguments(type);
1666-
} else if (tag == Tag.InvalidType) {
1667-
var type = InvalidTypeImpl.instance;
1668-
return _readAliasElementArguments(type);
1669-
} else if (tag == Tag.NeverType) {
1670-
var nullability = _readNullability();
1671-
var type = NeverTypeImpl.instance.withNullability(nullability);
1672-
return _readAliasElementArguments(type);
1673-
} else if (tag == Tag.RecordType) {
1674-
var type = _readRecordType();
1675-
return _readAliasElementArguments(type);
1676-
} else if (tag == Tag.TypeParameterType) {
1677-
var element = readElement() as TypeParameterElementImpl;
1678-
var nullability = _readNullability();
1679-
var type = element.instantiate(nullabilitySuffix: nullability);
1680-
return _readAliasElementArguments(type);
1681-
} else if (tag == Tag.VoidType) {
1682-
var type = VoidTypeImpl.instance;
1683-
return _readAliasElementArguments(type);
1684-
} else {
1685-
throw UnimplementedError('$tag');
1634+
var tag = readEnum(TypeTag.values);
1635+
switch (tag) {
1636+
case TypeTag.NullType:
1637+
return null;
1638+
case TypeTag.DynamicType:
1639+
var type = DynamicTypeImpl.instance;
1640+
return _readAliasElementArguments(type);
1641+
case TypeTag.FunctionType:
1642+
var type = _readFunctionType();
1643+
return _readAliasElementArguments(type);
1644+
case TypeTag.InterfaceType:
1645+
var element = readElement() as InterfaceElementImpl;
1646+
var typeArguments = _readTypeList();
1647+
var nullability = _readNullability();
1648+
var type = element.instantiateImpl(
1649+
typeArguments: typeArguments,
1650+
nullabilitySuffix: nullability,
1651+
);
1652+
return _readAliasElementArguments(type);
1653+
case TypeTag.InterfaceType_noTypeArguments_none:
1654+
var element = readElement() as InterfaceElementImpl;
1655+
var type = element.instantiateImpl(
1656+
typeArguments: const [],
1657+
nullabilitySuffix: NullabilitySuffix.none,
1658+
);
1659+
return _readAliasElementArguments(type);
1660+
case TypeTag.InterfaceType_noTypeArguments_question:
1661+
var element = readElement() as InterfaceElementImpl;
1662+
var type = element.instantiateImpl(
1663+
typeArguments: const [],
1664+
nullabilitySuffix: NullabilitySuffix.question,
1665+
);
1666+
return _readAliasElementArguments(type);
1667+
case TypeTag.InvalidType:
1668+
var type = InvalidTypeImpl.instance;
1669+
return _readAliasElementArguments(type);
1670+
case TypeTag.NeverType:
1671+
var nullability = _readNullability();
1672+
var type = NeverTypeImpl.instance.withNullability(nullability);
1673+
return _readAliasElementArguments(type);
1674+
case TypeTag.RecordType:
1675+
var type = _readRecordType();
1676+
return _readAliasElementArguments(type);
1677+
case TypeTag.TypeParameterType:
1678+
var element = readElement() as TypeParameterElementImpl;
1679+
var nullability = _readNullability();
1680+
var type = element.instantiate(nullabilitySuffix: nullability);
1681+
return _readAliasElementArguments(type);
1682+
case TypeTag.VoidType:
1683+
var type = VoidTypeImpl.instance;
1684+
return _readAliasElementArguments(type);
16861685
}
16871686
}
16881687

pkg/analyzer/lib/src/summary2/bundle_writer.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -922,9 +922,9 @@ class ResolutionSink extends _SummaryDataWriter {
922922

923923
void writeType(DartType? type) {
924924
if (type == null) {
925-
writeByte(Tag.NullType);
925+
writeEnum(TypeTag.NullType);
926926
} else if (type is DynamicTypeImpl) {
927-
writeByte(Tag.DynamicType);
927+
writeEnum(TypeTag.DynamicType);
928928
_writeTypeAliasElementArguments(type);
929929
} else if (type is FunctionTypeImpl) {
930930
_writeFunctionType(type);
@@ -934,14 +934,14 @@ class ResolutionSink extends _SummaryDataWriter {
934934
var nullabilitySuffix = type.nullabilitySuffix;
935935
if (typeArguments.isEmpty) {
936936
if (nullabilitySuffix == NullabilitySuffix.none) {
937-
writeByte(Tag.InterfaceType_noTypeArguments_none);
937+
writeEnum(TypeTag.InterfaceType_noTypeArguments_none);
938938
} else if (nullabilitySuffix == NullabilitySuffix.question) {
939-
writeByte(Tag.InterfaceType_noTypeArguments_question);
939+
writeEnum(TypeTag.InterfaceType_noTypeArguments_question);
940940
}
941941
// TODO(scheglov): Write raw
942942
writeElement(type.element);
943943
} else {
944-
writeByte(Tag.InterfaceType);
944+
writeEnum(TypeTag.InterfaceType);
945945
// TODO(scheglov): Write raw
946946
writeElement(type.element);
947947
writeUInt30(typeArguments.length);
@@ -952,22 +952,22 @@ class ResolutionSink extends _SummaryDataWriter {
952952
}
953953
_writeTypeAliasElementArguments(type);
954954
} else if (type is InvalidTypeImpl) {
955-
writeByte(Tag.InvalidType);
955+
writeEnum(TypeTag.InvalidType);
956956
_writeTypeAliasElementArguments(type);
957957
} else if (type is NeverTypeImpl) {
958-
writeByte(Tag.NeverType);
958+
writeEnum(TypeTag.NeverType);
959959
_writeNullabilitySuffix(type.nullabilitySuffix);
960960
_writeTypeAliasElementArguments(type);
961961
} else if (type is RecordTypeImpl) {
962962
_writeRecordType(type);
963963
_writeTypeAliasElementArguments(type);
964964
} else if (type is TypeParameterTypeImpl) {
965-
writeByte(Tag.TypeParameterType);
965+
writeEnum(TypeTag.TypeParameterType);
966966
writeElement(type.element);
967967
_writeNullabilitySuffix(type.nullabilitySuffix);
968968
_writeTypeAliasElementArguments(type);
969969
} else if (type is VoidTypeImpl) {
970-
writeByte(Tag.VoidType);
970+
writeEnum(TypeTag.VoidType);
971971
_writeTypeAliasElementArguments(type);
972972
} else {
973973
throw UnimplementedError('${type.runtimeType}');
@@ -1026,7 +1026,7 @@ class ResolutionSink extends _SummaryDataWriter {
10261026
void _writeFunctionType(FunctionTypeImpl type) {
10271027
type = _toSyntheticFunctionType(type);
10281028

1029-
writeByte(Tag.FunctionType);
1029+
writeEnum(TypeTag.FunctionType);
10301030

10311031
_writeTypeParameters(type.typeFormals, () {
10321032
writeType(type.returnType);
@@ -1070,7 +1070,7 @@ class ResolutionSink extends _SummaryDataWriter {
10701070
}
10711071

10721072
void _writeRecordType(RecordTypeImpl type) {
1073-
writeByte(Tag.RecordType);
1073+
writeEnum(TypeTag.RecordType);
10741074

10751075
writeList<RecordTypePositionalField>(type.positionalFields, (field) {
10761076
writeType(field.type);

0 commit comments

Comments
 (0)