1
0
mirror of https://github.com/dart-lang/sdk synced 2024-07-05 09:20:04 +00:00

[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 <nshahan@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
This commit is contained in:
Nicholas Shahan 2020-05-13 21:13:27 +00:00 committed by commit-bot@chromium.org
parent 123f92f239
commit 40c1f17434

View File

@ -3145,10 +3145,8 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
_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<js_ast.Expression>
// 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)) {