Skip to content

Commit 3203a07

Browse files
stereotype441Commit Bot
authored and
Commit Bot
committed
Additional tests for deferred function literals.
These tests exercise the "deferred type inference of function literals" part of dart-lang/language#731 (improved inference for fold etc.) for super-constructor invocations and redirecting constructor invocations, both of which have their own code paths in the analyzer. Change-Id: I6877ac3c07a3cca31550ba74d941d250c8410cfd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/241987 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent 609ba96 commit 3203a07

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

pkg/analyzer/test/src/dart/resolution/type_inference/inference_update_1_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,34 @@ void f({required void Function() g, Object? x}) {}
356356
// regardless of whether the experiment is enabled.
357357
assertType(findNode.simple('i; // (2)'), 'int?');
358358
}
359+
360+
test_write_capture_deferred_redirecting_constructor() async {
361+
await assertNoErrorsInCode('''
362+
class C {
363+
C(int? i) : this.other(i!, () { i = null; }, i);
364+
C.other(Object? x, void Function() g, Object? y);
365+
}
366+
''');
367+
// With the feature enabled, analysis of the closure is deferred until after
368+
// all the other arguments to `this.other`, so the `i` passed to `y` is not
369+
// yet write captured and retains its promoted value. With the experiment
370+
// disabled, it is write captured immediately.
371+
assertType(findNode.simple('i);'), _isEnabled ? 'int' : 'int?');
372+
}
373+
374+
test_write_capture_deferred_super_constructor() async {
375+
await assertNoErrorsInCode('''
376+
class B {
377+
B(Object? x, void Function() g, Object? y);
378+
}
379+
class C extends B {
380+
C(int? i) : super(i!, () { i = null; }, i);
381+
}
382+
''');
383+
// With the feature enabled, analysis of the closure is deferred until after
384+
// all the other arguments to `this.other`, so the `i` passed to `y` is not
385+
// yet write captured and retains its promoted value. With the experiment
386+
// disabled, it is write captured immediately.
387+
assertType(findNode.simple('i);'), _isEnabled ? 'int' : 'int?');
388+
}
359389
}

tests/language/inference_update_1/write_capture_deferral_disabled_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,19 @@ withIdentical_rhs(int? i) {
5252
}
5353
}
5454

55+
class B {
56+
B(Object? x, void Function() g, Object? y);
57+
B.redirectingConstructorInvocation(int? i)
58+
: this(i!, () {
59+
i = null;
60+
}, i..expectStaticType<Exactly<int?>>());
61+
}
62+
63+
class C extends B {
64+
C.superConstructorInvocation(int? i)
65+
: super(i!, () {
66+
i = null;
67+
}, i..expectStaticType<Exactly<int?>>());
68+
}
69+
5570
main() {}

tests/language/inference_update_1/write_capture_deferral_enabled_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,19 @@ withIdentical_rhs(int? i) {
5252
}
5353
}
5454

55+
class B {
56+
B(Object? x, void Function() g, Object? y);
57+
B.redirectingConstructorInvocation(int? i)
58+
: this(i!, () {
59+
i = null;
60+
}, i..expectStaticType<Exactly<int>>());
61+
}
62+
63+
class C extends B {
64+
C.superConstructorInvocation(int? i)
65+
: super(i!, () {
66+
i = null;
67+
}, i..expectStaticType<Exactly<int>>());
68+
}
69+
5570
main() {}

0 commit comments

Comments
 (0)