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

Fix: destructing record field in pattern #1436

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/analysis/destruct.ml
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,14 @@ let rec node config source selected_node parents =
(* If only one pattern is generated, then we're only refining the
current pattern, not generating new branches. *)
let ppat = filter_pat_attr (Untypeast.untype_pattern more_precise) in
let str = Mreader.print_pretty
config source (Pretty_pattern ppat) in
let str = Mreader.print_pretty config source (Pretty_pattern ppat) in
let str = (* { r<|> } should result in { r = <destructed value> } *)
match patt, parents with
| { pat_desc = Tpat_var (_, {txt; _ })},
(Pattern { pat_desc = Typedtree.Tpat_record _; _ }) :: _ ->
txt ^ " = " ^ str
| _ -> str
in
patt.Typedtree.pat_loc, str
| sub_patterns ->
let rev_before, after, top_patt =
Expand Down
50 changes: 50 additions & 0 deletions tests/test-dirs/destruct/record.t
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,53 @@ Record fields in patterns should also be refinable:
],
"notifications": []
}

Destructing a record field should leave the field name in place

$ $MERLIN single case-analysis -start 3:10 -end 3:10 -filename test.ml <<EOF
> type r = { x : int }
> type r_out = { r : r ; y : string }
> let f ({ r ; y } : r_out) = ()
> EOF
{
"class": "return",
"value": [
{
"start": {
"line": 3,
"col": 9
},
"end": {
"line": 3,
"col": 10
}
},
"r = { x }"
],
"notifications": []
}

Destructing a record field (variant)

$ $MERLIN single case-analysis -start 3:10 -end 3:10 -filename test.ml <<EOF
> type v = A | B of int
> type r = { v : v ; y:string }
> let f ({ v ; y } : r) = ()
> EOF
{
"class": "return",
"value": [
{
"start": {
"line": 3,
"col": 7
},
"end": {
"line": 3,
"col": 16
}
},
"({ v = A; y } : r) |({ v = B _; y } : r)"
],
"notifications": []
}