Fix comments in `InferenceVisitorImpl.visitIfNullExpression`.
The comments were based on an old draft of a type inference
specification which was never ratified by the language team never
committed, and which did not match the implementation.
I've replaced them with a description of how the analyzer and CFE
currently handle if-null expressions.
Change-Id: I91ae2cf54b659636b9642e0aea1238d9f0684ae7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/350780
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
diff --git a/pkg/front_end/lib/src/fasta/type_inference/inference_visitor.dart b/pkg/front_end/lib/src/fasta/type_inference/inference_visitor.dart
index 0cd61d0..f167c87 100644
--- a/pkg/front_end/lib/src/fasta/type_inference/inference_visitor.dart
+++ b/pkg/front_end/lib/src/fasta/type_inference/inference_visitor.dart
@@ -1893,8 +1893,8 @@
ExpressionInferenceResult visitIfNullExpression(
IfNullExpression node, DartType typeContext) {
- // To infer `e0 ?? e1` in context K:
- // - Infer e0 in context K to get T0
+ // To infer `e0 ?? e1` in context `K`:
+ // - Infer `e0` in context `K?` to get `T0`
ExpressionInferenceResult lhsResult = inferExpression(
node.left, computeNullable(typeContext),
isVoidAllowed: false);
@@ -1906,8 +1906,8 @@
flowAnalysis.ifNullExpression_rightBegin(node.left, lhsResult.inferredType);
- // - Let J = T0 if K is `?` else K.
- // - Infer e1 in context J to get T1
+ // - Let `J = T0` if `K` is `_`, otherwise `K`.
+ // - Infer `e1` in context `J` to get `T1`
ExpressionInferenceResult rhsResult;
if (typeContext is UnknownType) {
rhsResult = inferExpression(node.right, lhsResult.inferredType,
@@ -1917,9 +1917,7 @@
}
flowAnalysis.ifNullExpression_end();
- // - Let T = greatest closure of K with respect to `?` if K is not `_`, else
- // UP(t0, t1)
- // - Then the inferred type is T.
+ // - Then the inferred type is UP(NonNull(T0), T1).
DartType originalLhsType = lhsResult.inferredType;
DartType nonNullableLhsType = originalLhsType.toNonNull();
DartType inferredType = typeSchemaEnvironment.getStandardUpperBound(