HaxeFoundation / HaxeFoundation/haxe

[js] switch on const value leaves behind empty switch

Open
#7,786 1 comment 0 reactions 1 assignee Claimed by @Simn View on GitHub
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

Target JS, with analyzer. Same behavior on 3.4.7 and on Haxe 4 nightly as of 2019/02/13.
The following code works as expected, but leaves behind an empty switch. See http://try-haxe.mrcdk.com/#Df7e7.
```haxe
class Test {
static function main() {
// var anim = '';
var state = GoingLeft;
inline function set(s) {
state = s;
// anim = getAnim(s);
trace(getAnim(s));
}
if(Math.random() > 0.5) set(GoingLeft);
// trace(anim);
trace(state);
}

static inline function getAnim(state:ActorStateType):String {
return switch(state) {
case GoingLeft: 'left';
case GoingRight: 'right';
}
}
}

@:enum abstract ActorStateType(Int) from Int to Int {
var GoingLeft = 0;
var GoingRight = 1;
}
```
This code is in the compiled js:
```js
switch(s) {
case 0:
break;
case 1:
break;
}
```
Furthermore, uncommenting the extra lines also produces:
```js
switch(s) {
case 0:
anim = "left";
break;
case 1:
break;
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.