Skip to content

Commit e40980c

Browse files
mkustermannCommit Queue
authored andcommitted
[tfa] Fix bug in TFA transformation of unused fields
When a TFA eliminates a field it moves it's initializer to a `LocalInitializer` of the constructor. After [0] the `VariableDeclaration` of the local initializer will get the type of the field. Though that CL didn't ensure TFA also visits the type of the `VariableDeclaration`. This can lead to a situation where the type refers to a class that TFA has tree shaken, which causes dangling references in the AST. [0] https://dart-review.googlesource.com/c/sdk/+/415201 Issue flutter/flutter#166124 Issue b/406692736 TEST=pkg/vm/testcases/transformations/type_flow/transformer/write_only_field Change-Id: Ieb8f5c4daedd314c613617218da898059bb6941a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/419542 Reviewed-by: Slava Egorov <[email protected]> Commit-Queue: Martin Kustermann <[email protected]>
1 parent 3d0a29d commit e40980c

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,7 @@ class _TreeShakerPass1 extends RemovingTransformer {
18501850
null,
18511851
initializer: node.value,
18521852
isSynthesized: true,
1853-
type: field.type,
1853+
type: visitDartType(field.type, cannotRemoveSentinel),
18541854
),
18551855
);
18561856
} else {

pkg/vm/testcases/transformations/type_flow/transformer/write_only_field.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ class B {
2121

2222
class C {
2323
B? instanceField = new B();
24+
UsedAsType<int>? Function() instanceField2 = () => null;
2425
}
2526

27+
class UsedAsType<T> {}
28+
2629
void main() {
2730
field = null;
2831
new C().instanceField = null;

pkg/vm/testcases/transformations/type_flow/transformer/write_only_field.dart.expect

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ class B extends core::Object {
1212
}
1313
class C extends core::Object {
1414
synthetic constructor •() → self::C
15-
: self::B? #t1 = new self::B::•(), super core::Object::•()
15+
: self::B? #t1 = new self::B::•(), () → self::UsedAsType<core::int>? #t2 = () → Null => null, super core::Object::•()
1616
;
1717

1818
[@vm.procedure-attributes.metadata=methodOrSetterCalledDynamically:false,getterCalledDynamically:false,hasThisUses:false,hasTearOffUses:false,methodOrSetterSelectorId:1]
1919
set instanceField(synthesized self::B? value) → void;
2020
}
21+
abstract class UsedAsType<T extends core::Object? = dynamic> extends core::Object {
22+
}
2123

2224
[@vm.inferred-return-type.metadata=dart.core::Null? (value: null)]
2325
static method main() → void {

0 commit comments

Comments
 (0)