Skip to content

Commit 2564a76

Browse files
committed
Use match variable in mappings to avoid unsed variables
1 parent f6bb78c commit 2564a76

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

bitstring.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is generated by dune, edit dune-project instead
22
opam-version: "2.0"
3-
version: "5.0.1"
3+
version: "5.0.2"
44
synopsis: "Bitstrings and bitstring matching for OCaml"
55
description: """
66
The ocaml-bitstring project adds Erlang-style bitstrings and matching over bitstrings as a syntax extension and library for OCaml.

dune-project

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
(name bitstring)
44

5-
(version 5.0.1)
5+
(version 5.0.2)
66

77
(generate_opam_files true)
88

@@ -34,7 +34,7 @@
3434
)
3535
(depends
3636
(ocaml (>= 5.2.0))
37-
(bitstring (>= 5.0.1))
37+
(bitstring (>= 5.0.2))
3838
(ocaml (and :with-test (>= 5.2.0)))
3939
(ppxlib (>= 0.36.0))
4040
(ounit :with-test)))

ppx/ppx_bitstring.ml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ open Ppxlib
1818
open Printf
1919
open Ast_builder.Default
2020

21+
(* Exception *)
22+
23+
let location_exn ~loc msg =
24+
Location.raise_errorf ~loc "%s" msg
25+
;;
26+
2127
(* Type definition *)
2228

2329
module Entity = struct
@@ -167,13 +173,12 @@ module MatchField = struct
167173
| Any of Parsetree.pattern
168174
| Tuple of tuple
169175
;;
170-
end
171176

172-
(* Exception *)
173-
174-
let location_exn ~loc msg =
175-
Location.raise_errorf ~loc "%s" msg
176-
;;
177+
let as_evar v =
178+
match v.pat with
179+
| {ppat_desc = Ppat_var(v); _} -> evar ~loc:v.loc v.txt
180+
| {ppat_loc; _} -> location_exn ~loc:ppat_loc "Pattern is not a variable"
181+
end
177182

178183
(* Helper functions *)
179184

@@ -691,7 +696,8 @@ let gen_value ~loc fld res beh =
691696
| Some b, None ->
692697
[%expr let [%p fld.pat] = [%e b] in [%e beh]][@metaloc loc]
693698
| None, Some m ->
694-
[%expr let [%p fld.pat] = [%e m] [%e res] in [%e beh]][@metaloc loc]
699+
let exp = MatchField.as_evar fld in
700+
[%expr let [%p fld.pat] = [%e m] [%e exp] in [%e beh]][@metaloc loc]
695701
| _, _ -> beh
696702
;;
697703

ppx_bitstring.opam

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is generated by dune, edit dune-project instead
22
opam-version: "2.0"
3-
version: "5.0.1"
3+
version: "5.0.2"
44
synopsis: "Bitstrings and bitstring matching for OCaml - PPX extension"
55
description: """
66
The ocaml-bitstring project adds Erlang-style bitstrings and matching over bitstrings as a syntax extension and library for OCaml.
@@ -15,7 +15,7 @@ bug-reports: "https://github.com/xguerin/bitstring/issues"
1515
depends: [
1616
"dune" {>= "2.7"}
1717
"ocaml" {>= "5.2.0"}
18-
"bitstring" {>= "5.0.1"}
18+
"bitstring" {>= "5.0.2"}
1919
"ocaml" {with-test & >= "5.2.0"}
2020
"ppxlib" {>= "0.36.0"}
2121
"ounit" {with-test}

tests/BitstringQualifierTest.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@ open Bitstring
2222
*)
2323

2424
let map_test context =
25-
let source = [%bitstring {| 1 : 16 ; 2 : 16 |}] in
25+
let source = [%bitstring {| 1 : 16 ; 2 : 16 ; 0 : 16 |}] in
2626
match%bitstring source with
2727
| {| value0 : 16 : map (fun v -> v + 1)
2828
; value1 : 16 : map (fun v -> Some v)
29+
; value2 : 16 : map (fun v -> match v with | 0 -> false | _ -> true)
2930
|} ->
3031
assert_equal value0 2;
3132
begin match value1 with
3233
| Some v -> assert_equal v 2
3334
| _ -> assert_bool "Invalid map result" false
34-
end
35+
end;
36+
assert_bool "Invalid boolean value" (not value2)
3537
| {| _ |} -> assert_bool "Invalid pattern" false
3638

3739
(*

0 commit comments

Comments
 (0)