Skip to content

Commit 55ddbe5

Browse files
alexmarkovCommit Queue
authored and
Commit Queue
committed
[vm/aot/tfa] Avoid using null as an initial value of non-nullable and late variables without initializer
TEST=pkg/vm/testcases/transformations/type_flow/summary_collector/vars.dart Cq-Include-Trybots: luci.dart.try:vm-kernel-precomp-nnbd-linux-release-x64-try Change-Id: Ie6b3bdf7069e7d6389e3a729f23b9e1912a4eed2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/285061 Reviewed-by: Slava Egorov <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent a92274c commit 55ddbe5

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

pkg/vm/lib/transformations/type_flow/summary_collector.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,8 +2253,11 @@ class SummaryCollector extends RecursiveResultVisitor<TypeExpr?> {
22532253
TypeExpr? visitVariableDeclaration(VariableDeclaration node) {
22542254
node.annotations.forEach(_visit);
22552255
final initializer = node.initializer;
2256-
final TypeExpr initialValue =
2257-
initializer == null ? _nullType : _visit(initializer);
2256+
final TypeExpr initialValue = initializer == null
2257+
? ((node.type.nullability == Nullability.nonNullable || node.isLate)
2258+
? const EmptyType()
2259+
: _nullType)
2260+
: _visit(initializer);
22582261
_declareVariable(node, initialValue);
22592262
return null;
22602263
}

pkg/vm/testcases/transformations/type_flow/summary_collector/vars.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,22 @@ int loop2(int x) {
4040
return x;
4141
}
4242

43+
int nonNullableWithoutInitializer(bool cond) {
44+
int x;
45+
if (cond) {
46+
x = 5;
47+
} else if (true) {
48+
x = 7;
49+
}
50+
return x;
51+
}
52+
53+
int? lateWithoutInitializer(bool cond) {
54+
late int? x;
55+
if (cond) {
56+
x = 5;
57+
}
58+
return x;
59+
}
60+
4361
main() {}

pkg/vm/testcases/transformations/type_flow/summary_collector/vars.dart.expect

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ t3* = _Call [dart.core::num.<] (i_0, _T (dart.core::_Smi, 5))
4040
t4* = _Call [dart.core::num.+] (_T (dart.core::int)+?, _T (dart.core::_Smi, 10))
4141
x_0 = _Join [dart.core::int] (%x, t4)
4242
RESULT: x_0
43+
------------ nonNullableWithoutInitializer ------------
44+
%cond = _Parameter #0 [_T (dart.core::bool)+?]
45+
x_0 = _Join [dart.core::int] (_T (dart.core::_Smi, 5), _T (dart.core::_Smi, 7))
46+
RESULT: x_0
47+
------------ lateWithoutInitializer ------------
48+
%cond = _Parameter #0 [_T (dart.core::bool)+?]
49+
RESULT: _T (dart.core::_Smi, 5)
4350
------------ main ------------
4451

4552
RESULT: _T {}?

0 commit comments

Comments
 (0)