gwtproject / gwt

GWT Open Source Project
http://www.gwtproject.org
1.53k stars 377 forks source link

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

Open andreaskornstaedt opened 1 week ago

andreaskornstaedt commented 1 week ago

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 commented 2 days ago

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.");