From 9b5f0f32eee29651cc313c2ae3eccdcf9dfd57f2 Mon Sep 17 00:00:00 2001 From: Mayank Patke Date: Fri, 31 Jul 2020 00:45:54 +0000 Subject: [PATCH] [dart2js] Fix folding of `x is Object` when `x` may be null. Change-Id: Ie45dae49c936e6a163963773747a17006f9554c1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/156481 Commit-Queue: Mayank Patke Reviewed-by: Stephen Adams --- pkg/compiler/lib/src/ssa/builder_kernel.dart | 3 +- pkg/compiler/lib/src/ssa/nodes.dart | 36 +++++++++++--------- pkg/compiler/lib/src/ssa/optimize.dart | 4 --- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/pkg/compiler/lib/src/ssa/builder_kernel.dart b/pkg/compiler/lib/src/ssa/builder_kernel.dart index 1056d8a7068..24104e52532 100644 --- a/pkg/compiler/lib/src/ssa/builder_kernel.dart +++ b/pkg/compiler/lib/src/ssa/builder_kernel.dart @@ -6308,7 +6308,7 @@ class TryCatchFinallyBuilder { AbstractValue unwrappedType = kernelBuilder._typeInferenceMap .getReturnTypeOf(kernelBuilder._commonElements.exceptionUnwrapper); - if (!kernelBuilder.options.useLegacySubtyping) { + if (kernelBuilder.options.useNullSafety) { // Global type analysis does not currently understand that strong mode // `Object` is not nullable, so is imprecise in the return type of the // unwrapper, which leads to unnecessary checks for 'on Object'. @@ -6326,7 +6326,6 @@ class TryCatchFinallyBuilder { int catchesIndex = 0; void pushCondition(ir.Catch catchBlock) { - // `guard` is often `dynamic`, which generates `true`. kernelBuilder._pushIsTest(catchBlock.guard, unwrappedException, kernelBuilder._sourceInformationBuilder.buildCatch(catchBlock)); } diff --git a/pkg/compiler/lib/src/ssa/nodes.dart b/pkg/compiler/lib/src/ssa/nodes.dart index 86310fefc3a..cc52e28f68e 100644 --- a/pkg/compiler/lib/src/ssa/nodes.dart +++ b/pkg/compiler/lib/src/ssa/nodes.dart @@ -4130,26 +4130,30 @@ AbstractBool _isTestResult( AbstractValueDomain abstractValueDomain = closedWorld.abstractValueDomain; AbstractValue subsetType = expression.instructionType; AbstractValue supersetType = checkedAbstractValue.abstractValue; + AbstractBool expressionIsNull = expression.isNull(abstractValueDomain); - if (useNullSafety && - expression.isNull(abstractValueDomain).isDefinitelyTrue) { - if (dartType.isObject) return AbstractBool.False; - if (closedWorld.dartTypes.isTopType(dartType) || - dartType is NullableType || - dartType.isNull) { - return AbstractBool.True; - } - if (dartType is TypeVariableType || dartType is FunctionTypeVariable) { - return AbstractBool.Maybe; - } - if (dartType is LegacyType) { - DartType baseType = dartType.baseType; - if (baseType is NeverType) return AbstractBool.True; - if (baseType is TypeVariableType || baseType is FunctionTypeVariable) { + if (useNullSafety) { + if (expressionIsNull.isDefinitelyTrue) { + if (dartType.isObject) return AbstractBool.False; + if (closedWorld.dartTypes.isTopType(dartType) || + dartType is NullableType || + dartType.isNull) { + return AbstractBool.True; + } + if (dartType is TypeVariableType || dartType is FunctionTypeVariable) { return AbstractBool.Maybe; } + if (dartType is LegacyType) { + DartType baseType = dartType.baseType; + if (baseType is NeverType) return AbstractBool.True; + if (baseType is TypeVariableType || baseType is FunctionTypeVariable) { + return AbstractBool.Maybe; + } + } + return AbstractBool.False; + } else if (expressionIsNull.isPotentiallyTrue) { + if (dartType.isObject) return AbstractBool.Maybe; } - return AbstractBool.False; } if (checkedAbstractValue.isPrecise && diff --git a/pkg/compiler/lib/src/ssa/optimize.dart b/pkg/compiler/lib/src/ssa/optimize.dart index e21dd7d5b0e..a410a558373 100644 --- a/pkg/compiler/lib/src/ssa/optimize.dart +++ b/pkg/compiler/lib/src/ssa/optimize.dart @@ -1887,10 +1887,6 @@ class SsaInstructionSimplifier extends HBaseVisitor if (typeInput is HLoadType) { TypeExpressionRecipe recipe = typeInput.typeExpression; DartType dartType = recipe.type; - if (_closedWorld.dartTypes.isTopType(dartType)) { - return _graph.addConstantBool(true, _closedWorld); - } - IsTestSpecialization specialization = SpecializedChecks.findIsTestSpecialization( dartType, _graph, _closedWorld);