Skip to content

Commit 1b6f89d

Browse files
alexmarkovCommit Queue
authored andcommitted
[vm, tfa] Fix tree-shaking of late final fields with initializers
TFA tree shaker removes initializers of fields which cannot be accessed but retained due to an entry point pragma / dynamic interface. For late final fields with initializers that changes semantics as late final fields without initializers have an implicit setter. Such setter is not accounted in the kernel AST (Field.setterReference == null and Field.hasSetter == false) and it doesn't get an assigned selector id, which causes a crash in dispatch table generator. The change fixes this bug by retaining initializers for late final fields. The initializer code is still replaced with throw "Attempt to execute code removed by Dart AOT compiler (TFA)". TEST=pkg/vm/testcases/transformations/type_flow/transformer/regress_b404559785.dart Bug: b/404559785 Change-Id: Id998a715ea75a4768414997ce66bcdd1d4f19189 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/416720 Commit-Queue: Alexander Markov <[email protected]> Reviewed-by: Slava Egorov <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
1 parent 42bdd60 commit 1b6f89d

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,8 +2303,14 @@ class _TreeShakerPass2 extends RemovingTransformer {
23032303
}
23042304
Statistics.methodBodiesDropped++;
23052305
} else if (node is Field) {
2306-
node.initializer = null;
2307-
Statistics.fieldInitializersDropped++;
2306+
// Do not remove initializers of late final fields as
2307+
// late final fields without initializer would have
2308+
// an additional implicit setter which was not accounted
2309+
// (Field.setterReference == null and Field.hasSetter == false).
2310+
if (!(node.isLate && node.isFinal)) {
2311+
node.initializer = null;
2312+
Statistics.fieldInitializersDropped++;
2313+
}
23082314
} else if (node is Constructor) {
23092315
_makeUnreachableBody(node.function);
23102316
_removeDefaultValuesOfParameters(node.function);

pkg/vm/test/common_test_utils.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@ different kernel AST for the same Dart programs.
191191
192192
In order to re-generate expectations run tests with -D$kUpdateExpectations=true VM option:
193193
194-
tools/test.py -m release --vm-options -D$kUpdateExpectations=true pkg/vm/
194+
tools/test.py -m release --vm-options -D$kUpdateExpectations=true --timeout 600 pkg/vm/
195195
196196
In order to dump actual results into .actual files run tests with -D$kDumpActualResult=true VM option:
197197
198-
tools/test.py -m release --vm-options -D$kDumpActualResult=true pkg/vm/
198+
tools/test.py -m release --vm-options -D$kDumpActualResult=true --timeout 600 pkg/vm/
199199
200200
""");
201201
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Verifies that TFA does not remove a field initializer of a late final
6+
// field with initializer as that would add an extra implicit setter.
7+
// Regression test for b/404559785.
8+
9+
class Foo {
10+
int v;
11+
Foo(this.v);
12+
13+
@pragma('vm:entry-point')
14+
late final int hashCode = int.parse('1');
15+
}
16+
17+
void main() {
18+
print(Foo);
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
library #lib;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
abstract class Foo extends core::Object {
6+
7+
[@vm.unreachable.metadata=]
8+
[@vm.procedure-attributes.metadata=methodOrSetterCalledDynamically:false,getterCalledDynamically:false,hasThisUses:false,hasNonThisUses:false,hasTearOffUses:false,getterSelectorId:1]
9+
@#C3
10+
late final field core::int hashCode = throw "Attempt to execute code removed by Dart AOT compiler (TFA)";
11+
}
12+
13+
[@vm.inferred-return-type.metadata=dart.core::Null? (value: null)]
14+
static method main() → void {
15+
core::print(#C4);
16+
}
17+
constants {
18+
#C1 = "vm:entry-point"
19+
#C2 = null
20+
#C3 = core::pragma {name:#C1, options:#C2}
21+
#C4 = TypeLiteralConstant(self::Foo)
22+
}

0 commit comments

Comments
 (0)