1
0
mirror of https://github.com/dart-lang/sdk synced 2024-07-03 08:19:13 +00:00

Make the type schema for null-aware spread operations consistent.

Prior to this CL, the CFE used a nullable type schema for null-aware
spread operators in list literals, but a non-nullable type schema for
null-aware spread operators in set and map literals. This was clearly
an oversight; a nullable type schema should be used for for null-aware
spread operators in all kinds of collection literals.

This change brings the CFE into alignment with the analyzer.

Fixes https://github.com/dart-lang/sdk/issues/54828.

Change-Id: I0d5aa128656c22211228f0dd35ccee40925b4ef0
Bug: https://github.com/dart-lang/sdk/issues/54828
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/349921
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
This commit is contained in:
Paul Berry 2024-02-13 22:25:41 +00:00 committed by Commit Queue
parent 385ab26022
commit c9e2b012b1
3 changed files with 48 additions and 0 deletions

View File

@ -7,7 +7,15 @@
type), to align with the specification. This change is not expected
to make any difference in practice.
- **Breaking Change** [#54828][]: The type schema used by the compiler front end
to perform type inference on the operand of a null-aware spread operator
(`...?`) in map and set literals has been made nullable, to match what
currently happens in list literals. This makes the compiler front end behavior
consistent with that of the analyzer. This change is expected to be very low
impact.
[#54640]: https://github.com/dart-lang/sdk/issues/54640
[#54828]: https://github.com/dart-lang/sdk/issues/54828
### Tools

View File

@ -3888,6 +3888,9 @@ class InferenceVisitorImpl extends InferenceVisitorBase
Map<TreeNode, DartType> inferredSpreadTypes,
Map<Expression, DartType> inferredConditionTypes,
_MapLiteralEntryOffsets offsets) {
if (entry.isNullAware) {
spreadContext = computeNullable(spreadContext);
}
ExpressionInferenceResult spreadResult =
inferExpression(entry.expression, spreadContext, isVoidAllowed: true);
if (entry.isNullAware) {

View File

@ -0,0 +1,37 @@
// Copyright (c) 2024, 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.
// Test that the context type schema for a null-aware spread is correct (it
// should be nullable compared to context type schema for a non-null-aware
// spread).
import '../static_type_helper.dart';
main() {
{
List<int> notNullAware = [
...(contextType(<int>[])..expectStaticType<Exactly<Iterable<int>>>())
];
List<int> nullAware = [
...?(contextType(<int>[])..expectStaticType<Exactly<Iterable<int>?>>())
];
}
{
Set<int> notNullAware = {
...(contextType(<int>[])..expectStaticType<Exactly<Iterable<int>>>())
};
Set<int> nullAware = {
...?(contextType(<int>[])..expectStaticType<Exactly<Iterable<int>?>>())
};
}
{
Map<int, int> notNullAware = {
...(contextType(<int, int>{})..expectStaticType<Exactly<Map<int, int>>>())
};
Map<int, int> nullAware = {
...?(contextType(<int, int>{})
..expectStaticType<Exactly<Map<int, int>?>>())
};
}
}