From 40c1f174347d0c4aa2161538894234c7dd4c92cd Mon Sep 17 00:00:00 2001 From: Nicholas Shahan Date: Wed, 13 May 2020 21:13:27 +0000 Subject: [PATCH] [ddc] Fix null check in equality operator Move null check up so it can return before performing the cast in the case of a covariant parameter. Change-Id: I727822751e6613fac635fa49b254b9406eb93e1c Fixes: https://github.com/dart-lang/sdk/issues/41866 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/147812 Commit-Queue: Nicholas Shahan Reviewed-by: Sigmund Cherem --- pkg/dev_compiler/lib/src/kernel/compiler.dart | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/dev_compiler/lib/src/kernel/compiler.dart b/pkg/dev_compiler/lib/src/kernel/compiler.dart index 39e6830a10c..bdf70de91dd 100644 --- a/pkg/dev_compiler/lib/src/kernel/compiler.dart +++ b/pkg/dev_compiler/lib/src/kernel/compiler.dart @@ -3145,10 +3145,8 @@ class ProgramCompiler extends ComputeOnceConstantVisitor _emitCovarianceBoundsCheck(f.typeParameters, body); void initParameter(VariableDeclaration p, js_ast.Identifier jsParam) { - if (isCovariantParameter(p)) { - var castExpr = _emitCast(jsParam, p.type); - if (!identical(castExpr, jsParam)) body.add(castExpr.toStatement()); - } + // When the parameter is covariant, insert the null check before the + // covariant cast to avoid a TypeError when testing equality with null. if (name == '==') { // In Dart `operator ==` methods are not called with a null argument. // This is handled before calling them. For performance reasons, we push @@ -3158,7 +3156,15 @@ class ProgramCompiler extends ComputeOnceConstantVisitor // the Dart code already handles it (typically by an `is` check). // Eliminate it when possible. body.add(js.statement('if (# == null) return false;', [jsParam])); - } else if (_annotatedNullCheck(p.annotations)) { + } + if (isCovariantParameter(p)) { + var castExpr = _emitCast(jsParam, p.type); + if (!identical(castExpr, jsParam)) body.add(castExpr.toStatement()); + } + + if (name == '==') return; + + if (_annotatedNullCheck(p.annotations)) { body.add(_nullParameterCheck(jsParam)); } else if (_mustBeNonNullable(p.type) && !_annotatedNotNull(p.annotations)) {