From 4ecbae0aaffe2d770276a0f24fcd30e2462751cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Mon, 3 Apr 2023 14:17:49 +0000 Subject: [PATCH] [dart2wasm] Update switch-case compilation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With https://dart-review.googlesource.com/c/sdk/+/289224 the front-end can now generate switch-case expressions where type of the `switch` expression is different than the types of expressions in `case`s. To handle this case we have two options: 1. Compare type of `switch` and `case`s and compile the code to just evaluate the switch expressions and the default case. 2. Use reference equality This CL implements (2) as it's simpler and no correct code will have a switch-case with differently typed switch and case expressions. Fixes #51793, Fixes tests: - co19/Language/Statements/Switch/execution_t01 - co19/Language/Statements/Switch/type_t01 - co19/Language/Statements/Switch/type_t02 Change-Id: I7eece161249498cf19c9f5245b3898c15a721b60 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290063 Reviewed-by: Aske Simon Christensen Commit-Queue: Ömer Ağacan --- pkg/dart2wasm/lib/code_generator.dart | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/dart2wasm/lib/code_generator.dart b/pkg/dart2wasm/lib/code_generator.dart index 5538b47da44..d9cb67a51c5 100644 --- a/pkg/dart2wasm/lib/code_generator.dart +++ b/pkg/dart2wasm/lib/code_generator.dart @@ -1366,12 +1366,17 @@ class CodeGenerator extends ExpressionVisitor1 return; } + final switchExprClass = + translator.classForType(dartTypeOf(node.expression)); + bool check() => node.cases.expand((c) => c.expressions).every((e) => e is L || e is NullLiteral || (e is ConstantExpression && - (e.constant is C || e.constant is NullConstant))); + (e.constant is C || e.constant is NullConstant) && + (translator.hierarchy.isSubtypeOf( + translator.classForType(dartTypeOf(e)), switchExprClass)))); // Identify kind of switch. One of `nullableType` or `nonNullableType` will // be the type for Wasm local that holds the switch value. @@ -1407,7 +1412,6 @@ class CodeGenerator extends ExpressionVisitor1 compare = () => call(translator.stringEquals.reference); } else { // Object switch - assert(check()); nonNullableType = w.RefType.eq(nullable: false); nullableType = w.RefType.eq(nullable: true); compare = () => b.ref_eq();