[dart2js] Remove asserts in ConstConditionalSimplifier

Fixes: b/349652368
Change-Id: I0d5cad6a55cba6b31d8a352d278daf844db95efb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/373328
Reviewed-by: Nate Biggs <natebiggs@google.com>
Commit-Queue: Mayank Patke <fishythefish@google.com>
This commit is contained in:
Mayank Patke 2024-06-27 19:12:02 +00:00 committed by Commit Queue
parent a868b8d9d6
commit b64c64012f
4 changed files with 33 additions and 15 deletions

View file

@ -719,10 +719,22 @@ abstract class CommonElements {
/// The class for native annotations defined in dart:_js_helper.
late final ClassEntity nativeAnnotationClass = _findHelperClass('Native');
bool isAssertTest(MemberEntity member) =>
member.name == 'assertTest' &&
member.isFunction &&
member.isTopLevel &&
member.library == jsHelperLibrary;
late final assertTest = _findHelperFunction('assertTest');
late final assertThrow = _findHelperFunction('assertThrow');
bool isAssertHelper(MemberEntity member) =>
member.name == 'assertHelper' &&
member.isFunction &&
member.isTopLevel &&
member.library == jsHelperLibrary;
late final assertHelper = _findHelperFunction('assertHelper');
late final assertUnreachableMethod = _findHelperFunction('assertUnreachable');

View file

@ -144,7 +144,8 @@ void _simplifyConstConditionals(ir.Component component, CompilerOptions options,
evaluationMode: options.useLegacySubtyping
? fe.EvaluationMode.weak
: fe.EvaluationMode.strong,
shouldNotInline: shouldNotInline)
shouldNotInline: shouldNotInline,
removeAsserts: !options.enableUserAssertions)
.run();
}

View file

@ -1944,20 +1944,23 @@ class SsaInstructionSimplifier extends HBaseVisitor<HInstruction>
return argument;
}
}
} else if (element == commonElements.assertHelper ||
element == commonElements.assertTest) {
if (node.inputs.length == 1) {
HInstruction argument = node.inputs[0];
if (argument is HConstant) {
ConstantValue constant = argument.constant;
if (constant is BoolConstantValue) {
bool value = constant is TrueConstantValue;
if (element == commonElements.assertTest) {
// `assertTest(argument)` effectively negates the argument.
return _graph.addConstantBool(!value, _closedWorld);
} else {
final isAssertHelper = commonElements.isAssertHelper(element);
final isAssertTest = commonElements.isAssertTest(element);
if (isAssertHelper || isAssertTest) {
if (node.inputs.length == 1) {
HInstruction argument = node.inputs[0];
if (argument is HConstant) {
ConstantValue constant = argument.constant;
if (constant is BoolConstantValue) {
bool value = constant is TrueConstantValue;
if (isAssertTest) {
// `assertTest(argument)` effectively negates the argument.
return _graph.addConstantBool(!value, _closedWorld);
}
// `assertHelper(true)` is a no-op, other values throw.
if (value) return argument;
}
// `assertHelper(true)` is a no-op, other values throw.
if (value) return argument;
}
}
}

View file

@ -24,7 +24,9 @@ main(List<String> args) {
print('Testing computation of WorldImpact through ImpactData');
print('==================================================================');
await checkTests(dataDir, const ImpactDataComputer(),
args: args, testedConfigs: allSpecConfigs);
options: const ['--enable-asserts'],
args: args,
testedConfigs: allSpecConfigs);
});
}