Files
sdk/tests/language/null_aware/null_shortening_invocation_test.dart
T
Paul Berry 8e85492162 [analyzer] Re-work handling of null shorting using shared code.
In https://dart-review.googlesource.com/c/sdk/+/399480, a mechanism
was introduced to `_fe_analyzer_shared` to support null-shorting.
Sometime later, in
https://dart-review.googlesource.com/c/sdk/+/427341, the CFE was
changed to use this shared mechanism.

This CL completes the arc of work by changing the analyzer to use the
new shared mechanism.

The public API class `NullShortableExpression`, which was part of the
analyzer's old implementation of null shorting, is no longer used. It
should never have been exposed through the analyzer public API in the
first place, so it's been deprecated; I will update the pending
changes for analyzer 9.0.0 to delete it.

Change-Id: Ic711df6c537c454aa8a99861135e63c8dd277485
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/400021
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2025-08-22 20:54:14 -07:00

42 lines
1.1 KiB
Dart

// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Check null-shortening semantics of `?.` and `?[]` when they appear as the
// target of a function invocation.
import "package:expect/expect.dart";
import "package:expect/static_type_helper.dart";
Never get unreachable => Expect.fail("Unreachable") as Never;
class A {
int Function(Object?) getFn1() => (x) {
Expect.fail('Should never be called');
return 0;
};
int Function(Object?) Function() get getFn2 =>
() => (x) {
Expect.fail('Should never be called');
return 0;
};
int Function(Object?) operator [](int i) => (x) {
Expect.fail('Should never be called');
return 0;
};
}
main() {
A? a = null;
Expect.equals(
(a?.getFn1()(unreachable))..expectStaticType<Exactly<int?>>(),
null,
);
Expect.equals(
(a?.getFn2()(unreachable))..expectStaticType<Exactly<int?>>(),
null,
);
Expect.equals((a?[0](unreachable))..expectStaticType<Exactly<int?>>(), null);
}