Switch on String: forward default into each length bucket#60
Open
Pign wants to merge 1 commit into
Open
Conversation
`compileSwitchOptimizedForStrings` partitions cases by `String.length`
and emits a `switch(__temp.size()) { case N: { /* if-cascade */ } }`,
calling `compileSwitchAsIfs` per bucket with `null` for the default
expression. The user's `default:` is then only attached to the outer
size-switch (`compileDefaultCase(edef)`), which fires when the input's
length matches no bucket — but not when the length matches a bucket
and the value matches no case inside it. That input falls through the
inner if-cascade with no statement executed, silently breaking
statement-position string switches and tripping `-Werror=return-type`
on value-position ones.
Pass `edef` into the bucketed `compileSwitchAsIfs` so each cascade
ends with `else { /* user default */ }`. The outer-default emission
stays — it still handles unmatched-length inputs.
Repro: a switch on a String value with two same-length cases ("A",
"B") and a default; pass any other length-1 string and the default
never runs. Added two regression tests (statement-position and
value-position) under `test/unit_testing/tests/Switch/`.
4 tasks
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
compileSwitchOptimizedForStrings(insrc/cxxcompiler/subcompilers/Expressions.hx) buckets cases byString.lengthand emits aswitch(__temp.size()) { case N: { /* if-cascade */ } }. It callscompileSwitchAsIfsper bucket withnullfor the default expression, so the user'sdefault:only ends up attached to the outer size-switch viacompileDefaultCase(edef). That fires when the input's length matches no bucket, but not when the length matches a bucket and the value matches no case inside it. Such inputs fall through the inner if-cascade with no statement executed — silently breaking statement-position string switches and tripping-Werror=return-typeon value-position ones.Repro
The generated C++ has
case 1: { if (__temp == "H") { ... } else if (__temp == "W") ... }with no terminatingelse, so an input like"X"exits viabreak;, the size-switch finishes, and the function returns nothing.(Caught while writing a small bitmap-font helper in a real Haxe→C++ project; the
-Werror=return-typefrom gcc/clang made it surface immediately.)Fix
Pass
edefinto the bucketedcompileSwitchAsIfsso each cascade ends withelse { /* user default */ }. The outer-default emission is kept — it still covers unmatched-length inputs.Test plan
test/unit_testing/tests/Switch/:-Werror=return-type)auto __temp = ...;at function scope; calling two string switches from the same function would currently collide on__temp. That's a separate, pre-existing issue and isn't in scope here.haxe Test.hxml).-Werror.Reviewed in our fork at Pign#1 before opening here.