From eb3e3b72ba82507f55c76dfd27e6d5ab695c6298 Mon Sep 17 00:00:00 2001 From: Thierry Martinez Date: Sun, 24 May 2020 14:13:23 +0200 Subject: [PATCH] Cherry-picking stronger type equalities into pre-ppxlib This pull-request cherry-picks https://github.com/ocaml-ppx/ppx_deriving/pull/223 into pre-ppxlib. --- src/runtime/ppx_deriving_runtime.cppo.ml | 31 +++--- src/runtime/ppx_deriving_runtime.cppo.mli | 111 +++++++++------------- 2 files changed, 62 insertions(+), 80 deletions(-) diff --git a/src/runtime/ppx_deriving_runtime.cppo.ml b/src/runtime/ppx_deriving_runtime.cppo.ml index bcf252e8..a191b09f 100644 --- a/src/runtime/ppx_deriving_runtime.cppo.ml +++ b/src/runtime/ppx_deriving_runtime.cppo.ml @@ -14,22 +14,25 @@ type nonrec int64 = int64 type nonrec 'a lazy_t = 'a lazy_t type nonrec bytes = bytes -#if OCAML_VERSION >= (4, 08, 0) -(* We require 4.08 while 4.07 already has a Stdlib module. - In 4.07, the type equalities on Stdlib.Pervasives - are not strong enough for the 'include Stdlib' - below to satisfy the signature constraints on - Ppx_deriving_runtime.Pervasives. *) +#if OCAML_VERSION >= (4, 07, 0) module Stdlib = Stdlib include Stdlib module Result = struct - type ('a, 'b) t = ('a, 'b) Result.t = + (* Type manifest shoud be [('a, 'b) result]: + - it can't be [Result.t] because [Result] is not defined in 4.07 std-lib + and the result package just exposes [Result.t] as an alias to [result] + without re-exporting the constructors + - it can't be [Result.result] because the [include Stdlib] above makes + [Result] be [Stdlib.Result] (shadowing the [Result] module from the + result package), and [Stdlib.Result] does not define [result] (that's + why we override the [Result] module as the first place. *) + type ('a, 'b) t = ('a, 'b) result = | Ok of 'a | Error of 'b - type ('a, 'b) result = ('a, 'b) Result.t = + type ('a, 'b) result = ('a, 'b) t = | Ok of 'a | Error of 'b end @@ -58,9 +61,12 @@ module Weak = Weak module Printf = Printf module Format = Format module Buffer = Buffer + +include Pervasives + module Result = struct - (* the "result" compatibility module defines Result.result, - not Result.t as the 4.08 stdlib *) + (* the "result" compatibility module defines Result.result as a variant + and Result.t as an alias *) type ('a, 'b) t = ('a, 'b) Result.result = | Ok of 'a | Error of 'b @@ -70,6 +76,9 @@ module Result = struct | Ok of 'a | Error of 'b end +#endif + +#if OCAML_VERSION < (4, 08, 0) module Option = struct type 'a t = 'a option @@ -83,6 +92,4 @@ module Option = struct | None -> Result.Error none | Some x -> Result.Ok x end - -include Pervasives #endif diff --git a/src/runtime/ppx_deriving_runtime.cppo.mli b/src/runtime/ppx_deriving_runtime.cppo.mli index fe028347..05e9ab88 100644 --- a/src/runtime/ppx_deriving_runtime.cppo.mli +++ b/src/runtime/ppx_deriving_runtime.cppo.mli @@ -21,92 +21,75 @@ type nonrec bytes = bytes (** {2 Predefined modules} {3 Operations on predefined types} *) - -#if OCAML_VERSION >= (4, 08, 0) -include (module type of Stdlib with - type fpclass = Stdlib.fpclass and - type in_channel = Stdlib.in_channel and - type out_channel = Stdlib.out_channel and - type open_flag = Stdlib.open_flag and - type 'a ref = 'a Stdlib.ref and - type ('a, 'b, 'c, 'd, 'e, 'f) format6 = ('a, 'b, 'c, 'd, 'e, 'f) Stdlib.format6 and - type ('a, 'b, 'c, 'd) format4 = ('a, 'b, 'c, 'd) Stdlib.format4 and - type ('a, 'b, 'c) format = ('a, 'b, 'c) Stdlib.format -) +#if OCAML_VERSION >= (4, 07, 0) +include module type of struct + include Stdlib +end module Result : sig - type ('a, 'b) t = ('a, 'b) Result.t = + (* Type manifest shoud be [('a, 'b) result]: + - it can't be [Result.t] because [Result] is not defined in 4.07 std-lib + and the result package just exposes [Result.t] as an alias to [result] + without re-exporting the constructors + - it can't be [Result.result] because the [include Stdlib] above makes + [Result] be [Stdlib.Result] (shadowing the [Result] module from the + result package), and [Stdlib.Result] does not define [result] (that's + why we override the [Result] module as the first place. *) + type ('a, 'b) t = ('a, 'b) result = | Ok of 'a | Error of 'b (* we also expose Result.result for backward-compatibility with the Result package! *) - type ('a, 'b) result = ('a, 'b) Result.t = + type ('a, 'b) result = ('a, 'b) t = | Ok of 'a | Error of 'b end #else -module Pervasives : (module type of Pervasives with - type fpclass = Pervasives.fpclass and - type in_channel = Pervasives.in_channel and - type out_channel = Pervasives.out_channel and - type open_flag = Pervasives.open_flag and - type 'a ref = 'a Pervasives.ref and - type ('a, 'b, 'c, 'd, 'e, 'f) format6 = ('a, 'b, 'c, 'd, 'e, 'f) Pervasives.format6 and - type ('a, 'b, 'c, 'd) format4 = ('a, 'b, 'c, 'd) Pervasives.format4 and - type ('a, 'b, 'c) format = ('a, 'b, 'c) Pervasives.format) +module Pervasives = Pervasives module Stdlib = Pervasives -include (module type of Pervasives with - type fpclass = Pervasives.fpclass and - type in_channel = Pervasives.in_channel and - type out_channel = Pervasives.out_channel and - type open_flag = Pervasives.open_flag and - type 'a ref = 'a Pervasives.ref and - type ('a, 'b, 'c, 'd, 'e, 'f) format6 = ('a, 'b, 'c, 'd, 'e, 'f) Pervasives.format6 and - type ('a, 'b, 'c, 'd) format4 = ('a, 'b, 'c, 'd) Pervasives.format4 and - type ('a, 'b, 'c) format = ('a, 'b, 'c) Pervasives.format) +include module type of struct + include Pervasives +end -module Char : (module type of Char) -module String : (module type of String) -module Printexc : (module type of Printexc with - type raw_backtrace = Printexc.raw_backtrace and - type backtrace_slot = Printexc.backtrace_slot and - type location = Printexc.location) -module Array : (module type of Array) -module List : (module type of List) -module Nativeint : (module type of Nativeint) -module Int32 : (module type of Int32) -module Int64 : (module type of Int64) -module Lazy : (module type of Lazy) -module Bytes : (module type of Bytes) +module Char = Char +module String = String +module Printexc = Printexc +module Array = Array +module List = List +module Nativeint = Nativeint +module Int32 = Int32 +module Int64 = Int64 +module Lazy = Lazy +module Bytes = Bytes -(** {3 Data structures} *) +module Hashtbl = Hashtbl +module Queue = Queue +module Stack = Stack +module Set = Set +module Map = Map +module Weak = Weak + +module Printf = Printf +module Format = Format +module Buffer = Buffer -module Hashtbl : (module type of Hashtbl with - type ('a, 'b) t = ('a, 'b) Hashtbl.t and - type statistics = Hashtbl.statistics) -module Queue : (module type of Queue with - type 'a t = 'a Queue.t) -module Stack : (module type of Stack with - type 'a t = 'a Stack.t) -module Set : (module type of Set) -module Map : (module type of Map) -module Weak : (module type of Weak with - type 'a t = 'a Weak.t) -module Buffer : (module type of Buffer with - type t = Buffer.t) module Result : sig type ('a, 'b) t = ('a, 'b) Result.result = | Ok of 'a | Error of 'b - (* we also expose Result.result for backward-compatibility *) + (* we also expose Result.result for backward-compatibility + with the Result package! *) type ('a, 'b) result = ('a, 'b) Result.result = | Ok of 'a | Error of 'b end +#endif + +#if OCAML_VERSION < (4, 08, 0) module Option : sig type 'a t = 'a option @@ -114,12 +97,4 @@ module Option : sig val to_result : none:'e -> 'a option -> ('a, 'e) Result.result end - -(** {3 Formatting} *) - -module Printf : (module type of Printf) -module Format : (module type of Format with - type formatter_out_functions = Format.formatter_out_functions and - type formatter_tag_functions = Format.formatter_tag_functions and - type formatter = Format.formatter) #endif