Skip to content

Commit 34d2fd4

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Add summary deserialization support for nullability.
In a follow-up CL I will add nullability support to the summary linker. Change-Id: I99f3dce1a695da65c7c09c11e73462c93aeb637f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/101882 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 8dc57ac commit 34d2fd4

File tree

4 files changed

+212
-7
lines changed

4 files changed

+212
-7
lines changed

pkg/analyzer/lib/src/summary/resynthesize.dart

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,17 +1343,18 @@ class _UnitResynthesizer extends UnitResynthesizer with UnitResynthesizerMixin {
13431343
type = refinedType;
13441344
}
13451345
}
1346+
DartType result;
13461347
if (type.paramReference != 0) {
1347-
return context.typeParameterContext
1348+
result = context.typeParameterContext
13481349
.getTypeParameterType(type.paramReference);
13491350
} else if (type.entityKind == EntityRefKind.genericFunctionType) {
13501351
GenericFunctionTypeElement element =
13511352
new GenericFunctionTypeElementImpl.forSerialized(context, type);
1352-
return element.type;
1353+
result = element.type;
13531354
} else if (type.syntheticReturnType != null) {
13541355
FunctionElementImpl element =
13551356
new FunctionElementImpl_forLUB(context, type);
1356-
return element.type;
1357+
result = element.type;
13571358
} else {
13581359
DartType getTypeArgument(int i) {
13591360
if (i < type.typeArguments.length) {
@@ -1369,12 +1370,18 @@ class _UnitResynthesizer extends UnitResynthesizer with UnitResynthesizerMixin {
13691370
return DynamicTypeImpl.instance;
13701371
}
13711372

1372-
return referenceInfo.buildType(
1373+
result = referenceInfo.buildType(
13731374
instantiateToBoundsAllowed,
13741375
type.typeArguments.length,
13751376
getTypeArgument,
13761377
type.implicitFunctionTypeIndices);
13771378
}
1379+
var nullabilitySuffix = _translateNullabilitySuffix(type.nullabilitySuffix);
1380+
var resultAsImpl = result as TypeImpl;
1381+
if (resultAsImpl.nullabilitySuffix != nullabilitySuffix) {
1382+
result = resultAsImpl.withNullability(nullabilitySuffix);
1383+
}
1384+
return result;
13781385
}
13791386

13801387
@override
@@ -1621,6 +1628,19 @@ class _UnitResynthesizer extends UnitResynthesizer with UnitResynthesizerMixin {
16211628
return null;
16221629
}
16231630

1631+
NullabilitySuffix _translateNullabilitySuffix(
1632+
EntityRefNullabilitySuffix suffix) {
1633+
switch (suffix) {
1634+
case EntityRefNullabilitySuffix.none:
1635+
return NullabilitySuffix.none;
1636+
case EntityRefNullabilitySuffix.question:
1637+
return NullabilitySuffix.question;
1638+
case EntityRefNullabilitySuffix.starOrIrrelevant:
1639+
return NullabilitySuffix.star;
1640+
}
1641+
throw new StateError('Unrecognized nullability suffix');
1642+
}
1643+
16241644
/**
16251645
* If the given [kind] is a top-level or class member property accessor, and
16261646
* the given [name] does not end with `=`, i.e. does not denote a setter,

pkg/analyzer/test/src/summary/element_text.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@ void checkElementText(LibraryElement library, String expected,
5959
bool withOffsets: false,
6060
bool withSyntheticAccessors: false,
6161
bool withSyntheticFields: false,
62-
bool withTypes: false}) {
62+
bool withTypes: false,
63+
bool annotateNullability: false}) {
6364
var writer = new _ElementWriter(
6465
withCodeRanges: withCodeRanges,
6566
withConstElements: withConstElements,
6667
withExportScope: withExportScope,
6768
withOffsets: withOffsets,
6869
withSyntheticAccessors: withSyntheticAccessors,
6970
withSyntheticFields: withSyntheticFields,
70-
withTypes: withTypes);
71+
withTypes: withTypes,
72+
annotateNullability: annotateNullability);
7173
writer.writeLibraryElement(library);
7274

7375
String actualText = writer.buffer.toString();
@@ -135,6 +137,7 @@ class _ElementWriter {
135137
final bool withSyntheticAccessors;
136138
final bool withSyntheticFields;
137139
final bool withTypes;
140+
final bool annotateNullability;
138141
final StringBuffer buffer = new StringBuffer();
139142

140143
_ElementWriter(
@@ -144,7 +147,8 @@ class _ElementWriter {
144147
this.withOffsets: false,
145148
this.withSyntheticAccessors: false,
146149
this.withSyntheticFields: false,
147-
this.withTypes: false});
150+
this.withTypes: false,
151+
this.annotateNullability: false});
148152

149153
bool isDynamicType(DartType type) => type is DynamicTypeImpl;
150154

@@ -947,6 +951,18 @@ class _ElementWriter {
947951
} else {
948952
buffer.write(type.displayName);
949953
}
954+
if (annotateNullability) {
955+
switch ((type as TypeImpl).nullabilitySuffix) {
956+
case NullabilitySuffix.none:
957+
break;
958+
case NullabilitySuffix.question:
959+
buffer.write('?');
960+
break;
961+
case NullabilitySuffix.star:
962+
buffer.write('*');
963+
break;
964+
}
965+
}
950966
}
951967

952968
void writeType2(DartType type) {

pkg/analyzer/test/src/summary/resynthesize_ast2_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,25 @@ class ResynthesizeAst2Test extends ResynthesizeTestStrategyTwoPhase
112112
return elementFactory.libraryOfUri('${source.uri}');
113113
}
114114

115+
@override
116+
@failingTest
117+
test_class_ref_nullability_none() => super.test_class_ref_nullability_none();
118+
119+
@override
120+
@failingTest
121+
test_class_ref_nullability_question() =>
122+
super.test_class_ref_nullability_question();
123+
124+
@override
125+
@failingTest
126+
test_generic_function_type_nullability_none() =>
127+
super.test_generic_function_type_nullability_none();
128+
129+
@override
130+
@failingTest
131+
test_generic_function_type_nullability_question() =>
132+
super.test_generic_function_type_nullability_question();
133+
115134
@override
116135
@failingTest
117136
test_syntheticFunctionType_genericClosure() async {
@@ -120,6 +139,16 @@ class ResynthesizeAst2Test extends ResynthesizeTestStrategyTwoPhase
120139
await super.test_syntheticFunctionType_genericClosure();
121140
}
122141

142+
@override
143+
@failingTest
144+
test_type_param_ref_nullability_none() =>
145+
super.test_type_param_ref_nullability_none();
146+
147+
@override
148+
@failingTest
149+
test_type_param_ref_nullability_question() =>
150+
super.test_type_param_ref_nullability_question();
151+
123152
void _addLibraryUnits(
124153
Source definingSource,
125154
CompilationUnit definingUnit,

pkg/analyzer/test/src/summary/resynthesize_common.dart

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ mixin GetElementTestCases implements ResynthesizeTestHelpers {
204204
/// applied to a class implementing [ResynthesizeTestStrategy], along with the
205205
/// mixin [ResynthesizeTestHelpers].
206206
mixin ResynthesizeTestCases implements ResynthesizeTestHelpers {
207+
FeatureSet get disableNnbd => FeatureSet.forTesting(sdkVersion: '2.2.2');
208+
207209
FeatureSet get enableNnbd =>
208210
FeatureSet.forTesting(additionalFeatures: [Feature.non_nullable]);
209211

@@ -1476,6 +1478,54 @@ class C<T> {
14761478
''');
14771479
}
14781480

1481+
test_class_ref_nullability_none() async {
1482+
featureSet = enableNnbd;
1483+
var library = await checkLibrary('''
1484+
class C {}
1485+
C c;
1486+
''');
1487+
checkElementText(
1488+
library,
1489+
'''
1490+
class C {
1491+
}
1492+
C c;
1493+
''',
1494+
annotateNullability: true);
1495+
}
1496+
1497+
test_class_ref_nullability_question() async {
1498+
featureSet = enableNnbd;
1499+
var library = await checkLibrary('''
1500+
class C {}
1501+
C? c;
1502+
''');
1503+
checkElementText(
1504+
library,
1505+
'''
1506+
class C {
1507+
}
1508+
C? c;
1509+
''',
1510+
annotateNullability: true);
1511+
}
1512+
1513+
test_class_ref_nullability_star() async {
1514+
featureSet = disableNnbd;
1515+
var library = await checkLibrary('''
1516+
class C {}
1517+
C c;
1518+
''');
1519+
checkElementText(
1520+
library,
1521+
'''
1522+
class C {
1523+
}
1524+
C* c;
1525+
''',
1526+
annotateNullability: true);
1527+
}
1528+
14791529
test_class_setter_abstract() async {
14801530
var library =
14811531
await checkLibrary('abstract class C { void set x(int value); }');
@@ -6102,6 +6152,45 @@ FutureOr<int> f() {}
61026152
expect(y.type.toString(), 'dynamic');
61036153
}
61046154

6155+
test_generic_function_type_nullability_none() async {
6156+
featureSet = enableNnbd;
6157+
var library = await checkLibrary('''
6158+
void Function() f;
6159+
''');
6160+
checkElementText(
6161+
library,
6162+
'''
6163+
void Function() f;
6164+
''',
6165+
annotateNullability: true);
6166+
}
6167+
6168+
test_generic_function_type_nullability_question() async {
6169+
featureSet = enableNnbd;
6170+
var library = await checkLibrary('''
6171+
void Function()? f;
6172+
''');
6173+
checkElementText(
6174+
library,
6175+
'''
6176+
void Function()? f;
6177+
''',
6178+
annotateNullability: true);
6179+
}
6180+
6181+
test_generic_function_type_nullability_star() async {
6182+
featureSet = disableNnbd;
6183+
var library = await checkLibrary('''
6184+
void Function() f;
6185+
''');
6186+
checkElementText(
6187+
library,
6188+
'''
6189+
void Function()* f;
6190+
''',
6191+
annotateNullability: true);
6192+
}
6193+
61056194
test_generic_gClass_gMethodStatic() async {
61066195
var library = await checkLibrary('''
61076196
class C<T, U> {
@@ -9211,6 +9300,57 @@ dynamic v;
92119300
''');
92129301
}
92139302

9303+
test_type_param_ref_nullability_none() async {
9304+
featureSet = enableNnbd;
9305+
var library = await checkLibrary('''
9306+
class C<T> {
9307+
T t;
9308+
}
9309+
''');
9310+
checkElementText(
9311+
library,
9312+
'''
9313+
class C<T> {
9314+
T t;
9315+
}
9316+
''',
9317+
annotateNullability: true);
9318+
}
9319+
9320+
test_type_param_ref_nullability_question() async {
9321+
featureSet = enableNnbd;
9322+
var library = await checkLibrary('''
9323+
class C<T> {
9324+
T? t;
9325+
}
9326+
''');
9327+
checkElementText(
9328+
library,
9329+
'''
9330+
class C<T> {
9331+
T? t;
9332+
}
9333+
''',
9334+
annotateNullability: true);
9335+
}
9336+
9337+
test_type_param_ref_nullability_star() async {
9338+
featureSet = disableNnbd;
9339+
var library = await checkLibrary('''
9340+
class C<T> {
9341+
T t;
9342+
}
9343+
''');
9344+
checkElementText(
9345+
library,
9346+
'''
9347+
class C<T> {
9348+
T* t;
9349+
}
9350+
''',
9351+
annotateNullability: true);
9352+
}
9353+
92149354
test_type_reference_lib_to_lib() async {
92159355
var library = await checkLibrary('''
92169356
class C {}

0 commit comments

Comments
 (0)