Skip to content

Commit 4e5144a

Browse files
stereotype441Commit Queue
authored andcommitted
[front end] Rework null shorting to use shared infrastructure.
Rework the implementation of null shorting in the front end to make use of the shared infrastructure implemented in https://dart-review.googlesource.com/c/sdk/+/399480. The shared infrastructure keeps track of null-aware guards on a stack (owned by `NullShortingMixin`), so the class `NullAwareExpressionInferenceResult` is no longer necessary. And the plumbing that used to be necessary to pass around linked lists of null-aware guards is no longer needed. Also, several members of `ExpressionInferenceResult` are no longer needed (`nullAwareGuards`, `nullAwareAction`, `nullAwareActionType`, and `stopShorting`). Some code remains in the front end but had to be moved: - The logic to promote the synthetic temporary variable to non-nullable was moved from the `NullAwareGuard` constructor to `InferenceVisitorImpl.createNullAwareGuard`. This was necessary to ensure that the promotions are done in the correct order (first the shared method `startNullShorting` promotes the expression to the left of the `?.`, and then `InferenceVisitorImpl.createNullAwareGuard` promotes the synthetic temporary variable). - The logic for desugaring a null-aware cascade expression is now implemented directly in `visitCascade`, rather than taking advantage of the shared infrastructure for null shorting. The rationale for this is twofold: - Null-aware cascades don't fully participate in null-shorting, because cascade sections are greedily parsed, so it’s impossible for a cascade section to be followed by any selectors that might continue the null shorting. So trying to re-use the shared null shorting infrastructure for cascade expressions would be overkill. - The way the front end lowers a null-aware cascade is not ideal (`x?..f()` is lowered to `let tmp = x in x == null ? null : BlockExpression({ tmp.f(); }, tmp)`, which has the disadvantage that it's not obvious to back-end optimization passes that the value of the cascade expression is equal to the value of the temporary variable). Keeping the logic for null-aware cascades separate from the logic for null shorting will make it easier to experiment with better lowerings in the future. Also, since the front end doesn't always use the shared method `analyzeExpression` for analyzing subexpressions, the shared logic for null shorting in `analyzeExpression` was replicated in `InferenceVisitorImpl.inferExpression`. In a future CL I would like to change the front end to always use the shared method `analyzeExpression` for analyzing subexpressions, so `InferenceVisitorImpl.inferExpression` won't be needed. But that's not possible right now, because `InferenceVisitorImpl.inferExpression` has behaviors that aren't implemented in the shared method `analyzeExpression` yet (see the optional parameters `isVoidAllowed` and `forEffect`). Change-Id: I4d11e373bb87c3c51bcaf445880d1bffbb5c0b22 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398120 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent ebfcd43 commit 4e5144a

File tree

3 files changed

+210
-406
lines changed

3 files changed

+210
-406
lines changed

pkg/front_end/lib/src/type_inference/inference_results.dart

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:_fe_analyzer_shared/src/types/shared_type.dart';
6-
import 'package:_fe_analyzer_shared/src/util/link.dart';
75
import 'package:kernel/ast.dart';
86

97
import '../codes/cfe_codes.dart';
@@ -364,19 +362,6 @@ class ExpressionInferenceResult {
364362
{this.postCoercionType = null})
365363
: assert(isKnown(inferredType), "$inferredType is not known.");
366364

367-
/// The guards used for null-aware access if the expression is part of a
368-
/// null-shorting.
369-
Link<NullAwareGuard> get nullAwareGuards => const Link<NullAwareGuard>();
370-
371-
/// If the expression is part of a null-shorting, this is the action performed
372-
/// on the guarded variable, found as the first guard in [nullAwareGuards].
373-
/// Otherwise, this is the same as [expression].
374-
Expression get nullAwareAction => expression;
375-
376-
DartType get nullAwareActionType => inferredType;
377-
378-
ExpressionInferenceResult stopShorting() => this;
379-
380365
@override
381366
String toString() => 'ExpressionInferenceResult($inferredType,$expression)';
382367
}
@@ -392,20 +377,7 @@ class NullAwareGuard {
392377
final InferenceVisitorBase _inferrer;
393378

394379
NullAwareGuard(
395-
this._nullAwareVariable, this._nullAwareFileOffset, this._inferrer) {
396-
// Ensure the initializer of [_nullAwareVariable] is promoted to
397-
// non-nullable.
398-
_inferrer.flowAnalysis.nullAwareAccess_rightBegin(
399-
_nullAwareVariable.initializer,
400-
new SharedTypeView(_nullAwareVariable.type));
401-
// Ensure [_nullAwareVariable] is promoted to non-nullable.
402-
// TODO(johnniwinther): Avoid creating a [VariableGet] to promote the
403-
// variable.
404-
VariableGet read = new VariableGet(_nullAwareVariable);
405-
_inferrer.flowAnalysis.variableRead(read, _nullAwareVariable);
406-
_inferrer.flowAnalysis.nullAwareAccess_rightBegin(
407-
read, new SharedTypeView(_nullAwareVariable.type));
408-
}
380+
this._nullAwareVariable, this._nullAwareFileOffset, this._inferrer);
409381

410382
/// Creates the null-guarded application of [nullAwareAction] with the
411383
/// [inferredType].
@@ -417,8 +389,6 @@ class NullAwareGuard {
417389
///
418390
Expression createExpression(
419391
DartType inferredType, Expression nullAwareAction) {
420-
// End non-nullable promotion of [_nullAwareVariable].
421-
_inferrer.flowAnalysis.nullAwareAccess_end();
422392
// End non-nullable promotion of the initializer of [_nullAwareVariable].
423393
_inferrer.flowAnalysis.nullAwareAccess_end();
424394
Expression equalsNull = _inferrer.createEqualsNull(
@@ -437,53 +407,3 @@ class NullAwareGuard {
437407
String toString() =>
438408
'NullAwareGuard($_nullAwareVariable,$_nullAwareFileOffset)';
439409
}
440-
441-
/// The result of an expression inference that is guarded with a null aware
442-
/// variable.
443-
class NullAwareExpressionInferenceResult implements ExpressionInferenceResult {
444-
/// The inferred type of the expression.
445-
@override
446-
final DartType inferredType;
447-
448-
/// The inferred type of the [nullAwareAction].
449-
@override
450-
final DartType nullAwareActionType;
451-
452-
@override
453-
final Link<NullAwareGuard> nullAwareGuards;
454-
455-
@override
456-
final Expression nullAwareAction;
457-
458-
NullAwareExpressionInferenceResult(this.inferredType,
459-
this.nullAwareActionType, this.nullAwareGuards, this.nullAwareAction)
460-
: assert(nullAwareGuards.isNotEmpty);
461-
462-
@override
463-
Expression get expression {
464-
throw new UnsupportedError('Shorting must be explicitly stopped before'
465-
'accessing the expression result of a '
466-
'NullAwareExpressionInferenceResult');
467-
}
468-
469-
@override
470-
// Coverage-ignore(suite): Not run.
471-
DartType? get postCoercionType => null;
472-
473-
@override
474-
ExpressionInferenceResult stopShorting() {
475-
Expression expression = nullAwareAction;
476-
Link<NullAwareGuard> nullAwareGuard = nullAwareGuards;
477-
while (nullAwareGuard.isNotEmpty) {
478-
expression =
479-
nullAwareGuard.head.createExpression(inferredType, expression);
480-
nullAwareGuard = nullAwareGuard.tail!;
481-
}
482-
return new ExpressionInferenceResult(inferredType, expression);
483-
}
484-
485-
@override
486-
String toString() =>
487-
'NullAwareExpressionInferenceResult($inferredType,$nullAwareGuards,'
488-
'$nullAwareAction)';
489-
}

0 commit comments

Comments
 (0)