Skip to content

Commit c9e2b01

Browse files
stereotype441Commit Queue
authored and
Commit Queue
committed
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 #54828. Change-Id: I0d5aa128656c22211228f0dd35ccee40925b4ef0 Bug: #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>
1 parent 385ab26 commit c9e2b01

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77
type), to align with the specification. This change is not expected
88
to make any difference in practice.
99

10+
- **Breaking Change** [#54828][]: The type schema used by the compiler front end
11+
to perform type inference on the operand of a null-aware spread operator
12+
(`...?`) in map and set literals has been made nullable, to match what
13+
currently happens in list literals. This makes the compiler front end behavior
14+
consistent with that of the analyzer. This change is expected to be very low
15+
impact.
16+
1017
[#54640]: https://github.com/dart-lang/sdk/issues/54640
18+
[#54828]: https://github.com/dart-lang/sdk/issues/54828
1119

1220
### Tools
1321

pkg/front_end/lib/src/fasta/type_inference/inference_visitor.dart

+3
Original file line numberDiff line numberDiff line change
@@ -3888,6 +3888,9 @@ class InferenceVisitorImpl extends InferenceVisitorBase
38883888
Map<TreeNode, DartType> inferredSpreadTypes,
38893889
Map<Expression, DartType> inferredConditionTypes,
38903890
_MapLiteralEntryOffsets offsets) {
3891+
if (entry.isNullAware) {
3892+
spreadContext = computeNullable(spreadContext);
3893+
}
38913894
ExpressionInferenceResult spreadResult =
38923895
inferExpression(entry.expression, spreadContext, isVoidAllowed: true);
38933896
if (entry.isNullAware) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Test that the context type schema for a null-aware spread is correct (it
6+
// should be nullable compared to context type schema for a non-null-aware
7+
// spread).
8+
9+
import '../static_type_helper.dart';
10+
11+
main() {
12+
{
13+
List<int> notNullAware = [
14+
...(contextType(<int>[])..expectStaticType<Exactly<Iterable<int>>>())
15+
];
16+
List<int> nullAware = [
17+
...?(contextType(<int>[])..expectStaticType<Exactly<Iterable<int>?>>())
18+
];
19+
}
20+
{
21+
Set<int> notNullAware = {
22+
...(contextType(<int>[])..expectStaticType<Exactly<Iterable<int>>>())
23+
};
24+
Set<int> nullAware = {
25+
...?(contextType(<int>[])..expectStaticType<Exactly<Iterable<int>?>>())
26+
};
27+
}
28+
{
29+
Map<int, int> notNullAware = {
30+
...(contextType(<int, int>{})..expectStaticType<Exactly<Map<int, int>>>())
31+
};
32+
Map<int, int> nullAware = {
33+
...?(contextType(<int, int>{})
34+
..expectStaticType<Exactly<Map<int, int>?>>())
35+
};
36+
}
37+
}

0 commit comments

Comments
 (0)