ocaml / tuareg

Emacs OCaml mode
GNU General Public License v3.0
360 stars 79 forks source link

Prevent "Invalid search bound" error on long patterns #303

Closed bcc32 closed 1 year ago

bcc32 commented 1 year ago

When tuareg--pattern-pre-form-let is invoked at the beginning of a let binding whose LHS is very long (> 800 characters), it can signal the following error:

Invalid search bound (wrong side of point)

This happens when a balanced set of parentheses includes the limit position, so forward-list skips past it and thereby causes point to exceed limit.

Apply the same fix to tuareg--pattern-pre-form-fun by analogy. tuareg--pattern-equal-matcher is already correct.

Here's an example snippet where this error can happen:

let xxxx_xxxxxx
      (type value)
      ~(f :
          xxxx
        -> ( xxxxx * (xxxxx_xxxx, xxx_xxxxx) Xxxxxxxx_xxxxx.Xxxx_xx_xxxxxxx_xxxxx.x
           , (xxxxx_xxxx, xxx_xxxxx) Xxxxxx.x
           , xxxxx_xxxx )
             Xxxxxxx_xxxxxxxx_xxxxxx.Xx_xxx_xxxxxxxxxxx.x)
      ~(xxxx : Xxxx_xxxxxxx.Xxxxxx_xxxx.x)
      ~(xxxxxxxx : xxxxx Xxxxxxx.Xx_xxxxxxxxx.x)
      ~(xxxxxx_xxxxx : (xxxxx_xxxx, xxx_xxxxx) Xxxxxxxx_xxxxx.Xxxx_xx_xxxxxxx_xxxxx.xxxxxx)
      ~(xxxxxx_xxxxx : (xxxxx_xxxx, xxx_xxxxx) Xxxxxxxx_xxxxx.Xxxx_xx_xxxxxxx_xxxxx.xxxxxx)
      ~(xxxxxxxx_xxxxx : (xxxxx_xxxx, xxx_xxxxx) Xxxxxxxx_xxxxx.Xxxx_xx_xxxxxxx_xxxxx.x)
      ~(xxxxx_xxxxx : xxxxx -> xxxxx -> xxxx)
  : ( xxxxx * (xxxxx_xxxx, xxx_xxxxx) Xxxxxxxx_xxxxx.Xxxx_xx_xxxxxxx_xxxxx.x
    , (xxxxx_xxxx, xxx_xxxxx) Xxxxxx.x, xxxxx_xxxx )
      Xxxxxxx_xxxxxxxx_xxxxxx.Xx_xxx_xxxxxxxxxxx.x
  =
  xxxx
;;

Opening this file in tuareg-mode results in the following error in the *Messages* buffer:

Error during redisplay: (jit-lock-function 1) signaled (error "Invalid search bound (wrong side of point)")
mattiase commented 1 year ago

Looks sensible to me. (It would also make sense for re-search-forward to simply return nil if invoked beyond the starting limit, but unfortunately that's harder to change.)

monnier commented 1 year ago

Apply the same fix to tuareg--pattern-pre-form-fun by analogy. tuareg--pattern-equal-matcher is already correct.

Thanks, pushed.

Here's an example snippet where this error can happen:

let xxxx_xxxxxx
      (type value)
      ~(f :
          xxxx
        -> ( xxxxx * (xxxxx_xxxx, xxx_xxxxx) Xxxxxxxx_xxxxx.Xxxx_xx_xxxxxxx_xxxxx.x
           , (xxxxx_xxxx, xxx_xxxxx) Xxxxxx.x
           , xxxxx_xxxx )
             Xxxxxxx_xxxxxxxx_xxxxxx.Xx_xxx_xxxxxxxxxxx.x)
      ~(xxxx : Xxxx_xxxxxxx.Xxxxxx_xxxx.x)
      ~(xxxxxxxx : xxxxx Xxxxxxx.Xx_xxxxxxxxx.x)
      ~(xxxxxx_xxxxx : (xxxxx_xxxx, xxx_xxxxx) Xxxxxxxx_xxxxx.Xxxx_xx_xxxxxxx_xxxxx.xxxxxx)
      ~(xxxxxx_xxxxx : (xxxxx_xxxx, xxx_xxxxx) Xxxxxxxx_xxxxx.Xxxx_xx_xxxxxxx_xxxxx.xxxxxx)
      ~(xxxxxxxx_xxxxx : (xxxxx_xxxx, xxx_xxxxx) Xxxxxxxx_xxxxx.Xxxx_xx_xxxxxxx_xxxxx.x)
      ~(xxxxx_xxxxx : xxxxx -> xxxxx -> xxxx)
  : ( xxxxx * (xxxxx_xxxx, xxx_xxxxx) Xxxxxxxx_xxxxx.Xxxx_xx_xxxxxxx_xxxxx.x
    , (xxxxx_xxxx, xxx_xxxxx) Xxxxxx.x, xxxxx_xxxx )
      Xxxxxxx_xxxxxxxx_xxxxxx.Xx_xxx_xxxxxxxxxxx.x
  =
  xxxx
;;

That's some clever piece of code you have here. It probably deserves an explanatory comment :-)