Additional tests for deferred function literals.

These tests exercise the "deferred type inference of function
literals" part of https://github.com/dart-lang/language/issues/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 <paulberry@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
This commit is contained in:
Paul Berry 2022-04-27 20:30:35 +00:00 committed by Commit Bot
parent 609ba96115
commit 3203a0784a
3 changed files with 60 additions and 0 deletions

View file

@ -356,4 +356,34 @@ void f({required void Function() g, Object? x}) {}
// regardless of whether the experiment is enabled.
assertType(findNode.simple('i; // (2)'), 'int?');
}
test_write_capture_deferred_redirecting_constructor() async {
await assertNoErrorsInCode('''
class C {
C(int? i) : this.other(i!, () { i = null; }, i);
C.other(Object? x, void Function() g, Object? y);
}
''');
// With the feature enabled, analysis of the closure is deferred until after
// all the other arguments to `this.other`, so the `i` passed to `y` is not
// yet write captured and retains its promoted value. With the experiment
// disabled, it is write captured immediately.
assertType(findNode.simple('i);'), _isEnabled ? 'int' : 'int?');
}
test_write_capture_deferred_super_constructor() async {
await assertNoErrorsInCode('''
class B {
B(Object? x, void Function() g, Object? y);
}
class C extends B {
C(int? i) : super(i!, () { i = null; }, i);
}
''');
// With the feature enabled, analysis of the closure is deferred until after
// all the other arguments to `this.other`, so the `i` passed to `y` is not
// yet write captured and retains its promoted value. With the experiment
// disabled, it is write captured immediately.
assertType(findNode.simple('i);'), _isEnabled ? 'int' : 'int?');
}
}

View file

@ -52,4 +52,19 @@ withIdentical_rhs(int? i) {
}
}
class B {
B(Object? x, void Function() g, Object? y);
B.redirectingConstructorInvocation(int? i)
: this(i!, () {
i = null;
}, i..expectStaticType<Exactly<int?>>());
}
class C extends B {
C.superConstructorInvocation(int? i)
: super(i!, () {
i = null;
}, i..expectStaticType<Exactly<int?>>());
}
main() {}

View file

@ -52,4 +52,19 @@ withIdentical_rhs(int? i) {
}
}
class B {
B(Object? x, void Function() g, Object? y);
B.redirectingConstructorInvocation(int? i)
: this(i!, () {
i = null;
}, i..expectStaticType<Exactly<int>>());
}
class C extends B {
C.superConstructorInvocation(int? i)
: super(i!, () {
i = null;
}, i..expectStaticType<Exactly<int>>());
}
main() {}