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

Add OCaml 5.00 support to the tests and update documentation #258

Merged
merged 1 commit into from
Apr 20, 2022
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
(unreleased)
------------

* Introduce `Ppx_deriving_runtime.Stdlib` with OCaml >= 4.07. This module
already exists in OCaml < 4.07 but was missing otherwise.

5.2.1 (02/02/2021)
------------------

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ The `[%derive.x:]` syntax can be shortened to `[%x:]`, given that the deriver `x

### Working with existing types

At first, it may look like _deriving_ requires complete control of the type declaration. However, a lesser-known OCaml feature allows to derive functions for any existing type. Using `Pervasives.fpclass` as an example, _show_ can be derived as follows:
At first, it may look like _deriving_ requires complete control of the type declaration. However, a lesser-known OCaml feature allows to derive functions for any existing type. Using `Stdlib.fpclass` as an example, _show_ can be derived as follows:

``` ocaml
# module M = struct
Expand All @@ -113,7 +113,7 @@ module M :
- : string = "FP_normal"
```

The module is used to demonstrate that `show_myfpclass` really accepts `Pervasives.fpclass`, and not just `M.myfpclass`.
The module is used to demonstrate that `show_myfpclass` really accepts `Stdlib.fpclass`, and not just `M.myfpclass`.

To avoid the need to repeat the type definition, it is possible to use [ppx_import](/~https://github.com/ocaml-ppx/ppx_import#usage) to automatically pull in the type definition. Attributes can be attached using its `[@with]` replacement feature.

Expand Down Expand Up @@ -184,7 +184,7 @@ This code will create printers which return the string `X.C`, `X` is a module pa
Plugins: eq and ord
-------------------

_eq_ derives a function comparing values by semantic equality; structural or physical depending on context. _ord_ derives a function defining a total order for values, returning a negative value if lower, `0` if equal or a positive value if greater. They're similar to `Pervasives.(=)` and `Pervasives.compare`, but are faster, allow to customize the comparison rules, and never raise at runtime. _eq_ and _ord_ are short-circuiting.
_eq_ derives a function comparing values by semantic equality; structural or physical depending on context. _ord_ derives a function defining a total order for values, returning a negative value if lower, `0` if equal or a positive value if greater. They're similar to `Stdlib.(=)` and `Stdlib.compare`, but are faster, allow to customize the comparison rules, and never raise at runtime. _eq_ and _ord_ are short-circuiting.

``` ocaml
# type t = [ `A | `B of int ] [@@deriving eq, ord];;
Expand Down
2 changes: 1 addition & 1 deletion src/api/ppx_deriving.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ let raise_errorf ?sub ?loc fmt =
#endif
let err = Location.error ?sub ?loc str in
raise (Location.Error err) in
Printf.kprintf raise_msg fmt
Printf.ksprintf raise_msg fmt

let create =
let def_ext_str name ~options ~path typ_ext =
Expand Down
4 changes: 2 additions & 2 deletions src/api/ppx_deriving.cppo.mli
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ val create_quoter : unit -> quoter
val quote : quoter:quoter -> expression -> expression

(** [sanitize module_ quoter expr] wraps [expr] in a way that ensures that the
contents of [module_] and {!Pervasives}, as well as the identifiers in
contents of [module_] and {!Stdlib}, as well as the identifiers in
expressions returned by [quote] are in scope, and returns the wrapped
expression. [module_] defaults to {!Ppx_deriving_runtime} if it's not
provided*)
Expand Down Expand Up @@ -236,7 +236,7 @@ val attr_warning: expression -> attribute
val free_vars_in_core_type : core_type -> tyvar list

(** [remove_pervasives ~deriver typ] removes the leading "Pervasives."
module name in longidents.
and "Stdlib." module name in longidents.
Type expressions marked with [\[\@nobuiltin\]] are ignored.

The name of the deriving plugin should be passed as [deriver]; it is used
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/ppx_deriving_runtime.cppo.mli
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ include module type of struct
include Stdlib
end

module Stdlib = Stdlib

module Result : sig
type ('a, 'b) t = ('a, 'b) result =
| Ok of 'a
Expand Down
2 changes: 1 addition & 1 deletion src_test/eq/test_deriving_eq.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type a7 = char [@@deriving eq]
type a8 = string [@@deriving eq]
type a9 = bytes [@@deriving eq]
type r1 = int ref [@@deriving eq]
type r2 = int Pervasives.ref [@@ocaml.warning "-3"][@@deriving eq]
type r2 = int Stdlib.ref [@@deriving eq]
type l = int list [@@deriving eq]
type a = int array [@@deriving eq]
type o = int option [@@deriving eq]
Expand Down
3 changes: 1 addition & 2 deletions src_test/fold/test_deriving_fold.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ let test_btree ctxt =
let btree = (Node (Node (Leaf, 3, Leaf), 1, Node (Leaf, 2, Leaf))) in
assert_equal ~printer:string_of_int 6 (fold_btree (+) 0 btree)

type 'a reflist = 'a Pervasives.ref list
[@@ocaml.warning "-3"]
type 'a reflist = 'a Stdlib.ref list
[@@deriving fold]

let test_reflist ctxt =
Expand Down
6 changes: 2 additions & 4 deletions src_test/iter/test_deriving_iter.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ module T : sig
type ('a,'b) record = { a : 'a; b : 'b }
[@@deriving iter]

type 'a reflist = 'a Pervasives.ref list
[@@ocaml.warning "-3"]
type 'a reflist = 'a Stdlib.ref list
[@@deriving iter]

end = struct
Expand All @@ -21,8 +20,7 @@ end = struct
type ('a,'b) record = { a : 'a; b : 'b }
[@@deriving iter]

type 'a reflist = 'a Pervasives.ref list
[@@ocaml.warning "-3"]
type 'a reflist = 'a Stdlib.ref list
[@@deriving iter]

end
Expand Down
4 changes: 2 additions & 2 deletions src_test/map/test_deriving_map.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ let test_var2 ctxt =
assert_equal ~printer:(show_var2 fmt_int) (A2 5) (map_var2 int_of_float (A2 5.))

let test_var3 ctxt =
let show,map = show_var3 fmt_int fmt_str, map_var3 ((+)1) String.uppercase [@warning "-3"] in
let show,map = show_var3 fmt_int fmt_str, map_var3 ((+)1) String.uppercase_ascii in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity: I assume you checked that we don't support anymore the old OCaml versions without uppercase_ascii?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, uppercase_ascii was added in OCaml 4.03 which we don’t support anyway

assert_equal ~printer:show (A3 2) (map (A3 1));
assert_equal ~printer:show (B3 false) (map (B3 false));
assert_equal ~printer:show (C3("ABC", A3 3)) (map (C3("abc", A3 2)));
Expand All @@ -123,7 +123,7 @@ let test_record2 ctxt =

let test_record3 ctxt =
let show,map = show_record3 fmt_int fmt_str,
map_record3 ((+)1) String.uppercase [@warning "-3"]
map_record3 ((+)1) String.uppercase_ascii
in
assert_equal ~printer:show {a3=5;b3=false;c3="ABC"} (map {a3=4;b3=false;c3="abc"});
let show,map = show_record3 fmt_int fmt_flt, map_record3 Char.code float_of_int in
Expand Down
3 changes: 1 addition & 2 deletions src_test/ord/test_deriving_ord.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ let test_ref1 ctxt =
assert_equal ~printer (0) (compare_r1 (ref 0) (ref 0));
assert_equal ~printer (1) (compare_r1 (ref 1) (ref 0))

type r2 = int Pervasives.ref
[@@ocaml.warning "-3"]
type r2 = int Stdlib.ref
[@@deriving ord]
let test_ref2 ctxt =
assert_equal ~printer (-1) (compare_r2 (ref 0) (ref 1));
Expand Down
2 changes: 1 addition & 1 deletion src_test/runtime/test_runtime.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let test_ref_included
let test_ref_qualified
(x : 'a ref)
=
(x : 'a Ppx_deriving_runtime.Pervasives.ref[@ocaml.warning "-3"])
(x : 'a Ppx_deriving_runtime.Stdlib.ref)

let test_backtrace
(x : Printexc.raw_backtrace)
Expand Down
4 changes: 2 additions & 2 deletions src_test/show/test_deriving_show.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type a7 = char [@@deriving show]
type a8 = string [@@deriving show]
type a9 = bytes [@@deriving show]
type r = int ref [@@deriving show]
type r2 = int Pervasives.ref [@@ocaml.warning "-3"] [@@deriving show]
type r3 = int Pervasives.ref ref [@@ocaml.warning "-3"] [@@deriving show]
type r2 = int Stdlib.ref [@@deriving show]
type r3 = int Stdlib.ref ref [@@deriving show]
type l = int list [@@deriving show]
type a = int array [@@deriving show]
type o = int option [@@deriving show]
Expand Down