syntax icon indicating copy to clipboard operation
syntax copied to clipboard

annoymous function was not handled correctly

Open bobzhang opened this issue 5 years ago • 5 comments

type foo =
| Bar
| Baz;
let x = 2;
let fooToString =
fun
| Bar => string_of_int (x) + "Bar"
| Baz => "Baz";

npx bsc -format xx.re

type foo =
  | Bar
  | Baz
let x = 2

let fooToString = x =>
  switch x {
  | Bar => string_of_int(x) + "Bar"
  | Baz => "Baz"
  }

bobzhang avatar Sep 27 '20 04:09 bobzhang

ping? This seems a serious bug

bobzhang avatar Nov 12 '20 07:11 bobzhang

@bobzhang will prioritize, looking into this today

IwanKaramazow avatar Nov 12 '20 08:11 IwanKaramazow

IIUC, this is a bug only in printing, in rescript syntax, all parameters should be named, is it correct?

bobzhang avatar Jan 04 '21 09:01 bobzhang

Yes, to explain it in ast-terms, we don't have syntax for Pexp_function only for Pexp_fun. When you convert/print fun | (Pexp_function) from OCaml or Reason we transform it into a Pexp_fun with switch x { | … }.

I've looked into the naming problem, but the only "correct" way would be to re-introduce a syntax for Pexp_function. The idea was that forcing naming of parameters would be a good thing…

But this can be re-done. I'm open to suggestions.

IwanKaramazow avatar Jan 04 '21 11:01 IwanKaramazow

I hit a bug when doing the formatting, the function get broken is below:

let rec add x data = function
    Empty ->
    Node(Empty, x, data, Empty, 1)
  | Node(l, v, d, r, h) ->
    let c = compare x v in
    if c = 0 then
      Node(l, x, data, r, h)
    else if c < 0 then
      bal (add x data l) v d r
    else
      bal l v d (add x data r)

So the conversion is of high risky, a temporary solution is to change names from x to argument to reduce the risk.

Edit: since the parser already know one of the argument is called x, it should error out for such transformation to tell users to adapt their code

bobzhang avatar Mar 20 '21 07:03 bobzhang

The rescript-lang/syntax repo is obsolete and will be archived soon. If this issue is still relevant, please reopen in the compiler repo (https://github.com/rescript-lang/rescript-compiler) or comment here to ask for it to be moved. Thank you for your contributions.

stale[bot] avatar May 28 '23 23:05 stale[bot]