Skip to content

Commit a545a47

Browse files
committed
ppx: fix unused self
1 parent 0bd02fe commit a545a47

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Compiler: fix purity of comparison functions (again) (#2092)
1313
* Compiler: fix inlining (#2107)
1414
* Runtime/wasm: fix Unix.times (#2096)
15+
* Ppx: disable spurious warning for unused "self" in object litteral (#2128)
1516

1617
# 6.2.0 (2025-07-30) - Lille
1718

lib/tests/dune.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@
5959
(preprocess
6060
(pps ppx_js_internal ppx_expect)))
6161

62+
(library
63+
;; lib/tests/test_misc.ml
64+
(name test_misc_75)
65+
(enabled_if true)
66+
(modules test_misc)
67+
(libraries js_of_ocaml unix)
68+
(inline_tests (modes js wasm))
69+
(preprocess
70+
(pps ppx_js_internal ppx_expect)))
71+
6272
(library
6373
;; lib/tests/test_nodejs_filesystem_errors.ml
6474
(name test_nodejs_filesystem_errors_75)

lib/tests/test_misc.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
open Js_of_ocaml
2+
3+
(* Make sure we don't emit an unused warning for method not using "self" *)
4+
5+
let x =
6+
object%js (self)
7+
method foo = self##bar
8+
9+
method bar = ()
10+
end

ppx/ppx_js/lib_internal/ppx_js_internal.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,21 @@ let literal_object self_id (fields : field_desc list) =
697697
let body = function
698698
| Val (_, _, _, body) -> body
699699
| Meth (_, _, _, body, _) ->
700+
let body =
701+
match self_id.ppat_desc with
702+
| Ppat_var id ->
703+
(* mark self as used to avoid spurious unused-var-strict warning, see gh#2125 *)
704+
Ast_builder.Default.pexp_let
705+
~loc:Location.none
706+
Nonrecursive
707+
[ Ast_builder.Default.value_binding
708+
~pat:(Ast_builder.Default.ppat_any ~loc:Location.none)
709+
~loc:Location.none
710+
~expr:(Ast_builder.Default.evar id.txt ~loc:Location.none)
711+
]
712+
body
713+
| _ -> body
714+
in
700715
lift_function_body_constraint
701716
(Ast_builder.Default.pexp_fun
702717
~loc:{ body.pexp_loc with loc_ghost = true }

0 commit comments

Comments
 (0)