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 · 2 comments · May be fixed by #10059

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

niloc132 added a commit to niloc132/gwt that referenced this issue Dec 13, 2024
JDT generates a yield for arrow cases if the arrow points at a
expression, even though the expression cannot be returned. Synthesize a
block wrapping that expression as a statement, and add a break to the
end of it.

Fixes gwtproject#10044
@niloc132 niloc132 linked a pull request Dec 13, 2024 that will close this issue
@niloc132
Copy link
Member

A PR has been put up for review, passing all nightly CI tests.

The GWT SDK zip for this branch can be downloaded from https://github.com/niloc132/gwt/actions/runs/12319013728/artifacts/2318543974

There is a maven snapshot for this with version gwt-2.13.0-10044-case-yield-test-SNAPSHOT at https://repo.vertispan.com/gwt-snapshot/ also available for testing.

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

Successfully merging a pull request may close this issue.

3 participants