Skip to content

Commit

Permalink
[ddc] Change ! failure to a throw TypeError
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
nshahan authored and [email protected] committed Jul 10, 2020
1 parent 6861212 commit cf57f88
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion pkg/dev_compiler/lib/src/kernel/compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5212,8 +5212,9 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
@override
js_ast.Expression visitNullCheck(NullCheck node) {
var expr = node.operand;
var jsExpr = _visitExpression(expr);
// If the expression is non-nullable already, this is a no-op.
return isNullable(expr) ? notNull(expr) : _visitExpression(expr);
return isNullable(expr) ? runtimeCall('nullCheck(#)', [jsExpr]) : jsExpr;
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,9 @@ asInt(obj) {

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

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

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

/// Checks for null or undefined and returns [x].
///
/// Throws a [TypeError] when [x] is null or undefined.
///
/// This is only used by the compiler for the runtime null check operator `!`.
nullCheck(x) {
if (x == null) throw TypeErrorImpl("Unexpected null value.");
return x;
}

/// The global constant map table.
final constantMaps = JS<Object>('!', 'new Map()');

Expand Down

0 comments on commit cf57f88

Please sign in to comment.