rescript-lang / rescript-lang/reanalyze

-write produces syntax error in OCaml w.r.t. destructive substitution on module-items and tuple pattern-matching

Open
#187 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
OCaml
Stars
281
Forks
22
Avg merge
1d 19h
Merged PRs (30d)
7

Description

Given this input (a destructive substitution of a module-item):

module Frequency : Wrap.S with type u := int = struct
  exception UnknownFrequency of int

  type t =
    | Daily
    | Monthly

  (* TODO: Right now billing only supports 3, 5, and 7. Fix this once billing is updated. *)
  let wrap = function
    | 1 -> Daily
    | 30 -> Monthly
    | n -> raise (UnknownFrequency n)

  let unwrap = function
    | Daily -> 1
    | Monthly -> 30
end

… Reanalyze v2.23.0 invoked with -write produces this (syntactically invalid, and also nonsensical in this case hahaha) output:

module Frequency : Wrap.S with type u := int [@@dead "Frequency.+unwrap"]  = struct
  exception UnknownFrequency of int

  type t =
    | Daily
    | Monthly

  (* TODO: Right now billing only supports 3, 5, and 7. Fix this once billing is updated. *)
  let wrap = function
    | 1 -> Daily
    | 30 -> Monthly
    | n -> raise (UnknownFrequency n) [@@dead "Frequency.+wrap"] 

  let unwrap = function
    | Daily -> 1
    | Monthly -> 30 [@@dead "Frequency.+unwrap"] 
end

Note that [@@dead "Frequency.+unwrap"] appears twice, in both the correct and nonsensical locations. A bug perhaps? (=


Similar issues when pattern-matching on a tuple:

let target, _group, filters = populate_mapping target group filters

... becomes ...

let target, _group [@@dead "Mappings.+_group"] , filters = populate_mapping target group filters

All told, -write dropped a few dozen syntax-errors across our codebase. Not the biggest deal, except I'm not sure how to correctly annotate these values so the next -write dosen't simply re-add them …

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the destructive-substitution and tuple-pattern examples with Reanalyze v2.23.0 and tracing the -write path that places [@@dead] annotations. Check how module items and tuple patterns are mapped to annotations; done means -write emits syntactically valid OCaml without duplicate or misplaced annotations for these cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
ocaml
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.