Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An single-assigment case-statement without {} within a switch generates a superfluous return, leading to function termination #10044

Open
andreaskornstaedt opened this issue Nov 13, 2024 · 1 comment

Comments

@andreaskornstaedt
Copy link

**GWT version: 2.12.1
**Browser (with version): all
**Operating System: all


Description

Single assignments as case statements should behave the same whether within {} or not.

Instead, with {} around them, these assignments generate correct JS, but if not surrounded with {}, the generated case statements have superfluous return statements that lead to function termination

Steps to reproduce
public void onModuleLoad() {
  String s = "a";
  switch (s) {
    case "a" -> result = 1;
    default -> result = 2;
   }
   Window.alert("result:" + result); // is never reached 
}

generated JS:

_.onModuleLoad_0_g$ = function z_g$(){
  var s_0_g$;
  s_0_g$ = 'a';
  switch (s_0_g$) {
    case 'a':
      return this.result_1_g$ = 1;
    default:return this.result_1_g$ = 2;
  }
  X9c_g$('result:' + this.result_1_g$);
}
;
Known workarounds

Put {} around case-statements

public void onModuleLoad() {
  String s = "a";
  switch (s) {
    case "a" -> { result = 1; }
    default -> { result = 2; }
   }
   Window.alert("result:" + result); // is reached 
}

generated JS:

_.onModuleLoad_0_g$ = function z_g$(){
  var s_0_g$;
  s_0_g$ = 'a';
  switch (s_0_g$) {
    case 'a':
      {
        this.result_1_g$ = 1;
        break;
      }

    default:{
        this.result_1_g$ = 2;
        break;
      }

  }
  X9c_g$('result:' + this.result_1_g$);
}
;
@craigmit
Copy link

craigmit commented Nov 21, 2024

An even simpler example of the issue:

switch (1) {
	case 1 -> GWT.log("Blah1");
	case 2 -> GWT.log("Blah2");
}
Window.alert("This will never show.");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants