Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f3139f5

Browse files
Dmitry Stefantsovcommit-bot@chromium.org
authored andcommitted
[cfe][nnbd] (De)Serialize nullability modifiers in .dill files
Closes #37688. Bug: http://dartbug.com/37688 Change-Id: Ie3aa8fe09d859cd65bbb58da3f56297483b7b725 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112085 Commit-Queue: Dmitry Stefantsov <[email protected]> Reviewed-by: Jens Johansen <[email protected]>
1 parent dce0eeb commit f3139f5

File tree

11 files changed

+94
-21
lines changed

11 files changed

+94
-21
lines changed

pkg/kernel/binary.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ type CanonicalName {
139139

140140
type ComponentFile {
141141
UInt32 magic = 0x90ABCDEF;
142-
UInt32 formatVersion = 27;
142+
UInt32 formatVersion = 28;
143143
List<String> problemsAsJson; // Described in problems.md.
144144
Library[] libraries;
145145
UriSource sourceMap;
@@ -1195,6 +1195,8 @@ type FunctionDeclaration extends Statement {
11951195
FunctionNode function;
11961196
}
11971197

1198+
enum Nullability { nullable = 0, nonNullable = 1, neither = 2, legacy = 3, }
1199+
11981200
abstract type DartType extends Node {}
11991201

12001202
type InvalidType extends DartType {
@@ -1211,18 +1213,21 @@ type VoidType extends DartType {
12111213

12121214
type InterfaceType extends DartType {
12131215
Byte tag = 93;
1216+
Byte nullability; // Index into the Nullability enum above.
12141217
ClassReference class;
12151218
List<DartType> typeArguments;
12161219
}
12171220

12181221
type SimpleInterfaceType extends DartType {
12191222
Byte tag = 96; // Note: tag is out of order.
1223+
Byte nullability; // Index into the Nullability enum above.
12201224
ClassReference class;
12211225
// Equivalent to InterfaceType with empty list of type arguments.
12221226
}
12231227

12241228
type FunctionType extends DartType {
12251229
Byte tag = 94;
1230+
Byte nullability; // Index into the Nullability enum above.
12261231
List<TypeParameter> typeParameters;
12271232
UInt requiredParameterCount;
12281233
// positionalParameters.length + namedParameters.length
@@ -1235,6 +1240,7 @@ type FunctionType extends DartType {
12351240

12361241
type SimpleFunctionType extends DartType {
12371242
Byte tag = 97; // Note: tag is out of order.
1243+
Byte nullability; // Index into the Nullability enum above.
12381244
List<DartType> positionalParameters;
12391245
List<StringReference> positionalParameterNames;
12401246
DartType returnType;
@@ -1249,6 +1255,7 @@ type NamedDartType {
12491255

12501256
type TypeParameterType extends DartType {
12511257
Byte tag = 95;
1258+
Byte nullability; // Index into the Nullability enum above.
12521259

12531260
// Reference to the Nth type parameter in scope (with some caveats about
12541261
// type parameter bounds).
@@ -1273,6 +1280,7 @@ type TypeParameterType extends DartType {
12731280

12741281
type TypedefType {
12751282
Byte tag = 87;
1283+
Byte nullability; // Index into the Nullability enum above.
12761284
TypedefReference typedefReference;
12771285
List<DartType> typeArguments;
12781286
}

pkg/kernel/lib/ast.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4819,12 +4819,6 @@ class _PublicName extends Name {
48194819

48204820
/// Represents nullability of a type.
48214821
enum Nullability {
4822-
/// Types in opt-out libraries are 'legacy' types.
4823-
///
4824-
/// They are both subtypes and supertypes of the nullable and non-nullable
4825-
/// versions of the type.
4826-
legacy,
4827-
48284822
/// Nullable types are marked with the '?' modifier.
48294823
///
48304824
/// Null, dynamic, and void are nullable by default.
@@ -4849,7 +4843,13 @@ enum Nullability {
48494843
/// Object y = x; // Compile-time error.
48504844
/// }
48514845
/// }
4852-
neither
4846+
neither,
4847+
4848+
/// Types in opt-out libraries are 'legacy' types.
4849+
///
4850+
/// They are both subtypes and supertypes of the nullable and non-nullable
4851+
/// versions of the type.
4852+
legacy
48534853
}
48544854

48554855
/// A syntax-independent notion of a type.
@@ -5271,7 +5271,7 @@ class TypeParameterType extends DartType {
52715271
DartType promotedBound;
52725272

52735273
TypeParameterType(this.parameter,
5274-
[this.promotedBound, this.declaredNullability])
5274+
[this.promotedBound, this.declaredNullability = Nullability.legacy])
52755275
: this.nullability =
52765276
getNullability(parameter, promotedBound, declaredNullability);
52775277

pkg/kernel/lib/binary/ast_from_binary.dart

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,8 +1961,9 @@ class BinaryBuilder {
19611961
int tag = readByte();
19621962
switch (tag) {
19631963
case Tag.TypedefType:
1964-
return new TypedefType.byReference(
1965-
readTypedefReference(), readDartTypeList());
1964+
int nullabilityIndex = readByte();
1965+
return new TypedefType.byReference(readTypedefReference(),
1966+
readDartTypeList(), Nullability.values[nullabilityIndex]);
19661967
case Tag.BottomType:
19671968
return const BottomType();
19681969
case Tag.InvalidType:
@@ -1972,13 +1973,16 @@ class BinaryBuilder {
19721973
case Tag.VoidType:
19731974
return const VoidType();
19741975
case Tag.InterfaceType:
1975-
return new InterfaceType.byReference(
1976-
readClassReference(), readDartTypeList());
1976+
int nullabilityIndex = readByte();
1977+
return new InterfaceType.byReference(readClassReference(),
1978+
readDartTypeList(), Nullability.values[nullabilityIndex]);
19771979
case Tag.SimpleInterfaceType:
1978-
return new InterfaceType.byReference(
1979-
readClassReference(), const <DartType>[]);
1980+
int nullabilityIndex = readByte();
1981+
return new InterfaceType.byReference(readClassReference(),
1982+
const <DartType>[], Nullability.values[nullabilityIndex]);
19801983
case Tag.FunctionType:
19811984
int typeParameterStackHeight = typeParameterStack.length;
1985+
int nullabilityIndex = readByte();
19821986
var typeParameters = readAndPushTypeParameterList();
19831987
var requiredParameterCount = readUInt();
19841988
var totalParameterCount = readUInt();
@@ -1992,15 +1996,20 @@ class BinaryBuilder {
19921996
typeParameters: typeParameters,
19931997
requiredParameterCount: requiredParameterCount,
19941998
namedParameters: named,
1995-
typedefType: typedefType);
1999+
typedefType: typedefType,
2000+
nullability: Nullability.values[nullabilityIndex]);
19962001
case Tag.SimpleFunctionType:
2002+
int nullabilityIndex = readByte();
19972003
var positional = readDartTypeList();
19982004
var returnType = readDartType();
1999-
return new FunctionType(positional, returnType);
2005+
return new FunctionType(positional, returnType,
2006+
nullability: Nullability.values[nullabilityIndex]);
20002007
case Tag.TypeParameterType:
2008+
int declaredNullabilityIndex = readByte();
20012009
int index = readUInt();
20022010
var bound = readDartTypeOption();
2003-
return new TypeParameterType(typeParameterStack[index], bound);
2011+
return new TypeParameterType(typeParameterStack[index], bound,
2012+
Nullability.values[declaredNullabilityIndex]);
20042013
default:
20052014
throw fail('unexpected dart type tag: $tag');
20062015
}

pkg/kernel/lib/binary/ast_to_binary.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,21 +2006,29 @@ class BinaryPrinter implements Visitor<void>, BinarySink {
20062006
void visitInterfaceType(InterfaceType node) {
20072007
if (node.typeArguments.isEmpty) {
20082008
writeByte(Tag.SimpleInterfaceType);
2009+
writeByte(node.nullability.index);
20092010
writeNonNullReference(node.className);
20102011
} else {
20112012
writeByte(Tag.InterfaceType);
2013+
writeByte(node.nullability.index);
20122014
writeNonNullReference(node.className);
20132015
writeNodeList(node.typeArguments);
20142016
}
20152017
}
20162018

20172019
@override
20182020
void visitSupertype(Supertype node) {
2021+
// Writing nullability below is only necessary because
2022+
// BinaryBuilder.readSupertype reads the supertype as an InterfaceType and
2023+
// breaks it into components afterwards, and reading an InterfaceType
2024+
// requires the nullability byte.
20192025
if (node.typeArguments.isEmpty) {
20202026
writeByte(Tag.SimpleInterfaceType);
2027+
writeByte(Nullability.nonNullable.index);
20212028
writeNonNullReference(node.className);
20222029
} else {
20232030
writeByte(Tag.InterfaceType);
2031+
writeByte(Nullability.nonNullable.index);
20242032
writeNonNullReference(node.className);
20252033
writeNodeList(node.typeArguments);
20262034
}
@@ -2033,10 +2041,12 @@ class BinaryPrinter implements Visitor<void>, BinarySink {
20332041
node.namedParameters.isEmpty &&
20342042
node.typedefType == null) {
20352043
writeByte(Tag.SimpleFunctionType);
2044+
writeByte(node.nullability.index);
20362045
writeNodeList(node.positionalParameters);
20372046
writeNode(node.returnType);
20382047
} else {
20392048
writeByte(Tag.FunctionType);
2049+
writeByte(node.nullability.index);
20402050
enterScope(typeParameters: node.typeParameters);
20412051
writeNodeList(node.typeParameters);
20422052
writeUInt30(node.requiredParameterCount);
@@ -2059,13 +2069,15 @@ class BinaryPrinter implements Visitor<void>, BinarySink {
20592069
@override
20602070
void visitTypeParameterType(TypeParameterType node) {
20612071
writeByte(Tag.TypeParameterType);
2072+
writeByte(node.declaredNullability.index);
20622073
writeUInt30(_typeParameterIndexer[node.parameter]);
20632074
writeOptionalNode(node.promotedBound);
20642075
}
20652076

20662077
@override
20672078
void visitTypedefType(TypedefType node) {
20682079
writeByte(Tag.TypedefType);
2080+
writeByte(node.nullability.index);
20692081
writeNullAllowedReference(node.typedefReference);
20702082
writeNodeList(node.typeArguments);
20712083
}

pkg/kernel/lib/binary/tag.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class Tag {
150150
/// Internal version of kernel binary format.
151151
/// Bump it when making incompatible changes in kernel binaries.
152152
/// Keep in sync with runtime/vm/kernel_binary.h, pkg/kernel/binary.md.
153-
static const int BinaryFormatVersion = 27;
153+
static const int BinaryFormatVersion = 28;
154154
}
155155

156156
abstract class ConstantTag {

runtime/vm/compiler/frontend/kernel_binary_flowgraph.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,13 @@ Tag KernelReaderHelper::PeekTag(uint8_t* payload) {
13101310
return reader_.PeekTag(payload);
13111311
}
13121312

1313+
Nullability KernelReaderHelper::ReadNullability() {
1314+
if (translation_helper_.info().kernel_binary_version() >= 28) {
1315+
return reader_.ReadNullability();
1316+
}
1317+
return kLegacy;
1318+
}
1319+
13131320
void StreamingFlowGraphBuilder::loop_depth_inc() {
13141321
++flow_graph_builder_->loop_depth_;
13151322
}

runtime/vm/compiler/frontend/kernel_fingerprints.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,13 @@ void KernelFingerprintHelper::CalculateDartTypeFingerprint() {
239239
case kSimpleFunctionType:
240240
CalculateFunctionTypeFingerprint(true);
241241
break;
242-
case kTypeParameterType:
242+
case kTypeParameterType: {
243+
Nullability nullability = ReadNullability();
244+
BuildHash(nullability);
243245
ReadUInt(); // read index for parameter.
244246
CalculateOptionalDartTypeFingerprint(); // read bound bound.
245247
break;
248+
}
246249
default:
247250
ReportUnexpectedTag("type", tag);
248251
UNREACHABLE();
@@ -260,6 +263,8 @@ void KernelFingerprintHelper::CalculateOptionalDartTypeFingerprint() {
260263
}
261264

262265
void KernelFingerprintHelper::CalculateInterfaceTypeFingerprint(bool simple) {
266+
Nullability nullability = ReadNullability();
267+
BuildHash(nullability);
263268
NameIndex kernel_class = ReadCanonicalNameReference();
264269
ASSERT(H.IsClass(kernel_class));
265270
const String& class_name = H.DartClassName(kernel_class);
@@ -274,6 +279,9 @@ void KernelFingerprintHelper::CalculateInterfaceTypeFingerprint(bool simple) {
274279
}
275280

276281
void KernelFingerprintHelper::CalculateFunctionTypeFingerprint(bool simple) {
282+
Nullability nullability = ReadNullability();
283+
BuildHash(nullability);
284+
277285
if (!simple) {
278286
CalculateTypeParametersListFingerprint(); // read type_parameters.
279287
BuildHash(ReadUInt()); // read required parameter count.

runtime/vm/compiler/frontend/kernel_translation_helper.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,10 +1983,12 @@ void KernelReaderHelper::SkipDartType() {
19831983
SkipFunctionType(true);
19841984
return;
19851985
case kTypedefType:
1986+
ReadNullability(); // read nullability.
19861987
ReadUInt(); // read index for canonical name.
19871988
SkipListOfDartTypes(); // read list of types.
19881989
return;
19891990
case kTypeParameterType:
1991+
ReadNullability(); // read nullability.
19901992
ReadUInt(); // read index for parameter.
19911993
SkipOptionalDartType(); // read bound bound.
19921994
return;
@@ -2007,13 +2009,16 @@ void KernelReaderHelper::SkipOptionalDartType() {
20072009
}
20082010

20092011
void KernelReaderHelper::SkipInterfaceType(bool simple) {
2012+
ReadNullability(); // read nullability.
20102013
ReadUInt(); // read klass_name.
20112014
if (!simple) {
20122015
SkipListOfDartTypes(); // read list of types.
20132016
}
20142017
}
20152018

20162019
void KernelReaderHelper::SkipFunctionType(bool simple) {
2020+
ReadNullability(); // read nullability.
2021+
20172022
if (!simple) {
20182023
SkipTypeParametersList(); // read type_parameters.
20192024
ReadUInt(); // read required parameter count.
@@ -2793,6 +2798,8 @@ void TypeTranslator::BuildInterfaceType(bool simple) {
27932798
// malformed iff `T` is malformed.
27942799
// => We therefore ignore errors in `A` or `B`.
27952800

2801+
helper_->ReadNullability(); // read nullability.
2802+
27962803
NameIndex klass_name =
27972804
helper_->ReadCanonicalNameReference(); // read klass_name.
27982805

@@ -2830,6 +2837,8 @@ void TypeTranslator::BuildFunctionType(bool simple) {
28302837
: Function::Handle(Z),
28312838
TokenPosition::kNoSource));
28322839

2840+
helper_->ReadNullability(); // read nullability.
2841+
28332842
// Suspend finalization of types inside this one. They will be finalized after
28342843
// the whole function type is constructed.
28352844
//
@@ -2920,6 +2929,7 @@ void TypeTranslator::BuildFunctionType(bool simple) {
29202929
}
29212930

29222931
void TypeTranslator::BuildTypeParameterType() {
2932+
helper_->ReadNullability(); // read nullability.
29232933
intptr_t parameter_index = helper_->ReadUInt(); // read parameter index.
29242934
helper_->SkipOptionalDartType(); // read bound.
29252935

runtime/vm/compiler/frontend/kernel_translation_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@ class KernelReaderHelper {
10821082
TokenPosition ReadPosition(bool record = true);
10831083
Tag ReadTag(uint8_t* payload = NULL);
10841084
uint8_t ReadFlags() { return reader_.ReadFlags(); }
1085+
Nullability ReadNullability();
10851086

10861087
intptr_t SourceTableSize();
10871088
intptr_t GetOffsetForSourceInfo(intptr_t index);

runtime/vm/compiler/frontend/scope_builder.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,7 @@ void ScopeBuilder::VisitDartType() {
13181318
}
13191319

13201320
void ScopeBuilder::VisitInterfaceType(bool simple) {
1321+
helper_.ReadNullability(); // read nullability.
13211322
helper_.ReadUInt(); // read klass_name.
13221323
if (!simple) {
13231324
intptr_t length = helper_.ReadListLength(); // read number of types.
@@ -1328,6 +1329,8 @@ void ScopeBuilder::VisitInterfaceType(bool simple) {
13281329
}
13291330

13301331
void ScopeBuilder::VisitFunctionType(bool simple) {
1332+
helper_.ReadNullability(); // read nullability.
1333+
13311334
if (!simple) {
13321335
intptr_t list_length =
13331336
helper_.ReadListLength(); // read type_parameters list length.
@@ -1375,6 +1378,8 @@ void ScopeBuilder::VisitTypeParameterType() {
13751378
function = function.parent_function();
13761379
}
13771380

1381+
helper_.ReadNullability(); // read nullability.
1382+
13781383
// The index here is the index identifying the type parameter binding site
13791384
// inside the DILL file, which uses a different indexing system than the VM
13801385
// uses for its 'TypeParameter's internally. This index includes both class

0 commit comments

Comments
 (0)