From 5a595124b47efadbebf95146a20d4645864fd93b Mon Sep 17 00:00:00 2001 From: Stephen Adams Date: Thu, 22 Jun 2023 19:55:22 +0000 Subject: [PATCH] [dart2js] Handle `null` as a switch statement case value. Bug: 51527 Change-Id: I6203c9aa668689bb44800082864174b9fb215289 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/310500 Reviewed-by: Nicholas Shahan Commit-Queue: Stephen Adams --- pkg/compiler/lib/src/ssa/codegen.dart | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/compiler/lib/src/ssa/codegen.dart b/pkg/compiler/lib/src/ssa/codegen.dart index 1195670d0fce..0cd35a58c7f8 100644 --- a/pkg/compiler/lib/src/ssa/codegen.dart +++ b/pkg/compiler/lib/src/ssa/codegen.dart @@ -817,9 +817,18 @@ class SsaCodeGenerator implements HVisitor, HBlockInformationVisitor { // omit the code for it. if (successor.isLive) { do { - visit(inputs[inputIndex]); + final input = inputs[inputIndex]; + visit(input); currentContainer = js.Block.empty(); cases.add(js.Case(pop(), currentContainer)); + if (input is HConstant && input.constant is NullConstantValue) { + // JavaScript case expressions match on `===`, which means that the + // just emitted `case null:` will not catch `undefined`. + // Add `case void 0:` to catch `undefined`. + currentContainer = js.Block.empty(); + cases.add( + js.Case(js.Prefix('void', js.number(0)), currentContainer)); + } inputIndex++; } while ((successors[inputIndex - 1] == successor) && (inputIndex < inputs.length));