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

no break for switch #1118

Closed
unlessgames opened this issue Mar 23, 2024 · 4 comments · Fixed by #1141
Closed

no break for switch #1118

unlessgames opened this issue Mar 23, 2024 · 4 comments · Fixed by #1141
Labels
bug Something isn't working

Comments

@unlessgames
Copy link

unlessgames commented Mar 23, 2024

I would expect a break in this code at the end of the when 0 block

fn := (x : number) =>
  switch x
    when 0
      num := 10
      switch num
        <? "number"
          return 0
    when 1
      return 1

but it's not there in the output.

It's different when the same thing is not inside the function

x := 1

switch x
  when 0
    num := 10
    switch num
      <? "number"
        return 0
  when 1
    return 1

Now the break is included (of course this code is not valid because of the top level return but yeah..)

Currently using noFallthroughCasesInSwitch in tsconfig.json to spot these errors.

@edemaine edemaine added the bug Something isn't working label Mar 23, 2024
@bbrk24
Copy link
Contributor

bbrk24 commented Mar 26, 2024

It's omitting the break because it's the last statement of the function. If I add a call to an imaginary function g():

fn := (x : number) =>
  switch x
    when 0
      num := 10
      switch num
        <? "number"
          return 0
    when 1
      return 1
  g()

suddenly the break is there. Heck, the following statement doesn't even have to be substantial -- if I replace g() with ; in the above code it still emits the break.

@edemaine
Copy link
Collaborator

Right, it's clearly a bug in our code to detect whether a break is necessary (which we added to make eslint happier, IIRC).

@STRd6
Copy link
Contributor

STRd6 commented Mar 26, 2024

I think it's assuming that the inner switch is exhaustive when it's actually not. We should be able to do a bit better with it.

@bbrk24
Copy link
Contributor

bbrk24 commented Mar 26, 2024

No, surprisingly -- if you add an else block that doesn't return, it still doesn't break.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants