-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dart2wasm] Fix dynamic switch casts.
If the switch's expression has type 'dynamic' and all the case expressions have the same type, we compare them using '=='. This requires a cast to ensure all the types match for the dispatch to the '==' function. However, we don't check that the type of the switch expression matches the type of the case expressions. So the cast fails if they don't match. This adds a guard to ensure the types match before running through the case expressions. If the guard fails, we either jump to the default case or if there isn't one, we skip the switch entirely. Fixes: #59782 Change-Id: I12e81f98d1c2046ee47e8ca4371642fd40620636 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/402460 Commit-Queue: Nate Biggs <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:expect/expect.dart'; | ||
|
||
void main() { | ||
String? result; | ||
dynamic number = 42; | ||
switch (number) { | ||
case 'a': | ||
result = 'a'; | ||
case 'b': | ||
result = 'b'; | ||
default: | ||
result = 'default'; | ||
} | ||
|
||
Expect.equals(result, 'default'); | ||
|
||
result = null; | ||
switch (number) { | ||
case 'a': | ||
result = 'a'; | ||
case 'b': | ||
result = 'b'; | ||
} | ||
Expect.isNull(result); | ||
} |