Skip to content

Commit cf57f88

Browse files
nshahancommit-bot@chromium.org
authored andcommitted
[ddc] Change ! failure to a throw TypeError
Fixes test failures in language/unsorted/inv_cse_licm_test Change-Id: If8df024d0128568e1f65463d4a82fa593b5a6a1d Fixes: #42443 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/153481 Reviewed-by: Mark Zhou <[email protected]> Commit-Queue: Nicholas Shahan <[email protected]>
1 parent 6861212 commit cf57f88

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

pkg/dev_compiler/lib/src/kernel/compiler.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5212,8 +5212,9 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
52125212
@override
52135213
js_ast.Expression visitNullCheck(NullCheck node) {
52145214
var expr = node.operand;
5215+
var jsExpr = _visitExpression(expr);
52155216
// If the expression is non-nullable already, this is a no-op.
5216-
return isNullable(expr) ? notNull(expr) : _visitExpression(expr);
5217+
return isNullable(expr) ? runtimeCall('nullCheck(#)', [jsExpr]) : jsExpr;
52175218
}
52185219

52195220
@override

sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,9 @@ asInt(obj) {
474474

475475
asNullableInt(obj) => obj == null ? null : asInt(obj);
476476

477-
/// Checks that `x` is not null or undefined.
477+
/// Checks for null or undefined and returns [x].
478+
///
479+
/// Throws [NoSuchMethodError] when it is null or undefined.
478480
//
479481
// TODO(jmesserly): inline this, either by generating it as a function into
480482
// the module, or via some other pattern such as:
@@ -487,12 +489,13 @@ _notNull(x) {
487489
return x;
488490
}
489491

490-
/// Checks that `x` is not null or undefined.
492+
/// Checks for null or undefined and returns [x].
493+
///
494+
/// Throws a [TypeError] when [x] is null or undefined (under sound null safety
495+
/// mode) or emits a runtime warning (otherwise).
491496
///
492-
/// Unlike `_notNull`, this throws a `CastError` (under strict checking)
493-
/// or emits a runtime warning (otherwise). This is only used by the
494-
/// compiler when casting from nullable to non-nullable variants of the
495-
/// same type.
497+
/// This is only used by the compiler when casting from nullable to non-nullable
498+
/// variants of the same type.
496499
nullCast(x, type) {
497500
if (x == null) {
498501
if (!strictNullSafety) {
@@ -504,6 +507,16 @@ nullCast(x, type) {
504507
return x;
505508
}
506509

510+
/// Checks for null or undefined and returns [x].
511+
///
512+
/// Throws a [TypeError] when [x] is null or undefined.
513+
///
514+
/// This is only used by the compiler for the runtime null check operator `!`.
515+
nullCheck(x) {
516+
if (x == null) throw TypeErrorImpl("Unexpected null value.");
517+
return x;
518+
}
519+
507520
/// The global constant map table.
508521
final constantMaps = JS<Object>('!', 'new Map()');
509522

0 commit comments

Comments
 (0)