Potential bug in code generation of subpattern of integers (`| HttpError({status: 502 | 503 | 504}) =>`). Rescript 12 and 13

Look at the code generated by this snippet:

exception NetworkError({status: int, message: string})

type response
exception HttpError({status: int, message: string, response: response})

let isRetriable: exn => bool = error =>
  switch error {
  | NetworkError(_) => true
  | HttpError({status: 502 | 503 | 504}) => true
  | _ => false
  }

The generated JS uses (error.status - 502 | 0) <= 2 but that is also true for 500, 501, 404, etc.

function isRetriable(error) {
  if (error.RE_EXN_ID === NetworkError) {
    return true;
  } else if (error.RE_EXN_ID === HttpError) {
    return (error.status - 502 | 0) <= 2;
  } else {
    return false;
  }
}

Rescript 13.0 (alpha 5), emits the same JS.

4 Likes

hey thanks for reporting that, it looks like a serious bug indeed!

2 Likes

Rescript 10 and 11 generated:

(error.status - 502 >>> 0) <= 2

Which does get the right answer.

I used Clade Code to vibe a possible solution. :crossed_fingers:

2 Likes

thanks a lot for finding and fixing this bug @mvaled!

This one triggered a lot of fixes in a lot of places. Soon v13 compiling and running should work better on the playground as well.

2 Likes