Skip to content

Commit 2c5a56e

Browse files
liamappelbecommit-bot@chromium.org
authored andcommitted
[vm] Disable late field init for constructor initialized fields
Change-Id: Ibdab37839d07b77bf2c92a09f760f44b54d94bcb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/138620 Reviewed-by: Alexander Markov <[email protected]> Commit-Queue: Liam Appelbe <[email protected]>
1 parent d3c0d59 commit 2c5a56e

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

runtime/vm/compiler/frontend/kernel_binary_flowgraph.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,11 @@ Fragment StreamingFlowGraphBuilder::BuildInitializers(
314314
field_helper.ReadUntilExcluding(FieldHelper::kInitializer);
315315
const Tag initializer_tag = ReadTag();
316316
if (class_field.is_late()) {
317-
instructions +=
318-
BuildLateFieldInitializer(Field::ZoneHandle(Z, class_field.raw()),
319-
initializer_tag == kSomething);
317+
if (!is_constructor_initialized) {
318+
instructions += BuildLateFieldInitializer(
319+
Field::ZoneHandle(Z, class_field.raw()),
320+
initializer_tag == kSomething);
321+
}
320322
} else if (initializer_tag == kSomething) {
321323
EnterScope(field_offset);
322324
// If this field is initialized in constructor then we can ignore the

tests/language/nnbd/syntax/late_modifier_non_final_field_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class A {
1818
late int? fieldWithNullInit = null;
1919
late int fieldWithNoInit;
2020
late int? nullableFieldWithNoInit;
21+
late int? fieldWithOnlyCtorInit;
22+
late int? fieldWithOnlyBothInitAndCtorInit = 123;
23+
A()
24+
: fieldWithOnlyCtorInit = null,
25+
fieldWithOnlyBothInitAndCtorInit = null;
2126
}
2227

2328
main() {
@@ -32,6 +37,8 @@ main() {
3237
() => a.fieldWithNoInit, (error) => error is LateInitializationError);
3338
Expect.throws(() => a.nullableFieldWithNoInit,
3439
(error) => error is LateInitializationError);
40+
Expect.equals(null, a.fieldWithOnlyCtorInit);
41+
Expect.equals(null, a.fieldWithOnlyBothInitAndCtorInit);
3542
Expect.equals(1, initCalls);
3643
Expect.equals(123, a.fieldWithInit);
3744
Expect.equals(123, a.fieldWithTrivialInit);
@@ -40,18 +47,24 @@ main() {
4047
() => a.fieldWithNoInit, (error) => error is LateInitializationError);
4148
Expect.throws(() => a.nullableFieldWithNoInit,
4249
(error) => error is LateInitializationError);
50+
Expect.equals(null, a.fieldWithOnlyCtorInit);
51+
Expect.equals(null, a.fieldWithOnlyBothInitAndCtorInit);
4352
Expect.equals(1, initCalls);
4453
a.fieldWithInit = 456;
4554
a.fieldWithTrivialInit = 456;
4655
a.fieldWithNullInit = 456;
4756
a.fieldWithNoInit = 456;
4857
a.nullableFieldWithNoInit = null;
58+
a.fieldWithOnlyCtorInit = 456;
59+
a.fieldWithOnlyBothInitAndCtorInit = 456;
4960
Expect.equals(1, initCalls);
5061
Expect.equals(456, a.fieldWithInit);
5162
Expect.equals(456, a.fieldWithTrivialInit);
5263
Expect.equals(456, a.fieldWithNullInit);
5364
Expect.equals(456, a.fieldWithNoInit);
5465
Expect.equals(null, a.nullableFieldWithNoInit);
66+
Expect.equals(456, a.fieldWithOnlyCtorInit);
67+
Expect.equals(456, a.fieldWithOnlyBothInitAndCtorInit);
5568
Expect.equals(1, initCalls);
5669
initCalls = 0;
5770

0 commit comments

Comments
 (0)