2016-02-18 07:11:59 -08:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* OCaml *)
|
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique. *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. This file is distributed under the terms of *)
|
|
|
|
(* the GNU Lesser General Public License version 2.1, with the *)
|
|
|
|
(* special exception on linking described in the file LICENSE. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
1995-08-09 08:06:35 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Typechecking for the core language *)
|
|
|
|
|
1996-01-04 04:50:52 -08:00
|
|
|
open Misc
|
1995-05-04 03:15:53 -07:00
|
|
|
open Asttypes
|
|
|
|
open Parsetree
|
1996-09-23 04:33:27 -07:00
|
|
|
open Types
|
1995-05-04 03:15:53 -07:00
|
|
|
open Typedtree
|
1997-03-24 12:11:22 -08:00
|
|
|
open Btype
|
1995-05-04 03:15:53 -07:00
|
|
|
open Ctype
|
|
|
|
|
2017-12-02 04:44:54 -08:00
|
|
|
type type_forcing_context =
|
|
|
|
| If_conditional
|
|
|
|
| If_no_else_branch
|
|
|
|
| While_loop_conditional
|
|
|
|
| While_loop_body
|
|
|
|
| For_loop_start_index
|
|
|
|
| For_loop_stop_index
|
|
|
|
| For_loop_body
|
|
|
|
| Assert_condition
|
|
|
|
| Sequence_left_hand_side
|
2018-07-04 07:38:06 -07:00
|
|
|
| When_guard
|
2017-12-02 04:44:54 -08:00
|
|
|
|
|
|
|
type type_expected = {
|
|
|
|
ty: type_expr;
|
|
|
|
explanation: type_forcing_context option;
|
|
|
|
}
|
|
|
|
|
2019-12-14 04:49:28 -08:00
|
|
|
module Datatype_kind = struct
|
|
|
|
type t = Record | Variant
|
2019-12-12 06:09:35 -08:00
|
|
|
|
|
|
|
let type_name = function
|
2019-12-14 04:49:28 -08:00
|
|
|
| Record -> "record"
|
|
|
|
| Variant -> "variant"
|
2019-12-12 06:09:35 -08:00
|
|
|
|
2019-12-14 04:49:28 -08:00
|
|
|
let label_name = function
|
|
|
|
| Record -> "field"
|
|
|
|
| Variant -> "constructor"
|
2019-12-12 06:09:35 -08:00
|
|
|
end
|
|
|
|
|
2019-12-12 12:01:42 -08:00
|
|
|
type wrong_name = {
|
|
|
|
type_path: Path.t;
|
2019-12-14 04:49:28 -08:00
|
|
|
kind: Datatype_kind.t;
|
2019-12-12 12:01:42 -08:00
|
|
|
name: string loc;
|
|
|
|
valid_names: string list;
|
|
|
|
}
|
|
|
|
|
2018-04-21 07:45:43 -07:00
|
|
|
type existential_restriction =
|
|
|
|
| At_toplevel (** no existential types at the toplevel *)
|
2018-04-21 12:15:13 -07:00
|
|
|
| In_group (** nor with let ... and ... *)
|
|
|
|
| In_rec (** or recursive definition *)
|
|
|
|
| With_attributes (** or let[@any_attribute] = ... *)
|
|
|
|
| In_class_args (** or in class arguments *)
|
|
|
|
| In_class_def (** or in [class c = let ... in ...] *)
|
2018-04-21 07:45:43 -07:00
|
|
|
| In_self_pattern (** or in self pattern *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
type error =
|
|
|
|
| Constructor_arity_mismatch of Longident.t * int * int
|
2018-09-10 13:15:45 -07:00
|
|
|
| Label_mismatch of Longident.t * Ctype.Unification_trace.t
|
2019-09-20 06:23:07 -07:00
|
|
|
| Pattern_type_clash :
|
|
|
|
Ctype.Unification_trace.t * _ pattern_desc option -> error
|
2018-09-10 13:15:45 -07:00
|
|
|
| Or_pattern_type_clash of Ident.t * Ctype.Unification_trace.t
|
2008-01-11 08:13:18 -08:00
|
|
|
| Multiply_bound_variable of string
|
2014-12-13 06:46:22 -08:00
|
|
|
| Orpat_vars of Ident.t * Ident.t list
|
2017-08-12 13:24:41 -07:00
|
|
|
| Expr_type_clash of
|
2018-09-10 13:15:45 -07:00
|
|
|
Ctype.Unification_trace.t * type_forcing_context option
|
2019-03-07 07:36:46 -08:00
|
|
|
* expression_desc option
|
1995-05-04 03:15:53 -07:00
|
|
|
| Apply_non_function of type_expr
|
2020-04-17 07:43:38 -07:00
|
|
|
| Apply_wrong_label of arg_label * type_expr * bool
|
2012-11-14 08:59:33 -08:00
|
|
|
| Label_multiply_defined of string
|
2012-05-30 07:52:37 -07:00
|
|
|
| Label_missing of Ident.t list
|
1995-05-04 03:15:53 -07:00
|
|
|
| Label_not_mutable of Longident.t
|
2019-12-12 12:01:42 -08:00
|
|
|
| Wrong_name of string * type_expected * wrong_name
|
2013-03-22 11:20:25 -07:00
|
|
|
| Name_type_mismatch of
|
2019-12-14 04:49:28 -08:00
|
|
|
Datatype_kind.t * Longident.t * (Path.t * Path.t) * (Path.t * Path.t) list
|
2014-05-12 08:37:29 -07:00
|
|
|
| Invalid_format of string
|
2014-12-13 06:46:27 -08:00
|
|
|
| Undefined_method of type_expr * string * string list option
|
|
|
|
| Undefined_inherited_method of string * string list
|
1996-04-22 04:15:41 -07:00
|
|
|
| Virtual_class of Longident.t
|
2003-07-02 02:14:35 -07:00
|
|
|
| Private_type of type_expr
|
|
|
|
| Private_label of Longident.t * type_expr
|
2019-04-04 05:03:26 -07:00
|
|
|
| Private_constructor of constructor_description * type_expr
|
2014-12-13 06:46:29 -08:00
|
|
|
| Unbound_instance_variable of string * string list
|
2018-10-12 02:20:21 -07:00
|
|
|
| Instance_variable_not_mutable of string
|
2018-09-10 13:15:45 -07:00
|
|
|
| Not_subtype of Ctype.Unification_trace.t * Ctype.Unification_trace.t
|
1996-04-22 04:15:41 -07:00
|
|
|
| Outside_class
|
|
|
|
| Value_multiply_overridden of string
|
2002-06-12 02:52:08 -07:00
|
|
|
| Coercion_failure of
|
2018-09-10 13:15:45 -07:00
|
|
|
type_expr * type_expr * Ctype.Unification_trace.t * bool
|
2017-12-02 04:44:54 -08:00
|
|
|
| Too_many_arguments of bool * type_expr * type_forcing_context option
|
|
|
|
| Abstract_wrong_label of arg_label * type_expr * type_forcing_context option
|
1998-02-26 04:54:44 -08:00
|
|
|
| Scoping_let_module of string * type_expr
|
2000-02-21 19:08:08 -08:00
|
|
|
| Not_a_variant_type of Longident.t
|
2001-04-19 01:34:21 -07:00
|
|
|
| Incoherent_label_order
|
2018-09-10 13:15:45 -07:00
|
|
|
| Less_general of string * Ctype.Unification_trace.t
|
2010-10-21 16:59:33 -07:00
|
|
|
| Modules_not_allowed
|
|
|
|
| Cannot_infer_signature
|
|
|
|
| Not_a_packed_module of type_expr
|
2018-04-21 07:45:43 -07:00
|
|
|
| Unexpected_existential of existential_restriction * string * string list
|
2013-04-16 08:34:09 -07:00
|
|
|
| Invalid_interval
|
2013-12-02 10:00:18 -08:00
|
|
|
| Invalid_for_loop_index
|
2014-05-05 04:49:37 -07:00
|
|
|
| No_value_clauses
|
2018-01-15 03:11:06 -08:00
|
|
|
| Exception_pattern_disallowed
|
|
|
|
| Mixed_value_and_exception_patterns_under_guard
|
2014-10-14 08:51:30 -07:00
|
|
|
| Inlined_record_escape
|
2016-03-21 14:41:25 -07:00
|
|
|
| Inlined_record_expected
|
2015-10-14 18:55:52 -07:00
|
|
|
| Unrefuted_pattern of pattern
|
2015-10-30 04:37:07 -07:00
|
|
|
| Invalid_extension_constructor_payload
|
|
|
|
| Not_an_extension_constructor
|
2015-04-18 20:07:32 -07:00
|
|
|
| Literal_overflow of string
|
|
|
|
| Unknown_literal of string * char
|
2017-09-21 16:04:41 -07:00
|
|
|
| Illegal_letrec_pat
|
|
|
|
| Illegal_letrec_expr
|
|
|
|
| Illegal_class_expr
|
2018-07-29 14:29:01 -07:00
|
|
|
| Letop_type_clash of string * Ctype.Unification_trace.t
|
|
|
|
| Andop_type_clash of string * Ctype.Unification_trace.t
|
|
|
|
| Bindings_type_clash of Ctype.Unification_trace.t
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2012-01-21 19:15:14 -08:00
|
|
|
exception Error of Location.t * Env.t * error
|
2014-05-07 01:26:17 -07:00
|
|
|
exception Error_forward of Location.error
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2020-10-02 18:48:57 -07:00
|
|
|
let trace_of_error = function
|
|
|
|
Label_mismatch (_,tr)
|
|
|
|
| Pattern_type_clash (tr,_)
|
|
|
|
| Or_pattern_type_clash (_,tr)
|
|
|
|
| Expr_type_clash (tr,_,_)
|
|
|
|
| Coercion_failure (_,_,tr,_)
|
|
|
|
| Less_general (_,tr)
|
|
|
|
| Letop_type_clash (_,tr)
|
|
|
|
| Andop_type_clash (_,tr)
|
|
|
|
| Bindings_type_clash tr -> Some tr
|
|
|
|
| _ -> None
|
|
|
|
|
1998-02-26 04:54:44 -08:00
|
|
|
(* Forward declaration, to be filled in by Typemod.type_module *)
|
|
|
|
|
|
|
|
let type_module =
|
2016-03-09 02:40:16 -08:00
|
|
|
ref ((fun _env _md -> assert false) :
|
1998-02-26 04:54:44 -08:00
|
|
|
Env.t -> Parsetree.module_expr -> Typedtree.module_expr)
|
|
|
|
|
2009-11-01 13:52:29 -08:00
|
|
|
(* Forward declaration, to be filled in by Typemod.type_open *)
|
|
|
|
|
2017-07-19 23:17:30 -07:00
|
|
|
let type_open :
|
|
|
|
(?used_slot:bool ref -> override_flag -> Env.t -> Location.t ->
|
|
|
|
Longident.t loc -> Path.t * Env.t)
|
|
|
|
ref =
|
|
|
|
ref (fun ?used_slot:_ _ -> assert false)
|
2009-11-01 13:52:29 -08:00
|
|
|
|
2018-04-08 01:51:15 -07:00
|
|
|
let type_open_decl :
|
|
|
|
(?used_slot:bool ref -> Env.t -> Parsetree.open_declaration
|
|
|
|
-> open_declaration * Types.signature * Env.t)
|
|
|
|
ref =
|
|
|
|
ref (fun ?used_slot:_ _ -> assert false)
|
|
|
|
|
2010-10-21 16:59:33 -07:00
|
|
|
(* Forward declaration, to be filled in by Typemod.type_package *)
|
|
|
|
|
|
|
|
let type_package =
|
|
|
|
ref (fun _ -> assert false)
|
2009-11-01 13:52:29 -08:00
|
|
|
|
2003-11-25 01:20:45 -08:00
|
|
|
(* Forward declaration, to be filled in by Typeclass.class_structure *)
|
|
|
|
let type_object =
|
2016-03-09 02:40:16 -08:00
|
|
|
ref (fun _env _s -> assert false :
|
2003-11-25 01:20:45 -08:00
|
|
|
Env.t -> Location.t -> Parsetree.class_structure ->
|
2012-05-30 07:52:37 -07:00
|
|
|
Typedtree.class_structure * Types.class_signature * string list)
|
2003-04-01 17:32:09 -08:00
|
|
|
|
|
|
|
(*
|
|
|
|
Saving and outputting type information.
|
|
|
|
We keep these function names short, because they have to be
|
|
|
|
called each time we create a record of type [Typedtree.expression]
|
|
|
|
or [Typedtree.pattern] that will end up in the typed AST.
|
|
|
|
*)
|
|
|
|
let re node =
|
2012-05-30 07:52:37 -07:00
|
|
|
Cmt_format.add_saved_type (Cmt_format.Partial_expression node);
|
2003-04-01 17:32:09 -08:00
|
|
|
node
|
|
|
|
;;
|
|
|
|
let rp node =
|
2019-09-20 06:23:07 -07:00
|
|
|
Cmt_format.add_saved_type (Cmt_format.Partial_pattern (Value, node));
|
|
|
|
node
|
|
|
|
;;
|
|
|
|
let rcp node =
|
|
|
|
Cmt_format.add_saved_type (Cmt_format.Partial_pattern (Computation, node));
|
2003-04-01 17:32:09 -08:00
|
|
|
node
|
|
|
|
;;
|
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
|
2014-10-14 08:51:30 -07:00
|
|
|
type recarg =
|
|
|
|
| Allowed
|
|
|
|
| Required
|
|
|
|
| Rejected
|
|
|
|
|
|
|
|
|
2017-12-02 04:44:54 -08:00
|
|
|
let mk_expected ?explanation ty = { ty; explanation; }
|
|
|
|
|
2013-04-15 09:23:22 -07:00
|
|
|
let case lhs rhs =
|
|
|
|
{c_lhs = lhs; c_guard = None; c_rhs = rhs}
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Typing of constants *)
|
|
|
|
|
2013-05-23 08:12:04 -07:00
|
|
|
let type_constant = function
|
2018-07-24 08:17:53 -07:00
|
|
|
Const_int _ -> instance Predef.type_int
|
|
|
|
| Const_char _ -> instance Predef.type_char
|
|
|
|
| Const_string _ -> instance Predef.type_string
|
|
|
|
| Const_float _ -> instance Predef.type_float
|
|
|
|
| Const_int32 _ -> instance Predef.type_int32
|
|
|
|
| Const_int64 _ -> instance Predef.type_int64
|
|
|
|
| Const_nativeint _ -> instance Predef.type_nativeint
|
2005-08-13 13:59:37 -07:00
|
|
|
|
2016-02-16 04:23:31 -08:00
|
|
|
let constant : Parsetree.constant -> (Asttypes.constant, error) result =
|
|
|
|
function
|
2016-01-08 06:48:47 -08:00
|
|
|
| Pconst_integer (i,None) ->
|
2015-04-18 20:07:32 -07:00
|
|
|
begin
|
|
|
|
try Ok (Const_int (Misc.Int_literal_converter.int i))
|
|
|
|
with Failure _ -> Error (Literal_overflow "int")
|
|
|
|
end
|
2016-01-08 06:48:47 -08:00
|
|
|
| Pconst_integer (i,Some 'l') ->
|
2015-04-18 20:07:32 -07:00
|
|
|
begin
|
|
|
|
try Ok (Const_int32 (Misc.Int_literal_converter.int32 i))
|
|
|
|
with Failure _ -> Error (Literal_overflow "int32")
|
|
|
|
end
|
2016-01-08 06:48:47 -08:00
|
|
|
| Pconst_integer (i,Some 'L') ->
|
2015-04-18 20:07:32 -07:00
|
|
|
begin
|
|
|
|
try Ok (Const_int64 (Misc.Int_literal_converter.int64 i))
|
|
|
|
with Failure _ -> Error (Literal_overflow "int64")
|
|
|
|
end
|
2016-01-08 06:48:47 -08:00
|
|
|
| Pconst_integer (i,Some 'n') ->
|
2015-04-18 20:07:32 -07:00
|
|
|
begin
|
|
|
|
try Ok (Const_nativeint (Misc.Int_literal_converter.nativeint i))
|
|
|
|
with Failure _ -> Error (Literal_overflow "nativeint")
|
|
|
|
end
|
2016-01-08 06:48:47 -08:00
|
|
|
| Pconst_integer (i,Some c) -> Error (Unknown_literal (i, c))
|
|
|
|
| Pconst_char c -> Ok (Const_char c)
|
2019-07-22 09:21:26 -07:00
|
|
|
| Pconst_string (s,loc,d) -> Ok (Const_string (s,loc,d))
|
2016-01-08 06:48:47 -08:00
|
|
|
| Pconst_float (f,None)-> Ok (Const_float f)
|
|
|
|
| Pconst_float (f,Some c) -> Error (Unknown_literal (f, c))
|
2015-04-18 20:07:32 -07:00
|
|
|
|
|
|
|
let constant_or_raise env loc cst =
|
|
|
|
match constant cst with
|
|
|
|
| Ok c -> c
|
|
|
|
| Error err -> raise (Error (loc, env, err))
|
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
(* Specific version of type_option, using newty rather than newgenty *)
|
|
|
|
|
|
|
|
let type_option ty =
|
|
|
|
newty (Tconstr(Predef.path_option,[ty], ref Mnil))
|
|
|
|
|
2012-05-31 01:07:31 -07:00
|
|
|
let mkexp exp_desc exp_type exp_loc exp_env =
|
2013-03-25 07:16:07 -07:00
|
|
|
{ exp_desc; exp_type; exp_loc; exp_env; exp_extra = []; exp_attributes = [] }
|
2012-05-30 07:52:37 -07:00
|
|
|
|
2018-10-12 02:20:21 -07:00
|
|
|
let option_none env ty loc =
|
|
|
|
let lid = Longident.Lident "None" in
|
|
|
|
let cnone = Env.find_ident_constructor Predef.ident_none env in
|
2014-04-29 04:56:17 -07:00
|
|
|
mkexp (Texp_construct(mknoloc lid, cnone, [])) ty loc env
|
1999-11-30 08:07:38 -08:00
|
|
|
|
2018-10-12 02:20:21 -07:00
|
|
|
let option_some env texp =
|
2012-05-30 07:52:37 -07:00
|
|
|
let lid = Longident.Lident "Some" in
|
2018-10-12 02:20:21 -07:00
|
|
|
let csome = Env.find_ident_constructor Predef.ident_some env in
|
2013-04-17 02:46:52 -07:00
|
|
|
mkexp ( Texp_construct(mknoloc lid , csome, [texp]) )
|
2012-05-30 07:52:37 -07:00
|
|
|
(type_option texp.exp_type) texp.exp_loc texp.exp_env
|
1999-11-30 08:07:38 -08:00
|
|
|
|
|
|
|
let extract_option_type env ty =
|
|
|
|
match expand_head env ty with {desc = Tconstr(path, [ty], _)}
|
|
|
|
when Path.same path Predef.path_option -> ty
|
|
|
|
| _ -> assert false
|
|
|
|
|
2012-11-08 01:39:23 -08:00
|
|
|
let extract_concrete_record env ty =
|
|
|
|
match extract_concrete_typedecl env ty with
|
2012-11-10 19:46:59 -08:00
|
|
|
(p0, p, {type_kind=Type_record (fields, _)}) -> (p0, p, fields)
|
2012-11-08 01:39:23 -08:00
|
|
|
| _ -> raise Not_found
|
|
|
|
|
|
|
|
let extract_concrete_variant env ty =
|
|
|
|
match extract_concrete_typedecl env ty with
|
2014-05-04 16:08:45 -07:00
|
|
|
(p0, p, {type_kind=Type_variant cstrs}) -> (p0, p, cstrs)
|
2015-05-16 19:36:30 -07:00
|
|
|
| (p0, p, {type_kind=Type_open}) -> (p0, p, [])
|
2012-11-08 01:39:23 -08:00
|
|
|
| _ -> raise Not_found
|
|
|
|
|
2016-03-09 02:40:16 -08:00
|
|
|
let extract_label_names env ty =
|
2012-10-18 00:35:30 -07:00
|
|
|
try
|
2012-11-10 19:46:59 -08:00
|
|
|
let (_, _,fields) = extract_concrete_record env ty in
|
2013-09-27 03:54:55 -07:00
|
|
|
List.map (fun l -> l.Types.ld_id) fields
|
2012-10-18 00:35:30 -07:00
|
|
|
with Not_found ->
|
|
|
|
assert false
|
2001-06-28 18:46:46 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Typing of patterns *)
|
|
|
|
|
2010-11-09 00:21:44 -08:00
|
|
|
(* unification inside type_exp and type_expect *)
|
2010-10-15 23:09:25 -07:00
|
|
|
let unify_exp_types loc env ty expected_ty =
|
|
|
|
(* Format.eprintf "@[%a@ %a@]@." Printtyp.raw_type_expr exp.exp_type
|
|
|
|
Printtyp.raw_type_expr expected_ty; *)
|
2000-05-12 11:22:35 -07:00
|
|
|
try
|
2010-11-15 00:02:17 -08:00
|
|
|
unify env ty expected_ty
|
2002-01-03 18:02:50 -08:00
|
|
|
with
|
|
|
|
Unify trace ->
|
2019-03-07 07:36:46 -08:00
|
|
|
raise(Error(loc, env, Expr_type_clash(trace, None, None)))
|
2002-01-03 18:02:50 -08:00
|
|
|
| Tags(l1,l2) ->
|
2013-01-29 06:21:12 -08:00
|
|
|
raise(Typetexp.Error(loc, env, Typetexp.Variant_tags (l1, l2)))
|
2010-10-15 23:09:25 -07:00
|
|
|
|
2010-11-09 00:21:44 -08:00
|
|
|
(* level at which to create the local type declarations *)
|
2018-02-12 08:37:35 -08:00
|
|
|
let gadt_equations_level = ref None
|
|
|
|
let get_gadt_equations_level () =
|
|
|
|
match !gadt_equations_level with
|
2010-10-28 23:54:24 -07:00
|
|
|
Some y -> y
|
|
|
|
| None -> assert false
|
2010-10-04 01:38:22 -07:00
|
|
|
|
2020-07-08 01:35:40 -07:00
|
|
|
let nothing_equated = TypePairs.create 0
|
|
|
|
|
2019-12-02 11:49:51 -08:00
|
|
|
(* unification inside type_pat*)
|
2020-07-08 01:35:40 -07:00
|
|
|
let unify_pat_types_return_equated_pairs ?(refine = None) loc env ty ty' =
|
2019-12-02 11:49:51 -08:00
|
|
|
try
|
2020-06-15 01:26:26 -07:00
|
|
|
match refine with
|
|
|
|
| Some allow_recursive ->
|
|
|
|
unify_gadt ~equations_level:(get_gadt_equations_level ())
|
|
|
|
~allow_recursive env ty ty'
|
|
|
|
| None ->
|
2020-07-08 01:35:40 -07:00
|
|
|
unify !env ty ty';
|
|
|
|
nothing_equated
|
2010-09-18 21:55:40 -07:00
|
|
|
with
|
2018-02-12 08:37:35 -08:00
|
|
|
| Unify trace ->
|
2019-03-07 11:05:48 -08:00
|
|
|
raise(Error(loc, !env, Pattern_type_clash(trace, None)))
|
2010-09-18 21:55:40 -07:00
|
|
|
| Tags(l1,l2) ->
|
2013-01-29 06:21:12 -08:00
|
|
|
raise(Typetexp.Error(loc, !env, Typetexp.Variant_tags (l1, l2)))
|
2010-09-18 21:55:40 -07:00
|
|
|
|
2020-07-08 01:35:40 -07:00
|
|
|
let unify_pat_types ?refine loc env ty ty' =
|
|
|
|
ignore (unify_pat_types_return_equated_pairs ?refine loc env ty ty')
|
|
|
|
|
2019-12-02 11:49:51 -08:00
|
|
|
let unify_pat ?refine env pat expected_ty =
|
|
|
|
try unify_pat_types ?refine pat.pat_loc env pat.pat_type expected_ty
|
2019-03-07 11:05:48 -08:00
|
|
|
with Error (loc, env, Pattern_type_clash(trace, None)) ->
|
|
|
|
raise(Error(loc, env, Pattern_type_clash(trace, Some pat.pat_desc)))
|
2000-05-12 11:22:35 -07:00
|
|
|
|
2019-12-02 11:49:51 -08:00
|
|
|
(* Creating new conjunctive types is not allowed when typing patterns *)
|
2003-08-18 01:26:18 -07:00
|
|
|
(* make all Reither present in open variants *)
|
2019-09-20 06:23:07 -07:00
|
|
|
let finalize_variant pat tag opat r =
|
|
|
|
let row =
|
|
|
|
match expand_head pat.pat_env pat.pat_type with
|
|
|
|
{desc = Tvariant row} -> r := row; row_repr row
|
|
|
|
| _ -> assert false
|
|
|
|
in
|
|
|
|
begin match row_field tag row with
|
|
|
|
| Rabsent -> () (* assert false *)
|
|
|
|
| Reither (true, [], _, e) when not row.row_closed ->
|
|
|
|
set_row_field e (Rpresent None)
|
|
|
|
| Reither (false, ty::tl, _, e) when not row.row_closed ->
|
|
|
|
set_row_field e (Rpresent (Some ty));
|
|
|
|
begin match opat with None -> assert false
|
2019-12-02 11:49:51 -08:00
|
|
|
| Some pat ->
|
|
|
|
let env = ref pat.pat_env in List.iter (unify_pat env pat) (ty::tl)
|
2019-09-20 06:23:07 -07:00
|
|
|
end
|
|
|
|
| Reither (c, _l, true, e) when not (row_fixed row) ->
|
|
|
|
set_row_field e (Reither (c, [], false, ref None))
|
2003-08-18 01:26:18 -07:00
|
|
|
| _ -> ()
|
2019-09-20 06:23:07 -07:00
|
|
|
end
|
|
|
|
(* Force check of well-formedness WHY? *)
|
|
|
|
(* unify_pat pat.pat_env pat
|
|
|
|
(newty(Tvariant{row_fields=[]; row_more=newvar(); row_closed=false;
|
|
|
|
row_bound=(); row_fixed=false; row_name=None})); *)
|
2003-08-18 01:26:18 -07:00
|
|
|
|
|
|
|
let has_variants p =
|
2019-09-20 06:23:07 -07:00
|
|
|
exists_general_pattern
|
|
|
|
{ f = fun (type k) (p : k general_pattern) -> match p.pat_desc with
|
|
|
|
| (Tpat_variant _) -> true
|
|
|
|
| _ -> false } p
|
|
|
|
|
|
|
|
let finalize_variants p =
|
|
|
|
iter_general_pattern
|
|
|
|
{ f = fun (type k) (p : k general_pattern) -> match p.pat_desc with
|
|
|
|
| Tpat_variant(tag, opat, r) ->
|
|
|
|
finalize_variant p tag opat r
|
|
|
|
| _ -> () } p
|
2003-08-18 01:26:18 -07:00
|
|
|
|
|
|
|
(* pattern environment *)
|
2018-04-23 03:08:43 -07:00
|
|
|
type pattern_variable =
|
2018-06-07 04:54:39 -07:00
|
|
|
{
|
|
|
|
pv_id: Ident.t;
|
|
|
|
pv_type: type_expr;
|
|
|
|
pv_loc: Location.t;
|
|
|
|
pv_as_var: bool;
|
2018-06-07 04:57:47 -07:00
|
|
|
pv_attributes: attributes;
|
2018-06-07 04:54:39 -07:00
|
|
|
}
|
|
|
|
|
2018-04-23 03:08:43 -07:00
|
|
|
type module_variable =
|
|
|
|
string loc * Location.t
|
|
|
|
|
|
|
|
let pattern_variables = ref ([] : pattern_variable list)
|
2002-06-03 00:33:48 -07:00
|
|
|
let pattern_force = ref ([] : (unit -> unit) list)
|
2010-10-21 16:59:33 -07:00
|
|
|
let allow_modules = ref false
|
2018-04-23 03:08:43 -07:00
|
|
|
let module_variables = ref ([] : module_variable list)
|
2020-10-13 00:48:16 -07:00
|
|
|
let reset_pattern allow =
|
2002-06-03 00:33:48 -07:00
|
|
|
pattern_variables := [];
|
2007-05-16 01:21:41 -07:00
|
|
|
pattern_force := [];
|
2010-10-21 16:59:33 -07:00
|
|
|
allow_modules := allow;
|
|
|
|
module_variables := [];
|
2007-05-16 01:21:41 -07:00
|
|
|
;;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2018-06-07 04:54:39 -07:00
|
|
|
let maybe_add_pattern_variables_ghost loc_let env pv =
|
|
|
|
List.fold_right
|
2018-10-12 02:20:21 -07:00
|
|
|
(fun {pv_id; _} env ->
|
|
|
|
let name = Ident.name pv_id in
|
|
|
|
if Env.bound_value name env then env
|
|
|
|
else begin
|
|
|
|
Env.enter_unbound_value name
|
|
|
|
(Val_unbound_ghost_recursive loc_let) env
|
|
|
|
end
|
2018-06-07 04:54:39 -07:00
|
|
|
) pv env
|
|
|
|
|
2017-08-12 13:24:41 -07:00
|
|
|
let enter_variable ?(is_module=false) ?(is_as_variable=false) loc name ty
|
|
|
|
attrs =
|
2018-06-07 04:54:39 -07:00
|
|
|
if List.exists (fun {pv_id; _} -> Ident.name pv_id = name.txt)
|
2012-05-31 01:07:31 -07:00
|
|
|
!pattern_variables
|
2012-08-21 00:10:35 -07:00
|
|
|
then raise(Error(loc, Env.empty, Multiply_bound_variable name.txt));
|
2018-08-28 09:07:01 -07:00
|
|
|
let id = Ident.create_local name.txt in
|
2012-05-31 01:07:31 -07:00
|
|
|
pattern_variables :=
|
2018-06-07 04:54:39 -07:00
|
|
|
{pv_id = id;
|
|
|
|
pv_type = ty;
|
|
|
|
pv_loc = loc;
|
2018-06-07 04:57:47 -07:00
|
|
|
pv_as_var = is_as_variable;
|
|
|
|
pv_attributes = attrs} :: !pattern_variables;
|
2010-10-21 16:59:33 -07:00
|
|
|
if is_module then begin
|
|
|
|
(* Note: unpack patterns enter a variable of the same name *)
|
2012-01-21 19:15:14 -08:00
|
|
|
if not !allow_modules then
|
|
|
|
raise (Error (loc, Env.empty, Modules_not_allowed));
|
2010-10-21 16:59:33 -07:00
|
|
|
module_variables := (name, loc) :: !module_variables
|
2019-05-24 05:17:39 -07:00
|
|
|
end;
|
1995-05-04 03:15:53 -07:00
|
|
|
id
|
|
|
|
|
2000-10-02 07:18:05 -07:00
|
|
|
let sort_pattern_variables vs =
|
|
|
|
List.sort
|
2018-06-07 04:54:39 -07:00
|
|
|
(fun {pv_id = x; _} {pv_id = y; _} ->
|
2018-08-27 04:42:14 -07:00
|
|
|
Stdlib.compare (Ident.name x) (Ident.name y))
|
2000-10-02 07:18:05 -07:00
|
|
|
vs
|
|
|
|
|
|
|
|
let enter_orpat_variables loc env p1_vs p2_vs =
|
|
|
|
(* unify_vars operate on sorted lists *)
|
2005-08-13 13:59:37 -07:00
|
|
|
|
2000-10-02 07:18:05 -07:00
|
|
|
let p1_vs = sort_pattern_variables p1_vs
|
|
|
|
and p2_vs = sort_pattern_variables p2_vs in
|
|
|
|
|
2014-12-13 06:46:22 -08:00
|
|
|
let rec unify_vars p1_vs p2_vs =
|
2018-06-07 04:54:39 -07:00
|
|
|
let vars vs = List.map (fun {pv_id; _} -> pv_id) vs in
|
2014-12-13 06:46:22 -08:00
|
|
|
match p1_vs, p2_vs with
|
2018-06-07 04:54:39 -07:00
|
|
|
| {pv_id = x1; pv_type = t1; _}::rem1, {pv_id = x2; pv_type = t2; _}::rem2
|
2016-08-01 07:06:59 -07:00
|
|
|
when Ident.equal x1 x2 ->
|
2000-10-02 07:18:05 -07:00
|
|
|
if x1==x2 then
|
|
|
|
unify_vars rem1 rem2
|
|
|
|
else begin
|
|
|
|
begin try
|
2018-09-26 09:29:39 -07:00
|
|
|
unify_var env (newvar ()) t1;
|
2001-03-02 16:14:35 -08:00
|
|
|
unify env t1 t2
|
2000-10-02 07:18:05 -07:00
|
|
|
with
|
|
|
|
| Unify trace ->
|
2014-01-27 08:27:05 -08:00
|
|
|
raise(Error(loc, env, Or_pattern_type_clash(x1, trace)))
|
2011-10-25 05:11:06 -07:00
|
|
|
end;
|
2000-10-02 07:18:05 -07:00
|
|
|
(x2,x1)::unify_vars rem1 rem2
|
|
|
|
end
|
|
|
|
| [],[] -> []
|
2017-08-12 13:24:41 -07:00
|
|
|
| {pv_id; _}::_, [] | [],{pv_id; _}::_ ->
|
|
|
|
raise (Error (loc, env, Orpat_vars (pv_id, [])))
|
2018-06-07 04:54:39 -07:00
|
|
|
| {pv_id = x; _}::_, {pv_id = y; _}::_ ->
|
2014-12-13 06:46:22 -08:00
|
|
|
let err =
|
|
|
|
if Ident.name x < Ident.name y
|
|
|
|
then Orpat_vars (x, vars p2_vs)
|
|
|
|
else Orpat_vars (y, vars p1_vs) in
|
|
|
|
raise (Error (loc, env, err)) in
|
2000-10-02 07:18:05 -07:00
|
|
|
unify_vars p1_vs p2_vs
|
|
|
|
|
2000-06-29 02:11:42 -07:00
|
|
|
let rec build_as_type env p =
|
2020-07-09 03:20:37 -07:00
|
|
|
let as_ty = build_as_type_aux env p in
|
|
|
|
(* Cf. #1655 *)
|
|
|
|
List.fold_left (fun as_ty (extra, _loc, _attrs) ->
|
|
|
|
match extra with
|
|
|
|
| Tpat_type _ | Tpat_open _ | Tpat_unpack -> as_ty
|
|
|
|
| Tpat_constraint cty ->
|
2020-10-20 06:04:58 -07:00
|
|
|
(* [generic_instance] can only be used if the variables of the original
|
|
|
|
type ([cty.ctyp_type] here) are not at [generic_level], which they are
|
|
|
|
here.
|
|
|
|
If we used [generic_instance] we would lose the sharing between
|
|
|
|
[instance ty] and [ty]. *)
|
2020-07-09 03:20:37 -07:00
|
|
|
begin_def ();
|
|
|
|
let ty = instance cty.ctyp_type in
|
|
|
|
end_def ();
|
|
|
|
generalize_structure ty;
|
|
|
|
(* This call to unify can't fail since the pattern is well typed. *)
|
|
|
|
unify !env (instance as_ty) (instance ty);
|
|
|
|
ty
|
|
|
|
) as_ty p.pat_extra
|
|
|
|
|
|
|
|
and build_as_type_aux env p =
|
1999-11-30 08:07:38 -08:00
|
|
|
match p.pat_desc with
|
2012-05-30 07:52:37 -07:00
|
|
|
Tpat_alias(p1,_, _) -> build_as_type env p1
|
2000-06-29 02:11:42 -07:00
|
|
|
| Tpat_tuple pl ->
|
|
|
|
let tyl = List.map (build_as_type env) pl in
|
|
|
|
newty (Ttuple tyl)
|
2013-04-17 02:46:52 -07:00
|
|
|
| Tpat_construct(_, cstr, pl) ->
|
2010-12-13 22:33:06 -08:00
|
|
|
let keep = cstr.cstr_private = Private || cstr.cstr_existentials <> [] in
|
|
|
|
if keep then p.pat_type else
|
2000-06-29 02:11:42 -07:00
|
|
|
let tyl = List.map (build_as_type env) pl in
|
|
|
|
let ty_args, ty_res = instance_constructor cstr in
|
|
|
|
List.iter2 (fun (p,ty) -> unify_pat env {p with pat_type = ty})
|
|
|
|
(List.combine pl tyl) ty_args;
|
2000-06-12 23:59:29 -07:00
|
|
|
ty_res
|
2001-09-25 02:54:18 -07:00
|
|
|
| Tpat_variant(l, p', _) ->
|
2019-05-23 08:27:58 -07:00
|
|
|
let ty = Option.map (build_as_type env) p' in
|
2001-09-25 02:54:18 -07:00
|
|
|
newty (Tvariant{row_fields=[l, Rpresent ty]; row_more=newvar();
|
2008-01-11 08:13:18 -08:00
|
|
|
row_bound=(); row_name=None;
|
2019-07-12 08:47:25 -07:00
|
|
|
row_fixed=None; row_closed=false})
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_record (lpl,_) ->
|
2012-10-24 05:03:00 -07:00
|
|
|
let lbl = snd3 (List.hd lpl) in
|
2005-02-18 05:38:00 -08:00
|
|
|
if lbl.lbl_private = Private then p.pat_type else
|
2000-06-29 02:11:42 -07:00
|
|
|
let ty = newvar () in
|
2012-10-24 05:03:00 -07:00
|
|
|
let ppl = List.map (fun (_, l, p) -> l.lbl_pos, p) lpl in
|
2000-06-29 02:11:42 -07:00
|
|
|
let do_label lbl =
|
2002-04-18 00:27:47 -07:00
|
|
|
let _, ty_arg, ty_res = instance_label false lbl in
|
2000-06-29 02:11:42 -07:00
|
|
|
unify_pat env {p with pat_type = ty} ty_res;
|
2008-01-11 08:13:18 -08:00
|
|
|
let refinable =
|
|
|
|
lbl.lbl_mut = Immutable && List.mem_assoc lbl.lbl_pos ppl &&
|
|
|
|
match (repr lbl.lbl_arg).desc with Tpoly _ -> false | _ -> true in
|
|
|
|
if refinable then begin
|
2003-10-28 07:26:48 -08:00
|
|
|
let arg = List.assoc lbl.lbl_pos ppl in
|
2000-06-29 02:11:42 -07:00
|
|
|
unify_pat env {arg with pat_type = build_as_type env arg} ty_arg
|
|
|
|
end else begin
|
2002-04-18 00:27:47 -07:00
|
|
|
let _, ty_arg', ty_res' = instance_label false lbl in
|
2019-12-02 11:49:51 -08:00
|
|
|
unify !env ty_arg ty_arg';
|
2000-06-29 02:11:42 -07:00
|
|
|
unify_pat env p ty_res'
|
|
|
|
end in
|
|
|
|
Array.iter do_label lbl.lbl_all;
|
|
|
|
ty
|
2008-01-11 08:13:18 -08:00
|
|
|
| Tpat_or(p1, p2, row) ->
|
|
|
|
begin match row with
|
|
|
|
None ->
|
|
|
|
let ty1 = build_as_type env p1 and ty2 = build_as_type env p2 in
|
|
|
|
unify_pat env {p2 with pat_type = ty2} ty1;
|
|
|
|
ty1
|
|
|
|
| Some row ->
|
|
|
|
let row = row_repr row in
|
|
|
|
newty (Tvariant{row with row_closed=false; row_more=newvar()})
|
|
|
|
end
|
2008-07-09 06:03:38 -07:00
|
|
|
| Tpat_any | Tpat_var _ | Tpat_constant _
|
2019-09-20 06:23:07 -07:00
|
|
|
| Tpat_array _ | Tpat_lazy _ -> p.pat_type
|
2000-06-12 23:59:29 -07:00
|
|
|
|
2010-05-05 13:51:54 -07:00
|
|
|
let build_or_pat env loc lid =
|
2018-10-12 02:20:21 -07:00
|
|
|
let path, decl = Env.lookup_type ~loc:lid.loc lid.txt env in
|
2000-02-24 02:18:25 -08:00
|
|
|
let tyl = List.map (fun _ -> newvar()) decl.type_params in
|
2008-01-11 08:13:18 -08:00
|
|
|
let row0 =
|
2000-02-24 02:18:25 -08:00
|
|
|
let ty = expand_head env (newty(Tconstr(path, tyl, ref Mnil))) in
|
|
|
|
match ty.desc with
|
2008-01-11 08:13:18 -08:00
|
|
|
Tvariant row when static_row row -> row
|
2016-08-04 01:50:27 -07:00
|
|
|
| _ -> raise(Error(lid.loc, env, Not_a_variant_type lid.txt))
|
2000-02-21 19:08:08 -08:00
|
|
|
in
|
|
|
|
let pats, fields =
|
|
|
|
List.fold_left
|
|
|
|
(fun (pats,fields) (l,f) ->
|
|
|
|
match row_field_repr f with
|
|
|
|
Rpresent None ->
|
|
|
|
(l,None) :: pats,
|
2001-03-02 16:14:35 -08:00
|
|
|
(l, Reither(true,[], true, ref None)) :: fields
|
2000-02-21 19:08:08 -08:00
|
|
|
| Rpresent (Some ty) ->
|
2003-04-01 17:32:09 -08:00
|
|
|
(l, Some {pat_desc=Tpat_any; pat_loc=Location.none; pat_env=env;
|
2013-03-25 07:16:07 -07:00
|
|
|
pat_type=ty; pat_extra=[]; pat_attributes=[]})
|
2000-02-21 19:08:08 -08:00
|
|
|
:: pats,
|
2001-03-02 16:14:35 -08:00
|
|
|
(l, Reither(false, [ty], true, ref None)) :: fields
|
2000-02-21 19:08:08 -08:00
|
|
|
| _ -> pats, fields)
|
2008-01-11 08:13:18 -08:00
|
|
|
([],[]) (row_repr row0).row_fields in
|
2000-02-21 19:08:08 -08:00
|
|
|
let row =
|
2008-01-11 08:13:18 -08:00
|
|
|
{ row_fields = List.rev fields; row_more = newvar(); row_bound = ();
|
2019-07-12 08:47:25 -07:00
|
|
|
row_closed = false; row_fixed = None; row_name = Some (path, tyl) }
|
2000-02-21 19:08:08 -08:00
|
|
|
in
|
|
|
|
let ty = newty (Tvariant row) in
|
2002-05-26 20:09:18 -07:00
|
|
|
let gloc = {loc with Location.loc_ghost=true} in
|
2008-01-11 08:13:18 -08:00
|
|
|
let row' = ref {row with row_more=newvar()} in
|
2000-02-21 19:08:08 -08:00
|
|
|
let pats =
|
2014-01-22 17:58:37 -08:00
|
|
|
List.map
|
|
|
|
(fun (l,p) ->
|
|
|
|
{pat_desc=Tpat_variant(l,p,row'); pat_loc=gloc;
|
|
|
|
pat_env=env; pat_type=ty; pat_extra=[]; pat_attributes=[]})
|
2000-02-21 19:08:08 -08:00
|
|
|
pats
|
|
|
|
in
|
|
|
|
match pats with
|
2018-07-29 00:58:40 -07:00
|
|
|
[] ->
|
|
|
|
(* empty polymorphic variants: not possible with the concrete language
|
|
|
|
but valid at the ast level *)
|
|
|
|
raise(Error(lid.loc, env, Not_a_variant_type lid.txt))
|
2000-02-21 19:08:08 -08:00
|
|
|
| pat :: pats ->
|
2003-02-24 07:13:01 -08:00
|
|
|
let r =
|
|
|
|
List.fold_left
|
2014-01-22 17:58:37 -08:00
|
|
|
(fun pat pat0 ->
|
|
|
|
{pat_desc=Tpat_or(pat0,pat,Some row0); pat_extra=[];
|
|
|
|
pat_loc=gloc; pat_env=env; pat_type=ty; pat_attributes=[]})
|
2003-02-24 07:13:01 -08:00
|
|
|
pat pats in
|
2012-05-30 07:52:37 -07:00
|
|
|
(path, rp { r with pat_loc = loc },ty)
|
2000-02-21 19:08:08 -08:00
|
|
|
|
2018-01-15 03:11:06 -08:00
|
|
|
let split_cases env cases =
|
|
|
|
let add_case lst case = function
|
|
|
|
| None -> lst
|
|
|
|
| Some c_lhs -> { case with c_lhs } :: lst
|
|
|
|
in
|
|
|
|
List.fold_right (fun ({ c_lhs; c_guard } as case) (vals, exns) ->
|
|
|
|
match split_pattern c_lhs with
|
|
|
|
| Some _, Some _ when c_guard <> None ->
|
|
|
|
raise (Error (c_lhs.pat_loc, env,
|
|
|
|
Mixed_value_and_exception_patterns_under_guard))
|
|
|
|
| vp, ep -> add_case vals case vp, add_case exns case ep
|
|
|
|
) cases ([], [])
|
|
|
|
|
2012-10-29 00:54:06 -07:00
|
|
|
(* Type paths *)
|
2004-06-14 14:29:05 -07:00
|
|
|
|
2012-10-18 00:35:30 -07:00
|
|
|
let rec expand_path env p =
|
2012-10-18 20:21:27 -07:00
|
|
|
let decl =
|
|
|
|
try Some (Env.find_type p env) with Not_found -> None
|
|
|
|
in
|
|
|
|
match decl with
|
|
|
|
Some {type_manifest = Some ty} ->
|
|
|
|
begin match repr ty with
|
2013-09-04 08:12:37 -07:00
|
|
|
{desc=Tconstr(p,_,_)} -> expand_path env p
|
2017-11-03 11:23:20 -07:00
|
|
|
| _ -> assert false
|
2012-10-18 20:21:27 -07:00
|
|
|
end
|
2013-10-02 06:23:48 -07:00
|
|
|
| _ ->
|
2018-11-22 07:07:15 -08:00
|
|
|
let p' = Env.normalize_type_path None env p in
|
2013-10-02 06:23:48 -07:00
|
|
|
if Path.same p p' then p else expand_path env p'
|
2012-10-18 00:35:30 -07:00
|
|
|
|
2012-10-30 02:02:04 -07:00
|
|
|
let compare_type_path env tpath1 tpath2 =
|
2012-10-29 00:54:06 -07:00
|
|
|
Path.same (expand_path env tpath1) (expand_path env tpath2)
|
|
|
|
|
2011-06-15 02:26:30 -07:00
|
|
|
(* Records *)
|
2019-12-12 12:01:42 -08:00
|
|
|
exception Wrong_name_disambiguation of Env.t * wrong_name
|
2019-12-12 06:09:35 -08:00
|
|
|
|
2018-04-07 14:17:27 -07:00
|
|
|
let get_constr_type_path ty =
|
|
|
|
match (repr ty).desc with
|
|
|
|
| Tconstr(p, _, _) -> p
|
|
|
|
| _ -> assert false
|
|
|
|
|
2012-10-29 00:54:06 -07:00
|
|
|
module NameChoice(Name : sig
|
|
|
|
type t
|
2018-10-12 02:20:21 -07:00
|
|
|
type usage
|
2019-12-14 04:49:28 -08:00
|
|
|
val kind: Datatype_kind.t
|
2012-10-31 20:32:34 -07:00
|
|
|
val get_name: t -> string
|
|
|
|
val get_type: t -> type_expr
|
2018-10-12 02:20:21 -07:00
|
|
|
val lookup_all_from_type:
|
|
|
|
Location.t -> usage -> Path.t -> Env.t -> (t * (unit -> unit)) list
|
2019-12-12 07:37:51 -08:00
|
|
|
|
|
|
|
(** Some names (for example the fields of inline records) are not
|
|
|
|
in the typing environment -- they behave as structural labels
|
|
|
|
rather than nominal labels.*)
|
2014-10-14 08:51:30 -07:00
|
|
|
val in_env: t -> bool
|
2012-10-29 00:54:06 -07:00
|
|
|
end) = struct
|
|
|
|
open Name
|
|
|
|
|
2018-04-07 14:17:27 -07:00
|
|
|
let get_type_path d = get_constr_type_path (get_type d)
|
2012-10-31 20:32:34 -07:00
|
|
|
|
2019-12-12 05:52:12 -08:00
|
|
|
let lookup_from_type env type_path usage lid =
|
|
|
|
let descrs = lookup_all_from_type lid.loc usage type_path env in
|
2012-11-10 19:46:59 -08:00
|
|
|
match lid.txt with
|
2019-12-12 05:52:12 -08:00
|
|
|
| Longident.Lident name -> begin
|
2018-10-12 02:20:21 -07:00
|
|
|
match
|
2019-12-12 05:52:12 -08:00
|
|
|
List.find (fun (nd, _) -> get_name nd = name) descrs
|
2018-10-12 02:20:21 -07:00
|
|
|
with
|
|
|
|
| descr, use ->
|
|
|
|
use ();
|
|
|
|
descr
|
|
|
|
| exception Not_found ->
|
2019-12-12 05:52:12 -08:00
|
|
|
let valid_names = List.map (fun (nd, _) -> get_name nd) descrs in
|
2019-12-12 12:01:42 -08:00
|
|
|
raise (Wrong_name_disambiguation (env, {
|
2019-12-12 05:52:12 -08:00
|
|
|
type_path;
|
|
|
|
name = { lid with txt = name };
|
2019-12-12 06:09:35 -08:00
|
|
|
kind;
|
2019-12-12 05:52:12 -08:00
|
|
|
valid_names;
|
2019-12-12 12:01:42 -08:00
|
|
|
}))
|
2012-11-10 19:46:59 -08:00
|
|
|
end
|
2012-10-31 20:32:34 -07:00
|
|
|
| _ -> raise Not_found
|
|
|
|
|
2013-03-12 07:56:15 -07:00
|
|
|
let rec unique eq acc = function
|
|
|
|
[] -> List.rev acc
|
|
|
|
| x :: rem ->
|
|
|
|
if List.exists (eq x) acc then unique eq acc rem
|
|
|
|
else unique eq (x :: acc) rem
|
|
|
|
|
|
|
|
let ambiguous_types env lbl others =
|
2016-03-09 02:40:16 -08:00
|
|
|
let tpath = get_type_path lbl in
|
2012-10-30 02:02:04 -07:00
|
|
|
let others =
|
2016-03-09 02:40:16 -08:00
|
|
|
List.map (fun (lbl, _) -> get_type_path lbl) others in
|
2013-03-12 07:56:15 -07:00
|
|
|
let tpaths = unique (compare_type_path env) [tpath] others in
|
|
|
|
match tpaths with
|
|
|
|
[_] -> []
|
2018-06-26 13:03:45 -07:00
|
|
|
| _ -> let open Printtyp in
|
|
|
|
wrap_printing_env ~error:true env (fun () ->
|
|
|
|
reset(); strings_of_paths Type tpaths)
|
2012-10-29 00:54:06 -07:00
|
|
|
|
|
|
|
let disambiguate_by_type env tpath lbls =
|
2018-10-12 02:20:21 -07:00
|
|
|
match lbls with
|
|
|
|
| (Error _ : _ result) -> raise Not_found
|
|
|
|
| Ok lbls ->
|
|
|
|
let check_type (lbl, _) =
|
|
|
|
let lbl_tpath = get_type_path lbl in
|
|
|
|
compare_type_path env tpath lbl_tpath
|
|
|
|
in
|
|
|
|
List.find check_type lbls
|
2012-10-29 00:54:06 -07:00
|
|
|
|
2019-12-18 04:03:11 -08:00
|
|
|
(* warn if there are several distinct candidates in scope *)
|
2019-12-17 09:41:38 -08:00
|
|
|
let warn_if_ambiguous warn lid env lbl rest =
|
|
|
|
Printtyp.Conflicts.reset ();
|
|
|
|
let paths = ambiguous_types env lbl rest in
|
|
|
|
let expansion =
|
|
|
|
Format.asprintf "%t" Printtyp.Conflicts.print_explanations in
|
|
|
|
if paths <> [] then
|
|
|
|
warn lid.loc
|
|
|
|
(Warnings.Ambiguous_name ([Longident.last lid.txt],
|
|
|
|
paths, false, expansion))
|
|
|
|
|
2019-12-18 04:03:11 -08:00
|
|
|
(* a non-principal type was used for disambiguation *)
|
2019-12-17 09:41:38 -08:00
|
|
|
let warn_non_principal warn lid =
|
|
|
|
let name = Datatype_kind.label_name kind in
|
|
|
|
warn lid.loc
|
|
|
|
(Warnings.Not_principal
|
|
|
|
("this type-based " ^ name ^ " disambiguation"))
|
|
|
|
|
2019-12-18 04:03:11 -08:00
|
|
|
(* we selected a name out of the lexical scope *)
|
2019-12-17 09:41:38 -08:00
|
|
|
let warn_out_of_scope warn lid env tpath =
|
|
|
|
let path_s =
|
|
|
|
Printtyp.wrap_printing_env ~error:true env
|
|
|
|
(fun () -> Printtyp.string_of_path tpath) in
|
|
|
|
warn lid.loc
|
|
|
|
(Warnings.Name_out_of_scope (path_s, [Longident.last lid.txt], false))
|
|
|
|
|
2019-12-17 10:01:35 -08:00
|
|
|
(* warn if the selected name is not the last introduced in scope
|
|
|
|
-- in these cases the resolution is different from pre-disambiguation OCaml
|
|
|
|
(this warning is not enabled by default, it is specifically for people
|
|
|
|
wishing to write backward-compatible code).
|
|
|
|
*)
|
2019-12-18 04:03:11 -08:00
|
|
|
let warn_if_disambiguated_name warn lid lbl scope =
|
2019-12-17 09:41:38 -08:00
|
|
|
match scope with
|
|
|
|
| Ok ((lab1,_) :: _) when lab1 == lbl -> ()
|
|
|
|
| _ ->
|
|
|
|
warn lid.loc
|
|
|
|
(Warnings.Disambiguated_name (get_name lbl))
|
|
|
|
|
2019-12-18 02:39:55 -08:00
|
|
|
let force_error : ('a, _) result -> 'a = function
|
|
|
|
| Ok lbls -> lbls
|
|
|
|
| Error (loc', env', err) ->
|
|
|
|
Env.lookup_error loc' env' err
|
|
|
|
|
|
|
|
type candidate = t * (unit -> unit)
|
|
|
|
type nonempty_candidate_filter =
|
|
|
|
candidate list -> (candidate list, candidate list) result
|
|
|
|
(** This type is used for candidate filtering functions.
|
|
|
|
Filtering typically proceeds in several passes, filtering
|
|
|
|
candidates through increasingly precise conditions.
|
|
|
|
|
|
|
|
We assume that the input list is non-empty, and the output is one of
|
|
|
|
- [Ok result] for a non-empty list [result] of valid candidates
|
|
|
|
- [Error candidates] with there are no valid candidates,
|
|
|
|
and [candidates] is a non-empty subset of the input, typically
|
|
|
|
the result of the last non-empty filtering step.
|
|
|
|
*)
|
|
|
|
|
2019-12-17 23:56:17 -08:00
|
|
|
(** [disambiguate] selects a concrete description for [lid] using
|
2019-12-18 02:39:55 -08:00
|
|
|
some contextual information:
|
|
|
|
- An optional [expected_type].
|
|
|
|
- A list of candidates labels in the current lexical scope,
|
|
|
|
[candidates_in_scope], that is actually at the type
|
|
|
|
[(label_descr list, lookup_error) result] so that the
|
|
|
|
lookup error is only raised when necessary.
|
|
|
|
- A filtering criterion on candidates in scope [filter_candidates],
|
|
|
|
representing extra contextual information that can help
|
|
|
|
candidate selection (see [disambiguate_label_by_ids]).
|
|
|
|
*)
|
|
|
|
let disambiguate
|
|
|
|
?(warn=Location.prerr_warning)
|
|
|
|
?(filter : nonempty_candidate_filter = Result.ok)
|
|
|
|
usage lid env
|
|
|
|
expected_type
|
|
|
|
candidates_in_scope =
|
2019-12-12 07:06:01 -08:00
|
|
|
let lbl = match expected_type with
|
|
|
|
| None ->
|
2019-12-12 07:37:51 -08:00
|
|
|
(* no expected type => no disambiguation *)
|
2019-12-18 02:39:55 -08:00
|
|
|
begin match filter (force_error candidates_in_scope) with
|
|
|
|
| Ok [] | Error [] -> assert false
|
|
|
|
| Error((lbl, _use) :: _rest) -> lbl (* will fail later *)
|
2018-10-12 02:20:21 -07:00
|
|
|
| Ok((lbl, use) :: rest) ->
|
2013-09-04 08:12:37 -07:00
|
|
|
use ();
|
2019-12-17 09:41:38 -08:00
|
|
|
warn_if_ambiguous warn lid env lbl rest;
|
2013-09-04 08:12:37 -07:00
|
|
|
lbl
|
|
|
|
end
|
2019-12-12 07:06:01 -08:00
|
|
|
| Some(tpath0, tpath, principal) ->
|
2019-12-18 02:39:55 -08:00
|
|
|
(* If [expected_type] is available, the candidate selected
|
|
|
|
will correspond to the type-based resolution.
|
|
|
|
There are two reasons to still check the lexical scope:
|
|
|
|
- for warning purposes
|
|
|
|
- for extension types, the type environment does not contain
|
|
|
|
a list of constructors, so using only type-based selection
|
|
|
|
would fail.
|
|
|
|
*)
|
|
|
|
(* note that [disambiguate_by_type] does not
|
|
|
|
force [candidates_in_scope]: we just skip this case if there
|
|
|
|
are no candidates in scope *)
|
|
|
|
begin match disambiguate_by_type env tpath candidates_in_scope with
|
2019-12-12 07:06:01 -08:00
|
|
|
| lbl, use ->
|
2012-10-29 00:54:06 -07:00
|
|
|
use ();
|
2019-12-12 07:06:01 -08:00
|
|
|
if not principal then begin
|
2012-10-29 00:54:06 -07:00
|
|
|
(* Check if non-principal type is affecting result *)
|
2019-12-18 02:39:55 -08:00
|
|
|
match (candidates_in_scope : _ result) with
|
|
|
|
| Error _ -> warn_non_principal warn lid
|
|
|
|
| Ok lbls ->
|
|
|
|
match filter lbls with
|
|
|
|
| Error _ -> warn_non_principal warn lid
|
|
|
|
| Ok [] -> assert false
|
2018-10-12 02:20:21 -07:00
|
|
|
| Ok ((lbl', _use') :: rest) ->
|
2019-12-18 02:39:55 -08:00
|
|
|
let lbl_tpath = get_type_path lbl' in
|
|
|
|
(* no principality warning if the non-principal
|
|
|
|
type-based selection corresponds to the last
|
|
|
|
definition in scope *)
|
|
|
|
if not (compare_type_path env tpath lbl_tpath)
|
|
|
|
then warn_non_principal warn lid
|
|
|
|
else warn_if_ambiguous warn lid env lbl rest;
|
2012-10-29 00:54:06 -07:00
|
|
|
end;
|
|
|
|
lbl
|
2019-12-12 07:06:01 -08:00
|
|
|
| exception Not_found ->
|
2019-12-18 02:39:55 -08:00
|
|
|
(* look outside the lexical scope *)
|
2019-12-12 07:06:01 -08:00
|
|
|
match lookup_from_type env tpath usage lid with
|
|
|
|
| lbl ->
|
2019-12-12 07:37:51 -08:00
|
|
|
(* warn only on nominal labels;
|
|
|
|
structural labels cannot be qualified anyway *)
|
2019-12-17 09:41:38 -08:00
|
|
|
if in_env lbl then warn_out_of_scope warn lid env tpath;
|
|
|
|
if not principal then warn_non_principal warn lid;
|
2012-10-29 00:54:06 -07:00
|
|
|
lbl
|
2019-12-12 07:06:01 -08:00
|
|
|
| exception Not_found ->
|
2019-12-18 02:39:55 -08:00
|
|
|
match filter (force_error candidates_in_scope) with
|
|
|
|
| Ok lbls | Error lbls ->
|
2019-12-12 07:06:01 -08:00
|
|
|
let tp = (tpath0, expand_path env tpath) in
|
|
|
|
let tpl =
|
|
|
|
List.map
|
|
|
|
(fun (lbl, _) ->
|
|
|
|
let tp0 = get_type_path lbl in
|
|
|
|
let tp = expand_path env tp0 in
|
|
|
|
(tp0, tp))
|
|
|
|
lbls
|
|
|
|
in
|
|
|
|
raise (Error (lid.loc, env,
|
|
|
|
Name_type_mismatch (kind, lid.txt, tp, tpl)));
|
|
|
|
end
|
2013-02-18 18:45:09 -08:00
|
|
|
in
|
2019-12-12 07:37:51 -08:00
|
|
|
(* warn only on nominal labels *)
|
2014-10-14 08:51:30 -07:00
|
|
|
if in_env lbl then
|
2019-12-18 02:39:55 -08:00
|
|
|
warn_if_disambiguated_name warn lid lbl candidates_in_scope;
|
2013-02-18 18:45:09 -08:00
|
|
|
lbl
|
2012-10-29 00:54:06 -07:00
|
|
|
end
|
|
|
|
|
2019-12-12 06:09:35 -08:00
|
|
|
let wrap_disambiguate msg ty f x =
|
2019-12-12 05:52:12 -08:00
|
|
|
try f x with
|
2019-12-12 12:01:42 -08:00
|
|
|
| Wrong_name_disambiguation (env, wrong_name) ->
|
|
|
|
raise (Error (wrong_name.name.loc, env, Wrong_name (msg, ty, wrong_name)))
|
2013-09-19 00:25:51 -07:00
|
|
|
|
2012-10-29 00:54:06 -07:00
|
|
|
module Label = NameChoice (struct
|
|
|
|
type t = label_description
|
2018-10-12 02:20:21 -07:00
|
|
|
type usage = unit
|
2019-12-14 04:49:28 -08:00
|
|
|
let kind = Datatype_kind.Record
|
2012-10-31 20:32:34 -07:00
|
|
|
let get_name lbl = lbl.lbl_name
|
|
|
|
let get_type lbl = lbl.lbl_res
|
2018-10-12 02:20:21 -07:00
|
|
|
let lookup_all_from_type loc () path env =
|
|
|
|
Env.lookup_all_labels_from_type ~loc path env
|
2014-10-14 08:51:30 -07:00
|
|
|
let in_env lbl =
|
|
|
|
match lbl.lbl_repres with
|
2016-05-25 07:29:05 -07:00
|
|
|
| Record_regular | Record_float | Record_unboxed false -> true
|
2018-02-08 09:51:47 -08:00
|
|
|
| Record_unboxed true | Record_inlined _ | Record_extension _ -> false
|
2012-10-29 00:54:06 -07:00
|
|
|
end)
|
|
|
|
|
2019-12-12 07:37:51 -08:00
|
|
|
(* In record-construction expressions and patterns, we have many labels
|
|
|
|
at once; find a candidate type in the intersection of the candidates
|
|
|
|
of each label. In the [closed] expression case, this candidate must
|
2019-12-16 05:11:38 -08:00
|
|
|
contain exactly all the labels.
|
|
|
|
|
|
|
|
If our successive refinements result in an empty list,
|
|
|
|
return [Error] with the last non-empty list of candidates
|
|
|
|
for use in error messages.
|
|
|
|
*)
|
|
|
|
let disambiguate_label_by_ids closed ids labels : (_, _) result =
|
2012-10-29 00:54:06 -07:00
|
|
|
let check_ids (lbl, _) =
|
|
|
|
let lbls = Hashtbl.create 8 in
|
|
|
|
Array.iter (fun lbl -> Hashtbl.add lbls lbl.lbl_name ()) lbl.lbl_all;
|
|
|
|
List.for_all (Hashtbl.mem lbls) ids
|
|
|
|
and check_closed (lbl, _) =
|
|
|
|
(not closed || List.length ids = Array.length lbl.lbl_all)
|
2012-09-21 04:16:02 -07:00
|
|
|
in
|
2019-12-16 05:11:38 -08:00
|
|
|
match List.filter check_ids labels with
|
|
|
|
| [] -> Error labels
|
|
|
|
| labels ->
|
|
|
|
match List.filter check_closed labels with
|
|
|
|
| [] -> Error labels
|
|
|
|
| labels ->
|
|
|
|
Ok labels
|
2012-10-30 02:02:04 -07:00
|
|
|
|
2012-10-29 00:54:06 -07:00
|
|
|
(* Only issue warnings once per record constructor/pattern *)
|
2019-12-12 07:06:01 -08:00
|
|
|
let disambiguate_lid_a_list loc closed env expected_type lid_a_list =
|
2012-10-29 00:54:06 -07:00
|
|
|
let ids = List.map (fun (lid, _) -> Longident.last lid.txt) lid_a_list in
|
2013-04-26 06:09:24 -07:00
|
|
|
let w_pr = ref false and w_amb = ref []
|
|
|
|
and w_scope = ref [] and w_scope_ty = ref "" in
|
2012-10-29 00:54:06 -07:00
|
|
|
let warn loc msg =
|
2012-10-29 18:02:08 -07:00
|
|
|
let open Warnings in
|
|
|
|
match msg with
|
|
|
|
| Not_principal _ -> w_pr := true
|
2018-06-26 13:03:45 -07:00
|
|
|
| Ambiguous_name([s], l, _, ex) -> w_amb := (s, l, ex) :: !w_amb
|
2013-04-26 06:09:24 -07:00
|
|
|
| Name_out_of_scope(ty, [s], _) ->
|
|
|
|
w_scope := s :: !w_scope; w_scope_ty := ty
|
2012-10-29 18:02:08 -07:00
|
|
|
| _ -> Location.prerr_warning loc msg
|
|
|
|
in
|
|
|
|
let process_label lid =
|
2018-10-12 02:20:21 -07:00
|
|
|
let scope = Env.lookup_all_labels ~loc:lid.loc lid.txt env in
|
2019-12-18 02:39:55 -08:00
|
|
|
let filter : Label.nonempty_candidate_filter =
|
|
|
|
disambiguate_label_by_ids closed ids in
|
|
|
|
Label.disambiguate ~warn ~filter () lid env expected_type scope in
|
2012-10-29 18:02:08 -07:00
|
|
|
let lbl_a_list =
|
|
|
|
List.map (fun (lid,a) -> lid, process_label lid, a) lid_a_list in
|
|
|
|
if !w_pr then
|
|
|
|
Location.prerr_warning loc
|
2013-03-12 07:56:15 -07:00
|
|
|
(Warnings.Not_principal "this type-based record disambiguation")
|
|
|
|
else begin
|
|
|
|
match List.rev !w_amb with
|
2018-06-26 13:03:45 -07:00
|
|
|
(_,types,ex)::_ as amb ->
|
2013-03-12 07:56:15 -07:00
|
|
|
let paths =
|
2016-03-09 02:40:16 -08:00
|
|
|
List.map (fun (_,lbl,_) -> Label.get_type_path lbl) lbl_a_list in
|
2013-03-12 07:56:15 -07:00
|
|
|
let path = List.hd paths in
|
2018-06-26 13:03:45 -07:00
|
|
|
let fst3 (x,_,_) = x in
|
2013-03-12 07:56:15 -07:00
|
|
|
if List.for_all (compare_type_path env path) (List.tl paths) then
|
|
|
|
Location.prerr_warning loc
|
2018-06-26 13:03:45 -07:00
|
|
|
(Warnings.Ambiguous_name (List.map fst3 amb, types, true, ex))
|
2013-03-12 07:56:15 -07:00
|
|
|
else
|
|
|
|
List.iter
|
2018-06-26 13:03:45 -07:00
|
|
|
(fun (s,l,ex) -> Location.prerr_warning loc
|
|
|
|
(Warnings.Ambiguous_name ([s],l,false, ex)))
|
2013-03-12 07:56:15 -07:00
|
|
|
amb
|
|
|
|
| _ -> ()
|
|
|
|
end;
|
2012-10-30 02:02:04 -07:00
|
|
|
if !w_scope <> [] then
|
|
|
|
Location.prerr_warning loc
|
2013-04-26 06:09:24 -07:00
|
|
|
(Warnings.Name_out_of_scope (!w_scope_ty, List.rev !w_scope, true));
|
2012-10-29 18:02:08 -07:00
|
|
|
lbl_a_list
|
2012-10-29 00:54:06 -07:00
|
|
|
|
2004-06-14 14:29:05 -07:00
|
|
|
let rec find_record_qual = function
|
|
|
|
| [] -> None
|
2012-05-30 07:52:37 -07:00
|
|
|
| ({ txt = Longident.Ldot (modname, _) }, _) :: _ -> Some modname
|
2004-06-14 14:29:05 -07:00
|
|
|
| _ :: rest -> find_record_qual rest
|
|
|
|
|
2015-05-19 15:52:21 -07:00
|
|
|
let map_fold_cont f xs k =
|
|
|
|
List.fold_right (fun x k ys -> f x (fun y -> k (y :: ys)))
|
|
|
|
xs (fun ys -> k (List.rev ys)) []
|
|
|
|
|
2019-12-12 07:06:01 -08:00
|
|
|
let type_label_a_list
|
|
|
|
?labels loc closed env type_lbl_a expected_type lid_a_list k =
|
2011-06-15 02:26:30 -07:00
|
|
|
let lbl_a_list =
|
2012-10-29 00:54:06 -07:00
|
|
|
match lid_a_list, labels with
|
|
|
|
({txt=Longident.Lident s}, _)::_, Some labels when Hashtbl.mem labels s ->
|
|
|
|
(* Special case for rebuilt syntax trees *)
|
|
|
|
List.map
|
|
|
|
(function lid, a -> match lid.txt with
|
|
|
|
Longident.Lident s -> lid, Hashtbl.find labels s, a
|
|
|
|
| _ -> assert false)
|
|
|
|
lid_a_list
|
|
|
|
| _ ->
|
|
|
|
let lid_a_list =
|
|
|
|
match find_record_qual lid_a_list with
|
|
|
|
None -> lid_a_list
|
|
|
|
| Some modname ->
|
|
|
|
List.map
|
|
|
|
(fun (lid, a as lid_a) ->
|
|
|
|
match lid.txt with Longident.Lident s ->
|
|
|
|
{lid with txt=Longident.Ldot (modname, s)}, a
|
|
|
|
| _ -> lid_a)
|
|
|
|
lid_a_list
|
|
|
|
in
|
2019-12-12 07:06:01 -08:00
|
|
|
disambiguate_lid_a_list loc closed env expected_type lid_a_list
|
2012-10-29 00:54:06 -07:00
|
|
|
in
|
2011-06-15 02:26:30 -07:00
|
|
|
(* Invariant: records are sorted in the typed tree *)
|
|
|
|
let lbl_a_list =
|
|
|
|
List.sort
|
2012-10-29 00:54:06 -07:00
|
|
|
(fun (_,lbl1,_) (_,lbl2,_) -> compare lbl1.lbl_pos lbl2.lbl_pos)
|
2011-06-15 02:26:30 -07:00
|
|
|
lbl_a_list
|
|
|
|
in
|
2015-05-19 15:52:21 -07:00
|
|
|
map_fold_cont type_lbl_a lbl_a_list k
|
2012-05-30 07:52:37 -07:00
|
|
|
;;
|
2011-06-15 02:26:30 -07:00
|
|
|
|
2009-09-12 05:41:07 -07:00
|
|
|
(* Checks over the labels mentioned in a record pattern:
|
|
|
|
no duplicate definitions (error); properly closed (warning) *)
|
|
|
|
|
|
|
|
let check_recordpat_labels loc lbl_pat_list closed =
|
|
|
|
match lbl_pat_list with
|
|
|
|
| [] -> () (* should not happen *)
|
2012-10-24 05:03:00 -07:00
|
|
|
| (_, label1, _) :: _ ->
|
2009-09-12 05:41:07 -07:00
|
|
|
let all = label1.lbl_all in
|
|
|
|
let defined = Array.make (Array.length all) false in
|
2012-10-24 05:03:00 -07:00
|
|
|
let check_defined (_, label, _) =
|
2009-09-12 05:41:07 -07:00
|
|
|
if defined.(label.lbl_pos)
|
2013-01-29 06:21:12 -08:00
|
|
|
then raise(Error(loc, Env.empty, Label_multiply_defined label.lbl_name))
|
2009-09-12 05:41:07 -07:00
|
|
|
else defined.(label.lbl_pos) <- true in
|
|
|
|
List.iter check_defined lbl_pat_list;
|
|
|
|
if closed = Closed
|
2020-07-10 10:56:19 -07:00
|
|
|
&& Warnings.is_active (Warnings.Missing_record_field_pattern "")
|
2009-09-12 05:41:07 -07:00
|
|
|
then begin
|
|
|
|
let undefined = ref [] in
|
|
|
|
for i = 0 to Array.length all - 1 do
|
|
|
|
if not defined.(i) then undefined := all.(i).lbl_name :: !undefined
|
|
|
|
done;
|
|
|
|
if !undefined <> [] then begin
|
|
|
|
let u = String.concat ", " (List.rev !undefined) in
|
2020-07-10 10:56:19 -07:00
|
|
|
Location.prerr_warning loc (Warnings.Missing_record_field_pattern u)
|
2009-09-12 05:41:07 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-06 01:41:21 -08:00
|
|
|
(* Constructors *)
|
|
|
|
|
|
|
|
module Constructor = NameChoice (struct
|
|
|
|
type t = constructor_description
|
2018-10-12 02:20:21 -07:00
|
|
|
type usage = Env.constructor_usage
|
2019-12-14 04:49:28 -08:00
|
|
|
let kind = Datatype_kind.Variant
|
2012-12-06 01:41:21 -08:00
|
|
|
let get_name cstr = cstr.cstr_name
|
|
|
|
let get_type cstr = cstr.cstr_res
|
2018-10-12 02:20:21 -07:00
|
|
|
let lookup_all_from_type loc usage path env =
|
2018-04-07 14:17:27 -07:00
|
|
|
match Env.lookup_all_constructors_from_type ~loc usage path env with
|
|
|
|
| _ :: _ as x -> x
|
|
|
|
| [] ->
|
|
|
|
match (Env.find_type path env).type_kind with
|
|
|
|
| Type_open ->
|
|
|
|
(* Extension constructors cannot be found by looking at the type
|
|
|
|
declaration.
|
|
|
|
We scan the whole environment to get an accurate spellchecking
|
|
|
|
hint in the subsequent error message *)
|
|
|
|
let filter lbl =
|
|
|
|
compare_type_path env
|
|
|
|
path (get_constr_type_path @@ get_type lbl) in
|
|
|
|
let add_valid x acc = if filter x then (x,ignore)::acc else acc in
|
|
|
|
Env.fold_constructors add_valid None env []
|
|
|
|
| _ -> []
|
2014-10-14 08:51:30 -07:00
|
|
|
let in_env _ = true
|
2012-12-06 01:41:21 -08:00
|
|
|
end)
|
|
|
|
|
2011-10-25 05:11:06 -07:00
|
|
|
(* unification of a type with a tconstr with
|
|
|
|
freshly created arguments *)
|
2019-12-02 11:49:51 -08:00
|
|
|
let unify_head_only ~refine loc env ty constr =
|
2010-12-13 22:33:06 -08:00
|
|
|
let (_, ty_res) = instance_constructor constr in
|
2019-12-02 11:49:51 -08:00
|
|
|
let ty_res = repr ty_res in
|
|
|
|
match ty_res.desc with
|
2010-11-09 00:21:44 -08:00
|
|
|
| Tconstr(p,args,m) ->
|
2011-10-25 05:11:06 -07:00
|
|
|
ty_res.desc <- Tconstr(p,List.map (fun _ -> newvar ()) args,m);
|
2019-12-02 11:49:51 -08:00
|
|
|
enforce_constraints !env ty_res;
|
|
|
|
unify_pat_types ~refine loc env ty_res ty
|
2010-11-09 00:21:44 -08:00
|
|
|
| _ -> assert false
|
|
|
|
|
2009-09-12 05:41:07 -07:00
|
|
|
(* Typing of patterns *)
|
|
|
|
|
2018-04-23 03:08:43 -07:00
|
|
|
(* "half typed" cases are produced in [type_cases] when we've just typechecked
|
|
|
|
the pattern but haven't type-checked the body yet.
|
|
|
|
At this point we might have added some type equalities to the environment,
|
|
|
|
but haven't yet added identifiers bound by the pattern. *)
|
2019-09-20 06:23:07 -07:00
|
|
|
type 'case_pattern half_typed_case =
|
|
|
|
{ typed_pat: 'case_pattern;
|
2018-04-23 03:08:43 -07:00
|
|
|
pat_type_for_unif: type_expr;
|
|
|
|
untyped_case: Parsetree.case;
|
|
|
|
branch_env: Env.t;
|
|
|
|
pat_vars: pattern_variable list;
|
2018-04-23 03:15:19 -07:00
|
|
|
unpacks: module_variable list;
|
|
|
|
contains_gadt: bool; }
|
2018-04-23 03:08:43 -07:00
|
|
|
|
2018-11-03 05:08:55 -07:00
|
|
|
let rec has_literal_pattern p = match p.ppat_desc with
|
|
|
|
| Ppat_constant _
|
|
|
|
| Ppat_interval _ ->
|
|
|
|
true
|
|
|
|
| Ppat_any
|
|
|
|
| Ppat_variant (_, None)
|
|
|
|
| Ppat_construct (_, None)
|
|
|
|
| Ppat_type _
|
|
|
|
| Ppat_var _
|
|
|
|
| Ppat_unpack _
|
|
|
|
| Ppat_extension _ ->
|
|
|
|
false
|
|
|
|
| Ppat_exception p
|
|
|
|
| Ppat_variant (_, Some p)
|
|
|
|
| Ppat_construct (_, Some p)
|
|
|
|
| Ppat_constraint (p, _)
|
|
|
|
| Ppat_alias (p, _)
|
|
|
|
| Ppat_lazy p
|
|
|
|
| Ppat_open (_, p) ->
|
|
|
|
has_literal_pattern p
|
|
|
|
| Ppat_tuple ps
|
|
|
|
| Ppat_array ps ->
|
|
|
|
List.exists has_literal_pattern ps
|
|
|
|
| Ppat_record (ps, _) ->
|
|
|
|
List.exists (fun (_,p) -> has_literal_pattern p) ps
|
|
|
|
| Ppat_or (p, q) ->
|
|
|
|
has_literal_pattern p || has_literal_pattern q
|
2018-04-23 03:08:43 -07:00
|
|
|
|
2018-09-26 09:29:39 -07:00
|
|
|
let check_scope_escape loc env level ty =
|
|
|
|
try Ctype.check_scope_escape env level ty
|
|
|
|
with Unify trace ->
|
2019-03-07 11:05:48 -08:00
|
|
|
raise(Error(loc, env, Pattern_type_clash(trace, None)))
|
2018-09-26 09:29:39 -07:00
|
|
|
|
2019-10-08 10:16:44 -07:00
|
|
|
type pattern_checking_mode =
|
|
|
|
| Normal
|
|
|
|
(** We are checking user code. *)
|
|
|
|
| Counter_example of counter_example_checking_info
|
2019-10-16 02:26:03 -07:00
|
|
|
(** In [Counter_example] mode, we are checking a counter-example
|
|
|
|
candidate produced by Parmatch. This is a syntactic pattern that
|
|
|
|
represents a set of values by using or-patterns (p_1 | ... | p_n)
|
|
|
|
to enumerate all alternatives in the counter-example
|
|
|
|
search. These or-patterns occur at every choice point, possibly
|
|
|
|
deep inside the pattern.
|
|
|
|
|
|
|
|
Parmatch does not use type information, so this pattern may
|
|
|
|
exhibit two issues:
|
|
|
|
- some parts of the pattern may be ill-typed due to GADTs, and
|
|
|
|
- some wildcard patterns may not match any values: their type is
|
|
|
|
empty.
|
|
|
|
|
|
|
|
The aim of [type_pat] in the [Counter_example] mode is to refine
|
|
|
|
this syntactic pattern into a well-typed pattern, and ensure
|
|
|
|
that it matches at least one concrete value.
|
|
|
|
- It filters ill-typed branches of or-patterns.
|
|
|
|
(see {!splitting_mode} below)
|
|
|
|
- It tries to check that wildcard patterns are non-empty.
|
|
|
|
(see {!explosion_fuel})
|
|
|
|
*)
|
2019-10-15 06:03:40 -07:00
|
|
|
|
2019-10-08 10:16:44 -07:00
|
|
|
and counter_example_checking_info = {
|
2019-10-14 07:49:42 -07:00
|
|
|
explosion_fuel: int;
|
2019-10-08 10:16:44 -07:00
|
|
|
splitting_mode: splitting_mode;
|
2019-10-14 07:49:42 -07:00
|
|
|
constrs: (string, Types.constructor_description) Hashtbl.t;
|
|
|
|
labels: (string, Types.label_description) Hashtbl.t;
|
2019-10-08 10:16:44 -07:00
|
|
|
}
|
2019-10-15 06:03:40 -07:00
|
|
|
(**
|
2019-10-16 02:26:03 -07:00
|
|
|
[explosion_fuel] controls the checking of wildcard patterns. We
|
|
|
|
eliminate potentially-empty wildcard patterns by exploding them
|
|
|
|
into concrete sub-patterns, for example (K1 _ | K2 _) or
|
|
|
|
{ l1: _; l2: _ }. [explosion_fuel] is the depth limit on wildcard
|
|
|
|
explosion. Such depth limit is required to avoid non-termination
|
|
|
|
and compilation-time blowups.
|
|
|
|
|
|
|
|
[splitting_mode] controls the handling of or-patterns. In
|
|
|
|
[Counter_example] mode, we only need to select one branch that
|
|
|
|
leads to a well-typed pattern. Checking all branches is expensive,
|
|
|
|
we use different search strategies (see {!splitting_mode}) to
|
|
|
|
reduce the number of explored alternatives.
|
|
|
|
|
|
|
|
[constrs] and [labels] contain metadata produced by [Parmatch] to
|
|
|
|
type-check the given syntactic pattern. [Parmatch] produces
|
|
|
|
counter-examples by turning typed patterns into
|
|
|
|
[Parsetree.pattern]. In this process, constructor and label paths
|
|
|
|
are lost, and are replaced by generated strings. [constrs] and
|
|
|
|
[labels] map those synthetic names back to the typed descriptions
|
|
|
|
of the original names.
|
|
|
|
*)
|
2019-10-08 10:16:44 -07:00
|
|
|
|
2019-10-16 02:53:47 -07:00
|
|
|
(** Due to GADT constraints, an or-pattern produced within
|
|
|
|
a counter-example may have ill-typed branches. Consider for example
|
|
|
|
|
2020-02-19 01:36:55 -08:00
|
|
|
{[
|
2019-10-16 02:53:47 -07:00
|
|
|
type _ tag = Int : int tag | Bool : bool tag
|
2020-02-19 01:36:55 -08:00
|
|
|
]}
|
2019-10-16 02:53:47 -07:00
|
|
|
|
|
|
|
then [Parmatch] will propose the or-pattern [Int | Bool] whenever
|
|
|
|
a pattern of type [tag] is required to form a counter-example. For
|
|
|
|
example, a function expects a (int tag option) and only [None] is
|
|
|
|
handled by the user-written pattern. [Some (Int | Bool)] is not
|
|
|
|
well-typed in this context, only the sub-pattern [Some Int] is.
|
|
|
|
In this example, the expected type coming from the context
|
|
|
|
suffices to know which or-pattern branch must be chosen.
|
|
|
|
|
|
|
|
In the general case, choosing a branch can have non-local effects
|
|
|
|
on the typability of the term. For example, consider a tuple type
|
|
|
|
['a tag * ...'a...], where the first component is a GADT. All
|
|
|
|
constructor choices for this GADT lead to a well-typed branch in
|
|
|
|
isolation (['a] is unconstrained), but choosing one of them adds
|
|
|
|
a constraint on ['a] that may make the other tuple elements
|
|
|
|
ill-typed.
|
|
|
|
|
|
|
|
In general, after choosing each possible branch of the or-pattern,
|
|
|
|
[type_pat] has to check the rest of the pattern to tell if this
|
|
|
|
choice leads to a well-typed term. This may lead to an explosion
|
|
|
|
of typing/search work -- the rest of the term may in turn contain
|
|
|
|
alternatives.
|
|
|
|
|
|
|
|
We use careful strategies to try to limit counterexample-checking
|
|
|
|
time; [splitting_mode] represents those strategies.
|
|
|
|
*)
|
2019-10-14 07:49:42 -07:00
|
|
|
and splitting_mode =
|
|
|
|
| Backtrack_or
|
2019-10-16 02:26:03 -07:00
|
|
|
(** Always backtrack in or-patterns.
|
2019-10-08 10:16:44 -07:00
|
|
|
|
2019-10-16 02:26:03 -07:00
|
|
|
[Backtrack_or] selects a single alternative from an or-pattern
|
|
|
|
by using backtracking, trying to choose each branch in turn, and
|
|
|
|
to complete it into a valid sub-pattern. We call this
|
|
|
|
"splitting" the or-pattern.
|
2019-10-15 06:03:40 -07:00
|
|
|
|
2019-10-16 07:31:35 -07:00
|
|
|
We use this mode when looking for unused patterns or sub-patterns,
|
|
|
|
in particular to check a refutation clause (p -> .).
|
2019-10-14 07:49:42 -07:00
|
|
|
*)
|
|
|
|
| Refine_or of { inside_nonsplit_or: bool; }
|
2019-10-16 02:26:03 -07:00
|
|
|
(** Only backtrack when needed.
|
2019-10-08 10:16:44 -07:00
|
|
|
|
2020-02-19 01:36:55 -08:00
|
|
|
[Refine_or] tries another approach for refining or-pattern.
|
2019-10-16 02:26:03 -07:00
|
|
|
|
2020-02-19 01:36:55 -08:00
|
|
|
Instead of always splitting each or-pattern, It first attempts to
|
|
|
|
find branches that do not introduce new constraints (because they
|
|
|
|
do not contain GADT constructors). Those branches are such that,
|
|
|
|
if they fail, all other branches will fail.
|
2019-10-16 02:26:03 -07:00
|
|
|
|
2020-02-19 01:36:55 -08:00
|
|
|
If we find one such branch, we attempt to complete the subpattern
|
|
|
|
(checking what's outside the or-pattern), ignoring other
|
|
|
|
branches -- we never consider another branch choice again. If all
|
|
|
|
branches are constrained, it falls back to splitting the
|
|
|
|
or-pattern.
|
2019-10-08 10:16:44 -07:00
|
|
|
|
2020-02-19 01:36:55 -08:00
|
|
|
We use this mode when checking exhaustivity of pattern matching.
|
|
|
|
*)
|
2019-10-08 10:16:44 -07:00
|
|
|
|
2020-02-19 01:36:55 -08:00
|
|
|
(** This exception is only used internally within [type_pat_aux], in
|
|
|
|
counter-example mode, to jump back to the parent or-pattern in the
|
|
|
|
[Refine_or] strategy.
|
2019-10-16 02:26:03 -07:00
|
|
|
|
2020-02-19 01:36:55 -08:00
|
|
|
Such a parent exists precisely when [inside_nonsplit_or = true];
|
|
|
|
it's an invariant that we always setup an exception handler for
|
|
|
|
[Need_backtrack] when we set this flag. *)
|
|
|
|
exception Need_backtrack
|
|
|
|
|
|
|
|
(** This exception is only used internally within [type_pat_aux], in
|
|
|
|
counter-example mode. We use it to discard counter-example candidates
|
|
|
|
that do not match any value. *)
|
|
|
|
exception Empty_branch
|
2019-10-08 10:16:44 -07:00
|
|
|
|
2020-02-19 01:10:05 -08:00
|
|
|
type abort_reason = Adds_constraints | Empty
|
|
|
|
|
2019-10-08 10:16:44 -07:00
|
|
|
(** Remember current typing state for backtracking.
|
|
|
|
No variable information, as we only backtrack on
|
|
|
|
patterns without variables (cf. assert statements). *)
|
|
|
|
type state =
|
|
|
|
{ snapshot: Btype.snapshot;
|
|
|
|
levels: Ctype.levels;
|
|
|
|
env: Env.t; }
|
|
|
|
let save_state env =
|
|
|
|
{ snapshot = Btype.snapshot ();
|
|
|
|
levels = Ctype.save_levels ();
|
|
|
|
env = !env; }
|
|
|
|
let set_state s env =
|
|
|
|
Btype.backtrack s.snapshot;
|
|
|
|
Ctype.set_levels s.levels;
|
|
|
|
env := s.env
|
|
|
|
|
2019-10-02 08:37:54 -07:00
|
|
|
(** Find the first alternative in the tree of or-patterns for which
|
|
|
|
[f] does not raise an error. If all fail, the last error is
|
|
|
|
propagated *)
|
|
|
|
let rec find_valid_alternative f pat =
|
|
|
|
match pat.ppat_desc with
|
|
|
|
| Ppat_or(p1,p2) ->
|
2020-02-17 09:33:12 -08:00
|
|
|
(try find_valid_alternative f p1 with
|
|
|
|
| Empty_branch | Error _ -> find_valid_alternative f p2
|
|
|
|
)
|
2019-10-02 08:37:54 -07:00
|
|
|
| _ -> f pat
|
|
|
|
|
2019-10-08 10:16:44 -07:00
|
|
|
let no_explosion = function
|
|
|
|
| Normal -> Normal
|
|
|
|
| Counter_example info ->
|
2019-10-14 07:49:42 -07:00
|
|
|
Counter_example { info with explosion_fuel = 0 }
|
2019-10-08 10:16:44 -07:00
|
|
|
|
|
|
|
let get_splitting_mode = function
|
|
|
|
| Normal -> None
|
|
|
|
| Counter_example {splitting_mode} -> Some splitting_mode
|
|
|
|
|
2019-10-14 07:49:42 -07:00
|
|
|
let enter_nonsplit_or mode = match mode with
|
2019-10-08 10:16:44 -07:00
|
|
|
| Normal -> Normal
|
|
|
|
| Counter_example info ->
|
2019-10-14 07:49:42 -07:00
|
|
|
let splitting_mode = match info.splitting_mode with
|
|
|
|
| Backtrack_or ->
|
|
|
|
(* in Backtrack_or mode, or-patterns are always split *)
|
|
|
|
assert false
|
|
|
|
| Refine_or _ ->
|
|
|
|
Refine_or {inside_nonsplit_or = true}
|
|
|
|
in Counter_example { info with splitting_mode }
|
2019-10-08 10:16:44 -07:00
|
|
|
|
2019-09-20 06:23:07 -07:00
|
|
|
(** The typedtree has two distinct syntactic categories for patterns,
|
|
|
|
"value" patterns, matching on values, and "computation" patterns
|
|
|
|
that match on the effect of a computation -- typically, exception
|
|
|
|
patterns (exception p).
|
|
|
|
|
|
|
|
On the other hand, the parsetree has an unstructured representation
|
|
|
|
where all categories of patterns are mixed together. The
|
|
|
|
decomposition according to the value/computation structure has to
|
|
|
|
happen during type-checking.
|
|
|
|
|
|
|
|
We don't want to duplicate the type-checking logic in two different
|
|
|
|
functions, depending on the kind of pattern to be produced. In
|
|
|
|
particular, there are both value and computation or-patterns, and
|
|
|
|
the type-checking logic for or-patterns is horribly complex; having
|
|
|
|
it in two different places would be twice as horirble.
|
|
|
|
|
|
|
|
The solution is to pass a GADT tag to [type_pat] to indicate whether
|
|
|
|
a value or computation pattern is expected. This way, there is a single
|
|
|
|
place where [Ppat_or] nodes are type-checked, the checking logic is shared,
|
|
|
|
and only at the end do we inspect the tag to decide to produce a value
|
|
|
|
or computation pattern.
|
|
|
|
*)
|
|
|
|
let pure
|
|
|
|
: type k . k pattern_category -> value general_pattern -> k general_pattern
|
|
|
|
= fun category pat ->
|
|
|
|
match category with
|
|
|
|
| Value -> pat
|
2019-10-29 22:48:50 -07:00
|
|
|
| Computation -> as_computation_pattern pat
|
2019-09-20 06:23:07 -07:00
|
|
|
|
|
|
|
let only_impure
|
|
|
|
: type k . k pattern_category ->
|
|
|
|
computation general_pattern -> k general_pattern
|
|
|
|
= fun category pat ->
|
|
|
|
match category with
|
|
|
|
| Value ->
|
|
|
|
(* LATER: this exception could be renamed/generalized *)
|
|
|
|
raise (Error (pat.pat_loc, pat.pat_env,
|
|
|
|
Exception_pattern_disallowed))
|
|
|
|
| Computation -> pat
|
|
|
|
|
|
|
|
let as_comp_pattern
|
|
|
|
: type k . k pattern_category ->
|
|
|
|
k general_pattern -> computation general_pattern
|
|
|
|
= fun category pat ->
|
|
|
|
match category with
|
2019-10-29 22:48:50 -07:00
|
|
|
| Value -> as_computation_pattern pat
|
2019-09-20 06:23:07 -07:00
|
|
|
| Computation -> pat
|
|
|
|
|
2020-02-19 01:36:55 -08:00
|
|
|
(* type_pat propagates the expected type.
|
|
|
|
Unification may update the typing environment.
|
|
|
|
|
|
|
|
In counter-example mode, [Empty_branch] is raised when the counter-example
|
|
|
|
does not match any value. *)
|
2019-09-20 06:23:07 -07:00
|
|
|
let rec type_pat
|
|
|
|
: type k r . k pattern_category -> no_existentials:_ -> mode:_ ->
|
|
|
|
env:_ -> _ -> _ -> (k general_pattern -> r) -> r
|
|
|
|
= fun category ~no_existentials ~mode
|
|
|
|
~env sp expected_ty k ->
|
2017-07-17 14:34:43 -07:00
|
|
|
Builtin_attributes.warning_scope sp.ppat_attributes
|
|
|
|
(fun () ->
|
2019-09-20 06:23:07 -07:00
|
|
|
type_pat_aux category ~no_existentials ~mode
|
2019-10-08 10:16:44 -07:00
|
|
|
~env sp expected_ty k
|
2017-07-17 14:34:43 -07:00
|
|
|
)
|
|
|
|
|
2019-09-20 06:23:07 -07:00
|
|
|
and type_pat_aux
|
|
|
|
: type k r . k pattern_category -> no_existentials:_ -> mode:_ ->
|
|
|
|
env:_ -> _ -> _ -> (k general_pattern -> r) -> r
|
|
|
|
= fun category ~no_existentials ~mode
|
|
|
|
~env sp expected_ty k ->
|
|
|
|
let type_pat category ?(mode=mode) ?(env=env) =
|
|
|
|
type_pat category ~no_existentials ~mode ~env
|
2018-01-15 03:11:06 -08:00
|
|
|
in
|
2009-09-04 09:19:35 -07:00
|
|
|
let loc = sp.ppat_loc in
|
2020-06-15 01:26:26 -07:00
|
|
|
let refine =
|
|
|
|
match mode with Normal -> None | Counter_example _ -> Some true in
|
2019-09-20 06:23:07 -07:00
|
|
|
let unif (x : pattern) : pattern =
|
2019-12-02 11:49:51 -08:00
|
|
|
unify_pat ~refine env x (instance expected_ty);
|
2019-09-20 06:23:07 -07:00
|
|
|
x
|
2018-03-19 04:24:06 -07:00
|
|
|
in
|
2019-09-20 06:23:07 -07:00
|
|
|
let rp x =
|
|
|
|
let crp (x : k general_pattern) : k general_pattern =
|
|
|
|
match category with
|
|
|
|
| Value -> rp x
|
|
|
|
| Computation -> rcp x in
|
|
|
|
if mode = Normal then crp x else x in
|
|
|
|
let rp k x = k (rp x)
|
|
|
|
and rvp k x = k (rp (pure category x))
|
|
|
|
and rcp k x = k (rp (only_impure category x)) in
|
2019-10-08 10:16:44 -07:00
|
|
|
let construction_not_used_in_counterexamples = (mode = Normal) in
|
|
|
|
let must_backtrack_on_gadt = match get_splitting_mode mode with
|
|
|
|
| None -> false
|
2019-10-14 07:49:42 -07:00
|
|
|
| Some Backtrack_or -> false
|
|
|
|
| Some (Refine_or {inside_nonsplit_or}) -> inside_nonsplit_or
|
2019-10-08 10:16:44 -07:00
|
|
|
in
|
1995-05-04 03:15:53 -07:00
|
|
|
match sp.ppat_desc with
|
|
|
|
Ppat_any ->
|
2019-09-20 06:23:07 -07:00
|
|
|
let k' d = rvp k {
|
2015-05-19 15:52:21 -07:00
|
|
|
pat_desc = d;
|
2012-05-30 07:52:37 -07:00
|
|
|
pat_loc = loc; pat_extra=[];
|
2018-03-19 04:24:06 -07:00
|
|
|
pat_type = instance expected_ty;
|
2013-03-25 07:16:07 -07:00
|
|
|
pat_attributes = sp.ppat_attributes;
|
2010-09-18 21:55:40 -07:00
|
|
|
pat_env = !env }
|
2015-05-19 15:52:21 -07:00
|
|
|
in
|
2019-10-08 10:16:44 -07:00
|
|
|
begin match mode with
|
|
|
|
| Normal -> k' Tpat_any
|
2019-10-14 07:49:42 -07:00
|
|
|
| Counter_example {explosion_fuel; _} when explosion_fuel <= 0 ->
|
|
|
|
k' Tpat_any
|
|
|
|
| Counter_example ({explosion_fuel; _} as info) ->
|
2019-10-30 06:32:48 -07:00
|
|
|
let open Parmatch in
|
|
|
|
begin match ppat_of_type !env expected_ty with
|
2020-02-17 09:33:12 -08:00
|
|
|
| PT_empty -> raise Empty_branch
|
2019-10-30 06:32:48 -07:00
|
|
|
| PT_any -> k' Tpat_any
|
|
|
|
| PT_pattern (explosion, sp, constrs, labels) ->
|
2019-10-14 07:49:42 -07:00
|
|
|
let explosion_fuel =
|
2019-10-30 06:32:48 -07:00
|
|
|
match explosion with
|
|
|
|
| PE_single -> explosion_fuel - 1
|
|
|
|
| PE_gadt_cases ->
|
|
|
|
if must_backtrack_on_gadt then raise Need_backtrack;
|
|
|
|
explosion_fuel - 5
|
2019-10-08 10:16:44 -07:00
|
|
|
in
|
|
|
|
let mode =
|
2019-10-14 07:49:42 -07:00
|
|
|
Counter_example { info with explosion_fuel; constrs; labels }
|
|
|
|
in
|
2019-09-20 06:23:07 -07:00
|
|
|
type_pat category ~mode sp expected_ty k
|
2019-10-08 10:16:44 -07:00
|
|
|
end
|
|
|
|
end
|
2011-10-25 05:11:06 -07:00
|
|
|
| Ppat_var name ->
|
2018-03-19 04:24:06 -07:00
|
|
|
let ty = instance expected_ty in
|
2016-08-21 18:01:17 -07:00
|
|
|
let id = (* PR#7330 *)
|
2018-08-28 09:06:45 -07:00
|
|
|
if name.txt = "*extension*" then
|
2018-08-28 09:07:01 -07:00
|
|
|
Ident.create_local name.txt
|
2018-08-28 09:06:45 -07:00
|
|
|
else
|
|
|
|
enter_variable loc name ty sp.ppat_attributes
|
2016-08-21 18:01:17 -07:00
|
|
|
in
|
2019-09-20 06:23:07 -07:00
|
|
|
rvp k {
|
2012-05-30 07:52:37 -07:00
|
|
|
pat_desc = Tpat_var (id, name);
|
|
|
|
pat_loc = loc; pat_extra=[];
|
2018-03-19 04:24:06 -07:00
|
|
|
pat_type = ty;
|
2013-03-25 07:16:07 -07:00
|
|
|
pat_attributes = sp.ppat_attributes;
|
2010-09-18 21:55:40 -07:00
|
|
|
pat_env = !env }
|
2010-10-21 16:59:33 -07:00
|
|
|
| Ppat_unpack name ->
|
2019-10-08 10:16:44 -07:00
|
|
|
assert construction_not_used_in_counterexamples;
|
2018-03-19 04:24:06 -07:00
|
|
|
let t = instance expected_ty in
|
2019-10-09 06:15:37 -07:00
|
|
|
begin match name.txt with
|
|
|
|
| None ->
|
2019-09-20 06:23:07 -07:00
|
|
|
rvp k {
|
2019-10-09 06:15:37 -07:00
|
|
|
pat_desc = Tpat_any;
|
|
|
|
pat_loc = sp.ppat_loc;
|
|
|
|
pat_extra=[Tpat_unpack, name.loc, sp.ppat_attributes];
|
|
|
|
pat_type = t;
|
|
|
|
pat_attributes = [];
|
|
|
|
pat_env = !env }
|
|
|
|
| Some s ->
|
|
|
|
let v = { name with txt = s } in
|
|
|
|
let id = enter_variable loc v t ~is_module:true sp.ppat_attributes in
|
2019-09-20 06:23:07 -07:00
|
|
|
rvp k {
|
2019-10-09 06:15:37 -07:00
|
|
|
pat_desc = Tpat_var (id, v);
|
|
|
|
pat_loc = sp.ppat_loc;
|
|
|
|
pat_extra=[Tpat_unpack, loc, sp.ppat_attributes];
|
|
|
|
pat_type = t;
|
|
|
|
pat_attributes = [];
|
|
|
|
pat_env = !env }
|
|
|
|
end
|
2017-08-12 13:24:41 -07:00
|
|
|
| Ppat_constraint(
|
|
|
|
{ppat_desc=Ppat_var name; ppat_loc=lloc; ppat_attributes = attrs},
|
|
|
|
({ptyp_desc=Ptyp_poly _} as sty)) ->
|
2009-10-26 00:11:36 -07:00
|
|
|
(* explicitly polymorphic type *)
|
2019-10-08 10:16:44 -07:00
|
|
|
assert construction_not_used_in_counterexamples;
|
2020-07-09 03:20:37 -07:00
|
|
|
let cty, ty, force = Typetexp.transl_simple_type_delayed !env sty in
|
2019-12-02 11:49:51 -08:00
|
|
|
unify_pat_types ~refine lloc env ty (instance expected_ty);
|
2009-10-26 00:11:36 -07:00
|
|
|
pattern_force := force :: !pattern_force;
|
|
|
|
begin match ty.desc with
|
|
|
|
| Tpoly (body, tyl) ->
|
|
|
|
begin_def ();
|
2020-07-28 17:10:17 -07:00
|
|
|
init_def generic_level;
|
2011-12-27 00:52:45 -08:00
|
|
|
let _, ty' = instance_poly ~keep_names:true false tyl body in
|
2009-10-26 00:11:36 -07:00
|
|
|
end_def ();
|
2018-06-07 04:57:47 -07:00
|
|
|
let id = enter_variable lloc name ty' attrs in
|
2019-09-20 06:23:07 -07:00
|
|
|
rvp k {
|
2012-05-30 07:52:37 -07:00
|
|
|
pat_desc = Tpat_var (id, name);
|
|
|
|
pat_loc = lloc;
|
2013-03-25 07:16:07 -07:00
|
|
|
pat_extra = [Tpat_constraint cty, loc, sp.ppat_attributes];
|
2012-05-30 07:52:37 -07:00
|
|
|
pat_type = ty;
|
2013-03-25 07:16:07 -07:00
|
|
|
pat_attributes = [];
|
2012-05-30 07:52:37 -07:00
|
|
|
pat_env = !env
|
|
|
|
}
|
2009-10-26 00:11:36 -07:00
|
|
|
| _ -> assert false
|
2009-11-19 04:27:15 -08:00
|
|
|
end
|
2011-10-25 06:13:54 -07:00
|
|
|
| Ppat_alias(sq, name) ->
|
2019-10-08 10:16:44 -07:00
|
|
|
assert construction_not_used_in_counterexamples;
|
2019-09-20 06:23:07 -07:00
|
|
|
type_pat Value sq expected_ty (fun q ->
|
2015-05-19 15:52:21 -07:00
|
|
|
begin_def ();
|
2019-12-02 11:49:51 -08:00
|
|
|
let ty_var = build_as_type env q in
|
2015-05-19 15:52:21 -07:00
|
|
|
end_def ();
|
|
|
|
generalize ty_var;
|
2017-08-12 13:24:41 -07:00
|
|
|
let id =
|
|
|
|
enter_variable ~is_as_variable:true loc name ty_var sp.ppat_attributes
|
|
|
|
in
|
2019-09-20 06:23:07 -07:00
|
|
|
rvp k {
|
2015-05-19 15:52:21 -07:00
|
|
|
pat_desc = Tpat_alias(q, id, name);
|
|
|
|
pat_loc = loc; pat_extra=[];
|
|
|
|
pat_type = q.pat_type;
|
|
|
|
pat_attributes = sp.ppat_attributes;
|
|
|
|
pat_env = !env })
|
2011-10-25 05:11:06 -07:00
|
|
|
| Ppat_constant cst ->
|
2015-04-18 20:07:32 -07:00
|
|
|
let cst = constant_or_raise !env loc cst in
|
2019-09-20 06:23:07 -07:00
|
|
|
rvp k @@ unif {
|
2013-05-23 08:12:04 -07:00
|
|
|
pat_desc = Tpat_constant cst;
|
2012-05-30 07:52:37 -07:00
|
|
|
pat_loc = loc; pat_extra=[];
|
2018-03-19 04:24:06 -07:00
|
|
|
pat_type = type_constant cst;
|
2013-03-25 07:16:07 -07:00
|
|
|
pat_attributes = sp.ppat_attributes;
|
2010-09-18 21:55:40 -07:00
|
|
|
pat_env = !env }
|
2016-01-08 06:48:47 -08:00
|
|
|
| Ppat_interval (Pconst_char c1, Pconst_char c2) ->
|
2013-04-16 08:34:09 -07:00
|
|
|
let open Ast_helper.Pat in
|
2013-10-01 04:40:21 -07:00
|
|
|
let gloc = {loc with Location.loc_ghost=true} in
|
2013-04-16 08:34:09 -07:00
|
|
|
let rec loop c1 c2 =
|
2016-01-08 06:48:47 -08:00
|
|
|
if c1 = c2 then constant ~loc:gloc (Pconst_char c1)
|
2013-04-16 08:34:09 -07:00
|
|
|
else
|
2013-10-01 04:40:21 -07:00
|
|
|
or_ ~loc:gloc
|
2016-01-08 06:48:47 -08:00
|
|
|
(constant ~loc:gloc (Pconst_char c1))
|
2013-04-16 08:34:09 -07:00
|
|
|
(loop (Char.chr(Char.code c1 + 1)) c2)
|
|
|
|
in
|
|
|
|
let p = if c1 <= c2 then loop c1 c2 else loop c2 c1 in
|
2013-10-01 04:40:21 -07:00
|
|
|
let p = {p with ppat_loc=loc} in
|
2019-09-20 06:23:07 -07:00
|
|
|
type_pat category ~mode:(no_explosion mode) p expected_ty k
|
2014-01-22 17:58:37 -08:00
|
|
|
(* TODO: record 'extra' to remember about interval *)
|
2013-04-16 08:34:09 -07:00
|
|
|
| Ppat_interval _ ->
|
|
|
|
raise (Error (loc, !env, Invalid_interval))
|
2011-10-25 06:13:54 -07:00
|
|
|
| Ppat_tuple spl ->
|
2016-01-04 05:07:38 -08:00
|
|
|
assert (List.length spl >= 2);
|
2018-03-19 04:24:06 -07:00
|
|
|
let spl_ann = List.map (fun p -> (p,newgenvar ())) spl in
|
|
|
|
let ty = newgenty (Ttuple(List.map snd spl_ann)) in
|
2020-07-28 17:10:17 -07:00
|
|
|
let expected_ty = generic_instance expected_ty in
|
2019-12-02 11:49:51 -08:00
|
|
|
unify_pat_types ~refine loc env ty expected_ty;
|
2019-09-20 06:23:07 -07:00
|
|
|
map_fold_cont (fun (p,t) -> type_pat Value p t) spl_ann (fun pl ->
|
|
|
|
rvp k {
|
2003-04-01 17:32:09 -08:00
|
|
|
pat_desc = Tpat_tuple pl;
|
2012-05-30 07:52:37 -07:00
|
|
|
pat_loc = loc; pat_extra=[];
|
2018-03-19 04:24:06 -07:00
|
|
|
pat_type = newty (Ttuple(List.map (fun p -> p.pat_type) pl));
|
2013-03-25 07:16:07 -07:00
|
|
|
pat_attributes = sp.ppat_attributes;
|
2015-05-19 15:52:21 -07:00
|
|
|
pat_env = !env })
|
2013-04-17 02:46:52 -07:00
|
|
|
| Ppat_construct(lid, sarg) ->
|
2019-12-12 07:06:01 -08:00
|
|
|
let expected_type =
|
2013-04-16 08:34:09 -07:00
|
|
|
try
|
2012-11-10 19:46:59 -08:00
|
|
|
let (p0, p, _) = extract_concrete_variant !env expected_ty in
|
2020-07-08 01:35:40 -07:00
|
|
|
let principal =
|
|
|
|
(repr expected_ty).level = generic_level || not !Clflags.principal
|
|
|
|
in
|
|
|
|
Some (p0, p, principal)
|
2013-09-04 08:12:37 -07:00
|
|
|
with Not_found -> None
|
2012-10-29 00:54:06 -07:00
|
|
|
in
|
2019-10-04 09:54:07 -07:00
|
|
|
let constr =
|
2019-10-14 07:49:42 -07:00
|
|
|
match lid.txt, mode with
|
|
|
|
| Longident.Lident s, Counter_example {constrs; _} ->
|
2019-10-08 10:16:44 -07:00
|
|
|
(* assert: cf. {!counter_example_checking_info} documentation *)
|
|
|
|
assert (Hashtbl.mem constrs s);
|
|
|
|
Hashtbl.find constrs s
|
2018-10-12 02:20:21 -07:00
|
|
|
| _ ->
|
2019-10-04 09:54:07 -07:00
|
|
|
let candidates =
|
|
|
|
Env.lookup_all_constructors Env.Pattern ~loc:lid.loc lid.txt !env in
|
2017-12-02 04:44:54 -08:00
|
|
|
wrap_disambiguate "This variant pattern is expected to have"
|
|
|
|
(mk_expected expected_ty)
|
2019-12-12 07:06:01 -08:00
|
|
|
(Constructor.disambiguate Env.Pattern lid !env expected_type)
|
|
|
|
candidates
|
2013-09-19 00:25:51 -07:00
|
|
|
in
|
2019-10-08 10:16:44 -07:00
|
|
|
if constr.cstr_generalized && must_backtrack_on_gadt then
|
|
|
|
raise Need_backtrack;
|
2018-04-21 07:45:43 -07:00
|
|
|
begin match no_existentials, constr.cstr_existentials with
|
|
|
|
| None, _ | _, [] -> ()
|
|
|
|
| Some r, (_ :: _ as exs) ->
|
|
|
|
let exs = List.map (Ctype.existential_name constr) exs in
|
|
|
|
let name = constr.cstr_name in
|
|
|
|
raise (Error (loc, !env, Unexpected_existential (r,name, exs)))
|
|
|
|
end;
|
2010-12-03 06:06:31 -08:00
|
|
|
(* if constructor is gadt, we must verify that the expected type has the
|
|
|
|
correct head *)
|
2010-12-13 22:33:06 -08:00
|
|
|
if constr.cstr_generalized then
|
2019-12-02 11:49:51 -08:00
|
|
|
unify_head_only ~refine loc env (instance expected_ty) constr;
|
1995-05-04 03:15:53 -07:00
|
|
|
let sargs =
|
|
|
|
match sarg with
|
|
|
|
None -> []
|
2014-04-15 02:53:51 -07:00
|
|
|
| Some {ppat_desc = Ppat_tuple spl} when
|
2015-12-02 06:52:04 -08:00
|
|
|
constr.cstr_arity > 1 ||
|
|
|
|
Builtin_attributes.explicit_arity sp.ppat_attributes
|
2014-04-15 02:53:51 -07:00
|
|
|
-> spl
|
2008-01-22 09:08:48 -08:00
|
|
|
| Some({ppat_desc = Ppat_any} as sp) when constr.cstr_arity <> 1 ->
|
2009-11-19 04:27:15 -08:00
|
|
|
if constr.cstr_arity = 0 then
|
|
|
|
Location.prerr_warning sp.ppat_loc
|
|
|
|
Warnings.Wildcard_arg_to_constant_constr;
|
1996-01-04 04:50:52 -08:00
|
|
|
replicate_list sp constr.cstr_arity
|
1995-05-04 03:15:53 -07:00
|
|
|
| Some sp -> [sp] in
|
2018-11-03 05:08:55 -07:00
|
|
|
if Builtin_attributes.warn_on_literal_pattern constr.cstr_attributes then
|
|
|
|
begin match List.filter has_literal_pattern sargs with
|
|
|
|
| sp :: _ ->
|
|
|
|
Location.prerr_warning sp.ppat_loc Warnings.Fragile_literal_pattern
|
|
|
|
| _ -> ()
|
|
|
|
end;
|
1995-05-04 03:15:53 -07:00
|
|
|
if List.length sargs <> constr.cstr_arity then
|
2012-08-21 00:10:35 -07:00
|
|
|
raise(Error(loc, !env, Constructor_arity_mismatch(lid.txt,
|
1995-05-04 03:15:53 -07:00
|
|
|
constr.cstr_arity, List.length sargs)));
|
2018-03-19 04:24:06 -07:00
|
|
|
begin_def ();
|
2011-10-25 05:11:06 -07:00
|
|
|
let (ty_args, ty_res) =
|
2018-02-12 08:37:35 -08:00
|
|
|
instance_constructor ~in_pattern:(env, get_gadt_equations_level ())
|
|
|
|
constr
|
2010-11-09 01:29:08 -08:00
|
|
|
in
|
2018-03-19 04:24:06 -07:00
|
|
|
let expected_ty = instance expected_ty in
|
2016-04-07 23:52:03 -07:00
|
|
|
(* PR#7214: do not use gadt unification for toplevel lets *)
|
2020-06-15 01:26:26 -07:00
|
|
|
let refine =
|
|
|
|
if refine = None && constr.cstr_generalized && no_existentials = None
|
2020-07-08 01:35:40 -07:00
|
|
|
then Some false
|
|
|
|
else refine
|
|
|
|
in
|
|
|
|
let equated_types =
|
|
|
|
unify_pat_types_return_equated_pairs ~refine loc env ty_res expected_ty
|
|
|
|
in
|
2018-03-19 04:24:06 -07:00
|
|
|
end_def ();
|
|
|
|
generalize_structure expected_ty;
|
|
|
|
generalize_structure ty_res;
|
|
|
|
List.iter generalize_structure ty_args;
|
2020-07-08 01:35:40 -07:00
|
|
|
if !Clflags.principal then (
|
|
|
|
let exception Warn_only_once in
|
|
|
|
try
|
|
|
|
TypePairs.iter (fun (t1, t2) () ->
|
|
|
|
generalize_structure t1;
|
|
|
|
generalize_structure t2;
|
|
|
|
if not (fully_generic t1 && fully_generic t2) then
|
|
|
|
let msg =
|
|
|
|
Format.asprintf
|
|
|
|
"typing this pattern requires considering@ %a@ and@ %a@ as \
|
|
|
|
equal.@,\
|
|
|
|
But the knowledge of these types"
|
|
|
|
Printtyp.type_expr t1
|
|
|
|
Printtyp.type_expr t2
|
|
|
|
in
|
|
|
|
Location.prerr_warning loc (Warnings.Not_principal msg);
|
|
|
|
raise Warn_only_once
|
|
|
|
) equated_types
|
|
|
|
with Warn_only_once -> ()
|
|
|
|
);
|
2014-10-14 08:51:30 -07:00
|
|
|
|
|
|
|
let rec check_non_escaping p =
|
|
|
|
match p.ppat_desc with
|
|
|
|
| Ppat_or (p1, p2) ->
|
|
|
|
check_non_escaping p1;
|
|
|
|
check_non_escaping p2
|
|
|
|
| Ppat_alias (p, _) ->
|
|
|
|
check_non_escaping p
|
|
|
|
| Ppat_constraint _ ->
|
|
|
|
raise (Error (p.ppat_loc, !env, Inlined_record_escape))
|
|
|
|
| _ ->
|
|
|
|
()
|
|
|
|
in
|
|
|
|
if constr.cstr_inlined <> None then List.iter check_non_escaping sargs;
|
|
|
|
|
2019-09-20 06:23:07 -07:00
|
|
|
map_fold_cont
|
|
|
|
(fun (p,t) -> type_pat Value p t)
|
|
|
|
(List.combine sargs ty_args)
|
|
|
|
(fun args ->
|
|
|
|
rvp k {
|
|
|
|
pat_desc=Tpat_construct(lid, constr, args);
|
|
|
|
pat_loc = loc; pat_extra=[];
|
|
|
|
pat_type = instance expected_ty;
|
|
|
|
pat_attributes = sp.ppat_attributes;
|
|
|
|
pat_env = !env })
|
2011-10-25 06:13:54 -07:00
|
|
|
| Ppat_variant(l, sarg) ->
|
2018-03-19 04:24:06 -07:00
|
|
|
let arg_type = match sarg with None -> [] | Some _ -> [newgenvar()] in
|
2001-03-02 16:14:35 -08:00
|
|
|
let row = { row_fields =
|
2013-11-12 16:16:03 -08:00
|
|
|
[l, Reither(sarg = None, arg_type, true, ref None)];
|
2008-01-11 08:13:18 -08:00
|
|
|
row_bound = ();
|
1999-11-30 08:07:38 -08:00
|
|
|
row_closed = false;
|
2018-03-19 04:24:06 -07:00
|
|
|
row_more = newgenvar ();
|
2019-07-12 08:47:25 -07:00
|
|
|
row_fixed = None;
|
1999-11-30 08:07:38 -08:00
|
|
|
row_name = None } in
|
2020-07-28 17:10:17 -07:00
|
|
|
let expected_ty = generic_instance expected_ty in
|
2018-01-17 20:01:31 -08:00
|
|
|
(* PR#7404: allow some_private_tag blindly, as it would not unify with
|
2018-01-08 01:30:19 -08:00
|
|
|
the abstract row variable *)
|
2019-10-08 10:16:44 -07:00
|
|
|
if l = Parmatch.some_private_tag
|
|
|
|
then assert (match mode with Normal -> false | Counter_example _ -> true)
|
2019-12-02 11:49:51 -08:00
|
|
|
else unify_pat_types ~refine loc env (newgenty(Tvariant row)) expected_ty;
|
2015-05-19 15:52:21 -07:00
|
|
|
let k arg =
|
2019-09-20 06:23:07 -07:00
|
|
|
rvp k {
|
2008-01-11 08:13:18 -08:00
|
|
|
pat_desc = Tpat_variant(l, arg, ref {row with row_more = newvar()});
|
2012-05-30 07:52:37 -07:00
|
|
|
pat_loc = loc; pat_extra=[];
|
2018-03-19 04:24:06 -07:00
|
|
|
pat_type = instance expected_ty;
|
2013-03-25 07:16:07 -07:00
|
|
|
pat_attributes = sp.ppat_attributes;
|
2010-09-18 21:55:40 -07:00
|
|
|
pat_env = !env }
|
2015-05-19 15:52:21 -07:00
|
|
|
in begin
|
|
|
|
(* PR#6235: propagate type information *)
|
|
|
|
match sarg, arg_type with
|
2019-09-20 06:23:07 -07:00
|
|
|
Some p, [ty] -> type_pat Value p ty (fun p -> k (Some p))
|
2015-05-19 15:52:21 -07:00
|
|
|
| _ -> k None
|
|
|
|
end
|
2009-09-12 05:41:07 -07:00
|
|
|
| Ppat_record(lid_sp_list, closed) ->
|
2016-01-04 05:07:38 -08:00
|
|
|
assert (lid_sp_list <> []);
|
2019-12-12 07:06:01 -08:00
|
|
|
let expected_type, record_ty =
|
2012-11-08 01:39:23 -08:00
|
|
|
try
|
2012-11-10 19:46:59 -08:00
|
|
|
let (p0, p,_) = extract_concrete_record !env expected_ty in
|
2020-07-28 17:10:17 -07:00
|
|
|
let ty = generic_instance expected_ty in
|
2020-07-08 01:35:40 -07:00
|
|
|
let principal =
|
|
|
|
(repr expected_ty).level = generic_level || not !Clflags.principal
|
|
|
|
in
|
|
|
|
Some (p0, p, principal), ty
|
2013-09-04 08:12:37 -07:00
|
|
|
with Not_found -> None, newvar ()
|
2012-11-08 01:39:23 -08:00
|
|
|
in
|
2015-05-19 15:52:21 -07:00
|
|
|
let type_label_pat (label_lid, label, sarg) k =
|
2018-03-19 04:24:06 -07:00
|
|
|
begin_def ();
|
2018-04-30 05:49:52 -07:00
|
|
|
let (_, ty_arg, ty_res) = instance_label false label in
|
1995-05-04 03:15:53 -07:00
|
|
|
begin try
|
2019-12-02 11:49:51 -08:00
|
|
|
unify_pat_types ~refine loc env ty_res (instance record_ty)
|
2019-03-07 11:05:48 -08:00
|
|
|
with Error(_loc, _env, Pattern_type_clash(trace, _)) ->
|
2013-01-29 06:21:12 -08:00
|
|
|
raise(Error(label_lid.loc, !env,
|
2013-09-04 08:12:37 -07:00
|
|
|
Label_mismatch(label_lid.txt, trace)))
|
1995-05-04 03:15:53 -07:00
|
|
|
end;
|
2018-03-19 04:24:06 -07:00
|
|
|
end_def ();
|
|
|
|
generalize_structure ty_res;
|
|
|
|
generalize_structure ty_arg;
|
2019-09-20 06:23:07 -07:00
|
|
|
type_pat Value sarg ty_arg (fun arg ->
|
2015-05-19 15:52:21 -07:00
|
|
|
k (label_lid, label, arg))
|
2013-09-19 00:25:51 -07:00
|
|
|
in
|
2019-09-20 06:23:07 -07:00
|
|
|
let make_record_pat lbl_pat_list =
|
2015-05-19 15:52:21 -07:00
|
|
|
check_recordpat_labels loc lbl_pat_list closed;
|
2019-09-20 06:23:07 -07:00
|
|
|
{
|
|
|
|
pat_desc = Tpat_record (lbl_pat_list, closed);
|
|
|
|
pat_loc = loc; pat_extra=[];
|
|
|
|
pat_type = instance record_ty;
|
|
|
|
pat_attributes = sp.ppat_attributes;
|
|
|
|
pat_env = !env;
|
|
|
|
}
|
2015-05-19 15:52:21 -07:00
|
|
|
in
|
2019-09-20 06:23:07 -07:00
|
|
|
let k' pat = rvp k (unif pat) in
|
2019-10-08 10:16:44 -07:00
|
|
|
begin match mode with
|
|
|
|
| Normal ->
|
2019-09-20 06:23:07 -07:00
|
|
|
k' (wrap_disambiguate "This record pattern is expected to have"
|
2019-10-14 07:49:42 -07:00
|
|
|
(mk_expected expected_ty)
|
2019-12-12 07:06:01 -08:00
|
|
|
(type_label_a_list loc false !env type_label_pat expected_type
|
2019-10-14 07:49:42 -07:00
|
|
|
lid_sp_list)
|
2019-09-20 06:23:07 -07:00
|
|
|
make_record_pat)
|
2019-10-14 07:49:42 -07:00
|
|
|
| Counter_example {labels; _} ->
|
2019-12-12 07:06:01 -08:00
|
|
|
type_label_a_list ~labels loc false !env type_label_pat expected_type
|
2019-09-20 06:23:07 -07:00
|
|
|
lid_sp_list (fun lbl_pat_list -> k' (make_record_pat lbl_pat_list))
|
2019-10-08 10:16:44 -07:00
|
|
|
end
|
2011-10-25 05:11:06 -07:00
|
|
|
| Ppat_array spl ->
|
2018-03-19 04:24:06 -07:00
|
|
|
let ty_elt = newgenvar() in
|
2020-07-28 17:10:17 -07:00
|
|
|
let expected_ty = generic_instance expected_ty in
|
2019-12-02 11:49:51 -08:00
|
|
|
unify_pat_types ~refine
|
|
|
|
loc env (Predef.type_array ty_elt) expected_ty;
|
2019-09-20 06:23:07 -07:00
|
|
|
map_fold_cont (fun p -> type_pat Value p ty_elt) spl (fun pl ->
|
|
|
|
rvp k {
|
2003-04-01 17:32:09 -08:00
|
|
|
pat_desc = Tpat_array pl;
|
2012-05-30 07:52:37 -07:00
|
|
|
pat_loc = loc; pat_extra=[];
|
2018-03-19 04:24:06 -07:00
|
|
|
pat_type = instance expected_ty;
|
2013-03-25 07:16:07 -07:00
|
|
|
pat_attributes = sp.ppat_attributes;
|
2015-05-19 15:52:21 -07:00
|
|
|
pat_env = !env })
|
2011-10-25 06:13:54 -07:00
|
|
|
| Ppat_or(sp1, sp2) ->
|
2019-10-14 07:49:42 -07:00
|
|
|
let may_split, must_split =
|
|
|
|
match get_splitting_mode mode with
|
2019-10-08 10:16:44 -07:00
|
|
|
| None -> false, false
|
2019-10-14 07:49:42 -07:00
|
|
|
| Some Backtrack_or -> true, true
|
|
|
|
| Some (Refine_or _) -> true, false in
|
2015-05-20 20:29:47 -07:00
|
|
|
let state = save_state env in
|
2019-10-03 05:43:31 -07:00
|
|
|
let split_or sp =
|
2019-10-08 10:16:44 -07:00
|
|
|
assert may_split;
|
2019-09-20 06:23:07 -07:00
|
|
|
let typ pat = type_pat category pat expected_ty k in
|
2019-10-03 05:43:31 -07:00
|
|
|
find_valid_alternative (fun pat -> set_state state env; typ pat) sp in
|
2019-10-08 10:16:44 -07:00
|
|
|
if must_split then split_or sp else begin
|
2015-05-20 20:29:47 -07:00
|
|
|
let initial_pattern_variables = !pattern_variables in
|
2015-12-10 07:22:39 -08:00
|
|
|
let initial_module_variables = !module_variables in
|
2018-09-26 09:29:39 -07:00
|
|
|
let equation_level = !gadt_equations_level in
|
|
|
|
let outter_lev = get_current_level () in
|
|
|
|
(* introduce a new scope *)
|
|
|
|
begin_def ();
|
|
|
|
let lev = get_current_level () in
|
|
|
|
gadt_equations_level := Some lev;
|
|
|
|
let env1 = ref !env in
|
2019-10-14 07:49:42 -07:00
|
|
|
let inside_or = enter_nonsplit_or mode in
|
2020-02-19 01:14:16 -08:00
|
|
|
let type_pat_result env sp : (_, abort_reason) result =
|
|
|
|
match
|
|
|
|
type_pat category ~mode:inside_or sp expected_ty ~env (fun x -> x)
|
2020-02-17 09:33:12 -08:00
|
|
|
with
|
2020-02-19 01:14:16 -08:00
|
|
|
| res -> Ok res
|
|
|
|
| exception Need_backtrack -> Error Adds_constraints
|
|
|
|
| exception Empty_branch -> Error Empty
|
|
|
|
in
|
|
|
|
let p1 = type_pat_result env1 sp1 in
|
2015-05-20 20:29:47 -07:00
|
|
|
let p1_variables = !pattern_variables in
|
2015-12-10 07:22:39 -08:00
|
|
|
let p1_module_variables = !module_variables in
|
2015-05-20 20:29:47 -07:00
|
|
|
pattern_variables := initial_pattern_variables;
|
2015-12-10 07:22:39 -08:00
|
|
|
module_variables := initial_module_variables;
|
2018-09-26 09:29:39 -07:00
|
|
|
let env2 = ref !env in
|
2020-02-19 01:14:16 -08:00
|
|
|
let p2 = type_pat_result env2 sp2 in
|
2018-09-26 09:29:39 -07:00
|
|
|
end_def ();
|
|
|
|
gadt_equations_level := equation_level;
|
2015-05-20 20:29:47 -07:00
|
|
|
let p2_variables = !pattern_variables in
|
2018-09-26 09:29:39 -07:00
|
|
|
(* Make sure no variable with an ambiguous type gets added to the
|
|
|
|
environment. *)
|
|
|
|
List.iter (fun { pv_type; pv_loc; _ } ->
|
|
|
|
check_scope_escape pv_loc !env1 outter_lev pv_type
|
|
|
|
) p1_variables;
|
|
|
|
List.iter (fun { pv_type; pv_loc; _ } ->
|
|
|
|
check_scope_escape pv_loc !env2 outter_lev pv_type
|
|
|
|
) p2_variables;
|
2019-10-03 05:43:31 -07:00
|
|
|
begin match p1, p2 with
|
2020-02-19 01:10:05 -08:00
|
|
|
| Error Empty, Error Empty ->
|
2020-02-17 09:33:12 -08:00
|
|
|
raise Empty_branch
|
2020-02-19 01:36:55 -08:00
|
|
|
| Error Adds_constraints, Error _
|
|
|
|
| Error _, Error Adds_constraints ->
|
2020-02-17 09:33:12 -08:00
|
|
|
let inside_nonsplit_or =
|
|
|
|
match get_splitting_mode mode with
|
|
|
|
| None | Some Backtrack_or -> false
|
|
|
|
| Some (Refine_or {inside_nonsplit_or}) -> inside_nonsplit_or in
|
|
|
|
if inside_nonsplit_or
|
|
|
|
then raise Need_backtrack
|
|
|
|
else split_or sp
|
2020-02-19 01:36:55 -08:00
|
|
|
| Ok p, Error _
|
|
|
|
| Error _, Ok p ->
|
|
|
|
rp k p
|
2020-02-17 09:33:12 -08:00
|
|
|
| Ok p1, Ok p2 ->
|
2020-02-19 01:10:05 -08:00
|
|
|
let alpha_env =
|
|
|
|
enter_orpat_variables loc !env p1_variables p2_variables in
|
|
|
|
let p2 = alpha_pat alpha_env p2 in
|
|
|
|
pattern_variables := p1_variables;
|
|
|
|
module_variables := p1_module_variables;
|
|
|
|
let make_pat desc =
|
|
|
|
{ pat_desc = desc;
|
|
|
|
pat_loc = loc; pat_extra=[];
|
|
|
|
pat_type = instance expected_ty;
|
|
|
|
pat_attributes = sp.ppat_attributes;
|
|
|
|
pat_env = !env } in
|
|
|
|
rp k (make_pat (Tpat_or(p1, p2, None)))
|
2019-10-03 05:43:31 -07:00
|
|
|
end
|
2015-05-20 20:29:47 -07:00
|
|
|
end
|
2011-10-25 06:13:54 -07:00
|
|
|
| Ppat_lazy sp1 ->
|
2018-03-19 04:24:06 -07:00
|
|
|
let nv = newgenvar () in
|
2020-07-28 17:10:17 -07:00
|
|
|
unify_pat_types ~refine loc env (Predef.type_lazy_t nv)
|
|
|
|
(generic_instance expected_ty);
|
2016-12-07 23:28:24 -08:00
|
|
|
(* do not explode under lazy: PR#7421 *)
|
2019-09-20 06:23:07 -07:00
|
|
|
type_pat Value ~mode:(no_explosion mode) sp1 nv (fun p1 ->
|
|
|
|
rvp k {
|
2008-07-09 06:03:38 -07:00
|
|
|
pat_desc = Tpat_lazy p1;
|
2012-05-30 07:52:37 -07:00
|
|
|
pat_loc = loc; pat_extra=[];
|
2018-03-19 04:24:06 -07:00
|
|
|
pat_type = instance expected_ty;
|
2013-03-25 07:16:07 -07:00
|
|
|
pat_attributes = sp.ppat_attributes;
|
2015-05-19 15:52:21 -07:00
|
|
|
pat_env = !env })
|
2011-10-25 06:13:54 -07:00
|
|
|
| Ppat_constraint(sp, sty) ->
|
2018-07-04 07:18:24 -07:00
|
|
|
(* Pretend separate = true *)
|
|
|
|
begin_def();
|
2020-07-09 03:20:37 -07:00
|
|
|
let cty, ty, force = Typetexp.transl_simple_type_delayed !env sty in
|
2018-07-04 07:18:24 -07:00
|
|
|
end_def();
|
|
|
|
generalize_structure ty;
|
2018-03-19 04:24:06 -07:00
|
|
|
let ty, expected_ty' = instance ty, ty in
|
2019-12-02 11:49:51 -08:00
|
|
|
unify_pat_types ~refine loc env ty (instance expected_ty);
|
2019-09-20 06:23:07 -07:00
|
|
|
type_pat category sp expected_ty' (fun p ->
|
2015-05-19 15:52:21 -07:00
|
|
|
(*Format.printf "%a@.%a@."
|
|
|
|
Printtyp.raw_type_expr ty
|
|
|
|
Printtyp.raw_type_expr p.pat_type;*)
|
|
|
|
pattern_force := force :: !pattern_force;
|
|
|
|
let extra = (Tpat_constraint cty, loc, sp.ppat_attributes) in
|
2019-09-20 06:23:07 -07:00
|
|
|
let p : k general_pattern =
|
|
|
|
match category, (p : k general_pattern) with
|
|
|
|
| Value, {pat_desc = Tpat_var (id,s); _} ->
|
|
|
|
{p with
|
|
|
|
pat_type = ty;
|
|
|
|
pat_desc =
|
|
|
|
Tpat_alias
|
|
|
|
({p with pat_desc = Tpat_any; pat_attributes = []}, id,s);
|
|
|
|
pat_extra = [extra];
|
|
|
|
}
|
|
|
|
| _, p ->
|
|
|
|
{ p with pat_type = ty; pat_extra = extra::p.pat_extra }
|
2015-05-19 15:52:21 -07:00
|
|
|
in k p)
|
2011-10-25 06:13:54 -07:00
|
|
|
| Ppat_type lid ->
|
2016-08-04 01:50:27 -07:00
|
|
|
let (path, p,ty) = build_or_pat !env loc lid in
|
2019-12-02 11:49:51 -08:00
|
|
|
unify_pat_types ~refine loc env ty (instance expected_ty);
|
2019-09-20 06:23:07 -07:00
|
|
|
k @@ pure category @@ { p with pat_extra =
|
|
|
|
(Tpat_type (path, lid), loc, sp.ppat_attributes)
|
|
|
|
:: p.pat_extra }
|
2016-05-05 10:50:56 -07:00
|
|
|
| Ppat_open (lid,p) ->
|
2016-05-09 02:57:57 -07:00
|
|
|
let path, new_env =
|
|
|
|
!type_open Asttypes.Fresh !env sp.ppat_loc lid in
|
|
|
|
let new_env = ref new_env in
|
2019-09-20 06:23:07 -07:00
|
|
|
type_pat category ~env:new_env p expected_ty ( fun p ->
|
2016-05-09 02:57:57 -07:00
|
|
|
env := Env.copy_local !env ~from:!new_env;
|
|
|
|
k { p with pat_extra =( Tpat_open (path,lid,!new_env),
|
2016-05-05 10:50:56 -07:00
|
|
|
loc, sp.ppat_attributes) :: p.pat_extra }
|
|
|
|
)
|
2018-01-15 03:11:06 -08:00
|
|
|
| Ppat_exception p ->
|
2019-09-20 06:23:07 -07:00
|
|
|
type_pat Value p Predef.type_exn (fun p_exn ->
|
|
|
|
rcp k {
|
|
|
|
pat_desc = Tpat_exception p_exn;
|
|
|
|
pat_loc = sp.ppat_loc;
|
|
|
|
pat_extra = [];
|
|
|
|
pat_type = expected_ty;
|
|
|
|
pat_env = !env;
|
|
|
|
pat_attributes = sp.ppat_attributes;
|
|
|
|
})
|
2014-05-07 01:26:17 -07:00
|
|
|
| Ppat_extension ext ->
|
2015-12-02 05:46:14 -08:00
|
|
|
raise (Error_forward (Builtin_attributes.error_of_extension ext))
|
2010-12-12 18:59:58 -08:00
|
|
|
|
2019-09-20 06:23:07 -07:00
|
|
|
let type_pat category ?no_existentials ?(mode=Normal)
|
2019-10-08 10:16:44 -07:00
|
|
|
?(lev=get_current_level()) env sp expected_ty =
|
2018-07-25 07:23:07 -07:00
|
|
|
Misc.protect_refs [Misc.R (gadt_equations_level, Some lev)] (fun () ->
|
2019-09-20 06:23:07 -07:00
|
|
|
type_pat category ~no_existentials ~mode
|
2019-10-08 10:16:44 -07:00
|
|
|
~env sp expected_ty (fun x -> x)
|
2015-12-23 13:40:21 -08:00
|
|
|
)
|
2010-11-15 22:01:59 -08:00
|
|
|
|
2010-11-09 00:21:44 -08:00
|
|
|
(* this function is passed to Partial.parmatch
|
2011-10-25 05:11:06 -07:00
|
|
|
to type check gadt nonexhaustiveness *)
|
2019-10-08 10:16:44 -07:00
|
|
|
let partial_pred ~lev ~splitting_mode ?(explode=0)
|
|
|
|
env expected_ty constrs labels p =
|
2015-05-20 20:29:47 -07:00
|
|
|
let env = ref env in
|
|
|
|
let state = save_state env in
|
2019-10-08 10:16:44 -07:00
|
|
|
let mode =
|
|
|
|
Counter_example {
|
|
|
|
splitting_mode;
|
2019-10-14 07:49:42 -07:00
|
|
|
explosion_fuel = explode;
|
|
|
|
constrs; labels;
|
2019-10-08 10:16:44 -07:00
|
|
|
} in
|
2010-12-13 18:00:20 -08:00
|
|
|
try
|
2020-10-13 00:48:16 -07:00
|
|
|
reset_pattern true;
|
2020-06-03 08:47:12 -07:00
|
|
|
let typed_p = type_pat Value ~lev ~mode env p expected_ty in
|
2015-05-20 20:29:47 -07:00
|
|
|
set_state state env;
|
2010-12-13 16:53:47 -08:00
|
|
|
(* types are invalidated but we don't need them here *)
|
|
|
|
Some typed_p
|
2020-02-17 09:33:12 -08:00
|
|
|
with Error _ | Empty_branch ->
|
2015-05-20 20:29:47 -07:00
|
|
|
set_state state env;
|
2010-12-13 16:53:47 -08:00
|
|
|
None
|
2010-11-09 00:21:44 -08:00
|
|
|
|
2015-10-14 18:55:52 -07:00
|
|
|
let check_partial ?(lev=get_current_level ()) env expected_ty loc cases =
|
|
|
|
let explode = match cases with [_] -> 5 | _ -> 0 in
|
2019-10-14 07:49:42 -07:00
|
|
|
let splitting_mode = Refine_or {inside_nonsplit_or = false} in
|
2017-07-07 05:11:15 -07:00
|
|
|
Parmatch.check_partial
|
2019-10-08 10:16:44 -07:00
|
|
|
(partial_pred ~lev ~splitting_mode ~explode env expected_ty) loc cases
|
2015-05-08 19:17:36 -07:00
|
|
|
|
2015-10-14 18:55:52 -07:00
|
|
|
let check_unused ?(lev=get_current_level ()) env expected_ty cases =
|
2015-10-13 02:53:27 -07:00
|
|
|
Parmatch.check_unused
|
2015-10-14 18:55:52 -07:00
|
|
|
(fun refute constrs labels spat ->
|
|
|
|
match
|
2019-10-14 07:49:42 -07:00
|
|
|
partial_pred ~lev ~splitting_mode:Backtrack_or ~explode:5
|
2015-10-14 18:55:52 -07:00
|
|
|
env expected_ty constrs labels spat
|
|
|
|
with
|
|
|
|
Some pat when refute ->
|
|
|
|
raise (Error (spat.ppat_loc, env, Unrefuted_pattern pat))
|
|
|
|
| r -> r)
|
2016-03-09 02:40:16 -08:00
|
|
|
cases
|
2015-05-08 19:17:36 -07:00
|
|
|
|
2018-05-04 05:33:00 -07:00
|
|
|
let iter_pattern_variables_type f : pattern_variable list -> unit =
|
2018-06-07 04:54:39 -07:00
|
|
|
List.iter (fun {pv_type; _} -> f pv_type)
|
2018-05-04 05:33:00 -07:00
|
|
|
|
2018-04-20 06:09:06 -07:00
|
|
|
let add_pattern_variables ?check ?check_as env pv =
|
|
|
|
List.fold_right
|
2018-06-07 07:11:09 -07:00
|
|
|
(fun {pv_id; pv_type; pv_loc; pv_as_var; pv_attributes} env ->
|
2018-06-07 04:54:39 -07:00
|
|
|
let check = if pv_as_var then check_as else check in
|
|
|
|
Env.add_value ?check pv_id
|
|
|
|
{val_type = pv_type; val_kind = Val_reg; Types.val_loc = pv_loc;
|
2018-06-07 04:57:47 -07:00
|
|
|
val_attributes = pv_attributes;
|
2019-08-20 01:53:05 -07:00
|
|
|
val_uid = Uid.mk ~current_unit:(Env.get_unit_name ());
|
2013-09-26 08:24:11 -07:00
|
|
|
} env
|
2018-04-20 06:09:06 -07:00
|
|
|
)
|
|
|
|
pv env
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2020-10-13 00:48:16 -07:00
|
|
|
let type_pattern category ~lev env spat expected_ty =
|
|
|
|
reset_pattern true;
|
2011-10-25 05:11:06 -07:00
|
|
|
let new_env = ref env in
|
2019-09-20 06:23:07 -07:00
|
|
|
let pat = type_pat category ~lev new_env spat expected_ty in
|
2018-04-20 06:09:06 -07:00
|
|
|
let pvs = get_ref pattern_variables in
|
|
|
|
let unpacks = get_ref module_variables in
|
2018-04-20 06:21:01 -07:00
|
|
|
(pat, !new_env, get_ref pattern_force, pvs, unpacks)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2019-09-20 06:23:07 -07:00
|
|
|
let type_pattern_list
|
2020-10-13 00:48:16 -07:00
|
|
|
category no_existentials env spatl expected_tys allow
|
2019-09-20 06:23:07 -07:00
|
|
|
=
|
2020-10-13 00:48:16 -07:00
|
|
|
reset_pattern allow;
|
2011-10-25 05:11:06 -07:00
|
|
|
let new_env = ref env in
|
2017-09-11 15:05:01 -07:00
|
|
|
let type_pat (attrs, pat) ty =
|
|
|
|
Builtin_attributes.warning_scope ~ppwarning:false attrs
|
|
|
|
(fun () ->
|
2019-09-20 06:23:07 -07:00
|
|
|
type_pat category ~no_existentials new_env pat ty
|
2017-09-11 15:05:01 -07:00
|
|
|
)
|
|
|
|
in
|
|
|
|
let patl = List.map2 type_pat spatl expected_tys in
|
2018-04-20 06:09:06 -07:00
|
|
|
let pvs = get_ref pattern_variables in
|
2019-09-06 12:53:44 -07:00
|
|
|
let unpacks =
|
|
|
|
List.map (fun (name, loc) ->
|
|
|
|
name, loc, Uid.mk ~current_unit:(Env.get_unit_name ())
|
|
|
|
) (get_ref module_variables)
|
|
|
|
in
|
2018-04-20 06:09:06 -07:00
|
|
|
let new_env = add_pattern_variables !new_env pvs in
|
2018-04-11 10:45:21 -07:00
|
|
|
(patl, new_env, get_ref pattern_force, pvs, unpacks)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
let type_class_arg_pattern cl_num val_env met_env l spat =
|
2020-10-13 00:48:16 -07:00
|
|
|
reset_pattern false;
|
2011-10-25 05:11:06 -07:00
|
|
|
let nv = newvar () in
|
2019-09-20 06:23:07 -07:00
|
|
|
let pat =
|
|
|
|
type_pat Value ~no_existentials:In_class_args (ref val_env) spat nv in
|
2003-08-18 01:26:18 -07:00
|
|
|
if has_variants pat then begin
|
|
|
|
Parmatch.pressure_variants val_env [pat];
|
2019-09-20 06:23:07 -07:00
|
|
|
finalize_variants pat;
|
2003-08-18 01:26:18 -07:00
|
|
|
end;
|
2002-06-03 00:33:48 -07:00
|
|
|
List.iter (fun f -> f()) (get_ref pattern_force);
|
2019-12-02 11:49:51 -08:00
|
|
|
if is_optional l then unify_pat (ref val_env) pat (type_option (newvar ()));
|
2019-08-20 01:53:05 -07:00
|
|
|
let (pv, val_env, met_env) =
|
1998-06-24 12:22:26 -07:00
|
|
|
List.fold_right
|
2019-08-20 01:53:05 -07:00
|
|
|
(fun {pv_id; pv_type; pv_loc; pv_as_var; pv_attributes}
|
|
|
|
(pv, val_env, met_env) ->
|
2012-02-14 02:00:18 -08:00
|
|
|
let check s =
|
2018-06-07 04:54:39 -07:00
|
|
|
if pv_as_var then Warnings.Unused_var s
|
2012-02-14 02:00:18 -08:00
|
|
|
else Warnings.Unused_var_strict s in
|
2019-08-20 01:53:05 -07:00
|
|
|
let id' = Ident.rename pv_id in
|
|
|
|
let val_uid = Uid.mk ~current_unit:(Env.get_unit_name ()) in
|
|
|
|
let val_env =
|
|
|
|
Env.add_value pv_id
|
|
|
|
{ val_type = pv_type
|
|
|
|
; val_kind = Val_reg
|
|
|
|
; val_attributes = pv_attributes
|
|
|
|
; val_loc = pv_loc
|
|
|
|
; val_uid
|
|
|
|
}
|
|
|
|
val_env
|
|
|
|
in
|
|
|
|
let met_env =
|
|
|
|
Env.add_value id' ~check
|
|
|
|
{ val_type = pv_type
|
|
|
|
; val_kind = Val_ivar (Immutable, cl_num)
|
|
|
|
; val_attributes = pv_attributes
|
|
|
|
; val_loc = pv_loc
|
|
|
|
; val_uid
|
|
|
|
}
|
|
|
|
met_env
|
|
|
|
in
|
|
|
|
((id', pv_id, pv_type)::pv, val_env, met_env))
|
|
|
|
!pattern_variables ([], val_env, met_env)
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
(pat, pv, val_env, met_env)
|
|
|
|
|
2003-11-25 01:20:45 -08:00
|
|
|
let type_self_pattern cl_num privty val_env met_env par_env spat =
|
2013-03-08 06:59:45 -08:00
|
|
|
let open Ast_helper in
|
2005-08-13 13:59:37 -07:00
|
|
|
let spat =
|
2013-03-08 06:59:45 -08:00
|
|
|
Pat.mk (Ppat_alias (Pat.mk(Ppat_alias (spat, mknoloc "selfpat-*")),
|
|
|
|
mknoloc ("selfpat-" ^ cl_num)))
|
1998-11-29 09:34:05 -08:00
|
|
|
in
|
2020-10-13 00:48:16 -07:00
|
|
|
reset_pattern false;
|
2011-10-25 05:11:06 -07:00
|
|
|
let nv = newvar() in
|
2019-09-20 06:23:07 -07:00
|
|
|
let pat =
|
|
|
|
type_pat Value ~no_existentials:In_self_pattern (ref val_env) spat nv in
|
2002-06-03 00:33:48 -07:00
|
|
|
List.iter (fun f -> f()) (get_ref pattern_force);
|
1998-06-24 12:22:26 -07:00
|
|
|
let meths = ref Meths.empty in
|
|
|
|
let vars = ref Vars.empty in
|
|
|
|
let pv = !pattern_variables in
|
|
|
|
pattern_variables := [];
|
|
|
|
let (val_env, met_env, par_env) =
|
|
|
|
List.fold_right
|
2017-08-12 13:24:41 -07:00
|
|
|
(fun {pv_id; pv_type; pv_loc; pv_as_var; pv_attributes}
|
|
|
|
(val_env, met_env, par_env) ->
|
2018-10-12 02:20:21 -07:00
|
|
|
let name = Ident.name pv_id in
|
|
|
|
(Env.enter_unbound_value name Val_unbound_self val_env,
|
2019-08-20 01:53:05 -07:00
|
|
|
Env.add_value pv_id
|
|
|
|
{val_type = pv_type;
|
|
|
|
val_kind = Val_self (meths, vars, cl_num, privty);
|
|
|
|
val_attributes = pv_attributes;
|
|
|
|
val_loc = pv_loc;
|
|
|
|
val_uid = Uid.mk ~current_unit:(Env.get_unit_name ());
|
|
|
|
}
|
2018-06-07 04:54:39 -07:00
|
|
|
~check:(fun s -> if pv_as_var then Warnings.Unused_var s
|
2012-02-14 02:00:18 -08:00
|
|
|
else Warnings.Unused_var_strict s)
|
1998-06-24 12:22:26 -07:00
|
|
|
met_env,
|
2018-10-12 02:20:21 -07:00
|
|
|
Env.enter_unbound_value name Val_unbound_self par_env))
|
1998-06-24 12:22:26 -07:00
|
|
|
pv (val_env, met_env, par_env)
|
|
|
|
in
|
|
|
|
(pat, meths, vars, val_env, met_env, par_env)
|
|
|
|
|
2002-05-16 03:18:51 -07:00
|
|
|
let delayed_checks = ref []
|
|
|
|
let reset_delayed_checks () = delayed_checks := []
|
2014-08-22 06:45:02 -07:00
|
|
|
let add_delayed_check f =
|
|
|
|
delayed_checks := (f, Warnings.backup ()) :: !delayed_checks
|
|
|
|
|
2002-05-16 03:18:51 -07:00
|
|
|
let force_delayed_checks () =
|
2008-01-11 08:13:18 -08:00
|
|
|
(* checks may change type levels *)
|
|
|
|
let snap = Btype.snapshot () in
|
2014-08-22 06:45:02 -07:00
|
|
|
let w_old = Warnings.backup () in
|
|
|
|
List.iter
|
|
|
|
(fun (f, w) -> Warnings.restore w; f ())
|
|
|
|
(List.rev !delayed_checks);
|
|
|
|
Warnings.restore w_old;
|
2008-01-11 08:13:18 -08:00
|
|
|
reset_delayed_checks ();
|
|
|
|
Btype.backtrack snap
|
2002-05-16 03:18:51 -07:00
|
|
|
|
2019-06-28 05:00:59 -07:00
|
|
|
let rec final_subexpression exp =
|
|
|
|
match exp.exp_desc with
|
|
|
|
Texp_let (_, _, e)
|
|
|
|
| Texp_sequence (_, e)
|
|
|
|
| Texp_try (e, _)
|
|
|
|
| Texp_ifthenelse (_, e, _)
|
|
|
|
| Texp_match (_, {c_rhs=e} :: _, _)
|
2019-06-28 05:53:29 -07:00
|
|
|
| Texp_letmodule (_, _, _, _, e)
|
|
|
|
| Texp_letexception (_, e)
|
|
|
|
| Texp_open (_, e)
|
2012-07-24 01:57:52 -07:00
|
|
|
-> final_subexpression e
|
2019-06-28 05:00:59 -07:00
|
|
|
| _ -> exp
|
2012-07-24 01:57:52 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Generalization criterion for expressions *)
|
|
|
|
|
|
|
|
let rec is_nonexpansive exp =
|
|
|
|
match exp.exp_desc with
|
2019-04-09 03:33:10 -07:00
|
|
|
| Texp_ident _
|
|
|
|
| Texp_constant _
|
|
|
|
| Texp_unreachable
|
|
|
|
| Texp_function _
|
|
|
|
| Texp_array [] -> true
|
2016-03-09 02:40:16 -08:00
|
|
|
| Texp_let(_rec_flag, pat_exp_list, body) ->
|
2013-06-03 08:14:19 -07:00
|
|
|
List.for_all (fun vb -> is_nonexpansive vb.vb_expr) pat_exp_list &&
|
1995-05-04 03:15:53 -07:00
|
|
|
is_nonexpansive body
|
2015-11-24 15:37:46 -08:00
|
|
|
| Texp_apply(e, (_,None)::el) ->
|
|
|
|
is_nonexpansive e && List.for_all is_nonexpansive_opt (List.map snd el)
|
2018-01-15 03:11:06 -08:00
|
|
|
| Texp_match(e, cases, _) ->
|
|
|
|
(* Not sure this is necessary, if [e] is nonexpansive then we shouldn't
|
|
|
|
care if there are exception patterns. But the previous version enforced
|
|
|
|
that there be none, so... *)
|
2019-09-20 06:23:07 -07:00
|
|
|
let contains_exception_pat pat =
|
|
|
|
exists_general_pattern { f = fun (type k) (p : k general_pattern) ->
|
2018-01-15 03:11:06 -08:00
|
|
|
match p.pat_desc with
|
2019-09-20 06:23:07 -07:00
|
|
|
| Tpat_exception _ -> true
|
|
|
|
| _ -> false } pat
|
2018-01-15 03:11:06 -08:00
|
|
|
in
|
2013-06-28 03:43:25 -07:00
|
|
|
is_nonexpansive e &&
|
2013-07-16 06:34:30 -07:00
|
|
|
List.for_all
|
2018-01-15 03:11:06 -08:00
|
|
|
(fun {c_lhs; c_guard; c_rhs} ->
|
2013-07-16 06:34:30 -07:00
|
|
|
is_nonexpansive_opt c_guard && is_nonexpansive c_rhs
|
2018-01-15 03:11:06 -08:00
|
|
|
&& not (contains_exception_pat c_lhs)
|
2013-07-16 06:34:30 -07:00
|
|
|
) cases
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_tuple el ->
|
|
|
|
List.for_all is_nonexpansive el
|
2013-04-17 02:46:52 -07:00
|
|
|
| Texp_construct( _, _, el) ->
|
1995-05-04 03:15:53 -07:00
|
|
|
List.for_all is_nonexpansive el
|
2000-06-01 06:14:36 -07:00
|
|
|
| Texp_variant(_, arg) -> is_nonexpansive_opt arg
|
2016-07-11 07:36:44 -07:00
|
|
|
| Texp_record { fields; extended_expression } ->
|
2016-07-07 09:07:31 -07:00
|
|
|
Array.for_all
|
2016-07-11 05:50:11 -07:00
|
|
|
(fun (lbl, definition) ->
|
2016-03-17 15:15:08 -07:00
|
|
|
match definition with
|
|
|
|
| Overridden (_, exp) ->
|
|
|
|
lbl.lbl_mut = Immutable && is_nonexpansive exp
|
|
|
|
| Kept _ -> true)
|
2016-07-07 09:07:31 -07:00
|
|
|
fields
|
2016-07-11 07:36:44 -07:00
|
|
|
&& is_nonexpansive_opt extended_expression
|
2016-03-09 02:40:16 -08:00
|
|
|
| Texp_field(exp, _, _) -> is_nonexpansive exp
|
|
|
|
| Texp_ifthenelse(_cond, ifso, ifnot) ->
|
2000-06-01 06:14:36 -07:00
|
|
|
is_nonexpansive ifso && is_nonexpansive_opt ifnot
|
2016-03-09 02:40:16 -08:00
|
|
|
| Texp_sequence (_e1, e2) -> is_nonexpansive e2 (* PR#4354 *)
|
2019-04-09 03:33:49 -07:00
|
|
|
| Texp_new (_, _, cl_decl) -> Ctype.class_type_arity cl_decl.cty_type > 0
|
2003-11-25 01:20:45 -08:00
|
|
|
(* Note: nonexpansive only means no _observable_ side effects *)
|
|
|
|
| Texp_lazy e -> is_nonexpansive e
|
2013-09-27 08:04:03 -07:00
|
|
|
| Texp_object ({cstr_fields=fields; cstr_type = { csig_vars=vars}}, _) ->
|
2003-11-25 01:20:45 -08:00
|
|
|
let count = ref 0 in
|
|
|
|
List.for_all
|
2012-05-30 07:52:37 -07:00
|
|
|
(fun field -> match field.cf_desc with
|
2013-04-10 04:17:41 -07:00
|
|
|
Tcf_method _ -> true
|
|
|
|
| Tcf_val (_, _, _, Tcfk_concrete (_, e), _) ->
|
2012-05-30 07:52:37 -07:00
|
|
|
incr count; is_nonexpansive e
|
2013-04-10 04:17:41 -07:00
|
|
|
| Tcf_val (_, _, _, Tcfk_virtual _, _) ->
|
2012-05-30 07:52:37 -07:00
|
|
|
incr count; true
|
2013-04-10 04:17:41 -07:00
|
|
|
| Tcf_initializer e -> is_nonexpansive e
|
|
|
|
| Tcf_constraint _ -> true
|
2014-05-04 13:42:34 -07:00
|
|
|
| Tcf_inherit _ -> false
|
|
|
|
| Tcf_attribute _ -> true)
|
2003-11-25 01:20:45 -08:00
|
|
|
fields &&
|
2006-04-04 19:28:13 -07:00
|
|
|
Vars.fold (fun _ (mut,_,_) b -> decr count; b && mut = Immutable)
|
2003-11-25 01:20:45 -08:00
|
|
|
vars true &&
|
|
|
|
!count = 0
|
2018-11-27 05:28:40 -08:00
|
|
|
| Texp_letmodule (_, _, _, mexp, e)
|
|
|
|
| Texp_open ({ open_expr = mexp; _}, e) ->
|
2012-06-01 01:05:48 -07:00
|
|
|
is_nonexpansive_mod mexp && is_nonexpansive e
|
2010-08-02 07:37:22 -07:00
|
|
|
| Texp_pack mexp ->
|
|
|
|
is_nonexpansive_mod mexp
|
2017-08-12 13:24:41 -07:00
|
|
|
(* Computations which raise exceptions are nonexpansive, since (raise e) is
|
|
|
|
equivalent to (raise e; diverge), and a nonexpansive "diverge" can be
|
|
|
|
produced using lazy values or the relaxed value restriction.
|
|
|
|
See GPR#1142 *)
|
2017-04-07 08:03:57 -07:00
|
|
|
| Texp_assert exp ->
|
|
|
|
is_nonexpansive exp
|
2017-05-12 06:09:06 -07:00
|
|
|
| Texp_apply (
|
|
|
|
{ exp_desc = Texp_ident (_, _, {val_kind =
|
2018-02-23 09:00:41 -08:00
|
|
|
Val_prim {Primitive.prim_name =
|
|
|
|
("%raise" | "%reraise" | "%raise_notrace")}}) },
|
2017-05-12 06:09:06 -07:00
|
|
|
[Nolabel, Some e]) ->
|
|
|
|
is_nonexpansive e
|
2019-04-09 03:28:55 -07:00
|
|
|
| Texp_array (_ :: _)
|
|
|
|
| Texp_apply _
|
|
|
|
| Texp_try _
|
|
|
|
| Texp_setfield _
|
|
|
|
| Texp_while _
|
|
|
|
| Texp_for _
|
|
|
|
| Texp_send _
|
|
|
|
| Texp_instvar _
|
|
|
|
| Texp_setinstvar _
|
|
|
|
| Texp_override _
|
|
|
|
| Texp_letexception _
|
|
|
|
| Texp_letop _
|
|
|
|
| Texp_extension_constructor _ ->
|
|
|
|
false
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2010-08-02 07:37:22 -07:00
|
|
|
and is_nonexpansive_mod mexp =
|
|
|
|
match mexp.mod_desc with
|
2019-04-09 03:33:10 -07:00
|
|
|
| Tmod_ident _
|
2010-08-02 07:37:22 -07:00
|
|
|
| Tmod_functor _ -> true
|
|
|
|
| Tmod_unpack (e, _) -> is_nonexpansive e
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tmod_constraint (m, _, _, _) -> is_nonexpansive_mod m
|
|
|
|
| Tmod_structure str ->
|
2010-08-02 07:37:22 -07:00
|
|
|
List.for_all
|
2012-05-30 07:52:37 -07:00
|
|
|
(fun item -> match item.str_desc with
|
2014-05-04 16:08:45 -07:00
|
|
|
| Tstr_eval _ | Tstr_primitive _ | Tstr_type _
|
2018-11-28 02:35:47 -08:00
|
|
|
| Tstr_modtype _ | Tstr_class_type _ -> true
|
2013-06-03 08:14:19 -07:00
|
|
|
| Tstr_value (_, pat_exp_list) ->
|
|
|
|
List.for_all (fun vb -> is_nonexpansive vb.vb_expr) pat_exp_list
|
2013-10-02 01:34:01 -07:00
|
|
|
| Tstr_module {mb_expr=m;_}
|
2018-11-28 02:35:47 -08:00
|
|
|
| Tstr_open {open_expr=m;_}
|
2014-04-15 04:26:00 -07:00
|
|
|
| Tstr_include {incl_mod=m;_} -> is_nonexpansive_mod m
|
2010-08-02 07:37:22 -07:00
|
|
|
| Tstr_recmodule id_mod_list ->
|
2013-03-26 01:09:26 -07:00
|
|
|
List.for_all (fun {mb_expr=m;_} -> is_nonexpansive_mod m)
|
2012-05-31 01:07:31 -07:00
|
|
|
id_mod_list
|
2018-04-06 10:02:46 -07:00
|
|
|
| Tstr_exception {tyexn_constructor = {ext_kind = Text_decl _}} ->
|
2014-05-04 16:08:45 -07:00
|
|
|
false (* true would be unsound *)
|
2017-08-12 13:24:41 -07:00
|
|
|
| Tstr_exception {tyexn_constructor = {ext_kind = Text_rebind _}} ->
|
|
|
|
true
|
2014-05-04 16:08:45 -07:00
|
|
|
| Tstr_typext te ->
|
|
|
|
List.for_all
|
|
|
|
(function {ext_kind = Text_decl _} -> false
|
|
|
|
| {ext_kind = Text_rebind _} -> true)
|
|
|
|
te.tyext_constructors
|
2010-08-02 07:37:22 -07:00
|
|
|
| Tstr_class _ -> false (* could be more precise *)
|
2013-03-25 07:16:07 -07:00
|
|
|
| Tstr_attribute _ -> true
|
2010-08-02 07:37:22 -07:00
|
|
|
)
|
2012-05-30 07:52:37 -07:00
|
|
|
str.str_items
|
2010-08-02 07:37:22 -07:00
|
|
|
| Tmod_apply _ -> false
|
|
|
|
|
2000-06-01 06:14:36 -07:00
|
|
|
and is_nonexpansive_opt = function
|
2019-04-09 03:33:10 -07:00
|
|
|
| None -> true
|
2000-06-01 06:14:36 -07:00
|
|
|
| Some e -> is_nonexpansive e
|
|
|
|
|
2019-04-09 03:28:13 -07:00
|
|
|
let maybe_expansive e = not (is_nonexpansive e)
|
|
|
|
|
2017-09-21 16:04:41 -07:00
|
|
|
let check_recursive_bindings env valbinds =
|
2018-06-04 02:07:06 -07:00
|
|
|
let ids = let_bound_idents valbinds in
|
2017-09-21 16:04:41 -07:00
|
|
|
List.iter
|
|
|
|
(fun {vb_expr} ->
|
2018-05-23 02:59:08 -07:00
|
|
|
if not (Rec_check.is_valid_recursive_expression ids vb_expr) then
|
2018-05-07 15:05:54 -07:00
|
|
|
raise(Error(vb_expr.exp_loc, env, Illegal_letrec_expr))
|
|
|
|
)
|
2017-09-21 16:04:41 -07:00
|
|
|
valbinds
|
|
|
|
|
|
|
|
let check_recursive_class_bindings env ids exprs =
|
|
|
|
List.iter
|
|
|
|
(fun expr ->
|
2018-05-23 02:59:08 -07:00
|
|
|
if not (Rec_check.is_valid_class_expr ids expr) then
|
2018-05-07 15:05:54 -07:00
|
|
|
raise(Error(expr.cl_loc, env, Illegal_class_expr)))
|
2017-09-21 16:04:41 -07:00
|
|
|
exprs
|
|
|
|
|
2020-04-08 06:01:56 -07:00
|
|
|
let is_prim ~name funct =
|
|
|
|
match funct.exp_desc with
|
|
|
|
| Texp_ident (_, _, {val_kind=Val_prim{Primitive.prim_name; _}}) ->
|
|
|
|
prim_name = name
|
|
|
|
| _ -> false
|
1999-11-30 08:07:38 -08:00
|
|
|
(* Approximate the type of an expression, for better recursion *)
|
|
|
|
|
2005-03-24 04:19:54 -08:00
|
|
|
let rec approx_type env sty =
|
1999-11-30 08:07:38 -08:00
|
|
|
match sty.ptyp_desc with
|
|
|
|
Ptyp_arrow (p, _, sty) ->
|
|
|
|
let ty1 = if is_optional p then type_option (newvar ()) else newvar () in
|
2005-03-24 04:19:54 -08:00
|
|
|
newty (Tarrow (p, ty1, approx_type env sty, Cok))
|
|
|
|
| Ptyp_tuple args ->
|
|
|
|
newty (Ttuple (List.map (approx_type env) args))
|
|
|
|
| Ptyp_constr (lid, ctl) ->
|
2018-10-12 02:20:21 -07:00
|
|
|
let path, decl = Env.lookup_type ~use:false ~loc:lid.loc lid.txt env in
|
|
|
|
if List.length ctl <> decl.type_arity then newvar ()
|
|
|
|
else begin
|
2005-03-24 04:19:54 -08:00
|
|
|
let tyl = List.map (approx_type env) ctl in
|
|
|
|
newconstr path tyl
|
|
|
|
end
|
2009-10-26 00:11:36 -07:00
|
|
|
| Ptyp_poly (_, sty) ->
|
|
|
|
approx_type env sty
|
1999-11-30 08:07:38 -08:00
|
|
|
| _ -> newvar ()
|
|
|
|
|
|
|
|
let rec type_approx env sexp =
|
|
|
|
match sexp.pexp_desc with
|
|
|
|
Pexp_let (_, _, e) -> type_approx env e
|
2014-12-22 00:45:55 -08:00
|
|
|
| Pexp_fun (p, _, _, e) ->
|
|
|
|
let ty = if is_optional p then type_option (newvar ()) else newvar () in
|
|
|
|
newty (Tarrow(p, ty, type_approx env e, Cok))
|
2013-04-17 04:43:29 -07:00
|
|
|
| Pexp_function ({pc_rhs=e}::_) ->
|
2014-12-22 00:45:55 -08:00
|
|
|
newty (Tarrow(Nolabel, newvar (), type_approx env e, Cok))
|
2013-04-15 09:23:22 -07:00
|
|
|
| Pexp_match (_, {pc_rhs=e}::_) -> type_approx env e
|
1999-11-30 08:07:38 -08:00
|
|
|
| Pexp_try (e, _) -> type_approx env e
|
|
|
|
| Pexp_tuple l -> newty (Ttuple(List.map (type_approx env) l))
|
|
|
|
| Pexp_ifthenelse (_,e,_) -> type_approx env e
|
|
|
|
| Pexp_sequence (_,e) -> type_approx env e
|
2013-04-17 05:23:44 -07:00
|
|
|
| Pexp_constraint (e, sty) ->
|
|
|
|
let ty = type_approx env e in
|
|
|
|
let ty1 = approx_type env sty in
|
|
|
|
begin try unify env ty ty1 with Unify trace ->
|
2019-03-07 07:36:46 -08:00
|
|
|
raise(Error(sexp.pexp_loc, env, Expr_type_clash (trace, None, None)))
|
2013-04-17 05:23:44 -07:00
|
|
|
end;
|
|
|
|
ty1
|
|
|
|
| Pexp_coerce (e, sty1, sty2) ->
|
2005-03-24 04:19:54 -08:00
|
|
|
let approx_ty_opt = function
|
|
|
|
| None -> newvar ()
|
|
|
|
| Some sty -> approx_type env sty
|
|
|
|
in
|
1999-11-30 08:07:38 -08:00
|
|
|
let ty = type_approx env e
|
2005-03-24 04:19:54 -08:00
|
|
|
and ty1 = approx_ty_opt sty1
|
2013-04-17 05:23:44 -07:00
|
|
|
and ty2 = approx_type env sty2 in
|
2005-04-03 21:34:53 -07:00
|
|
|
begin try unify env ty ty1 with Unify trace ->
|
2019-03-07 07:36:46 -08:00
|
|
|
raise(Error(sexp.pexp_loc, env, Expr_type_clash (trace, None, None)))
|
2005-04-03 21:34:53 -07:00
|
|
|
end;
|
2013-04-17 05:23:44 -07:00
|
|
|
ty2
|
1999-11-30 08:07:38 -08:00
|
|
|
| _ -> newvar ()
|
|
|
|
|
2001-12-05 16:19:35 -08:00
|
|
|
(* List labels in a function type, and whether return type is a variable *)
|
|
|
|
let rec list_labels_aux env visited ls ty_fun =
|
|
|
|
let ty = expand_head env ty_fun in
|
2002-06-09 19:39:35 -07:00
|
|
|
if List.memq ty visited then
|
2001-12-05 16:19:35 -08:00
|
|
|
List.rev ls, false
|
|
|
|
else match ty.desc with
|
|
|
|
Tarrow (l, _, ty_res, _) ->
|
|
|
|
list_labels_aux env (ty::visited) (l::ls) ty_res
|
|
|
|
| _ ->
|
2011-09-22 02:05:42 -07:00
|
|
|
List.rev ls, is_Tvar ty
|
2001-12-05 16:19:35 -08:00
|
|
|
|
2013-10-28 05:08:04 -07:00
|
|
|
let list_labels env ty =
|
|
|
|
wrap_trace_gadt_instances env (list_labels_aux env [] []) ty
|
2001-12-05 16:19:35 -08:00
|
|
|
|
2017-02-27 10:09:09 -08:00
|
|
|
(* Check that all univars are safe in a type. Both exp.exp_type and
|
|
|
|
ty_expected should already be generalized. *)
|
|
|
|
let check_univars env kind exp ty_expected vars =
|
2020-06-02 13:05:30 -07:00
|
|
|
let pty = instance ty_expected in
|
|
|
|
begin_def ();
|
|
|
|
let exp_ty, vars =
|
|
|
|
match pty.desc with
|
|
|
|
Tpoly (body, tl) ->
|
|
|
|
(* Enforce scoping for type_let:
|
|
|
|
since body is not generic, instance_poly only makes
|
|
|
|
copies of nodes that have a Tvar as descendant *)
|
|
|
|
let _, ty' = instance_poly true tl body in
|
|
|
|
let vars, exp_ty = instance_parameterized_type vars exp.exp_type in
|
|
|
|
unify_exp_types exp.exp_loc env exp_ty ty';
|
|
|
|
exp_ty, vars
|
|
|
|
| _ -> assert false
|
|
|
|
in
|
|
|
|
end_def ();
|
|
|
|
generalize exp_ty;
|
|
|
|
List.iter generalize vars;
|
|
|
|
let ty, complete = polyfy env exp_ty vars in
|
2017-02-27 10:09:09 -08:00
|
|
|
if not complete then
|
2020-06-02 13:05:30 -07:00
|
|
|
let ty_expected = instance ty_expected in
|
2017-02-27 10:09:09 -08:00
|
|
|
raise (Error (exp.exp_loc, env,
|
2020-06-02 13:05:30 -07:00
|
|
|
Less_general(kind, [Unification_trace.diff ty ty_expected])))
|
2017-02-27 10:09:09 -08:00
|
|
|
|
|
|
|
let generalize_and_check_univars env kind exp ty_expected vars =
|
|
|
|
generalize exp.exp_type;
|
|
|
|
generalize ty_expected;
|
|
|
|
List.iter generalize vars;
|
|
|
|
check_univars env kind exp ty_expected vars
|
2002-04-18 00:27:47 -07:00
|
|
|
|
2018-08-07 06:02:12 -07:00
|
|
|
let check_partial_application statement exp =
|
|
|
|
let rec f delay =
|
|
|
|
let ty = (expand_head exp.exp_env exp.exp_type).desc in
|
|
|
|
let check_statement () =
|
|
|
|
match ty with
|
|
|
|
| Tconstr (p, _, _) when Path.same p Predef.path_unit ->
|
|
|
|
()
|
|
|
|
| _ ->
|
|
|
|
if statement then
|
|
|
|
let rec loop {exp_loc; exp_desc; exp_extra; _} =
|
|
|
|
match exp_desc with
|
|
|
|
| Texp_let (_, _, e)
|
|
|
|
| Texp_sequence (_, e)
|
|
|
|
| Texp_letexception (_, e)
|
2018-02-08 09:51:47 -08:00
|
|
|
| Texp_letmodule (_, _, _, _, e) ->
|
2018-08-07 06:02:12 -07:00
|
|
|
loop e
|
|
|
|
| _ ->
|
|
|
|
let loc =
|
|
|
|
match List.find_opt (function
|
|
|
|
| (Texp_constraint _, _, _) -> true
|
|
|
|
| _ -> false) exp_extra
|
|
|
|
with
|
|
|
|
| Some (_, loc, _) -> loc
|
|
|
|
| None -> exp_loc
|
|
|
|
in
|
2020-07-10 08:55:42 -07:00
|
|
|
Location.prerr_warning loc Warnings.Non_unit_statement
|
2018-08-07 06:02:12 -07:00
|
|
|
in
|
|
|
|
loop exp
|
|
|
|
in
|
|
|
|
match ty, exp.exp_desc with
|
|
|
|
| Tarrow _, _ ->
|
|
|
|
let rec check {exp_desc; exp_loc; exp_extra; _} =
|
|
|
|
if List.exists (function
|
|
|
|
| (Texp_constraint _, _, _) -> true
|
|
|
|
| _ -> false) exp_extra then check_statement ()
|
|
|
|
else begin
|
|
|
|
match exp_desc with
|
|
|
|
| Texp_ident _ | Texp_constant _ | Texp_tuple _
|
|
|
|
| Texp_construct _ | Texp_variant _ | Texp_record _
|
|
|
|
| Texp_field _ | Texp_setfield _ | Texp_array _
|
|
|
|
| Texp_while _ | Texp_for _ | Texp_instvar _
|
|
|
|
| Texp_setinstvar _ | Texp_override _ | Texp_assert _
|
|
|
|
| Texp_lazy _ | Texp_object _ | Texp_pack _ | Texp_unreachable
|
|
|
|
| Texp_extension_constructor _ | Texp_ifthenelse (_, _, None)
|
|
|
|
| Texp_function _ ->
|
|
|
|
check_statement ()
|
|
|
|
| Texp_match (_, cases, _) ->
|
|
|
|
List.iter (fun {c_rhs; _} -> check c_rhs) cases
|
|
|
|
| Texp_try (e, cases) ->
|
|
|
|
check e; List.iter (fun {c_rhs; _} -> check c_rhs) cases
|
|
|
|
| Texp_ifthenelse (_, e1, Some e2) ->
|
|
|
|
check e1; check e2
|
2018-04-08 01:51:15 -07:00
|
|
|
| Texp_let (_, _, e) | Texp_sequence (_, e) | Texp_open (_, e)
|
2018-02-08 09:51:47 -08:00
|
|
|
| Texp_letexception (_, e) | Texp_letmodule (_, _, _, _, e) ->
|
2018-08-07 06:02:12 -07:00
|
|
|
check e
|
2018-07-29 14:29:01 -07:00
|
|
|
| Texp_apply _ | Texp_send _ | Texp_new _ | Texp_letop _ ->
|
2020-07-10 10:53:58 -07:00
|
|
|
Location.prerr_warning exp_loc
|
|
|
|
Warnings.Ignored_partial_application
|
2018-08-07 06:02:12 -07:00
|
|
|
end
|
|
|
|
in
|
|
|
|
check exp
|
|
|
|
| Tvar _, _ ->
|
|
|
|
if delay then add_delayed_check (fun () -> f false)
|
|
|
|
| _ ->
|
|
|
|
check_statement ()
|
|
|
|
in
|
|
|
|
f true
|
2002-05-16 03:18:51 -07:00
|
|
|
|
2008-07-17 19:48:38 -07:00
|
|
|
(* Check that a type is generalizable at some level *)
|
|
|
|
let generalizable level ty =
|
|
|
|
let rec check ty =
|
|
|
|
let ty = repr ty in
|
|
|
|
if ty.level < lowest_level then () else
|
|
|
|
if ty.level <= level then raise Exit else
|
|
|
|
(mark_type_node ty; iter_type_expr check ty)
|
|
|
|
in
|
|
|
|
try check ty; unmark_type ty; true
|
|
|
|
with Exit -> unmark_type ty; false
|
|
|
|
|
2001-12-05 16:19:35 -08:00
|
|
|
(* Hack to allow coercion of self. Will clean-up later. *)
|
2001-11-05 01:12:59 -08:00
|
|
|
let self_coercion = ref ([] : (Path.t * Location.t list ref) list)
|
|
|
|
|
2009-10-26 03:53:16 -07:00
|
|
|
(* Helpers for packaged modules. *)
|
|
|
|
let create_package_type loc env (p, l) =
|
|
|
|
let s = !Typetexp.transl_modtype_longident loc env p in
|
2012-05-30 07:52:37 -07:00
|
|
|
let fields = List.map (fun (name, ct) ->
|
2012-07-30 11:04:46 -07:00
|
|
|
name, Typetexp.transl_simple_type env false ct) l in
|
2012-05-30 07:52:37 -07:00
|
|
|
let ty = newty (Tpackage (s,
|
|
|
|
List.map fst l,
|
|
|
|
List.map (fun (_, cty) -> cty.ctyp_type) fields))
|
|
|
|
in
|
|
|
|
(s, fields, ty)
|
|
|
|
|
2011-11-24 01:02:48 -08:00
|
|
|
(* Helpers for type_cases *)
|
2013-01-10 23:26:23 -08:00
|
|
|
|
|
|
|
let contains_variant_either ty =
|
2013-09-04 08:12:37 -07:00
|
|
|
let rec loop ty =
|
2013-01-10 23:26:23 -08:00
|
|
|
let ty = repr ty in
|
|
|
|
if ty.level >= lowest_level then begin
|
|
|
|
mark_type_node ty;
|
|
|
|
match ty.desc with
|
|
|
|
Tvariant row ->
|
|
|
|
let row = row_repr row in
|
2019-07-12 08:47:25 -07:00
|
|
|
if not (is_fixed row) then
|
2013-01-10 23:26:23 -08:00
|
|
|
List.iter
|
|
|
|
(fun (_,f) ->
|
|
|
|
match row_field_repr f with Reither _ -> raise Exit | _ -> ())
|
|
|
|
row.row_fields;
|
|
|
|
iter_row loop row
|
|
|
|
| _ ->
|
|
|
|
iter_type_expr loop ty
|
|
|
|
end
|
|
|
|
in
|
|
|
|
try loop ty; unmark_type ty; false
|
|
|
|
with Exit -> unmark_type ty; true
|
|
|
|
|
2019-09-23 05:34:53 -07:00
|
|
|
let shallow_iter_ppat f p =
|
2010-11-11 02:02:56 -08:00
|
|
|
match p.ppat_desc with
|
2013-04-16 08:34:09 -07:00
|
|
|
| Ppat_any | Ppat_var _ | Ppat_constant _ | Ppat_interval _
|
2013-03-04 05:52:23 -08:00
|
|
|
| Ppat_extension _
|
2012-05-30 07:52:37 -07:00
|
|
|
| Ppat_type _ | Ppat_unpack _ -> ()
|
2010-11-11 02:02:56 -08:00
|
|
|
| Ppat_array pats -> List.iter f pats
|
|
|
|
| Ppat_or (p1,p2) -> f p1; f p2
|
2019-05-23 08:27:58 -07:00
|
|
|
| Ppat_variant (_, arg) | Ppat_construct (_, arg) -> Option.iter f arg
|
2010-11-11 02:02:56 -08:00
|
|
|
| Ppat_tuple lst -> List.iter f lst
|
2014-05-05 04:49:37 -07:00
|
|
|
| Ppat_exception p | Ppat_alias (p,_)
|
2016-05-05 10:50:56 -07:00
|
|
|
| Ppat_open (_,p)
|
2014-05-05 04:49:37 -07:00
|
|
|
| Ppat_constraint (p,_) | Ppat_lazy p -> f p
|
2016-03-09 02:40:16 -08:00
|
|
|
| Ppat_record (args, _flag) -> List.iter (fun (_,p) -> f p) args
|
2010-11-11 02:02:56 -08:00
|
|
|
|
2019-09-23 05:34:53 -07:00
|
|
|
let exists_ppat f p =
|
|
|
|
let exception Found in
|
2011-10-25 05:11:06 -07:00
|
|
|
let rec loop p =
|
2019-09-23 05:34:53 -07:00
|
|
|
if f p then raise Found else ();
|
|
|
|
shallow_iter_ppat loop p in
|
|
|
|
match loop p with
|
|
|
|
| exception Found -> true
|
|
|
|
| () -> false
|
2009-10-26 03:53:16 -07:00
|
|
|
|
2019-09-23 05:34:53 -07:00
|
|
|
let contains_polymorphic_variant p =
|
|
|
|
exists_ppat
|
|
|
|
(function
|
|
|
|
| {ppat_desc = (Ppat_variant _ | Ppat_type _)} -> true
|
|
|
|
| _ -> false)
|
|
|
|
p
|
|
|
|
|
2019-09-20 06:23:07 -07:00
|
|
|
let contains_gadt p =
|
|
|
|
exists_general_pattern { f = fun (type k) (p : k general_pattern) ->
|
|
|
|
match p.pat_desc with
|
|
|
|
| Tpat_construct (_, cd, _) when cd.cstr_generalized -> true
|
|
|
|
| _ -> false } p
|
2018-03-06 07:09:31 -08:00
|
|
|
|
|
|
|
(* There are various things that we need to do in presence of GADT constructors
|
|
|
|
that aren't required if there are none.
|
|
|
|
However, because of disambiguation, we can't know for sure whether the
|
|
|
|
patterns contain some GADT constructors. So we conservatively assume that
|
|
|
|
any constructor might be a GADT constructor. *)
|
|
|
|
let may_contain_gadts p =
|
2019-09-23 05:34:53 -07:00
|
|
|
exists_ppat
|
|
|
|
(function
|
|
|
|
| {ppat_desc = Ppat_construct (_, _)} -> true
|
|
|
|
| _ -> false)
|
|
|
|
p
|
2011-11-24 01:02:48 -08:00
|
|
|
|
2013-01-10 23:26:23 -08:00
|
|
|
let check_absent_variant env =
|
2019-09-20 06:23:07 -07:00
|
|
|
iter_general_pattern { f = fun (type k) (pat : k general_pattern) ->
|
|
|
|
match pat.pat_desc with
|
|
|
|
| Tpat_variant (s, arg, row) ->
|
2013-01-10 23:26:23 -08:00
|
|
|
let row = row_repr !row in
|
|
|
|
if List.exists (fun (s',fi) -> s = s' && row_field_repr fi <> Rabsent)
|
|
|
|
row.row_fields
|
2019-07-12 08:47:25 -07:00
|
|
|
|| not (is_fixed row) && not (static_row row) (* same as Ctype.poly *)
|
2013-01-10 23:26:23 -08:00
|
|
|
then () else
|
|
|
|
let ty_arg =
|
|
|
|
match arg with None -> [] | Some p -> [correct_levels p.pat_type] in
|
|
|
|
let row' = {row_fields = [s, Reither(arg=None,ty_arg,true,ref None)];
|
|
|
|
row_more = newvar (); row_bound = ();
|
2019-07-12 08:47:25 -07:00
|
|
|
row_closed = false; row_fixed = None; row_name = None} in
|
2013-01-10 23:26:23 -08:00
|
|
|
(* Should fail *)
|
2019-12-02 11:49:51 -08:00
|
|
|
unify_pat (ref env) {pat with pat_type = newty (Tvariant row')}
|
|
|
|
(correct_levels pat.pat_type)
|
2019-09-20 06:23:07 -07:00
|
|
|
| _ -> () }
|
2013-01-10 23:26:23 -08:00
|
|
|
|
2016-01-21 06:29:42 -08:00
|
|
|
(* Getting proper location of already typed expressions.
|
|
|
|
|
|
|
|
Used to avoid confusing locations on type error messages in presence of
|
|
|
|
type constraints.
|
|
|
|
For example:
|
|
|
|
|
|
|
|
(* Before patch *)
|
|
|
|
# let x : string = (5 : int);;
|
|
|
|
^
|
|
|
|
(* After patch *)
|
|
|
|
# let x : string = (5 : int);;
|
|
|
|
^^^^^^^^^
|
|
|
|
*)
|
|
|
|
let proper_exp_loc exp =
|
|
|
|
let rec aux = function
|
|
|
|
| [] -> exp.exp_loc
|
|
|
|
| ((Texp_constraint _ | Texp_coerce _), loc, _) :: _ -> loc
|
|
|
|
| _ :: rest -> aux rest
|
|
|
|
in
|
|
|
|
aux exp.exp_extra
|
|
|
|
|
2016-10-14 01:34:50 -07:00
|
|
|
(* To find reasonable names for let-bound and lambda-bound idents *)
|
|
|
|
|
|
|
|
let rec name_pattern default = function
|
2018-08-28 09:07:01 -07:00
|
|
|
[] -> Ident.create_local default
|
2018-01-15 03:11:06 -08:00
|
|
|
| p :: rem ->
|
|
|
|
match p.pat_desc with
|
|
|
|
Tpat_var (id, _) -> id
|
|
|
|
| Tpat_alias(_, id, _) -> id
|
|
|
|
| _ -> name_pattern default rem
|
|
|
|
|
|
|
|
let name_cases default lst =
|
|
|
|
name_pattern default (List.map (fun c -> c.c_lhs) lst)
|
2016-10-14 01:34:50 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Typing of expressions *)
|
|
|
|
|
|
|
|
let unify_exp env exp expected_ty =
|
2016-01-21 06:29:42 -08:00
|
|
|
let loc = proper_exp_loc exp in
|
2019-03-07 07:36:46 -08:00
|
|
|
try
|
|
|
|
unify_exp_types loc env exp.exp_type expected_ty
|
|
|
|
with Error(loc, env, Expr_type_clash(trace, tfc, None)) ->
|
|
|
|
raise (Error(loc, env, Expr_type_clash(trace, tfc, Some exp.exp_desc)))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2014-10-14 08:51:30 -07:00
|
|
|
let rec type_exp ?recarg env sexp =
|
2010-11-11 19:09:11 -08:00
|
|
|
(* We now delegate everything to type_expect *)
|
2017-12-02 04:44:54 -08:00
|
|
|
type_expect ?recarg env sexp (mk_expected (newvar ()))
|
2010-11-11 19:09:11 -08:00
|
|
|
|
|
|
|
(* Typing of an expression with an expected type.
|
|
|
|
This provide better error messages, and allows controlled
|
|
|
|
propagation of return type information.
|
|
|
|
In the principal case, [type_expected'] may be at generic_level.
|
|
|
|
*)
|
|
|
|
|
2017-12-02 04:44:54 -08:00
|
|
|
and type_expect ?in_function ?recarg env sexp ty_expected_explained =
|
2012-11-08 02:51:00 -08:00
|
|
|
let previous_saved_types = Cmt_format.get_saved_types () in
|
2017-07-17 14:26:48 -07:00
|
|
|
let exp =
|
|
|
|
Builtin_attributes.warning_scope sexp.pexp_attributes
|
|
|
|
(fun () ->
|
2017-12-02 04:44:54 -08:00
|
|
|
type_expect_ ?in_function ?recarg env sexp ty_expected_explained
|
2017-07-17 14:26:48 -07:00
|
|
|
)
|
|
|
|
in
|
2014-04-02 20:59:17 -07:00
|
|
|
Cmt_format.set_saved_types
|
|
|
|
(Cmt_format.Partial_expression exp :: previous_saved_types);
|
2012-11-08 02:51:00 -08:00
|
|
|
exp
|
|
|
|
|
2017-12-02 04:44:54 -08:00
|
|
|
and with_explanation explanation f =
|
|
|
|
match explanation with
|
|
|
|
| None -> f ()
|
|
|
|
| Some explanation ->
|
|
|
|
try f ()
|
2019-03-07 07:36:46 -08:00
|
|
|
with Error (loc', env', Expr_type_clash(trace', None, exp'))
|
2017-12-02 04:44:54 -08:00
|
|
|
when not loc'.Location.loc_ghost ->
|
2019-03-07 07:36:46 -08:00
|
|
|
let err = Expr_type_clash(trace', Some explanation, exp') in
|
|
|
|
raise (Error (loc', env', err))
|
2017-12-02 04:44:54 -08:00
|
|
|
|
|
|
|
and type_expect_
|
|
|
|
?in_function ?(recarg=Rejected)
|
|
|
|
env sexp ty_expected_explained =
|
|
|
|
let { ty = ty_expected; explanation } = ty_expected_explained in
|
2009-09-04 09:19:35 -07:00
|
|
|
let loc = sexp.pexp_loc in
|
2010-11-11 19:09:11 -08:00
|
|
|
(* Record the expression type before unifying it with the expected type *)
|
2017-12-02 04:44:54 -08:00
|
|
|
let with_explanation = with_explanation explanation in
|
2010-11-11 19:09:11 -08:00
|
|
|
let rue exp =
|
2017-12-02 04:44:54 -08:00
|
|
|
with_explanation (fun () ->
|
2018-02-12 08:37:35 -08:00
|
|
|
unify_exp env (re exp) (instance ty_expected));
|
2010-11-11 19:09:11 -08:00
|
|
|
exp
|
|
|
|
in
|
1995-05-04 03:15:53 -07:00
|
|
|
match sexp.pexp_desc with
|
2009-09-04 09:19:35 -07:00
|
|
|
| Pexp_ident lid ->
|
2018-07-29 14:29:01 -07:00
|
|
|
let path, desc = type_ident env ~recarg lid in
|
|
|
|
let exp_desc =
|
|
|
|
match desc.val_kind with
|
|
|
|
| Val_ivar (_, cl_num) ->
|
|
|
|
let (self_path, _) =
|
2018-10-12 02:20:21 -07:00
|
|
|
Env.find_value_by_name
|
|
|
|
(Longident.Lident ("self-" ^ cl_num)) env
|
2018-07-29 14:29:01 -07:00
|
|
|
in
|
|
|
|
Texp_instvar(self_path, path,
|
|
|
|
match lid.txt with
|
|
|
|
Longident.Lident txt -> { txt; loc = lid.loc }
|
|
|
|
| _ -> assert false)
|
|
|
|
| Val_self (_, _, cl_num, _) ->
|
|
|
|
let (path, _) =
|
2018-10-12 02:20:21 -07:00
|
|
|
Env.find_value_by_name (Longident.Lident ("self-" ^ cl_num)) env
|
2018-07-29 14:29:01 -07:00
|
|
|
in
|
|
|
|
Texp_ident(path, lid, desc)
|
|
|
|
| _ ->
|
|
|
|
Texp_ident(path, lid, desc)
|
|
|
|
in
|
|
|
|
rue {
|
|
|
|
exp_desc; exp_loc = loc; exp_extra = [];
|
|
|
|
exp_type = instance desc.val_type;
|
|
|
|
exp_attributes = sexp.pexp_attributes;
|
|
|
|
exp_env = env }
|
2019-07-22 09:04:09 -07:00
|
|
|
| Pexp_constant(Pconst_string (str, _, _) as cst) -> (
|
2015-04-18 20:07:32 -07:00
|
|
|
let cst = constant_or_raise env loc cst in
|
2014-05-12 08:37:29 -07:00
|
|
|
(* Terrible hack for format strings *)
|
2014-05-14 08:07:17 -07:00
|
|
|
let ty_exp = expand_head env ty_expected in
|
2014-06-09 06:53:45 -07:00
|
|
|
let fmt6_path =
|
|
|
|
Path.(Pdot (Pident (Ident.create_persistent "CamlinternalFormatBasics"),
|
2018-02-08 09:51:47 -08:00
|
|
|
"format6"))
|
|
|
|
in
|
2014-06-09 06:53:45 -07:00
|
|
|
let is_format = match ty_exp.desc with
|
|
|
|
| Tconstr(path, _, _) when Path.same path fmt6_path ->
|
2014-05-14 08:07:17 -07:00
|
|
|
if !Clflags.principal && ty_exp.level <> generic_level then
|
|
|
|
Location.prerr_warning loc
|
|
|
|
(Warnings.Not_principal "this coercion to format6");
|
|
|
|
true
|
2014-05-12 08:37:29 -07:00
|
|
|
| _ -> false
|
|
|
|
in
|
2014-05-12 08:37:33 -07:00
|
|
|
if is_format then
|
Use a nominal datatype for CamlinternalFormat.format6
This should make the type-checking of formats simpler and more robust:
instead of trying to find a pair as previously, we can now use the
path of the format6 type directly.
A nice side-effect of the change is that the internal definition of
formats (as a pair) is not printed in error messages anymore.
Because format6 is in fact defined in the CamlinternalFormatBasics
submodule of Pervasives, and has an alias at the toplevel of
Pervasives, error messages still expand the definition:
> Error: This expression has type
> ('a, 'b, 'c, 'd, 'd, 'a) format6 =
> ('a, 'b, 'c, 'd, 'd, 'a) CamlinternalFormatBasics.format6
> but an expression was expected of type ...
Passing the option `-short-paths` does avoid this expansion and
returns exactly the same error message as 4.01:
> Error: This expression has type ('a, 'b, 'c, 'd, 'd, 'a) format6
> but an expression was expected of type ...
(To get this error message without -short-paths, one would need to
define format6 directly in Pervasives; but this type is mutually
recursive with several GADT types that we don't want to add in the
Pervasives namespace unqualified. This is why I'll keep the alias
for now.)
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@14868 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2014-05-14 08:07:13 -07:00
|
|
|
let format_parsetree =
|
2014-06-09 06:53:45 -07:00
|
|
|
{ (type_format loc str env) with pexp_loc = sexp.pexp_loc } in
|
2017-12-02 04:44:54 -08:00
|
|
|
type_expect ?in_function env format_parsetree ty_expected_explained
|
2014-05-12 08:37:33 -07:00
|
|
|
else
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
2013-05-23 08:12:04 -07:00
|
|
|
exp_desc = Texp_constant cst;
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2018-07-24 08:17:53 -07:00
|
|
|
exp_type = instance Predef.type_string;
|
2014-05-12 08:37:33 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
|
|
|
exp_env = env }
|
|
|
|
)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pexp_constant cst ->
|
2015-04-18 20:07:32 -07:00
|
|
|
let cst = constant_or_raise env loc cst in
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
2013-05-23 08:12:04 -07:00
|
|
|
exp_desc = Texp_constant cst;
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_type = type_constant cst;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
2014-01-22 17:58:37 -08:00
|
|
|
| Pexp_let(Nonrecursive,
|
|
|
|
[{pvb_pat=spat; pvb_expr=sval; pvb_attributes=[]}], sbody)
|
2018-03-06 07:09:31 -08:00
|
|
|
when may_contain_gadts spat ->
|
2013-06-03 08:14:19 -07:00
|
|
|
(* TODO: allow non-empty attributes? *)
|
2011-11-24 01:02:48 -08:00
|
|
|
type_expect ?in_function env
|
2014-01-22 17:58:37 -08:00
|
|
|
{sexp with
|
|
|
|
pexp_desc = Pexp_match (sval, [Ast_helper.Exp.case spat sbody])}
|
2017-12-02 04:44:54 -08:00
|
|
|
ty_expected_explained
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pexp_let(rec_flag, spat_sexp_list, sbody) ->
|
2018-04-21 12:15:13 -07:00
|
|
|
let existential_context =
|
|
|
|
if rec_flag = Recursive then In_rec
|
|
|
|
else if List.compare_length_with spat_sexp_list 1 > 0 then In_group
|
|
|
|
else With_attributes in
|
2010-10-21 16:59:33 -07:00
|
|
|
let (pat_exp_list, new_env, unpacks) =
|
2020-10-13 00:48:16 -07:00
|
|
|
type_let existential_context env rec_flag spat_sexp_list true in
|
2019-09-06 12:53:44 -07:00
|
|
|
let body = type_unpacks new_env unpacks sbody ty_expected_explained in
|
2017-09-21 16:04:41 -07:00
|
|
|
let () =
|
|
|
|
if rec_flag = Recursive then
|
|
|
|
check_recursive_bindings env pat_exp_list
|
|
|
|
in
|
2003-04-01 17:32:09 -08:00
|
|
|
re {
|
|
|
|
exp_desc = Texp_let(rec_flag, pat_exp_list, body);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_type = body.exp_type;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
2015-01-06 03:16:23 -08:00
|
|
|
| Pexp_fun (l, Some default, spat, sbody) ->
|
2013-04-17 03:01:24 -07:00
|
|
|
assert(is_optional l); (* default allowed only with optional argument *)
|
2013-03-08 06:59:45 -08:00
|
|
|
let open Ast_helper in
|
2013-04-11 05:50:38 -07:00
|
|
|
let default_loc = default.pexp_loc in
|
2010-11-11 19:09:11 -08:00
|
|
|
let scases = [
|
2013-04-15 09:23:22 -07:00
|
|
|
Exp.case
|
|
|
|
(Pat.construct ~loc:default_loc
|
|
|
|
(mknoloc (Longident.(Ldot (Lident "*predef*", "Some"))))
|
2013-04-17 02:46:52 -07:00
|
|
|
(Some (Pat.var ~loc:default_loc (mknoloc "*sth*"))))
|
2013-04-15 09:23:22 -07:00
|
|
|
(Exp.ident ~loc:default_loc (mknoloc (Longident.Lident "*sth*")));
|
|
|
|
|
|
|
|
Exp.case
|
|
|
|
(Pat.construct ~loc:default_loc
|
|
|
|
(mknoloc (Longident.(Ldot (Lident "*predef*", "None"))))
|
2013-04-17 02:46:52 -07:00
|
|
|
None)
|
2013-04-15 09:23:22 -07:00
|
|
|
default;
|
2013-03-08 06:59:45 -08:00
|
|
|
]
|
|
|
|
in
|
2016-06-02 03:37:11 -07:00
|
|
|
let sloc =
|
|
|
|
{ Location.loc_start = spat.ppat_loc.Location.loc_start;
|
|
|
|
loc_end = default_loc.Location.loc_end;
|
|
|
|
loc_ghost = true }
|
|
|
|
in
|
2013-03-08 06:59:45 -08:00
|
|
|
let smatch =
|
2016-08-01 07:06:59 -07:00
|
|
|
Exp.match_ ~loc:sloc
|
|
|
|
(Exp.ident ~loc (mknoloc (Longident.Lident "*opt*")))
|
2013-03-08 06:59:45 -08:00
|
|
|
scases
|
|
|
|
in
|
2016-06-02 03:37:11 -07:00
|
|
|
let pat = Pat.var ~loc:sloc (mknoloc "*opt*") in
|
2015-01-06 03:16:23 -08:00
|
|
|
let body =
|
2018-07-21 05:04:53 -07:00
|
|
|
Exp.let_ ~loc Nonrecursive
|
|
|
|
~attrs:[Attr.mk (mknoloc "#default") (PStr [])]
|
2015-01-06 03:16:23 -08:00
|
|
|
[Vb.mk spat smatch] sbody
|
2013-03-08 06:59:45 -08:00
|
|
|
in
|
2017-08-12 13:24:41 -07:00
|
|
|
type_function ?in_function loc sexp.pexp_attributes env
|
|
|
|
ty_expected_explained l [Exp.case pat body]
|
2015-01-06 03:02:17 -08:00
|
|
|
| Pexp_fun (l, None, spat, sbody) ->
|
2017-08-12 13:24:41 -07:00
|
|
|
type_function ?in_function loc sexp.pexp_attributes env
|
|
|
|
ty_expected_explained l [Ast_helper.Exp.case spat sbody]
|
2013-04-17 04:43:29 -07:00
|
|
|
| Pexp_function caselist ->
|
|
|
|
type_function ?in_function
|
2017-12-02 04:44:54 -08:00
|
|
|
loc sexp.pexp_attributes env ty_expected_explained Nolabel caselist
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pexp_apply(sfunct, sargs) ->
|
2016-01-04 05:07:38 -08:00
|
|
|
assert (sargs <> []);
|
2004-11-28 18:27:25 -08:00
|
|
|
begin_def (); (* one more level for non-returning functions *)
|
2002-04-18 00:27:47 -07:00
|
|
|
if !Clflags.principal then begin_def ();
|
1995-05-04 03:15:53 -07:00
|
|
|
let funct = type_exp env sfunct in
|
2002-04-18 00:27:47 -07:00
|
|
|
if !Clflags.principal then begin
|
2012-05-30 07:52:37 -07:00
|
|
|
end_def ();
|
|
|
|
generalize_structure funct.exp_type
|
|
|
|
end;
|
2006-08-11 15:40:19 -07:00
|
|
|
let rec lower_args seen ty_fun =
|
|
|
|
let ty = expand_head env ty_fun in
|
|
|
|
if List.memq ty seen then () else
|
|
|
|
match ty.desc with
|
2016-03-09 02:40:16 -08:00
|
|
|
Tarrow (_l, ty_arg, ty_fun, _com) ->
|
2010-12-12 18:59:58 -08:00
|
|
|
(try unify_var env (newvar()) ty_arg with Unify _ -> assert false);
|
2006-08-11 15:40:19 -07:00
|
|
|
lower_args (ty::seen) ty_fun
|
2004-11-28 18:27:25 -08:00
|
|
|
| _ -> ()
|
|
|
|
in
|
2018-02-12 08:37:35 -08:00
|
|
|
let ty = instance funct.exp_type in
|
2004-11-28 18:27:25 -08:00
|
|
|
end_def ();
|
2013-10-28 05:08:04 -07:00
|
|
|
wrap_trace_gadt_instances env (lower_args []) ty;
|
2004-11-28 18:27:25 -08:00
|
|
|
begin_def ();
|
1999-11-30 08:07:38 -08:00
|
|
|
let (args, ty_res) = type_application env funct sargs in
|
2004-11-28 18:27:25 -08:00
|
|
|
end_def ();
|
|
|
|
unify_var env (newvar()) funct.exp_type;
|
2020-05-13 05:26:43 -07:00
|
|
|
let exp =
|
|
|
|
{ exp_desc = Texp_apply(funct, args);
|
|
|
|
exp_loc = loc; exp_extra = [];
|
|
|
|
exp_type = ty_res;
|
|
|
|
exp_attributes = sexp.pexp_attributes;
|
|
|
|
exp_env = env } in
|
|
|
|
begin
|
|
|
|
try rue exp
|
|
|
|
with Error (_, _, Expr_type_clash _) as err ->
|
|
|
|
Misc.reraise_preserving_backtrace err (fun () ->
|
|
|
|
check_partial_application false exp)
|
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pexp_match(sarg, caselist) ->
|
2011-11-24 01:02:48 -08:00
|
|
|
begin_def ();
|
1995-05-04 03:15:53 -07:00
|
|
|
let arg = type_exp env sarg in
|
2011-11-24 01:02:48 -08:00
|
|
|
end_def ();
|
2019-04-09 03:28:13 -07:00
|
|
|
if maybe_expansive arg then lower_contravariant env arg.exp_type;
|
2016-07-07 02:10:37 -07:00
|
|
|
generalize arg.exp_type;
|
2018-01-15 03:11:06 -08:00
|
|
|
let cases, partial =
|
2019-09-20 06:23:07 -07:00
|
|
|
type_cases Computation env arg.exp_type ty_expected true loc caselist in
|
2003-04-01 17:32:09 -08:00
|
|
|
re {
|
2018-01-15 03:11:06 -08:00
|
|
|
exp_desc = Texp_match(arg, cases, partial);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2018-02-12 08:37:35 -08:00
|
|
|
exp_type = instance ty_expected;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pexp_try(sbody, caselist) ->
|
2017-12-02 04:44:54 -08:00
|
|
|
let body = type_expect env sbody ty_expected_explained in
|
2000-11-06 01:49:27 -08:00
|
|
|
let cases, _ =
|
2019-09-20 06:23:07 -07:00
|
|
|
type_cases Value env Predef.type_exn ty_expected false loc caselist in
|
2003-04-01 17:32:09 -08:00
|
|
|
re {
|
|
|
|
exp_desc = Texp_try(body, cases);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_type = body.exp_type;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pexp_tuple sexpl ->
|
2016-01-04 05:07:38 -08:00
|
|
|
assert (List.length sexpl >= 2);
|
2011-10-25 05:11:06 -07:00
|
|
|
let subtypes = List.map (fun _ -> newgenvar ()) sexpl in
|
2010-11-11 19:09:11 -08:00
|
|
|
let to_unify = newgenty (Ttuple subtypes) in
|
2017-12-02 04:44:54 -08:00
|
|
|
with_explanation (fun () ->
|
2020-07-28 17:10:17 -07:00
|
|
|
unify_exp_types loc env to_unify (generic_instance ty_expected));
|
2011-10-25 05:11:06 -07:00
|
|
|
let expl =
|
2017-12-02 04:44:54 -08:00
|
|
|
List.map2 (fun body ty -> type_expect env body (mk_expected ty))
|
|
|
|
sexpl subtypes
|
2010-11-11 19:09:11 -08:00
|
|
|
in
|
2003-04-01 17:32:09 -08:00
|
|
|
re {
|
|
|
|
exp_desc = Texp_tuple expl;
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2011-02-23 22:29:26 -08:00
|
|
|
(* Keep sharing *)
|
|
|
|
exp_type = newty (Ttuple (List.map (fun e -> e.exp_type) expl));
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
2013-04-17 02:46:52 -07:00
|
|
|
| Pexp_construct(lid, sarg) ->
|
2017-12-02 04:44:54 -08:00
|
|
|
type_construct env loc lid sarg ty_expected_explained sexp.pexp_attributes
|
1999-11-30 08:07:38 -08:00
|
|
|
| Pexp_variant(l, sarg) ->
|
2011-02-23 22:29:26 -08:00
|
|
|
(* Keep sharing *)
|
2018-02-12 08:37:35 -08:00
|
|
|
let ty_expected0 = instance ty_expected in
|
2011-02-23 22:29:26 -08:00
|
|
|
begin try match
|
|
|
|
sarg, expand_head env ty_expected, expand_head env ty_expected0 with
|
|
|
|
| Some sarg, {desc = Tvariant row}, {desc = Tvariant row0} ->
|
2010-11-11 19:09:11 -08:00
|
|
|
let row = row_repr row in
|
2011-02-23 22:29:26 -08:00
|
|
|
begin match row_field_repr (List.assoc l row.row_fields),
|
|
|
|
row_field_repr (List.assoc l row0.row_fields) with
|
|
|
|
Rpresent (Some ty), Rpresent (Some ty0) ->
|
|
|
|
let arg = type_argument env sarg ty ty0 in
|
2010-11-11 19:09:11 -08:00
|
|
|
re { exp_desc = Texp_variant(l, Some arg);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2011-02-23 22:29:26 -08:00
|
|
|
exp_type = ty_expected0;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
2010-11-11 19:09:11 -08:00
|
|
|
exp_env = env }
|
|
|
|
| _ -> raise Not_found
|
|
|
|
end
|
|
|
|
| _ -> raise Not_found
|
|
|
|
with Not_found ->
|
2019-05-23 08:27:58 -07:00
|
|
|
let arg = Option.map (type_exp env) sarg in
|
|
|
|
let arg_type = Option.map (fun arg -> arg.exp_type) arg in
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
|
|
|
exp_desc = Texp_variant(l, arg);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2010-11-11 19:09:11 -08:00
|
|
|
exp_type= newty (Tvariant{row_fields = [l, Rpresent arg_type];
|
|
|
|
row_more = newvar ();
|
|
|
|
row_bound = ();
|
|
|
|
row_closed = false;
|
2019-07-12 08:47:25 -07:00
|
|
|
row_fixed = None;
|
2010-11-11 19:09:11 -08:00
|
|
|
row_name = None});
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
2010-11-11 19:09:11 -08:00
|
|
|
exp_env = env }
|
|
|
|
end
|
1998-04-27 08:17:11 -07:00
|
|
|
| Pexp_record(lid_sexp_list, opt_sexp) ->
|
2016-01-04 05:07:38 -08:00
|
|
|
assert (lid_sexp_list <> []);
|
2012-09-21 04:16:02 -07:00
|
|
|
let opt_exp =
|
|
|
|
match opt_sexp with
|
|
|
|
None -> None
|
|
|
|
| Some sexp ->
|
|
|
|
if !Clflags.principal then begin_def ();
|
2014-10-14 08:51:30 -07:00
|
|
|
let exp = type_exp ~recarg env sexp in
|
2012-09-21 04:16:02 -07:00
|
|
|
if !Clflags.principal then begin
|
|
|
|
end_def ();
|
|
|
|
generalize_structure exp.exp_type
|
|
|
|
end;
|
|
|
|
Some exp
|
|
|
|
in
|
2019-12-12 07:06:01 -08:00
|
|
|
let ty_record, expected_type =
|
2012-09-21 04:16:02 -07:00
|
|
|
let get_path ty =
|
2013-09-04 08:12:37 -07:00
|
|
|
try
|
|
|
|
let (p0, p,_) = extract_concrete_record env ty in
|
2018-09-19 07:22:23 -07:00
|
|
|
let principal =
|
|
|
|
(repr ty).level = generic_level || not !Clflags.principal
|
|
|
|
in
|
|
|
|
Some (p0, p, principal)
|
2012-10-18 00:35:30 -07:00
|
|
|
with Not_found -> None
|
2012-09-21 04:16:02 -07:00
|
|
|
in
|
2020-06-28 14:20:12 -07:00
|
|
|
let opath = get_path ty_expected in
|
|
|
|
match opath with
|
|
|
|
None | Some (_, _, false) ->
|
|
|
|
let ty = if opath = None then newvar () else ty_expected in
|
2013-09-19 00:25:51 -07:00
|
|
|
begin match opt_exp with
|
2020-06-28 14:20:12 -07:00
|
|
|
None -> ty, opath
|
2013-09-19 00:25:51 -07:00
|
|
|
| Some exp ->
|
|
|
|
match get_path exp.exp_type with
|
2020-06-28 14:20:12 -07:00
|
|
|
None ->
|
|
|
|
ty, opath
|
|
|
|
| Some (_, p', _) as opath ->
|
2013-09-19 00:25:51 -07:00
|
|
|
let decl = Env.find_type p' env in
|
|
|
|
begin_def ();
|
|
|
|
let ty =
|
2018-02-12 08:37:35 -08:00
|
|
|
newconstr p' (instance_list decl.type_params) in
|
2013-09-19 00:25:51 -07:00
|
|
|
end_def ();
|
2014-01-22 17:58:37 -08:00
|
|
|
generalize_structure ty;
|
2020-06-28 14:20:12 -07:00
|
|
|
ty, opath
|
2013-09-19 00:25:51 -07:00
|
|
|
end
|
2020-06-28 14:20:12 -07:00
|
|
|
| _ -> ty_expected, opath
|
2012-09-21 04:16:02 -07:00
|
|
|
in
|
2012-10-29 00:54:06 -07:00
|
|
|
let closed = (opt_sexp = None) in
|
2010-08-02 07:37:22 -07:00
|
|
|
let lbl_exp_list =
|
2017-12-02 04:44:54 -08:00
|
|
|
wrap_disambiguate "This record expression is expected to have"
|
|
|
|
(mk_expected ty_record)
|
2013-09-19 00:25:51 -07:00
|
|
|
(type_label_a_list loc closed env
|
2015-05-19 15:52:21 -07:00
|
|
|
(fun e k -> k (type_label_exp true env loc ty_record e))
|
2019-12-12 07:06:01 -08:00
|
|
|
expected_type lid_sexp_list)
|
2015-05-19 15:52:21 -07:00
|
|
|
(fun x -> x)
|
2013-09-19 00:25:51 -07:00
|
|
|
in
|
2017-12-02 04:44:54 -08:00
|
|
|
with_explanation (fun () ->
|
2020-07-28 17:10:17 -07:00
|
|
|
unify_exp_types loc env (instance ty_record) (instance ty_expected));
|
2013-09-04 08:12:37 -07:00
|
|
|
|
2012-11-14 08:59:33 -08:00
|
|
|
(* type_label_a_list returns a list of labels sorted by lbl_pos *)
|
|
|
|
(* note: check_duplicates would better be implemented in
|
|
|
|
type_label_a_list directly *)
|
2012-11-15 05:57:46 -08:00
|
|
|
let rec check_duplicates = function
|
|
|
|
| (_, lbl1, _) :: (_, lbl2, _) :: _ when lbl1.lbl_pos = lbl2.lbl_pos ->
|
2013-01-29 06:21:12 -08:00
|
|
|
raise(Error(loc, env, Label_multiply_defined lbl1.lbl_name))
|
2012-11-14 08:59:33 -08:00
|
|
|
| _ :: rem ->
|
|
|
|
check_duplicates rem
|
|
|
|
| [] -> ()
|
|
|
|
in
|
|
|
|
check_duplicates lbl_exp_list;
|
2016-03-17 15:15:08 -07:00
|
|
|
let opt_exp, label_definitions =
|
|
|
|
let (_lid, lbl, _lbl_exp) = List.hd lbl_exp_list in
|
2016-03-17 15:18:16 -07:00
|
|
|
let matching_label lbl =
|
|
|
|
List.find
|
|
|
|
(fun (_, lbl',_) -> lbl'.lbl_pos = lbl.lbl_pos)
|
|
|
|
lbl_exp_list
|
|
|
|
in
|
2016-03-17 15:15:08 -07:00
|
|
|
match opt_exp with
|
|
|
|
None ->
|
|
|
|
let label_definitions =
|
|
|
|
Array.map (fun lbl ->
|
2016-03-17 16:15:31 -07:00
|
|
|
match matching_label lbl with
|
|
|
|
| (lid, _lbl, lbl_exp) ->
|
|
|
|
Overridden (lid, lbl_exp)
|
|
|
|
| exception Not_found ->
|
|
|
|
let present_indices =
|
2016-08-01 07:06:59 -07:00
|
|
|
List.map (fun (_, lbl, _) -> lbl.lbl_pos) lbl_exp_list
|
|
|
|
in
|
2016-03-17 16:15:31 -07:00
|
|
|
let label_names = extract_label_names env ty_expected in
|
|
|
|
let rec missing_labels n = function
|
|
|
|
[] -> []
|
|
|
|
| lbl :: rem ->
|
2016-08-01 07:06:59 -07:00
|
|
|
if List.mem n present_indices
|
|
|
|
then missing_labels (n + 1) rem
|
2016-03-17 16:15:31 -07:00
|
|
|
else lbl :: missing_labels (n + 1) rem
|
|
|
|
in
|
|
|
|
let missing = missing_labels 0 label_names in
|
|
|
|
raise(Error(loc, env, Label_missing missing)))
|
2016-03-17 15:15:08 -07:00
|
|
|
lbl.lbl_all
|
|
|
|
in
|
|
|
|
None, label_definitions
|
|
|
|
| Some exp ->
|
2018-02-12 08:37:35 -08:00
|
|
|
let ty_exp = instance exp.exp_type in
|
2000-06-12 23:59:29 -07:00
|
|
|
let unify_kept lbl =
|
2016-11-09 13:27:20 -08:00
|
|
|
let _, ty_arg1, ty_res1 = instance_label false lbl in
|
|
|
|
unify_exp_types exp.exp_loc env ty_exp ty_res1;
|
2016-03-17 15:18:16 -07:00
|
|
|
match matching_label lbl with
|
2016-03-17 15:15:08 -07:00
|
|
|
| lid, _lbl, lbl_exp ->
|
2016-11-09 13:27:20 -08:00
|
|
|
(* do not connect result types for overridden labels *)
|
2016-03-17 15:15:08 -07:00
|
|
|
Overridden (lid, lbl_exp)
|
|
|
|
| exception Not_found -> begin
|
2016-11-09 13:27:20 -08:00
|
|
|
let _, ty_arg2, ty_res2 = instance_label false lbl in
|
2017-12-28 10:36:56 -08:00
|
|
|
unify_exp_types loc env ty_arg1 ty_arg2;
|
2017-12-02 04:44:54 -08:00
|
|
|
with_explanation (fun () ->
|
2018-02-12 08:37:35 -08:00
|
|
|
unify_exp_types loc env (instance ty_expected) ty_res2);
|
2016-03-17 15:15:08 -07:00
|
|
|
Kept ty_arg1
|
|
|
|
end
|
|
|
|
in
|
|
|
|
let label_definitions = Array.map unify_kept lbl.lbl_all in
|
|
|
|
Some {exp with exp_type = ty_exp}, label_definitions
|
2000-06-12 23:59:29 -07:00
|
|
|
in
|
2010-08-02 07:37:22 -07:00
|
|
|
let num_fields =
|
|
|
|
match lbl_exp_list with [] -> assert false
|
2012-10-24 05:03:00 -07:00
|
|
|
| (_, lbl,_)::_ -> Array.length lbl.lbl_all in
|
2020-04-08 09:58:43 -07:00
|
|
|
if opt_sexp <> None && List.length lid_sexp_list = num_fields then
|
|
|
|
Location.prerr_warning loc Warnings.Useless_record_with;
|
2016-07-11 07:36:44 -07:00
|
|
|
let label_descriptions, representation =
|
2016-03-17 15:15:08 -07:00
|
|
|
let (_, { lbl_all; lbl_repres }, _) = List.hd lbl_exp_list in
|
|
|
|
lbl_all, lbl_repres
|
|
|
|
in
|
2016-07-07 09:07:31 -07:00
|
|
|
let fields =
|
2016-07-11 05:50:11 -07:00
|
|
|
Array.map2 (fun descr def -> descr, def)
|
|
|
|
label_descriptions label_definitions
|
2016-07-07 09:07:31 -07:00
|
|
|
in
|
2003-04-01 17:32:09 -08:00
|
|
|
re {
|
2016-07-11 07:36:44 -07:00
|
|
|
exp_desc = Texp_record {
|
|
|
|
fields; representation;
|
|
|
|
extended_expression = opt_exp
|
|
|
|
};
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2018-02-12 08:37:35 -08:00
|
|
|
exp_type = instance ty_expected;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
2012-09-21 00:01:44 -07:00
|
|
|
| Pexp_field(srecord, lid) ->
|
2016-03-09 02:40:16 -08:00
|
|
|
let (record, label, _) = type_label_access env srecord lid in
|
2002-04-18 00:27:47 -07:00
|
|
|
let (_, ty_arg, ty_res) = instance_label false label in
|
2012-09-21 00:01:44 -07:00
|
|
|
unify_exp env record ty_res;
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
2012-10-24 05:57:16 -07:00
|
|
|
exp_desc = Texp_field(record, lid, label);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_type = ty_arg;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pexp_setfield(srecord, lid, snewval) ->
|
2019-12-12 07:06:01 -08:00
|
|
|
let (record, label, expected_type) =
|
|
|
|
type_label_access env srecord lid in
|
|
|
|
let ty_record =
|
|
|
|
if expected_type = None then newvar () else record.exp_type in
|
2012-10-24 05:03:00 -07:00
|
|
|
let (label_loc, label, newval) =
|
2012-11-08 01:39:23 -08:00
|
|
|
type_label_exp false env loc ty_record (lid, label, snewval) in
|
|
|
|
unify_exp env record ty_record;
|
1995-05-04 03:15:53 -07:00
|
|
|
if label.lbl_mut = Immutable then
|
2012-08-21 00:10:35 -07:00
|
|
|
raise(Error(loc, env, Label_not_mutable lid.txt));
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
2012-10-24 05:03:00 -07:00
|
|
|
exp_desc = Texp_setfield(record, label_loc, label, newval);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2018-07-24 08:17:53 -07:00
|
|
|
exp_type = instance Predef.type_unit;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pexp_array(sargl) ->
|
2010-11-11 19:09:11 -08:00
|
|
|
let ty = newgenvar() in
|
|
|
|
let to_unify = Predef.type_array ty in
|
2017-12-02 04:44:54 -08:00
|
|
|
with_explanation (fun () ->
|
2020-07-28 17:10:17 -07:00
|
|
|
unify_exp_types loc env to_unify (generic_instance ty_expected));
|
2017-12-02 04:44:54 -08:00
|
|
|
let argl =
|
|
|
|
List.map (fun sarg -> type_expect env sarg (mk_expected ty)) sargl in
|
2003-04-01 17:32:09 -08:00
|
|
|
re {
|
|
|
|
exp_desc = Texp_array argl;
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2018-02-12 08:37:35 -08:00
|
|
|
exp_type = instance ty_expected;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pexp_ifthenelse(scond, sifso, sifnot) ->
|
2017-12-02 04:44:54 -08:00
|
|
|
let cond = type_expect env scond
|
|
|
|
(mk_expected ~explanation:If_conditional Predef.type_bool) in
|
1995-05-04 03:15:53 -07:00
|
|
|
begin match sifnot with
|
|
|
|
None ->
|
2017-12-02 04:44:54 -08:00
|
|
|
let ifso = type_expect env sifso
|
|
|
|
(mk_expected ~explanation:If_no_else_branch Predef.type_unit) in
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
2003-04-01 17:32:09 -08:00
|
|
|
exp_desc = Texp_ifthenelse(cond, ifso, None);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2010-11-11 19:09:11 -08:00
|
|
|
exp_type = ifso.exp_type;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
1996-05-31 05:30:26 -07:00
|
|
|
| Some sifnot ->
|
2017-12-02 04:44:54 -08:00
|
|
|
let ifso = type_expect env sifso ty_expected_explained in
|
|
|
|
let ifnot = type_expect env sifnot ty_expected_explained in
|
2011-02-23 22:29:26 -08:00
|
|
|
(* Keep sharing *)
|
|
|
|
unify_exp env ifnot ifso.exp_type;
|
2003-04-01 17:32:09 -08:00
|
|
|
re {
|
|
|
|
exp_desc = Texp_ifthenelse(cond, ifso, Some ifnot);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_type = ifso.exp_type;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
1995-05-04 03:15:53 -07:00
|
|
|
end
|
|
|
|
| Pexp_sequence(sexp1, sexp2) ->
|
2017-12-02 04:44:54 -08:00
|
|
|
let exp1 = type_statement ~explanation:Sequence_left_hand_side
|
|
|
|
env sexp1 in
|
|
|
|
let exp2 = type_expect env sexp2 ty_expected_explained in
|
2003-04-01 17:32:09 -08:00
|
|
|
re {
|
|
|
|
exp_desc = Texp_sequence(exp1, exp2);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_type = exp2.exp_type;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pexp_while(scond, sbody) ->
|
2017-12-02 04:44:54 -08:00
|
|
|
let cond = type_expect env scond
|
|
|
|
(mk_expected ~explanation:While_loop_conditional Predef.type_bool) in
|
|
|
|
let body = type_statement ~explanation:While_loop_body env sbody in
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
2003-04-01 17:32:09 -08:00
|
|
|
exp_desc = Texp_while(cond, body);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2018-07-24 08:17:53 -07:00
|
|
|
exp_type = instance Predef.type_unit;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
1995-05-04 03:15:53 -07:00
|
|
|
| Pexp_for(param, slow, shigh, dir, sbody) ->
|
2017-12-02 04:44:54 -08:00
|
|
|
let low = type_expect env slow
|
|
|
|
(mk_expected ~explanation:For_loop_start_index Predef.type_int) in
|
|
|
|
let high = type_expect env shigh
|
|
|
|
(mk_expected ~explanation:For_loop_stop_index Predef.type_int) in
|
2013-12-02 10:00:18 -08:00
|
|
|
let id, new_env =
|
|
|
|
match param.ppat_desc with
|
2018-08-28 09:07:01 -07:00
|
|
|
| Ppat_any -> Ident.create_local "_for", env
|
2013-12-02 10:00:18 -08:00
|
|
|
| Ppat_var {txt} ->
|
2019-08-20 01:53:05 -07:00
|
|
|
Env.enter_value txt
|
|
|
|
{val_type = instance Predef.type_int;
|
|
|
|
val_attributes = [];
|
|
|
|
val_kind = Val_reg;
|
|
|
|
val_loc = loc;
|
|
|
|
val_uid = Uid.mk ~current_unit:(Env.get_unit_name ());
|
|
|
|
} env
|
2013-12-02 10:00:18 -08:00
|
|
|
~check:(fun s -> Warnings.Unused_for_index s)
|
|
|
|
| _ ->
|
|
|
|
raise (Error (param.ppat_loc, env, Invalid_for_loop_index))
|
2011-12-29 09:49:58 -08:00
|
|
|
in
|
2017-12-02 04:44:54 -08:00
|
|
|
let body = type_statement ~explanation:For_loop_body new_env sbody in
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_desc = Texp_for(id, param, low, high, dir, body);
|
|
|
|
exp_loc = loc; exp_extra = [];
|
2018-07-24 08:17:53 -07:00
|
|
|
exp_type = instance Predef.type_unit;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
2013-04-17 05:23:44 -07:00
|
|
|
| Pexp_constraint (sarg, sty) ->
|
2018-07-04 07:18:24 -07:00
|
|
|
(* Pretend separate = true, 1% slowdown for lablgtk *)
|
|
|
|
begin_def ();
|
2013-04-17 05:23:44 -07:00
|
|
|
let cty = Typetexp.transl_simple_type env false sty in
|
|
|
|
let ty = cty.ctyp_type in
|
2018-07-04 07:18:24 -07:00
|
|
|
end_def ();
|
|
|
|
generalize_structure ty;
|
|
|
|
let (arg, ty') = (type_argument env sarg ty (instance ty), instance ty) in
|
2013-04-17 05:23:44 -07:00
|
|
|
rue {
|
|
|
|
exp_desc = arg.exp_desc;
|
|
|
|
exp_loc = arg.exp_loc;
|
|
|
|
exp_type = ty';
|
|
|
|
exp_attributes = arg.exp_attributes;
|
|
|
|
exp_env = env;
|
2014-01-22 17:58:37 -08:00
|
|
|
exp_extra =
|
|
|
|
(Texp_constraint cty, loc, sexp.pexp_attributes) :: arg.exp_extra;
|
2013-04-17 05:23:44 -07:00
|
|
|
}
|
|
|
|
| Pexp_coerce(sarg, sty, sty') ->
|
2018-07-04 07:18:24 -07:00
|
|
|
(* Pretend separate = true, 1% slowdown for lablgtk *)
|
2016-04-28 07:00:18 -07:00
|
|
|
(* Also see PR#7199 for a problem with the following:
|
|
|
|
let separate = !Clflags.principal || Env.has_local_constraints env in*)
|
2012-05-30 07:52:37 -07:00
|
|
|
let (arg, ty',cty,cty') =
|
2013-04-17 05:23:44 -07:00
|
|
|
match sty with
|
|
|
|
| None ->
|
2020-07-09 03:20:37 -07:00
|
|
|
let (cty', ty', force) =
|
1997-01-20 09:11:47 -08:00
|
|
|
Typetexp.transl_simple_type_delayed env sty'
|
|
|
|
in
|
2018-07-04 07:18:24 -07:00
|
|
|
begin_def ();
|
1996-05-26 06:42:34 -07:00
|
|
|
let arg = type_exp env sarg in
|
2018-07-04 07:18:24 -07:00
|
|
|
end_def ();
|
|
|
|
let tv = newvar () in
|
|
|
|
let gen = generalizable tv.level arg.exp_type in
|
|
|
|
unify_var env tv arg.exp_type;
|
2001-11-05 01:12:59 -08:00
|
|
|
begin match arg.exp_desc, !self_coercion, (repr ty').desc with
|
2012-05-30 07:52:37 -07:00
|
|
|
Texp_ident(_, _, {val_kind=Val_self _}), (path,r) :: _,
|
2001-11-05 01:12:59 -08:00
|
|
|
Tconstr(path',_,_) when Path.same path path' ->
|
2009-05-20 04:52:42 -07:00
|
|
|
(* prerr_endline "self coercion"; *)
|
2009-09-04 09:19:35 -07:00
|
|
|
r := loc :: !r;
|
2001-11-05 01:12:59 -08:00
|
|
|
force ()
|
2009-05-20 04:52:42 -07:00
|
|
|
| _ when free_variables ~env arg.exp_type = []
|
|
|
|
&& free_variables ~env ty' = [] ->
|
2008-07-17 19:48:38 -07:00
|
|
|
if not gen && (* first try a single coercion *)
|
|
|
|
let snap = snapshot () in
|
2016-03-09 02:40:16 -08:00
|
|
|
let ty, _b = enlarge_type env ty' in
|
2008-07-17 19:48:38 -07:00
|
|
|
try
|
|
|
|
force (); Ctype.unify env arg.exp_type ty; true
|
|
|
|
with Unify _ ->
|
|
|
|
backtrack snap; false
|
|
|
|
then ()
|
|
|
|
else begin try
|
|
|
|
let force' = subtype env arg.exp_type ty' in
|
|
|
|
force (); force' ();
|
2016-02-04 17:54:24 -08:00
|
|
|
if not gen && !Clflags.principal then
|
2009-09-04 09:19:35 -07:00
|
|
|
Location.prerr_warning loc
|
2008-07-17 19:48:38 -07:00
|
|
|
(Warnings.Not_principal "this ground coercion");
|
|
|
|
with Subtype (tr1, tr2) ->
|
2009-05-20 04:52:42 -07:00
|
|
|
(* prerr_endline "coercion failed"; *)
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(loc, env, Not_subtype(tr1, tr2)))
|
2008-07-17 19:48:38 -07:00
|
|
|
end;
|
2001-11-05 01:12:59 -08:00
|
|
|
| _ ->
|
2002-05-29 23:24:45 -07:00
|
|
|
let ty, b = enlarge_type env ty' in
|
2001-11-05 01:12:59 -08:00
|
|
|
force ();
|
|
|
|
begin try Ctype.unify env arg.exp_type ty with Unify trace ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(sarg.pexp_loc, env,
|
2002-06-12 02:52:08 -07:00
|
|
|
Coercion_failure(ty', full_expand env ty', trace, b)))
|
2001-11-05 01:12:59 -08:00
|
|
|
end
|
1996-05-26 06:42:34 -07:00
|
|
|
end;
|
2013-04-17 05:23:44 -07:00
|
|
|
(arg, ty', None, cty')
|
|
|
|
| Some sty ->
|
2018-07-04 07:18:24 -07:00
|
|
|
begin_def ();
|
2020-07-09 03:20:37 -07:00
|
|
|
let (cty, ty, force) =
|
1997-01-20 09:11:47 -08:00
|
|
|
Typetexp.transl_simple_type_delayed env sty
|
2020-07-09 03:20:37 -07:00
|
|
|
and (cty', ty', force') =
|
1997-01-20 09:11:47 -08:00
|
|
|
Typetexp.transl_simple_type_delayed env sty'
|
|
|
|
in
|
|
|
|
begin try
|
|
|
|
let force'' = subtype env ty ty' in
|
|
|
|
force (); force' (); force'' ()
|
|
|
|
with Subtype (tr1, tr2) ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(loc, env, Not_subtype(tr1, tr2)))
|
1997-01-20 09:11:47 -08:00
|
|
|
end;
|
2018-07-04 07:18:24 -07:00
|
|
|
end_def ();
|
|
|
|
generalize_structure ty;
|
|
|
|
generalize_structure ty';
|
|
|
|
(type_argument env sarg ty (instance ty),
|
|
|
|
instance ty', Some cty, cty')
|
1996-05-26 06:42:34 -07:00
|
|
|
in
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
2003-04-01 17:32:09 -08:00
|
|
|
exp_desc = arg.exp_desc;
|
1995-05-04 03:15:53 -07:00
|
|
|
exp_loc = arg.exp_loc;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_type = ty';
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = arg.exp_attributes;
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_env = env;
|
2013-04-17 05:23:44 -07:00
|
|
|
exp_extra = (Texp_coerce (cty, cty'), loc, sexp.pexp_attributes) ::
|
|
|
|
arg.exp_extra;
|
2012-05-30 07:52:37 -07:00
|
|
|
}
|
2016-08-29 07:21:38 -07:00
|
|
|
| Pexp_send (e, {txt=met}) ->
|
2002-04-18 00:27:47 -07:00
|
|
|
if !Clflags.principal then begin_def ();
|
1998-06-24 12:22:26 -07:00
|
|
|
let obj = type_exp env e in
|
2014-12-13 06:46:27 -08:00
|
|
|
let obj_meths = ref None in
|
1996-04-22 04:15:41 -07:00
|
|
|
begin try
|
2012-05-30 07:52:37 -07:00
|
|
|
let (meth, exp, typ) =
|
1998-06-24 12:22:26 -07:00
|
|
|
match obj.exp_desc with
|
2016-03-09 02:40:16 -08:00
|
|
|
Texp_ident(_path, _, {val_kind = Val_self (meths, _, _, privty)}) ->
|
2014-12-13 06:46:27 -08:00
|
|
|
obj_meths := Some meths;
|
1998-06-24 12:22:26 -07:00
|
|
|
let (id, typ) =
|
2003-11-25 01:20:45 -08:00
|
|
|
filter_self_method env met Private meths privty
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
2011-09-22 02:05:42 -07:00
|
|
|
if is_Tvar (repr typ) then
|
2009-09-04 09:19:35 -07:00
|
|
|
Location.prerr_warning loc
|
2004-11-30 10:57:04 -08:00
|
|
|
(Warnings.Undeclared_virtual_method met);
|
2012-05-30 07:52:37 -07:00
|
|
|
(Tmeth_val id, None, typ)
|
2016-03-09 02:40:16 -08:00
|
|
|
| Texp_ident(_path, lid, {val_kind = Val_anc (methods, cl_num)}) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let method_id =
|
|
|
|
begin try List.assoc met methods with Not_found ->
|
2014-12-13 06:46:27 -08:00
|
|
|
let valid_methods = List.map fst methods in
|
|
|
|
raise(Error(e.pexp_loc, env,
|
|
|
|
Undefined_inherited_method (met, valid_methods)))
|
1998-06-24 12:22:26 -07:00
|
|
|
end
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
begin match
|
2018-10-12 02:20:21 -07:00
|
|
|
Env.find_value_by_name
|
|
|
|
(Longident.Lident ("selfpat-" ^ cl_num)) env,
|
|
|
|
Env.find_value_by_name
|
|
|
|
(Longident.Lident ("self-" ^cl_num)) env
|
1998-06-24 12:22:26 -07:00
|
|
|
with
|
2018-10-12 02:20:21 -07:00
|
|
|
| (_, ({val_kind = Val_self (meths, _, _, privty)} as desc)),
|
1998-06-24 12:22:26 -07:00
|
|
|
(path, _) ->
|
2014-12-13 06:46:27 -08:00
|
|
|
obj_meths := Some meths;
|
1998-06-24 12:22:26 -07:00
|
|
|
let (_, typ) =
|
2003-11-25 01:20:45 -08:00
|
|
|
filter_self_method env met Private meths privty
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
let method_type = newvar () in
|
2014-12-22 00:45:55 -08:00
|
|
|
let (obj_ty, res_ty) = filter_arrow env method_type Nolabel in
|
1998-06-24 12:22:26 -07:00
|
|
|
unify env obj_ty desc.val_type;
|
2018-02-12 08:37:35 -08:00
|
|
|
unify env res_ty (instance typ);
|
2018-02-08 09:51:47 -08:00
|
|
|
let method_desc =
|
|
|
|
{val_type = method_type;
|
|
|
|
val_kind = Val_reg;
|
|
|
|
val_attributes = [];
|
2019-08-20 01:53:05 -07:00
|
|
|
val_loc = Location.none;
|
|
|
|
val_uid = Uid.internal_not_actually_unique;
|
|
|
|
}
|
2018-02-08 09:51:47 -08:00
|
|
|
in
|
|
|
|
let exp_env = Env.add_value method_id method_desc env in
|
2012-07-30 11:04:46 -07:00
|
|
|
let exp =
|
|
|
|
Texp_apply({exp_desc =
|
2018-02-08 09:51:47 -08:00
|
|
|
Texp_ident(Path.Pident method_id,
|
|
|
|
lid, method_desc);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
1998-06-24 12:22:26 -07:00
|
|
|
exp_type = method_type;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = []; (* check *)
|
2018-02-08 09:51:47 -08:00
|
|
|
exp_env = exp_env},
|
2014-12-22 00:45:55 -08:00
|
|
|
[ Nolabel,
|
2012-05-30 07:52:37 -07:00
|
|
|
Some {exp_desc = Texp_ident(path, lid, desc);
|
2012-05-31 01:07:31 -07:00
|
|
|
exp_loc = obj.exp_loc; exp_extra = [];
|
|
|
|
exp_type = desc.val_type;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = []; (* check *)
|
2018-02-08 09:51:47 -08:00
|
|
|
exp_env = exp_env}
|
2015-11-24 15:37:46 -08:00
|
|
|
])
|
2012-07-30 11:04:46 -07:00
|
|
|
in
|
2012-05-31 01:07:31 -07:00
|
|
|
(Tmeth_name met, Some (re {exp_desc = exp;
|
2012-07-30 11:04:46 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
|
|
|
exp_type = typ;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = []; (* check *)
|
2018-02-08 09:51:47 -08:00
|
|
|
exp_env = exp_env}), typ)
|
1998-06-24 12:22:26 -07:00
|
|
|
| _ ->
|
|
|
|
assert false
|
|
|
|
end
|
1996-04-22 04:15:41 -07:00
|
|
|
| _ ->
|
2012-05-30 07:52:37 -07:00
|
|
|
(Tmeth_name met, None,
|
1998-06-24 12:22:26 -07:00
|
|
|
filter_method env met Public obj.exp_type)
|
2002-04-18 00:27:47 -07:00
|
|
|
in
|
|
|
|
if !Clflags.principal then begin
|
|
|
|
end_def ();
|
|
|
|
generalize_structure typ;
|
|
|
|
end;
|
|
|
|
let typ =
|
|
|
|
match repr typ with
|
|
|
|
{desc = Tpoly (ty, [])} ->
|
2018-02-12 08:37:35 -08:00
|
|
|
instance ty
|
2002-04-18 00:27:47 -07:00
|
|
|
| {desc = Tpoly (ty, tl); level = l} ->
|
|
|
|
if !Clflags.principal && l <> generic_level then
|
2009-09-04 09:19:35 -07:00
|
|
|
Location.prerr_warning loc
|
2004-11-30 10:57:04 -08:00
|
|
|
(Warnings.Not_principal "this use of a polymorphic method");
|
2002-04-18 00:27:47 -07:00
|
|
|
snd (instance_poly false tl ty)
|
2011-09-22 02:05:42 -07:00
|
|
|
| {desc = Tvar _} as ty ->
|
2002-04-18 00:27:47 -07:00
|
|
|
let ty' = newvar () in
|
2018-07-24 08:17:53 -07:00
|
|
|
unify env (instance ty) (newty(Tpoly(ty',[])));
|
2002-04-18 00:27:47 -07:00
|
|
|
(* if not !Clflags.nolabels then
|
|
|
|
Location.prerr_warning loc (Warnings.Unknown_method met); *)
|
|
|
|
ty'
|
|
|
|
| _ ->
|
|
|
|
assert false
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_desc = Texp_send(obj, meth, exp);
|
|
|
|
exp_loc = loc; exp_extra = [];
|
2010-11-11 19:09:11 -08:00
|
|
|
exp_type = typ;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
2010-11-11 19:09:11 -08:00
|
|
|
exp_env = env }
|
1996-05-20 09:43:29 -07:00
|
|
|
with Unify _ ->
|
2014-12-13 06:46:27 -08:00
|
|
|
let valid_methods =
|
|
|
|
match !obj_meths with
|
|
|
|
| Some meths ->
|
|
|
|
Some (Meths.fold (fun meth _meth_ty li -> meth::li) !meths [])
|
|
|
|
| None ->
|
|
|
|
match (expand_head env obj.exp_type).desc with
|
|
|
|
| Tobject (fields, _) ->
|
|
|
|
let (fields, _) = Ctype.flatten_fields fields in
|
|
|
|
let collect_fields li (meth, meth_kind, _meth_ty) =
|
|
|
|
if meth_kind = Fpresent then meth::li else li in
|
|
|
|
Some (List.fold_left collect_fields [] fields)
|
|
|
|
| _ -> None
|
|
|
|
in
|
|
|
|
raise(Error(e.pexp_loc, env,
|
|
|
|
Undefined_method (obj.exp_type, met, valid_methods)))
|
1996-04-22 04:15:41 -07:00
|
|
|
end
|
|
|
|
| Pexp_new cl ->
|
2018-10-12 02:20:21 -07:00
|
|
|
let (cl_path, cl_decl) = Env.lookup_class ~loc:cl.loc cl.txt env in
|
2013-09-27 08:04:03 -07:00
|
|
|
begin match cl_decl.cty_new with
|
1997-05-19 08:42:21 -07:00
|
|
|
None ->
|
2012-08-21 00:10:35 -07:00
|
|
|
raise(Error(loc, env, Virtual_class cl.txt))
|
1996-04-22 04:15:41 -07:00
|
|
|
| Some ty ->
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_desc = Texp_new (cl_path, cl, cl_decl);
|
|
|
|
exp_loc = loc; exp_extra = [];
|
2018-07-24 08:17:53 -07:00
|
|
|
exp_type = instance ty;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_env = env }
|
1996-04-22 04:15:41 -07:00
|
|
|
end
|
2018-10-12 02:20:21 -07:00
|
|
|
| Pexp_setinstvar (lab, snewval) -> begin
|
|
|
|
let (path, mut, cl_num, ty) =
|
|
|
|
Env.lookup_instance_variable ~loc lab.txt env
|
|
|
|
in
|
|
|
|
match mut with
|
|
|
|
| Mutable ->
|
|
|
|
let newval =
|
|
|
|
type_expect env snewval (mk_expected (instance ty))
|
|
|
|
in
|
|
|
|
let (path_self, _) =
|
|
|
|
Env.find_value_by_name (Longident.Lident ("self-" ^ cl_num)) env
|
|
|
|
in
|
|
|
|
rue {
|
|
|
|
exp_desc = Texp_setinstvar(path_self, path, lab, newval);
|
|
|
|
exp_loc = loc; exp_extra = [];
|
|
|
|
exp_type = instance Predef.type_unit;
|
|
|
|
exp_attributes = sexp.pexp_attributes;
|
|
|
|
exp_env = env }
|
|
|
|
| _ ->
|
|
|
|
raise(Error(loc, env, Instance_variable_not_mutable lab.txt))
|
|
|
|
end
|
1996-04-22 04:15:41 -07:00
|
|
|
| Pexp_override lst ->
|
2005-08-13 13:59:37 -07:00
|
|
|
let _ =
|
1998-11-12 06:53:46 -08:00
|
|
|
List.fold_right
|
1997-05-19 08:42:21 -07:00
|
|
|
(fun (lab, _) l ->
|
2012-05-30 07:52:37 -07:00
|
|
|
if List.exists (fun l -> l.txt = lab.txt) l then
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(loc, env,
|
2012-05-30 07:52:37 -07:00
|
|
|
Value_multiply_overridden lab.txt));
|
1997-05-19 08:42:21 -07:00
|
|
|
lab::l)
|
|
|
|
lst
|
1998-11-12 06:53:46 -08:00
|
|
|
[] in
|
1998-06-24 12:22:26 -07:00
|
|
|
begin match
|
1997-05-19 08:42:21 -07:00
|
|
|
try
|
2018-10-12 02:20:21 -07:00
|
|
|
Env.find_value_by_name (Longident.Lident "selfpat-*") env,
|
|
|
|
Env.find_value_by_name (Longident.Lident "self-*") env
|
1997-05-19 08:42:21 -07:00
|
|
|
with Not_found ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(loc, env, Outside_class))
|
1998-06-24 12:22:26 -07:00
|
|
|
with
|
2003-11-25 01:20:45 -08:00
|
|
|
(_, {val_type = self_ty; val_kind = Val_self (_, vars, _, _)}),
|
1998-06-24 12:22:26 -07:00
|
|
|
(path_self, _) ->
|
|
|
|
let type_override (lab, snewval) =
|
|
|
|
begin try
|
2012-05-30 07:52:37 -07:00
|
|
|
let (id, _, _, ty) = Vars.find lab.txt !vars in
|
2017-12-02 04:44:54 -08:00
|
|
|
(Path.Pident id, lab,
|
2018-02-12 08:37:35 -08:00
|
|
|
type_expect env snewval (mk_expected (instance ty)))
|
1998-06-24 12:22:26 -07:00
|
|
|
with
|
|
|
|
Not_found ->
|
2014-12-13 06:46:29 -08:00
|
|
|
let vars = Vars.fold (fun var _ li -> var::li) !vars [] in
|
2015-09-11 04:58:31 -07:00
|
|
|
raise(Error(loc, env,
|
|
|
|
Unbound_instance_variable (lab.txt, vars)))
|
1998-06-24 12:22:26 -07:00
|
|
|
end
|
|
|
|
in
|
|
|
|
let modifs = List.map type_override lst in
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
2003-04-01 17:32:09 -08:00
|
|
|
exp_desc = Texp_override(path_self, modifs);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
1998-06-24 12:22:26 -07:00
|
|
|
exp_type = self_ty;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1998-06-24 12:22:26 -07:00
|
|
|
exp_env = env }
|
|
|
|
| _ ->
|
|
|
|
assert false
|
|
|
|
end
|
1998-02-26 04:54:44 -08:00
|
|
|
| Pexp_letmodule(name, smodl, sbody) ->
|
|
|
|
let ty = newvar() in
|
2010-11-11 19:09:11 -08:00
|
|
|
(* remember original level *)
|
|
|
|
begin_def ();
|
2002-08-04 22:58:08 -07:00
|
|
|
let context = Typetexp.narrow () in
|
1998-02-26 04:54:44 -08:00
|
|
|
let modl = !type_module env smodl in
|
2018-05-09 06:14:49 -07:00
|
|
|
Mtype.lower_nongen ty.level modl.mod_type;
|
2018-02-08 09:51:47 -08:00
|
|
|
let pres =
|
|
|
|
match modl.mod_type with
|
|
|
|
| Mty_alias _ -> Mp_absent
|
|
|
|
| _ -> Mp_present
|
|
|
|
in
|
2018-09-14 05:04:49 -07:00
|
|
|
let scope = create_scope () in
|
2019-08-21 04:52:24 -07:00
|
|
|
let md =
|
2019-08-20 01:53:05 -07:00
|
|
|
{ md_type = modl.mod_type; md_attributes = []; md_loc = name.loc;
|
|
|
|
md_uid = Uid.mk ~current_unit:(Env.get_unit_name ()); }
|
2019-08-21 04:52:24 -07:00
|
|
|
in
|
2018-02-08 09:51:47 -08:00
|
|
|
let (id, new_env) =
|
2019-10-09 06:15:37 -07:00
|
|
|
match name.txt with
|
|
|
|
| None -> None, env
|
|
|
|
| Some name ->
|
|
|
|
let id, env = Env.enter_module_declaration ~scope name pres md env in
|
|
|
|
Some id, env
|
2018-02-08 09:51:47 -08:00
|
|
|
in
|
2002-08-04 22:58:08 -07:00
|
|
|
Typetexp.widen context;
|
2018-07-02 07:52:44 -07:00
|
|
|
(* ideally, we should catch Expr_type_clash errors
|
|
|
|
in type_expect triggered by escaping identifiers from the local module
|
|
|
|
and refine them into Scoping_let_module errors
|
|
|
|
*)
|
2017-12-02 04:44:54 -08:00
|
|
|
let body = type_expect new_env sbody ty_expected_explained in
|
2010-11-11 19:09:11 -08:00
|
|
|
(* go back to original level *)
|
|
|
|
end_def ();
|
2018-07-02 07:52:44 -07:00
|
|
|
Ctype.unify_var new_env ty body.exp_type;
|
2017-08-28 17:04:47 -07:00
|
|
|
re {
|
2018-02-08 09:51:47 -08:00
|
|
|
exp_desc = Texp_letmodule(id, name, pres, modl, body);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
1998-02-26 04:54:44 -08:00
|
|
|
exp_type = ty;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
1998-02-26 04:54:44 -08:00
|
|
|
exp_env = env }
|
2015-11-20 01:18:49 -08:00
|
|
|
| Pexp_letexception(cd, sbody) ->
|
|
|
|
let (cd, newenv) = Typedecl.transl_exception env cd in
|
2017-12-02 04:44:54 -08:00
|
|
|
let body = type_expect newenv sbody ty_expected_explained in
|
2015-11-20 01:18:49 -08:00
|
|
|
re {
|
|
|
|
exp_desc = Texp_letexception(cd, body);
|
|
|
|
exp_loc = loc; exp_extra = [];
|
|
|
|
exp_type = body.exp_type;
|
|
|
|
exp_attributes = sexp.pexp_attributes;
|
|
|
|
exp_env = env }
|
|
|
|
|
2000-12-04 07:37:05 -08:00
|
|
|
| Pexp_assert (e) ->
|
2017-12-02 04:44:54 -08:00
|
|
|
let cond = type_expect env e
|
|
|
|
(mk_expected ~explanation:Assert_condition Predef.type_bool) in
|
2013-04-17 02:18:03 -07:00
|
|
|
let exp_type =
|
|
|
|
match cond.exp_desc with
|
2013-04-17 02:46:52 -07:00
|
|
|
| Texp_construct(_, {cstr_name="false"}, _) ->
|
2018-02-12 08:37:35 -08:00
|
|
|
instance ty_expected
|
2013-04-17 02:18:03 -07:00
|
|
|
| _ ->
|
2018-07-24 08:17:53 -07:00
|
|
|
instance Predef.type_unit
|
2013-04-17 02:18:03 -07:00
|
|
|
in
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
2013-04-17 02:18:03 -07:00
|
|
|
exp_desc = Texp_assert cond;
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2013-04-17 02:18:03 -07:00
|
|
|
exp_type;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
2010-11-11 19:09:11 -08:00
|
|
|
exp_env = env;
|
|
|
|
}
|
2008-07-09 06:03:38 -07:00
|
|
|
| Pexp_lazy e ->
|
2010-11-11 19:09:11 -08:00
|
|
|
let ty = newgenvar () in
|
|
|
|
let to_unify = Predef.type_lazy_t ty in
|
2017-12-02 04:44:54 -08:00
|
|
|
with_explanation (fun () ->
|
2020-07-28 17:10:17 -07:00
|
|
|
unify_exp_types loc env to_unify (generic_instance ty_expected));
|
2017-12-02 04:44:54 -08:00
|
|
|
let arg = type_expect env e (mk_expected ty) in
|
2010-11-11 19:09:11 -08:00
|
|
|
re {
|
|
|
|
exp_desc = Texp_lazy arg;
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2018-02-12 08:37:35 -08:00
|
|
|
exp_type = instance ty_expected;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
2010-11-11 19:09:11 -08:00
|
|
|
exp_env = env;
|
|
|
|
}
|
2003-11-25 01:20:45 -08:00
|
|
|
| Pexp_object s ->
|
2009-09-04 09:19:35 -07:00
|
|
|
let desc, sign, meths = !type_object env loc s in
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_desc = Texp_object (desc, (*sign,*) meths);
|
|
|
|
exp_loc = loc; exp_extra = [];
|
2013-09-27 08:04:03 -07:00
|
|
|
exp_type = sign.csig_self;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
2003-11-25 01:20:45 -08:00
|
|
|
exp_env = env;
|
|
|
|
}
|
2010-11-11 19:09:11 -08:00
|
|
|
| Pexp_poly(sbody, sty) ->
|
|
|
|
if !Clflags.principal then begin_def ();
|
2012-05-30 07:52:37 -07:00
|
|
|
let ty, cty =
|
|
|
|
match sty with None -> repr ty_expected, None
|
2010-11-11 19:09:11 -08:00
|
|
|
| Some sty ->
|
2013-04-09 07:10:54 -07:00
|
|
|
let sty = Ast_helper.Typ.force_poly sty in
|
2012-05-30 07:52:37 -07:00
|
|
|
let cty = Typetexp.transl_simple_type env false sty in
|
|
|
|
repr cty.ctyp_type, Some cty
|
2010-11-11 19:09:11 -08:00
|
|
|
in
|
|
|
|
if !Clflags.principal then begin
|
|
|
|
end_def ();
|
|
|
|
generalize_structure ty
|
|
|
|
end;
|
|
|
|
if sty <> None then
|
2017-12-02 04:44:54 -08:00
|
|
|
with_explanation (fun () ->
|
2018-02-12 08:37:35 -08:00
|
|
|
unify_exp_types loc env (instance ty) (instance ty_expected));
|
2012-05-30 07:52:37 -07:00
|
|
|
let exp =
|
2010-11-11 19:09:11 -08:00
|
|
|
match (expand_head env ty).desc with
|
|
|
|
Tpoly (ty', []) ->
|
2017-12-02 04:44:54 -08:00
|
|
|
let exp = type_expect env sbody (mk_expected ty') in
|
2018-02-12 08:37:35 -08:00
|
|
|
{ exp with exp_type = instance ty }
|
2010-11-11 19:09:11 -08:00
|
|
|
| Tpoly (ty', tl) ->
|
|
|
|
(* One more level to generalize locally *)
|
|
|
|
begin_def ();
|
|
|
|
if !Clflags.principal then begin_def ();
|
|
|
|
let vars, ty'' = instance_poly true tl ty' in
|
|
|
|
if !Clflags.principal then begin
|
|
|
|
end_def ();
|
|
|
|
generalize_structure ty''
|
|
|
|
end;
|
2017-12-02 04:44:54 -08:00
|
|
|
let exp = type_expect env sbody (mk_expected ty'') in
|
2010-11-11 19:09:11 -08:00
|
|
|
end_def ();
|
2017-02-27 10:09:09 -08:00
|
|
|
generalize_and_check_univars env "method" exp ty_expected vars;
|
2018-02-12 08:37:35 -08:00
|
|
|
{ exp with exp_type = instance ty }
|
2011-09-22 02:05:42 -07:00
|
|
|
| Tvar _ ->
|
2010-12-13 22:33:06 -08:00
|
|
|
let exp = type_exp env sbody in
|
|
|
|
let exp = {exp with exp_type = newty (Tpoly (exp.exp_type, []))} in
|
|
|
|
unify_exp env exp ty;
|
2012-07-10 01:25:58 -07:00
|
|
|
exp
|
2010-11-11 19:09:11 -08:00
|
|
|
| _ -> assert false
|
2012-05-30 07:52:37 -07:00
|
|
|
in
|
2014-01-22 17:58:37 -08:00
|
|
|
re { exp with exp_extra =
|
|
|
|
(Texp_poly cty, loc, sexp.pexp_attributes) :: exp.exp_extra }
|
2016-08-29 07:21:38 -07:00
|
|
|
| Pexp_newtype({txt=name}, sbody) ->
|
2019-03-25 16:22:21 -07:00
|
|
|
let ty =
|
|
|
|
if Typetexp.valid_tyvar_name name then
|
|
|
|
newvar ~name ()
|
|
|
|
else
|
|
|
|
newvar ()
|
|
|
|
in
|
2012-06-13 01:38:32 -07:00
|
|
|
(* remember original level *)
|
|
|
|
begin_def ();
|
2009-10-06 05:51:42 -07:00
|
|
|
(* Create a fake abstract type declaration for name. *)
|
|
|
|
let decl = {
|
|
|
|
type_params = [];
|
|
|
|
type_arity = 0;
|
|
|
|
type_kind = Type_abstract;
|
|
|
|
type_private = Public;
|
|
|
|
type_manifest = None;
|
|
|
|
type_variance = [];
|
2018-11-04 13:37:46 -08:00
|
|
|
type_separability = [];
|
2018-02-12 08:37:35 -08:00
|
|
|
type_is_newtype = true;
|
2018-09-26 15:14:43 -07:00
|
|
|
type_expansion_scope = Btype.lowest_level;
|
2010-05-21 08:06:01 -07:00
|
|
|
type_loc = loc;
|
2013-09-27 03:54:55 -07:00
|
|
|
type_attributes = [];
|
2019-03-12 01:11:27 -07:00
|
|
|
type_immediate = Unknown;
|
2016-09-01 09:39:32 -07:00
|
|
|
type_unboxed = unboxed_false_default_false;
|
2019-08-20 01:53:05 -07:00
|
|
|
type_uid = Uid.mk ~current_unit:(Env.get_unit_name ());
|
2009-10-06 05:51:42 -07:00
|
|
|
}
|
|
|
|
in
|
2018-09-14 05:04:49 -07:00
|
|
|
let scope = create_scope () in
|
2018-08-28 09:06:45 -07:00
|
|
|
let (id, new_env) = Env.enter_type ~scope name decl env in
|
2009-10-06 05:51:42 -07:00
|
|
|
|
|
|
|
let body = type_exp new_env sbody in
|
2010-11-11 19:09:11 -08:00
|
|
|
(* Replace every instance of this type constructor in the resulting
|
|
|
|
type. *)
|
2009-10-06 05:51:42 -07:00
|
|
|
let seen = Hashtbl.create 8 in
|
|
|
|
let rec replace t =
|
|
|
|
if Hashtbl.mem seen t.id then ()
|
|
|
|
else begin
|
|
|
|
Hashtbl.add seen t.id ();
|
|
|
|
match t.desc with
|
|
|
|
| Tconstr (Path.Pident id', _, _) when id == id' -> link_type t ty
|
|
|
|
| _ -> Btype.iter_type_expr replace t
|
|
|
|
end
|
|
|
|
in
|
|
|
|
let ety = Subst.type_expr Subst.identity body.exp_type in
|
|
|
|
replace ety;
|
2010-11-11 19:09:11 -08:00
|
|
|
(* back to original level *)
|
|
|
|
end_def ();
|
|
|
|
(* lower the levels of the result type *)
|
|
|
|
(* unify_var env ty ety; *)
|
2009-10-06 05:51:42 -07:00
|
|
|
|
|
|
|
(* non-expansive if the body is non-expansive, so we don't introduce
|
|
|
|
any new extra node in the typed AST. *)
|
2012-07-10 01:25:58 -07:00
|
|
|
rue { body with exp_loc = loc; exp_type = ety;
|
2014-01-22 17:58:37 -08:00
|
|
|
exp_extra =
|
|
|
|
(Texp_newtype name, loc, sexp.pexp_attributes) :: body.exp_extra }
|
2010-10-21 16:59:33 -07:00
|
|
|
| Pexp_pack m ->
|
2016-03-09 02:40:16 -08:00
|
|
|
let (p, nl) =
|
2018-02-12 08:37:35 -08:00
|
|
|
match Ctype.expand_head env (instance ty_expected) with
|
2016-03-09 02:40:16 -08:00
|
|
|
{desc = Tpackage (p, nl, _tl)} ->
|
2010-11-11 19:09:11 -08:00
|
|
|
if !Clflags.principal &&
|
|
|
|
(Ctype.expand_head env ty_expected).level < Btype.generic_level
|
|
|
|
then
|
|
|
|
Location.prerr_warning loc
|
|
|
|
(Warnings.Not_principal "this module packing");
|
2016-03-09 02:40:16 -08:00
|
|
|
(p, nl)
|
2011-09-22 02:05:42 -07:00
|
|
|
| {desc = Tvar _} ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise (Error (loc, env, Cannot_infer_signature))
|
2010-11-11 19:09:11 -08:00
|
|
|
| _ ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise (Error (loc, env, Not_a_packed_module ty_expected))
|
2010-11-11 19:09:11 -08:00
|
|
|
in
|
2016-03-09 02:40:16 -08:00
|
|
|
let (modl, tl') = !type_package env m p nl in
|
2010-11-11 19:09:11 -08:00
|
|
|
rue {
|
|
|
|
exp_desc = Texp_pack modl;
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2010-11-11 19:09:11 -08:00
|
|
|
exp_type = newty (Tpackage (p, nl, tl'));
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
2010-11-11 19:09:11 -08:00
|
|
|
exp_env = env }
|
2018-04-08 01:51:15 -07:00
|
|
|
| Pexp_open (od, e) ->
|
|
|
|
let (od, _, newenv) = !type_open_decl env od in
|
2017-12-02 04:44:54 -08:00
|
|
|
let exp = type_expect newenv e ty_expected_explained in
|
2018-04-08 01:51:15 -07:00
|
|
|
rue {
|
|
|
|
exp_desc = Texp_open (od, exp);
|
|
|
|
exp_type = exp.exp_type;
|
|
|
|
exp_loc = loc;
|
|
|
|
exp_extra = [];
|
|
|
|
exp_attributes = sexp.pexp_attributes;
|
|
|
|
exp_env = env;
|
2012-05-30 07:52:37 -07:00
|
|
|
}
|
2018-07-29 14:29:01 -07:00
|
|
|
| Pexp_letop{ let_ = slet; ands = sands; body = sbody } ->
|
|
|
|
let rec loop spat_acc ty_acc sands =
|
|
|
|
match sands with
|
|
|
|
| [] -> spat_acc, ty_acc
|
|
|
|
| { pbop_pat = spat; _} :: rest ->
|
|
|
|
let ty = newvar () in
|
|
|
|
let loc = { slet.pbop_op.loc with Location.loc_ghost = true } in
|
|
|
|
let spat_acc = Ast_helper.Pat.tuple ~loc [spat_acc; spat] in
|
|
|
|
let ty_acc = newty (Ttuple [ty_acc; ty]) in
|
|
|
|
loop spat_acc ty_acc rest
|
|
|
|
in
|
|
|
|
if !Clflags.principal then begin_def ();
|
|
|
|
let let_loc = slet.pbop_op.loc in
|
|
|
|
let op_path, op_desc = type_binding_op_ident env slet.pbop_op in
|
|
|
|
let op_type = instance op_desc.val_type in
|
|
|
|
let spat_params, ty_params = loop slet.pbop_pat (newvar ()) sands in
|
|
|
|
let ty_func_result = newvar () in
|
|
|
|
let ty_func = newty (Tarrow(Nolabel, ty_params, ty_func_result, Cok)) in
|
|
|
|
let ty_result = newvar () in
|
|
|
|
let ty_andops = newvar () in
|
|
|
|
let ty_op =
|
|
|
|
newty (Tarrow(Nolabel, ty_andops,
|
|
|
|
newty (Tarrow(Nolabel, ty_func, ty_result, Cok)), Cok))
|
|
|
|
in
|
|
|
|
begin try
|
|
|
|
unify env op_type ty_op
|
|
|
|
with Unify trace ->
|
|
|
|
raise(Error(let_loc, env, Letop_type_clash(slet.pbop_op.txt, trace)))
|
|
|
|
end;
|
|
|
|
if !Clflags.principal then begin
|
|
|
|
end_def ();
|
|
|
|
generalize_structure ty_andops;
|
|
|
|
generalize_structure ty_params;
|
|
|
|
generalize_structure ty_func_result;
|
|
|
|
generalize_structure ty_result
|
|
|
|
end;
|
|
|
|
let exp, ands = type_andops env slet.pbop_exp sands ty_andops in
|
|
|
|
let scase = Ast_helper.Exp.case spat_params sbody in
|
|
|
|
let cases, partial =
|
2019-09-20 06:23:07 -07:00
|
|
|
type_cases Value env ty_params ty_func_result true loc [scase]
|
2018-07-29 14:29:01 -07:00
|
|
|
in
|
|
|
|
let body =
|
|
|
|
match cases with
|
|
|
|
| [case] -> case
|
|
|
|
| _ -> assert false
|
|
|
|
in
|
|
|
|
let param = name_cases "param" cases in
|
|
|
|
let let_ =
|
|
|
|
{ bop_op_name = slet.pbop_op;
|
|
|
|
bop_op_path = op_path;
|
|
|
|
bop_op_val = op_desc;
|
|
|
|
bop_op_type = op_type;
|
|
|
|
bop_exp = exp;
|
|
|
|
bop_loc = slet.pbop_loc; }
|
|
|
|
in
|
|
|
|
let desc =
|
|
|
|
Texp_letop{let_; ands; param; body; partial}
|
|
|
|
in
|
|
|
|
rue { exp_desc = desc;
|
|
|
|
exp_loc = sexp.pexp_loc;
|
|
|
|
exp_extra = [];
|
|
|
|
exp_type = instance ty_result;
|
|
|
|
exp_env = env;
|
|
|
|
exp_attributes = sexp.pexp_attributes; }
|
|
|
|
|
2016-02-16 04:23:31 -08:00
|
|
|
| Pexp_extension ({ txt = ("ocaml.extension_constructor"
|
|
|
|
|"extension_constructor"); _ },
|
2015-10-30 04:37:07 -07:00
|
|
|
payload) ->
|
|
|
|
begin match payload with
|
|
|
|
| PStr [ { pstr_desc =
|
|
|
|
Pstr_eval ({ pexp_desc = Pexp_construct (lid, None); _ }, _)
|
|
|
|
} ] ->
|
|
|
|
let path =
|
2018-10-12 02:20:21 -07:00
|
|
|
let cd =
|
|
|
|
Env.lookup_constructor Env.Positive ~loc:lid.loc lid.txt env
|
|
|
|
in
|
|
|
|
match cd.cstr_tag with
|
2015-10-30 04:37:07 -07:00
|
|
|
| Cstr_extension (path, _) -> path
|
|
|
|
| _ -> raise (Error (lid.loc, env, Not_an_extension_constructor))
|
|
|
|
in
|
|
|
|
rue {
|
|
|
|
exp_desc = Texp_extension_constructor (lid, path);
|
|
|
|
exp_loc = loc; exp_extra = [];
|
2018-07-24 08:17:53 -07:00
|
|
|
exp_type = instance Predef.type_extension_constructor;
|
2015-10-30 04:37:07 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
|
|
|
exp_env = env }
|
|
|
|
| _ ->
|
|
|
|
raise (Error (loc, env, Invalid_extension_constructor_payload))
|
|
|
|
end
|
2014-05-07 01:26:17 -07:00
|
|
|
| Pexp_extension ext ->
|
2015-12-02 05:46:14 -08:00
|
|
|
raise (Error_forward (Builtin_attributes.error_of_extension ext))
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2015-10-15 17:13:40 -07:00
|
|
|
| Pexp_unreachable ->
|
|
|
|
re { exp_desc = Texp_unreachable;
|
|
|
|
exp_loc = loc; exp_extra = [];
|
2018-02-12 08:37:35 -08:00
|
|
|
exp_type = instance ty_expected;
|
2015-10-15 17:13:40 -07:00
|
|
|
exp_attributes = sexp.pexp_attributes;
|
|
|
|
exp_env = env }
|
|
|
|
|
2018-07-29 14:29:01 -07:00
|
|
|
and type_ident env ?(recarg=Rejected) lid =
|
2018-10-12 02:20:21 -07:00
|
|
|
let (path, desc) = Env.lookup_value ~loc:lid.loc lid.txt env in
|
2018-07-29 14:29:01 -07:00
|
|
|
let is_recarg =
|
|
|
|
match (repr desc.val_type).desc with
|
|
|
|
| Tconstr(p, _, _) -> Path.is_constructor_typath p
|
|
|
|
| _ -> false
|
|
|
|
in
|
|
|
|
begin match is_recarg, recarg, (repr desc.val_type).desc with
|
|
|
|
| _, Allowed, _
|
|
|
|
| true, Required, _
|
|
|
|
| false, Rejected, _ -> ()
|
|
|
|
| true, Rejected, _
|
|
|
|
| false, Required, (Tvar _ | Tconstr _) ->
|
|
|
|
raise (Error (lid.loc, env, Inlined_record_escape))
|
|
|
|
| false, Required, _ -> () (* will fail later *)
|
|
|
|
end;
|
|
|
|
path, desc
|
|
|
|
|
|
|
|
and type_binding_op_ident env s =
|
|
|
|
let loc = s.loc in
|
|
|
|
let lid = Location.mkloc (Longident.Lident s.txt) loc in
|
|
|
|
let path, desc = type_ident env lid in
|
|
|
|
let path =
|
|
|
|
match desc.val_kind with
|
2018-10-12 02:20:21 -07:00
|
|
|
| Val_ivar _ ->
|
2018-07-29 14:29:01 -07:00
|
|
|
fatal_error "Illegal name for instance variable"
|
|
|
|
| Val_self (_, _, cl_num, _) ->
|
|
|
|
let path, _ =
|
2018-10-12 02:20:21 -07:00
|
|
|
Env.find_value_by_name (Longident.Lident ("self-" ^ cl_num)) env
|
2018-07-29 14:29:01 -07:00
|
|
|
in
|
|
|
|
path
|
|
|
|
| _ -> path
|
|
|
|
in
|
|
|
|
path, desc
|
|
|
|
|
2017-12-02 04:44:54 -08:00
|
|
|
and type_function ?in_function loc attrs env ty_expected_explained l caselist =
|
|
|
|
let { ty = ty_expected; explanation } = ty_expected_explained in
|
2013-04-17 04:43:29 -07:00
|
|
|
let (loc_fun, ty_fun) =
|
|
|
|
match in_function with Some p -> p
|
2018-02-12 08:37:35 -08:00
|
|
|
| None -> (loc, instance ty_expected)
|
2013-04-17 04:43:29 -07:00
|
|
|
in
|
|
|
|
let separate = !Clflags.principal || Env.has_local_constraints env in
|
|
|
|
if separate then begin_def ();
|
|
|
|
let (ty_arg, ty_res) =
|
2018-02-12 08:37:35 -08:00
|
|
|
try filter_arrow env (instance ty_expected) l
|
2013-04-17 04:43:29 -07:00
|
|
|
with Unify _ ->
|
|
|
|
match expand_head env ty_expected with
|
|
|
|
{desc = Tarrow _} as ty ->
|
2017-12-02 04:44:54 -08:00
|
|
|
raise(Error(loc, env, Abstract_wrong_label(l, ty, explanation)))
|
2013-04-17 04:43:29 -07:00
|
|
|
| _ ->
|
|
|
|
raise(Error(loc_fun, env,
|
2017-12-02 04:44:54 -08:00
|
|
|
Too_many_arguments (in_function <> None,
|
|
|
|
ty_fun,
|
|
|
|
explanation)))
|
2013-04-17 04:43:29 -07:00
|
|
|
in
|
|
|
|
let ty_arg =
|
|
|
|
if is_optional l then
|
|
|
|
let tv = newvar() in
|
|
|
|
begin
|
|
|
|
try unify env ty_arg (type_option tv)
|
|
|
|
with Unify _ -> assert false
|
|
|
|
end;
|
|
|
|
type_option tv
|
|
|
|
else ty_arg
|
|
|
|
in
|
|
|
|
if separate then begin
|
|
|
|
end_def ();
|
|
|
|
generalize_structure ty_arg;
|
|
|
|
generalize_structure ty_res
|
|
|
|
end;
|
|
|
|
let cases, partial =
|
2019-09-20 06:23:07 -07:00
|
|
|
type_cases Value ~in_function:(loc_fun,ty_fun) env ty_arg ty_res
|
2013-04-17 04:43:29 -07:00
|
|
|
true loc caselist in
|
2020-07-20 09:56:40 -07:00
|
|
|
let not_nolabel_function ty =
|
2013-04-17 04:43:29 -07:00
|
|
|
let ls, tvar = list_labels env ty in
|
2020-07-20 09:56:40 -07:00
|
|
|
List.for_all ((<>) Nolabel) ls && not tvar
|
2013-04-17 04:43:29 -07:00
|
|
|
in
|
2020-07-20 09:56:40 -07:00
|
|
|
if is_optional l && not_nolabel_function ty_res then
|
2013-04-17 04:43:29 -07:00
|
|
|
Location.prerr_warning (List.hd cases).c_lhs.pat_loc
|
|
|
|
Warnings.Unerasable_optional_argument;
|
2018-01-15 03:11:06 -08:00
|
|
|
let param = name_cases "param" cases in
|
2013-04-17 04:43:29 -07:00
|
|
|
re {
|
2016-10-14 01:34:50 -07:00
|
|
|
exp_desc = Texp_function { arg_label = l; param; cases; partial; };
|
2013-04-17 04:43:29 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2018-02-12 08:37:35 -08:00
|
|
|
exp_type = instance (newgenty (Tarrow(l, ty_arg, ty_res, Cok)));
|
2013-04-17 04:43:29 -07:00
|
|
|
exp_attributes = attrs;
|
|
|
|
exp_env = env }
|
|
|
|
|
|
|
|
|
2016-03-09 02:40:16 -08:00
|
|
|
and type_label_access env srecord lid =
|
2012-10-29 00:54:06 -07:00
|
|
|
if !Clflags.principal then begin_def ();
|
2014-10-14 08:51:30 -07:00
|
|
|
let record = type_exp ~recarg:Allowed env srecord in
|
2012-10-29 00:54:06 -07:00
|
|
|
if !Clflags.principal then begin
|
|
|
|
end_def ();
|
|
|
|
generalize_structure record.exp_type
|
|
|
|
end;
|
|
|
|
let ty_exp = record.exp_type in
|
2019-12-12 07:06:01 -08:00
|
|
|
let expected_type =
|
2012-10-29 00:54:06 -07:00
|
|
|
try
|
2012-11-10 19:46:59 -08:00
|
|
|
let (p0, p,_) = extract_concrete_record env ty_exp in
|
2018-03-15 03:25:59 -07:00
|
|
|
Some(p0, p, (repr ty_exp).level = generic_level || not !Clflags.principal)
|
2012-10-29 00:54:06 -07:00
|
|
|
with Not_found -> None
|
|
|
|
in
|
2018-10-12 02:20:21 -07:00
|
|
|
let labels = Env.lookup_all_labels ~loc:lid.loc lid.txt env in
|
2013-09-19 00:25:51 -07:00
|
|
|
let label =
|
2017-12-02 04:44:54 -08:00
|
|
|
wrap_disambiguate "This expression has" (mk_expected ty_exp)
|
2019-12-12 07:06:01 -08:00
|
|
|
(Label.disambiguate () lid env expected_type) labels in
|
|
|
|
(record, label, expected_type)
|
2012-09-21 00:01:44 -07:00
|
|
|
|
2014-05-12 08:37:29 -07:00
|
|
|
(* Typing format strings for printing or reading.
|
|
|
|
These formats are used by functions in modules Printf, Format, and Scanf.
|
|
|
|
(Handling of * modifiers contributed by Thorsten Ohl.) *)
|
|
|
|
|
2014-05-12 08:38:07 -07:00
|
|
|
and type_format loc str env =
|
2014-06-09 06:53:45 -07:00
|
|
|
let loc = {loc with Location.loc_ghost = true} in
|
2014-05-12 08:37:29 -07:00
|
|
|
try
|
|
|
|
CamlinternalFormatBasics.(CamlinternalFormat.(
|
|
|
|
let mk_exp_loc pexp_desc = {
|
|
|
|
pexp_desc = pexp_desc;
|
2014-06-09 06:53:45 -07:00
|
|
|
pexp_loc = loc;
|
2018-08-02 04:25:49 -07:00
|
|
|
pexp_loc_stack = [];
|
2014-05-12 08:37:33 -07:00
|
|
|
pexp_attributes = [];
|
2014-05-12 08:37:29 -07:00
|
|
|
} and mk_lid_loc lid = {
|
|
|
|
txt = lid;
|
2014-06-09 06:53:45 -07:00
|
|
|
loc = loc;
|
2014-05-12 08:37:29 -07:00
|
|
|
} in
|
|
|
|
let mk_constr name args =
|
2014-06-09 06:53:45 -07:00
|
|
|
let lid = Longident.(Ldot(Lident "CamlinternalFormatBasics", name)) in
|
2014-05-12 08:37:29 -07:00
|
|
|
let arg = match args with
|
|
|
|
| [] -> None
|
|
|
|
| [ e ] -> Some e
|
|
|
|
| _ :: _ :: _ -> Some (mk_exp_loc (Pexp_tuple args)) in
|
2014-05-12 08:37:31 -07:00
|
|
|
mk_exp_loc (Pexp_construct (mk_lid_loc lid, arg)) in
|
2014-05-12 08:37:29 -07:00
|
|
|
let mk_cst cst = mk_exp_loc (Pexp_constant cst) in
|
2018-08-30 10:15:32 -07:00
|
|
|
let mk_int n = mk_cst (Pconst_integer (Int.to_string n, None))
|
2019-07-22 09:04:09 -07:00
|
|
|
and mk_string str = mk_cst (Pconst_string (str, loc, None))
|
2016-01-08 06:48:47 -08:00
|
|
|
and mk_char chr = mk_cst (Pconst_char chr) in
|
2014-06-09 06:53:47 -07:00
|
|
|
let rec mk_formatting_lit fmting = match fmting with
|
2014-05-12 08:37:29 -07:00
|
|
|
| Close_box ->
|
|
|
|
mk_constr "Close_box" []
|
|
|
|
| Close_tag ->
|
|
|
|
mk_constr "Close_tag" []
|
|
|
|
| Break (org, ns, ni) ->
|
|
|
|
mk_constr "Break" [ mk_string org; mk_int ns; mk_int ni ]
|
|
|
|
| FFlush ->
|
|
|
|
mk_constr "FFlush" []
|
|
|
|
| Force_newline ->
|
|
|
|
mk_constr "Force_newline" []
|
|
|
|
| Flush_newline ->
|
|
|
|
mk_constr "Flush_newline" []
|
|
|
|
| Magic_size (org, sz) ->
|
|
|
|
mk_constr "Magic_size" [ mk_string org; mk_int sz ]
|
|
|
|
| Escaped_at ->
|
|
|
|
mk_constr "Escaped_at" []
|
|
|
|
| Escaped_percent ->
|
|
|
|
mk_constr "Escaped_percent" []
|
|
|
|
| Scan_indic c ->
|
|
|
|
mk_constr "Scan_indic" [ mk_char c ]
|
2014-06-09 06:53:47 -07:00
|
|
|
and mk_formatting_gen : type a b c d e f .
|
|
|
|
(a, b, c, d, e, f) formatting_gen -> Parsetree.expression =
|
|
|
|
fun fmting -> match fmting with
|
|
|
|
| Open_tag (Format (fmt', str')) ->
|
|
|
|
mk_constr "Open_tag" [ mk_format fmt' str' ]
|
PR#6418: support "@[<hov %d>" in the new format implementation (Benoît Vaugon)
The bootstrap procedure, as for commit trunk@14973 (see there for
detailed build instructions), requires to first commit a temporary
patch:
> diff -Naur old/typing/typecore.ml new/typing/typecore.ml
> --- old/typing/typecore.ml 2014-06-11 18:16:24.851647309 +0200
> +++ new/typing/typecore.ml 2014-06-11 18:15:50.075646418 +0200
> @@ -2758,16 +2758,9 @@
> let mk_int n = mk_cst (Const_int n)
> and mk_string str = mk_cst (Const_string (str, None))
> and mk_char chr = mk_cst (Const_char chr) in
> - let mk_block_type bty = match bty with
> - | Pp_hbox -> mk_constr "Pp_hbox" []
> - | Pp_vbox -> mk_constr "Pp_vbox" []
> - | Pp_hvbox -> mk_constr "Pp_hvbox" []
> - | Pp_hovbox -> mk_constr "Pp_hovbox" []
> - | Pp_box -> mk_constr "Pp_box" []
> - | Pp_fits -> mk_constr "Pp_fits" [] in
> let rec mk_formatting_lit fmting = match fmting with
> - | Open_box (org, bty, idt) ->
> - mk_constr "Open_box" [ mk_string org; mk_block_type bty; mk_int idt ]
> + | Open_box _ ->
> + assert false
> | Close_box ->
> mk_constr "Close_box" []
> | Close_tag ->
> @@ -2950,6 +2943,19 @@
> mk_constr "Alpha" [ mk_fmt rest ]
> | Theta rest ->
> mk_constr "Theta" [ mk_fmt rest ]
> + | Formatting_lit (Open_box (org, _bty, _idt), rest) ->
> + mk_constr "Formatting_gen" [
> + mk_constr "Open_box" [
> + mk_constr "Format" [
> + mk_constr "String_literal" [
> + mk_string "<>";
> + mk_constr "End_of_format" [];
> + ];
> + mk_string "@[<>";
> + ]
> + ];
> + mk_fmt rest;
> + ]
> | Formatting_lit (fmting, rest) ->
> mk_constr "Formatting_lit" [ mk_formatting_lit fmting; mk_fmt rest ]
> | Formatting_gen (fmting, rest) ->
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@14984 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2014-06-14 14:08:50 -07:00
|
|
|
| Open_box (Format (fmt', str')) ->
|
|
|
|
mk_constr "Open_box" [ mk_format fmt' str' ]
|
2014-06-09 06:53:47 -07:00
|
|
|
and mk_format : type a b c d e f .
|
|
|
|
(a, b, c, d, e, f) CamlinternalFormatBasics.fmt -> string ->
|
|
|
|
Parsetree.expression = fun fmt str ->
|
|
|
|
mk_constr "Format" [ mk_fmt fmt; mk_string str ]
|
2014-05-12 08:37:29 -07:00
|
|
|
and mk_side side = match side with
|
|
|
|
| Left -> mk_constr "Left" []
|
|
|
|
| Right -> mk_constr "Right" []
|
|
|
|
| Zeros -> mk_constr "Zeros" []
|
|
|
|
and mk_iconv iconv = match iconv with
|
|
|
|
| Int_d -> mk_constr "Int_d" [] | Int_pd -> mk_constr "Int_pd" []
|
|
|
|
| Int_sd -> mk_constr "Int_sd" [] | Int_i -> mk_constr "Int_i" []
|
|
|
|
| Int_pi -> mk_constr "Int_pi" [] | Int_si -> mk_constr "Int_si" []
|
|
|
|
| Int_x -> mk_constr "Int_x" [] | Int_Cx -> mk_constr "Int_Cx" []
|
|
|
|
| Int_X -> mk_constr "Int_X" [] | Int_CX -> mk_constr "Int_CX" []
|
|
|
|
| Int_o -> mk_constr "Int_o" [] | Int_Co -> mk_constr "Int_Co" []
|
2017-05-25 00:34:51 -07:00
|
|
|
| Int_u -> mk_constr "Int_u" [] | Int_Cd -> mk_constr "Int_Cd" []
|
|
|
|
| Int_Ci -> mk_constr "Int_Ci" [] | Int_Cu -> mk_constr "Int_Cu" []
|
2019-02-23 06:35:39 -08:00
|
|
|
and mk_fconv fconv =
|
|
|
|
let flag = match fst fconv with
|
|
|
|
| Float_flag_ -> mk_constr "Float_flag_" []
|
|
|
|
| Float_flag_p -> mk_constr "Float_flag_p" []
|
|
|
|
| Float_flag_s -> mk_constr "Float_flag_s" [] in
|
|
|
|
let kind = match snd fconv with
|
2014-05-12 08:37:29 -07:00
|
|
|
| Float_f -> mk_constr "Float_f" []
|
|
|
|
| Float_e -> mk_constr "Float_e" []
|
|
|
|
| Float_E -> mk_constr "Float_E" []
|
|
|
|
| Float_g -> mk_constr "Float_g" []
|
|
|
|
| Float_G -> mk_constr "Float_G" []
|
2015-07-26 02:29:33 -07:00
|
|
|
| Float_h -> mk_constr "Float_h" []
|
|
|
|
| Float_H -> mk_constr "Float_H" []
|
2019-04-29 06:23:45 -07:00
|
|
|
| Float_F -> mk_constr "Float_F" []
|
|
|
|
| Float_CF -> mk_constr "Float_CF" [] in
|
2019-02-23 06:35:39 -08:00
|
|
|
mk_exp_loc (Pexp_tuple [flag; kind])
|
2014-05-12 08:37:29 -07:00
|
|
|
and mk_counter cnt = match cnt with
|
|
|
|
| Line_counter -> mk_constr "Line_counter" []
|
|
|
|
| Char_counter -> mk_constr "Char_counter" []
|
|
|
|
| Token_counter -> mk_constr "Token_counter" []
|
|
|
|
and mk_int_opt n_opt = match n_opt with
|
|
|
|
| None ->
|
|
|
|
let lid_loc = mk_lid_loc (Longident.Lident "None") in
|
2014-05-12 08:37:31 -07:00
|
|
|
mk_exp_loc (Pexp_construct (lid_loc, None))
|
2014-05-12 08:37:29 -07:00
|
|
|
| Some n ->
|
|
|
|
let lid_loc = mk_lid_loc (Longident.Lident "Some") in
|
2014-06-09 06:53:47 -07:00
|
|
|
mk_exp_loc (Pexp_construct (lid_loc, Some (mk_int n)))
|
|
|
|
and mk_fmtty : type a b c d e f g h i j k l .
|
2015-09-11 04:58:31 -07:00
|
|
|
(a, b, c, d, e, f, g, h, i, j, k, l) fmtty_rel -> Parsetree.expression
|
|
|
|
=
|
2014-05-12 08:37:29 -07:00
|
|
|
fun fmtty -> match fmtty with
|
|
|
|
| Char_ty rest -> mk_constr "Char_ty" [ mk_fmtty rest ]
|
|
|
|
| String_ty rest -> mk_constr "String_ty" [ mk_fmtty rest ]
|
|
|
|
| Int_ty rest -> mk_constr "Int_ty" [ mk_fmtty rest ]
|
|
|
|
| Int32_ty rest -> mk_constr "Int32_ty" [ mk_fmtty rest ]
|
|
|
|
| Nativeint_ty rest -> mk_constr "Nativeint_ty" [ mk_fmtty rest ]
|
|
|
|
| Int64_ty rest -> mk_constr "Int64_ty" [ mk_fmtty rest ]
|
|
|
|
| Float_ty rest -> mk_constr "Float_ty" [ mk_fmtty rest ]
|
|
|
|
| Bool_ty rest -> mk_constr "Bool_ty" [ mk_fmtty rest ]
|
|
|
|
| Alpha_ty rest -> mk_constr "Alpha_ty" [ mk_fmtty rest ]
|
|
|
|
| Theta_ty rest -> mk_constr "Theta_ty" [ mk_fmtty rest ]
|
2015-03-08 01:36:10 -08:00
|
|
|
| Any_ty rest -> mk_constr "Any_ty" [ mk_fmtty rest ]
|
2014-05-12 08:37:29 -07:00
|
|
|
| Reader_ty rest -> mk_constr "Reader_ty" [ mk_fmtty rest ]
|
|
|
|
| Ignored_reader_ty rest ->
|
|
|
|
mk_constr "Ignored_reader_ty" [ mk_fmtty rest ]
|
|
|
|
| Format_arg_ty (sub_fmtty, rest) ->
|
|
|
|
mk_constr "Format_arg_ty" [ mk_fmtty sub_fmtty; mk_fmtty rest ]
|
2014-05-21 06:23:13 -07:00
|
|
|
| Format_subst_ty (sub_fmtty1, sub_fmtty2, rest) ->
|
2014-05-12 08:37:29 -07:00
|
|
|
mk_constr "Format_subst_ty"
|
2014-05-21 06:23:13 -07:00
|
|
|
[ mk_fmtty sub_fmtty1; mk_fmtty sub_fmtty2; mk_fmtty rest ]
|
2014-05-12 08:37:29 -07:00
|
|
|
| End_of_fmtty -> mk_constr "End_of_fmtty" []
|
2014-06-09 06:53:47 -07:00
|
|
|
and mk_ignored : type a b c d e f .
|
2014-05-12 08:37:29 -07:00
|
|
|
(a, b, c, d, e, f) ignored -> Parsetree.expression =
|
|
|
|
fun ign -> match ign with
|
|
|
|
| Ignored_char ->
|
|
|
|
mk_constr "Ignored_char" []
|
|
|
|
| Ignored_caml_char ->
|
|
|
|
mk_constr "Ignored_caml_char" []
|
|
|
|
| Ignored_string pad_opt ->
|
|
|
|
mk_constr "Ignored_string" [ mk_int_opt pad_opt ]
|
|
|
|
| Ignored_caml_string pad_opt ->
|
|
|
|
mk_constr "Ignored_caml_string" [ mk_int_opt pad_opt ]
|
|
|
|
| Ignored_int (iconv, pad_opt) ->
|
|
|
|
mk_constr "Ignored_int" [ mk_iconv iconv; mk_int_opt pad_opt ]
|
|
|
|
| Ignored_int32 (iconv, pad_opt) ->
|
|
|
|
mk_constr "Ignored_int32" [ mk_iconv iconv; mk_int_opt pad_opt ]
|
|
|
|
| Ignored_nativeint (iconv, pad_opt) ->
|
|
|
|
mk_constr "Ignored_nativeint" [ mk_iconv iconv; mk_int_opt pad_opt ]
|
|
|
|
| Ignored_int64 (iconv, pad_opt) ->
|
|
|
|
mk_constr "Ignored_int64" [ mk_iconv iconv; mk_int_opt pad_opt ]
|
|
|
|
| Ignored_float (pad_opt, prec_opt) ->
|
|
|
|
mk_constr "Ignored_float" [ mk_int_opt pad_opt; mk_int_opt prec_opt ]
|
2017-09-01 05:22:00 -07:00
|
|
|
| Ignored_bool pad_opt ->
|
|
|
|
mk_constr "Ignored_bool" [ mk_int_opt pad_opt ]
|
2014-05-12 08:37:29 -07:00
|
|
|
| Ignored_format_arg (pad_opt, fmtty) ->
|
|
|
|
mk_constr "Ignored_format_arg" [ mk_int_opt pad_opt; mk_fmtty fmtty ]
|
|
|
|
| Ignored_format_subst (pad_opt, fmtty) ->
|
|
|
|
mk_constr "Ignored_format_subst" [
|
|
|
|
mk_int_opt pad_opt; mk_fmtty fmtty ]
|
|
|
|
| Ignored_reader ->
|
|
|
|
mk_constr "Ignored_reader" []
|
|
|
|
| Ignored_scan_char_set (width_opt, char_set) ->
|
|
|
|
mk_constr "Ignored_scan_char_set" [
|
2014-05-12 08:37:59 -07:00
|
|
|
mk_int_opt width_opt; mk_string char_set ]
|
|
|
|
| Ignored_scan_get_counter counter ->
|
|
|
|
mk_constr "Ignored_scan_get_counter" [
|
|
|
|
mk_counter counter
|
|
|
|
]
|
2014-10-15 06:34:58 -07:00
|
|
|
| Ignored_scan_next_char ->
|
|
|
|
mk_constr "Ignored_scan_next_char" []
|
2014-06-09 06:53:47 -07:00
|
|
|
and mk_padding : type x y . (x, y) padding -> Parsetree.expression =
|
2014-05-12 08:37:29 -07:00
|
|
|
fun pad -> match pad with
|
|
|
|
| No_padding -> mk_constr "No_padding" []
|
|
|
|
| Lit_padding (s, w) -> mk_constr "Lit_padding" [ mk_side s; mk_int w ]
|
|
|
|
| Arg_padding s -> mk_constr "Arg_padding" [ mk_side s ]
|
|
|
|
and mk_precision : type x y . (x, y) precision -> Parsetree.expression =
|
|
|
|
fun prec -> match prec with
|
|
|
|
| No_precision -> mk_constr "No_precision" []
|
|
|
|
| Lit_precision w -> mk_constr "Lit_precision" [ mk_int w ]
|
2014-06-09 06:53:47 -07:00
|
|
|
| Arg_precision -> mk_constr "Arg_precision" []
|
|
|
|
and mk_fmt : type a b c d e f .
|
2014-05-12 08:37:29 -07:00
|
|
|
(a, b, c, d, e, f) fmt -> Parsetree.expression =
|
|
|
|
fun fmt -> match fmt with
|
|
|
|
| Char rest ->
|
|
|
|
mk_constr "Char" [ mk_fmt rest ]
|
|
|
|
| Caml_char rest ->
|
|
|
|
mk_constr "Caml_char" [ mk_fmt rest ]
|
|
|
|
| String (pad, rest) ->
|
|
|
|
mk_constr "String" [ mk_padding pad; mk_fmt rest ]
|
|
|
|
| Caml_string (pad, rest) ->
|
|
|
|
mk_constr "Caml_string" [ mk_padding pad; mk_fmt rest ]
|
|
|
|
| Int (iconv, pad, prec, rest) ->
|
|
|
|
mk_constr "Int" [
|
|
|
|
mk_iconv iconv; mk_padding pad; mk_precision prec; mk_fmt rest ]
|
|
|
|
| Int32 (iconv, pad, prec, rest) ->
|
|
|
|
mk_constr "Int32" [
|
|
|
|
mk_iconv iconv; mk_padding pad; mk_precision prec; mk_fmt rest ]
|
|
|
|
| Nativeint (iconv, pad, prec, rest) ->
|
|
|
|
mk_constr "Nativeint" [
|
|
|
|
mk_iconv iconv; mk_padding pad; mk_precision prec; mk_fmt rest ]
|
|
|
|
| Int64 (iconv, pad, prec, rest) ->
|
|
|
|
mk_constr "Int64" [
|
|
|
|
mk_iconv iconv; mk_padding pad; mk_precision prec; mk_fmt rest ]
|
|
|
|
| Float (fconv, pad, prec, rest) ->
|
|
|
|
mk_constr "Float" [
|
|
|
|
mk_fconv fconv; mk_padding pad; mk_precision prec; mk_fmt rest ]
|
2017-09-01 05:22:00 -07:00
|
|
|
| Bool (pad, rest) ->
|
|
|
|
mk_constr "Bool" [ mk_padding pad; mk_fmt rest ]
|
2014-05-12 08:37:29 -07:00
|
|
|
| Flush rest ->
|
|
|
|
mk_constr "Flush" [ mk_fmt rest ]
|
|
|
|
| String_literal (s, rest) ->
|
|
|
|
mk_constr "String_literal" [ mk_string s; mk_fmt rest ]
|
|
|
|
| Char_literal (c, rest) ->
|
|
|
|
mk_constr "Char_literal" [ mk_char c; mk_fmt rest ]
|
|
|
|
| Format_arg (pad_opt, fmtty, rest) ->
|
|
|
|
mk_constr "Format_arg" [
|
|
|
|
mk_int_opt pad_opt; mk_fmtty fmtty; mk_fmt rest ]
|
2014-05-21 06:23:13 -07:00
|
|
|
| Format_subst (pad_opt, fmtty, rest) ->
|
2014-05-12 08:37:29 -07:00
|
|
|
mk_constr "Format_subst" [
|
2014-05-21 06:23:13 -07:00
|
|
|
mk_int_opt pad_opt; mk_fmtty fmtty; mk_fmt rest ]
|
2014-05-12 08:37:29 -07:00
|
|
|
| Alpha rest ->
|
|
|
|
mk_constr "Alpha" [ mk_fmt rest ]
|
|
|
|
| Theta rest ->
|
|
|
|
mk_constr "Theta" [ mk_fmt rest ]
|
2014-06-09 06:53:47 -07:00
|
|
|
| Formatting_lit (fmting, rest) ->
|
|
|
|
mk_constr "Formatting_lit" [ mk_formatting_lit fmting; mk_fmt rest ]
|
|
|
|
| Formatting_gen (fmting, rest) ->
|
|
|
|
mk_constr "Formatting_gen" [ mk_formatting_gen fmting; mk_fmt rest ]
|
2014-05-12 08:37:29 -07:00
|
|
|
| Reader rest ->
|
|
|
|
mk_constr "Reader" [ mk_fmt rest ]
|
|
|
|
| Scan_char_set (width_opt, char_set, rest) ->
|
|
|
|
mk_constr "Scan_char_set" [
|
|
|
|
mk_int_opt width_opt; mk_string char_set; mk_fmt rest ]
|
|
|
|
| Scan_get_counter (cnt, rest) ->
|
|
|
|
mk_constr "Scan_get_counter" [ mk_counter cnt; mk_fmt rest ]
|
2014-10-15 06:34:58 -07:00
|
|
|
| Scan_next_char rest ->
|
|
|
|
mk_constr "Scan_next_char" [ mk_fmt rest ]
|
2014-05-12 08:37:29 -07:00
|
|
|
| Ignored_param (ign, rest) ->
|
|
|
|
mk_constr "Ignored_param" [ mk_ignored ign; mk_fmt rest ]
|
|
|
|
| End_of_format ->
|
2014-06-09 06:53:47 -07:00
|
|
|
mk_constr "End_of_format" []
|
2015-03-08 01:36:10 -08:00
|
|
|
| Custom _ ->
|
|
|
|
(* Custom formatters have no syntax so they will never appear
|
|
|
|
in formats parsed from strings. *)
|
|
|
|
assert false
|
2014-06-09 06:53:47 -07:00
|
|
|
in
|
2014-08-29 10:14:00 -07:00
|
|
|
let legacy_behavior = not !Clflags.strict_formats in
|
|
|
|
let Fmt_EBB fmt = fmt_ebb_of_string ~legacy_behavior str in
|
2014-06-09 06:53:45 -07:00
|
|
|
mk_constr "Format" [ mk_fmt fmt; mk_string str ]
|
2014-05-12 08:37:29 -07:00
|
|
|
))
|
|
|
|
with Failure msg ->
|
2014-05-12 08:37:33 -07:00
|
|
|
raise (Error (loc, env, Invalid_format msg))
|
2014-05-12 08:37:29 -07:00
|
|
|
|
2013-09-04 08:12:37 -07:00
|
|
|
and type_label_exp create env loc ty_expected
|
2012-10-24 05:03:00 -07:00
|
|
|
(lid, label, sarg) =
|
2010-11-11 19:09:11 -08:00
|
|
|
(* Here also ty_expected may be at generic_level *)
|
2010-08-02 07:37:22 -07:00
|
|
|
begin_def ();
|
2011-11-24 01:02:48 -08:00
|
|
|
let separate = !Clflags.principal || Env.has_local_constraints env in
|
|
|
|
if separate then (begin_def (); begin_def ());
|
2010-08-02 07:37:22 -07:00
|
|
|
let (vars, ty_arg, ty_res) = instance_label true label in
|
2011-11-24 01:02:48 -08:00
|
|
|
if separate then begin
|
2010-08-02 07:37:22 -07:00
|
|
|
end_def ();
|
2010-11-11 19:09:11 -08:00
|
|
|
(* Generalize label information *)
|
2010-08-02 07:37:22 -07:00
|
|
|
generalize_structure ty_arg;
|
|
|
|
generalize_structure ty_res
|
|
|
|
end;
|
|
|
|
begin try
|
2018-07-24 08:17:53 -07:00
|
|
|
unify env (instance ty_res) (instance ty_expected)
|
2010-08-02 07:37:22 -07:00
|
|
|
with Unify trace ->
|
2013-01-29 06:21:12 -08:00
|
|
|
raise (Error(lid.loc, env, Label_mismatch(lid.txt, trace)))
|
2010-08-02 07:37:22 -07:00
|
|
|
end;
|
2010-11-11 19:09:11 -08:00
|
|
|
(* Instantiate so that we can generalize internal nodes *)
|
2018-07-24 08:17:53 -07:00
|
|
|
let ty_arg = instance ty_arg in
|
2011-11-24 01:02:48 -08:00
|
|
|
if separate then begin
|
2010-11-11 19:09:11 -08:00
|
|
|
end_def ();
|
|
|
|
(* Generalize information merged from ty_expected *)
|
|
|
|
generalize_structure ty_arg
|
2010-08-02 07:37:22 -07:00
|
|
|
end;
|
|
|
|
if label.lbl_private = Private then
|
2012-06-13 16:45:01 -07:00
|
|
|
if create then
|
2012-08-21 00:10:35 -07:00
|
|
|
raise (Error(loc, env, Private_type ty_expected))
|
2012-06-13 16:45:01 -07:00
|
|
|
else
|
2013-01-29 06:21:12 -08:00
|
|
|
raise (Error(lid.loc, env, Private_label(lid.txt, ty_expected)));
|
2010-08-02 07:37:22 -07:00
|
|
|
let arg =
|
|
|
|
let snap = if vars = [] then None else Some (Btype.snapshot ()) in
|
2018-02-12 08:37:35 -08:00
|
|
|
let arg = type_argument env sarg ty_arg (instance ty_arg) in
|
2010-08-02 07:37:22 -07:00
|
|
|
end_def ();
|
|
|
|
try
|
2017-02-27 10:09:09 -08:00
|
|
|
if (vars = []) then arg
|
|
|
|
else begin
|
|
|
|
if maybe_expansive arg then
|
|
|
|
lower_contravariant env arg.exp_type;
|
|
|
|
generalize_and_check_univars env "field value" arg label.lbl_arg vars;
|
|
|
|
{arg with exp_type = instance arg.exp_type}
|
|
|
|
end
|
2019-04-09 03:28:13 -07:00
|
|
|
with exn when maybe_expansive arg -> try
|
2010-08-02 07:37:22 -07:00
|
|
|
(* Try to retype without propagating ty_arg, cf PR#4862 *)
|
2019-05-23 08:27:58 -07:00
|
|
|
Option.iter Btype.backtrack snap;
|
2010-08-02 07:37:22 -07:00
|
|
|
begin_def ();
|
|
|
|
let arg = type_exp env sarg in
|
|
|
|
end_def ();
|
2019-04-08 23:03:19 -07:00
|
|
|
lower_contravariant env arg.exp_type;
|
2017-02-27 10:09:09 -08:00
|
|
|
begin_def ();
|
|
|
|
let arg = {arg with exp_type = instance arg.exp_type} in
|
|
|
|
unify_exp env arg (instance ty_arg);
|
|
|
|
end_def ();
|
|
|
|
generalize_and_check_univars env "field value" arg label.lbl_arg vars;
|
|
|
|
{arg with exp_type = instance arg.exp_type}
|
2012-01-21 19:15:14 -08:00
|
|
|
with Error (_, _, Less_general _) as e -> raise e
|
2010-08-02 07:37:22 -07:00
|
|
|
| _ -> raise exn (* In case of failure return the first error *)
|
|
|
|
in
|
2017-02-27 10:09:09 -08:00
|
|
|
(lid, label, arg)
|
2010-08-02 07:37:22 -07:00
|
|
|
|
2019-03-10 07:38:15 -07:00
|
|
|
and type_argument ?explanation ?recarg env sarg ty_expected' ty_expected =
|
2002-04-18 00:27:47 -07:00
|
|
|
(* ty_expected' may be generic *)
|
2001-12-05 16:19:35 -08:00
|
|
|
let no_labels ty =
|
|
|
|
let ls, tvar = list_labels env ty in
|
2014-12-22 00:45:55 -08:00
|
|
|
not tvar && List.for_all ((=) Nolabel) ls
|
2000-04-04 03:03:53 -07:00
|
|
|
in
|
2012-03-24 23:41:42 -07:00
|
|
|
let rec is_inferred sexp =
|
|
|
|
match sexp.pexp_desc with
|
2014-04-02 20:59:17 -07:00
|
|
|
Pexp_ident _ | Pexp_apply _ | Pexp_field _ | Pexp_constraint _
|
|
|
|
| Pexp_coerce _ | Pexp_send _ | Pexp_new _ -> true
|
2018-04-08 01:51:15 -07:00
|
|
|
| Pexp_sequence (_, e) | Pexp_open (_, e) -> is_inferred e
|
2014-04-02 20:59:17 -07:00
|
|
|
| Pexp_ifthenelse (_, e1, Some e2) -> is_inferred e1 && is_inferred e2
|
2012-03-24 23:41:42 -07:00
|
|
|
| _ -> false
|
|
|
|
in
|
|
|
|
match expand_head env ty_expected' with
|
2015-09-11 04:58:31 -07:00
|
|
|
{desc = Tarrow(Nolabel,ty_arg,ty_res,_); level = lv}
|
|
|
|
when is_inferred sarg ->
|
1999-11-30 08:07:38 -08:00
|
|
|
(* apply optional arguments when expected type is "" *)
|
2000-04-04 03:03:53 -07:00
|
|
|
(* we must be very careful about not breaking the semantics *)
|
2002-04-18 00:27:47 -07:00
|
|
|
if !Clflags.principal then begin_def ();
|
1999-11-30 08:07:38 -08:00
|
|
|
let texp = type_exp env sarg in
|
2002-04-18 00:27:47 -07:00
|
|
|
if !Clflags.principal then begin
|
|
|
|
end_def ();
|
|
|
|
generalize_structure texp.exp_type
|
|
|
|
end;
|
1999-11-30 08:07:38 -08:00
|
|
|
let rec make_args args ty_fun =
|
|
|
|
match (expand_head env ty_fun).desc with
|
2001-04-19 01:34:21 -07:00
|
|
|
| Tarrow (l,ty_arg,ty_fun,_) when is_optional l ->
|
2018-10-12 02:20:21 -07:00
|
|
|
let ty = option_none env (instance ty_arg) sarg.pexp_loc in
|
2015-11-24 15:37:46 -08:00
|
|
|
make_args ((l, Some ty) :: args) ty_fun
|
2014-12-22 00:45:55 -08:00
|
|
|
| Tarrow (l,_,ty_res',_) when l = Nolabel || !Clflags.classic ->
|
2014-03-26 11:09:28 -07:00
|
|
|
List.rev args, ty_fun, no_labels ty_res'
|
|
|
|
| Tvar _ -> List.rev args, ty_fun, false
|
2001-04-19 01:34:21 -07:00
|
|
|
| _ -> [], texp.exp_type, false
|
1999-11-30 08:07:38 -08:00
|
|
|
in
|
2002-04-18 00:27:47 -07:00
|
|
|
let args, ty_fun', simple_res = make_args [] texp.exp_type in
|
|
|
|
let warn = !Clflags.principal &&
|
|
|
|
(lv <> generic_level || (repr ty_fun').level <> generic_level)
|
2018-02-12 08:37:35 -08:00
|
|
|
and texp = {texp with exp_type = instance texp.exp_type}
|
|
|
|
and ty_fun = instance ty_fun' in
|
2001-12-05 16:19:35 -08:00
|
|
|
if not (simple_res || no_labels ty_res) then begin
|
2001-10-21 19:39:25 -07:00
|
|
|
unify_exp env texp ty_expected;
|
|
|
|
texp
|
|
|
|
end else begin
|
1999-11-30 08:07:38 -08:00
|
|
|
unify_exp env {texp with exp_type = ty_fun} ty_expected;
|
|
|
|
if args = [] then texp else
|
|
|
|
(* eta-expand to avoid side effects *)
|
|
|
|
let var_pair name ty =
|
2018-08-28 09:07:01 -07:00
|
|
|
let id = Ident.create_local name in
|
2018-02-08 09:51:47 -08:00
|
|
|
let desc =
|
|
|
|
{ val_type = ty; val_kind = Val_reg;
|
|
|
|
val_attributes = [];
|
2019-08-20 01:53:05 -07:00
|
|
|
val_loc = Location.none;
|
|
|
|
val_uid = Uid.mk ~current_unit:(Env.get_unit_name ());
|
|
|
|
}
|
2018-02-08 09:51:47 -08:00
|
|
|
in
|
|
|
|
let exp_env = Env.add_value id desc env in
|
2012-05-30 07:52:37 -07:00
|
|
|
{pat_desc = Tpat_var (id, mknoloc name); pat_type = ty;pat_extra=[];
|
2013-03-25 07:16:07 -07:00
|
|
|
pat_attributes = [];
|
1999-11-30 08:07:38 -08:00
|
|
|
pat_loc = Location.none; pat_env = env},
|
2018-02-08 09:51:47 -08:00
|
|
|
{exp_type = ty; exp_loc = Location.none; exp_env = exp_env;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_extra = []; exp_attributes = [];
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_desc =
|
2018-02-08 09:51:47 -08:00
|
|
|
Texp_ident(Path.Pident id, mknoloc (Longident.Lident name), desc)}
|
1999-11-30 08:07:38 -08:00
|
|
|
in
|
|
|
|
let eta_pat, eta_var = var_pair "eta" ty_arg in
|
|
|
|
let func texp =
|
2013-04-15 09:23:22 -07:00
|
|
|
let e =
|
|
|
|
{texp with exp_type = ty_res; exp_desc =
|
|
|
|
Texp_apply
|
|
|
|
(texp,
|
2015-11-24 15:37:46 -08:00
|
|
|
args @ [Nolabel, Some eta_var])}
|
2013-04-15 09:23:22 -07:00
|
|
|
in
|
2016-10-14 01:34:50 -07:00
|
|
|
let cases = [case eta_pat e] in
|
2018-01-15 03:11:06 -08:00
|
|
|
let param = name_cases "param" cases in
|
1999-11-30 08:07:38 -08:00
|
|
|
{ texp with exp_type = ty_fun; exp_desc =
|
2016-10-14 01:34:50 -07:00
|
|
|
Texp_function { arg_label = Nolabel; param; cases;
|
|
|
|
partial = Total; } }
|
2013-04-15 09:23:22 -07:00
|
|
|
in
|
2014-03-26 11:09:28 -07:00
|
|
|
Location.prerr_warning texp.exp_loc
|
2014-12-22 00:45:55 -08:00
|
|
|
(Warnings.Eliminated_optional_arguments
|
2015-11-24 15:37:46 -08:00
|
|
|
(List.map (fun (l, _) -> Printtyp.string_of_label l) args));
|
2002-04-18 00:27:47 -07:00
|
|
|
if warn then Location.prerr_warning texp.exp_loc
|
2020-07-10 09:00:05 -07:00
|
|
|
(Warnings.Non_principal_labels "eliminated optional argument");
|
1999-11-30 08:07:38 -08:00
|
|
|
(* let-expand to have side effects *)
|
2013-07-24 01:11:20 -07:00
|
|
|
let let_pat, let_var = var_pair "arg" texp.exp_type in
|
2003-04-01 17:32:09 -08:00
|
|
|
re { texp with exp_type = ty_fun; exp_desc =
|
2014-01-22 17:58:37 -08:00
|
|
|
Texp_let (Nonrecursive,
|
2014-04-22 08:28:20 -07:00
|
|
|
[{vb_pat=let_pat; vb_expr=texp; vb_attributes=[];
|
|
|
|
vb_loc=Location.none;
|
|
|
|
}],
|
2014-01-22 17:58:37 -08:00
|
|
|
func let_var) }
|
2001-04-19 01:34:21 -07:00
|
|
|
end
|
1999-11-30 08:07:38 -08:00
|
|
|
| _ ->
|
2019-03-10 07:38:15 -07:00
|
|
|
let texp = type_expect ?recarg env sarg
|
|
|
|
(mk_expected ?explanation ty_expected') in
|
2011-02-23 22:29:26 -08:00
|
|
|
unify_exp env texp ty_expected;
|
|
|
|
texp
|
1999-11-30 08:07:38 -08:00
|
|
|
|
|
|
|
and type_application env funct sargs =
|
2002-04-18 00:27:47 -07:00
|
|
|
(* funct.exp_type may be generic *)
|
1999-11-30 08:07:38 -08:00
|
|
|
let result_type omitted ty_fun =
|
|
|
|
List.fold_left
|
2001-04-19 01:34:21 -07:00
|
|
|
(fun ty_fun (l,ty,lv) -> newty2 lv (Tarrow(l,ty,ty_fun,Cok)))
|
1999-11-30 08:07:38 -08:00
|
|
|
ty_fun omitted
|
|
|
|
in
|
2001-12-05 16:19:35 -08:00
|
|
|
let has_label l ty_fun =
|
|
|
|
let ls, tvar = list_labels env ty_fun in
|
|
|
|
tvar || List.mem l ls
|
2001-04-19 01:34:21 -07:00
|
|
|
in
|
2020-04-09 01:02:53 -07:00
|
|
|
let eliminated_optional_arguments = ref [] in
|
2020-04-17 03:05:20 -07:00
|
|
|
let omitted_parameters = ref [] in
|
2020-04-09 01:54:11 -07:00
|
|
|
let type_unknown_arg (ty_fun, typed_args) (lbl, sarg) =
|
2020-04-09 01:02:53 -07:00
|
|
|
let (ty_arg, ty_res) =
|
2020-04-08 05:47:20 -07:00
|
|
|
let ty_fun = expand_head env ty_fun in
|
|
|
|
match ty_fun.desc with
|
|
|
|
| Tvar _ ->
|
|
|
|
let t1 = newvar () and t2 = newvar () in
|
|
|
|
if ty_fun.level >= t1.level &&
|
|
|
|
not (is_prim ~name:"%identity" funct)
|
|
|
|
then
|
2020-07-11 02:02:01 -07:00
|
|
|
Location.prerr_warning sarg.pexp_loc
|
|
|
|
Warnings.Ignored_extra_argument;
|
2020-04-08 05:47:20 -07:00
|
|
|
unify env ty_fun (newty (Tarrow(lbl,t1,t2,Clink(ref Cunknown))));
|
|
|
|
(t1, t2)
|
|
|
|
| Tarrow (l,t1,t2,_) when l = lbl
|
|
|
|
|| !Clflags.classic && lbl = Nolabel && not (is_optional l) ->
|
|
|
|
(t1, t2)
|
|
|
|
| td ->
|
|
|
|
let ty_fun = match td with Tarrow _ -> newty td | _ -> ty_fun in
|
2020-04-09 01:02:53 -07:00
|
|
|
let ty_res =
|
2020-04-17 03:05:20 -07:00
|
|
|
result_type (!omitted_parameters @ !eliminated_optional_arguments)
|
2020-04-09 01:02:53 -07:00
|
|
|
ty_fun
|
|
|
|
in
|
2020-04-08 05:47:20 -07:00
|
|
|
match ty_res.desc with
|
|
|
|
| Tarrow _ ->
|
|
|
|
if !Clflags.classic || not (has_label lbl ty_fun) then
|
|
|
|
raise (Error(sarg.pexp_loc, env,
|
2020-04-17 07:43:38 -07:00
|
|
|
Apply_wrong_label(lbl, ty_res, false)))
|
2020-04-08 05:47:20 -07:00
|
|
|
else
|
|
|
|
raise (Error(funct.exp_loc, env, Incoherent_label_order))
|
|
|
|
| _ ->
|
|
|
|
raise(Error(funct.exp_loc, env, Apply_non_function
|
|
|
|
(expand_head env funct.exp_type)))
|
|
|
|
in
|
|
|
|
let arg () =
|
2020-04-09 01:02:53 -07:00
|
|
|
let arg = type_expect env sarg (mk_expected ty_arg) in
|
2020-04-08 05:47:20 -07:00
|
|
|
if is_optional lbl then
|
|
|
|
unify_exp env arg (type_option(newvar()));
|
|
|
|
arg
|
|
|
|
in
|
2020-04-09 01:02:53 -07:00
|
|
|
(ty_res, (lbl, Some arg) :: typed_args)
|
2020-04-08 05:47:20 -07:00
|
|
|
in
|
2001-09-06 01:52:32 -07:00
|
|
|
let ignore_labels =
|
|
|
|
!Clflags.classic ||
|
2001-12-05 16:19:35 -08:00
|
|
|
begin
|
|
|
|
let ls, tvar = list_labels env funct.exp_type in
|
|
|
|
not tvar &&
|
|
|
|
let labels = List.filter (fun l -> not (is_optional l)) ls in
|
|
|
|
List.length labels = List.length sargs &&
|
2014-12-22 00:45:55 -08:00
|
|
|
List.for_all (fun (l,_) -> l = Nolabel) sargs &&
|
|
|
|
List.exists (fun l -> l <> Nolabel) labels &&
|
2015-05-24 00:33:32 -07:00
|
|
|
(Location.prerr_warning
|
2015-09-11 04:58:31 -07:00
|
|
|
funct.exp_loc
|
|
|
|
(Warnings.Labels_omitted
|
|
|
|
(List.map Printtyp.string_of_label
|
|
|
|
(List.filter ((<>) Nolabel) labels)));
|
2001-12-05 16:19:35 -08:00
|
|
|
true)
|
|
|
|
end
|
2001-09-06 01:52:32 -07:00
|
|
|
in
|
2002-04-18 00:27:47 -07:00
|
|
|
let warned = ref false in
|
2020-04-09 01:54:11 -07:00
|
|
|
let rec type_args args ty_fun ty_fun0 sargs =
|
2011-02-23 22:29:26 -08:00
|
|
|
match expand_head env ty_fun, expand_head env ty_fun0 with
|
2020-04-08 02:18:13 -07:00
|
|
|
| {desc=Tarrow (l, ty, ty_fun, com); level=lv} as ty_fun',
|
2011-02-23 22:29:26 -08:00
|
|
|
{desc=Tarrow (_, ty0, ty_fun0, _)}
|
2020-04-01 00:14:55 -07:00
|
|
|
when sargs <> [] && commu_repr com = Cok ->
|
2004-11-30 10:57:04 -08:00
|
|
|
let may_warn loc w =
|
2002-04-18 00:27:47 -07:00
|
|
|
if not !warned && !Clflags.principal && lv <> generic_level
|
|
|
|
then begin
|
|
|
|
warned := true;
|
2004-11-30 10:57:04 -08:00
|
|
|
Location.prerr_warning loc w
|
2002-04-18 00:27:47 -07:00
|
|
|
end
|
|
|
|
in
|
2000-09-04 01:49:32 -07:00
|
|
|
let name = label_name l
|
2015-11-24 15:37:46 -08:00
|
|
|
and optional = is_optional l in
|
2020-04-09 01:02:53 -07:00
|
|
|
let use_arg sarg l' =
|
2020-04-07 08:05:01 -07:00
|
|
|
Some (
|
|
|
|
if not optional || is_optional l' then
|
2020-04-09 01:02:53 -07:00
|
|
|
(fun () -> type_argument env sarg ty ty0)
|
2020-04-07 08:05:01 -07:00
|
|
|
else begin
|
2020-04-09 01:02:53 -07:00
|
|
|
may_warn sarg.pexp_loc
|
2020-04-07 08:05:01 -07:00
|
|
|
(Warnings.Not_principal "using an optional argument here");
|
2020-04-09 01:02:53 -07:00
|
|
|
(fun () -> option_some env (type_argument env sarg
|
2020-04-07 08:05:01 -07:00
|
|
|
(extract_option_type env ty)
|
|
|
|
(extract_option_type env ty0)))
|
|
|
|
end
|
|
|
|
)
|
|
|
|
in
|
|
|
|
let eliminate_optional_arg () =
|
|
|
|
may_warn funct.exp_loc
|
2020-07-10 09:00:05 -07:00
|
|
|
(Warnings.Non_principal_labels "eliminated optional argument");
|
2020-04-09 01:02:53 -07:00
|
|
|
eliminated_optional_arguments :=
|
|
|
|
(l,ty,lv) :: !eliminated_optional_arguments;
|
2020-04-07 08:05:01 -07:00
|
|
|
Some (fun () -> option_none env (instance ty) Location.none)
|
|
|
|
in
|
2020-04-09 01:02:53 -07:00
|
|
|
let remaining_sargs, arg =
|
2020-04-07 08:05:01 -07:00
|
|
|
if ignore_labels then begin
|
2020-04-08 02:18:13 -07:00
|
|
|
(* No reordering is allowed, process arguments in order *)
|
2020-04-01 00:14:55 -07:00
|
|
|
match sargs with
|
|
|
|
| [] -> assert false
|
2020-04-09 01:02:53 -07:00
|
|
|
| (l', sarg) :: remaining_sargs ->
|
2020-04-07 08:05:01 -07:00
|
|
|
if name = label_name l' || (not optional && l' = Nolabel) then
|
2020-04-09 01:02:53 -07:00
|
|
|
(remaining_sargs, use_arg sarg l')
|
2020-04-17 07:36:00 -07:00
|
|
|
else if
|
|
|
|
optional &&
|
|
|
|
not (List.exists (fun (l, _) -> name = label_name l)
|
|
|
|
remaining_sargs) &&
|
|
|
|
List.exists (function (Nolabel, _) -> true | _ -> false)
|
|
|
|
sargs
|
2020-04-07 08:05:01 -07:00
|
|
|
then
|
|
|
|
(sargs, eliminate_optional_arg ())
|
|
|
|
else
|
2020-04-09 01:02:53 -07:00
|
|
|
raise(Error(sarg.pexp_loc, env,
|
2020-04-17 07:43:38 -07:00
|
|
|
Apply_wrong_label(l', ty_fun', optional)))
|
2020-04-01 00:14:55 -07:00
|
|
|
end else
|
2020-04-08 02:18:13 -07:00
|
|
|
(* Arguments can be commuted, try to fetch the argument
|
|
|
|
corresponding to the first parameter. *)
|
2020-04-01 00:14:55 -07:00
|
|
|
match extract_label name sargs with
|
2020-04-09 01:02:53 -07:00
|
|
|
| Some (l', sarg, commuted, remaining_sargs) ->
|
2020-04-01 00:56:19 -07:00
|
|
|
if commuted then begin
|
2020-04-09 01:02:53 -07:00
|
|
|
may_warn sarg.pexp_loc
|
2020-04-01 00:56:19 -07:00
|
|
|
(Warnings.Not_principal "commuting this argument")
|
|
|
|
end;
|
2020-04-01 00:14:55 -07:00
|
|
|
if not optional && is_optional l' then
|
2020-04-09 01:02:53 -07:00
|
|
|
Location.prerr_warning sarg.pexp_loc
|
2020-04-01 00:14:55 -07:00
|
|
|
(Warnings.Nonoptional_label (Printtyp.string_of_label l));
|
2020-04-09 01:02:53 -07:00
|
|
|
remaining_sargs, use_arg sarg l'
|
2020-04-01 00:14:55 -07:00
|
|
|
| None ->
|
|
|
|
sargs,
|
2020-04-07 08:05:01 -07:00
|
|
|
if optional && List.mem_assoc Nolabel sargs then
|
|
|
|
eliminate_optional_arg ()
|
|
|
|
else begin
|
2020-04-17 03:05:20 -07:00
|
|
|
(* No argument was given for this parameter, we abstract over
|
|
|
|
it. *)
|
2020-04-01 00:14:55 -07:00
|
|
|
may_warn funct.exp_loc
|
2020-07-10 09:00:05 -07:00
|
|
|
(Warnings.Non_principal_labels "commuted an argument");
|
2020-04-17 03:05:20 -07:00
|
|
|
omitted_parameters := (l,ty,lv) :: !omitted_parameters;
|
2020-04-01 00:14:55 -07:00
|
|
|
None
|
|
|
|
end
|
1999-11-30 08:07:38 -08:00
|
|
|
in
|
2020-04-09 01:02:53 -07:00
|
|
|
type_args ((l,arg)::args) ty_fun ty_fun0 remaining_sargs
|
2020-04-09 01:01:32 -07:00
|
|
|
| _ ->
|
2020-04-09 01:02:53 -07:00
|
|
|
(* We're not looking at a *known* function type anymore, or there are no
|
|
|
|
arguments left. *)
|
2020-04-09 01:01:32 -07:00
|
|
|
let ty_fun, typed_args =
|
2020-04-09 01:54:11 -07:00
|
|
|
List.fold_left type_unknown_arg (ty_fun0, args) sargs
|
2020-04-09 01:01:32 -07:00
|
|
|
in
|
|
|
|
let args =
|
2020-04-09 01:02:53 -07:00
|
|
|
(* Force typing of arguments.
|
|
|
|
Careful: the order matters here. Using [List.rev_map] would be
|
|
|
|
incorrect. *)
|
2020-04-09 01:01:32 -07:00
|
|
|
List.map
|
|
|
|
(function
|
|
|
|
| l, None -> l, None
|
|
|
|
| l, Some f -> l, Some (f ()))
|
|
|
|
(List.rev typed_args)
|
|
|
|
in
|
2020-04-17 03:05:20 -07:00
|
|
|
let result_ty = instance (result_type !omitted_parameters ty_fun) in
|
2020-04-09 01:01:32 -07:00
|
|
|
args, result_ty
|
1999-11-30 08:07:38 -08:00
|
|
|
in
|
2015-11-29 23:45:44 -08:00
|
|
|
let is_ignore funct =
|
2020-04-08 06:01:56 -07:00
|
|
|
is_prim ~name:"%ignore" funct &&
|
|
|
|
(try ignore (filter_arrow env (instance funct.exp_type) Nolabel); true
|
|
|
|
with Unify _ -> false)
|
2015-11-29 23:45:44 -08:00
|
|
|
in
|
|
|
|
match sargs with
|
2020-04-08 02:18:13 -07:00
|
|
|
| (* Special case for ignore: avoid discarding warning *)
|
2015-11-29 23:45:44 -08:00
|
|
|
[Nolabel, sarg] when is_ignore funct ->
|
2020-04-08 02:18:13 -07:00
|
|
|
let ty_arg, ty_res = filter_arrow env (instance funct.exp_type) Nolabel in
|
2017-12-02 04:44:54 -08:00
|
|
|
let exp = type_expect env sarg (mk_expected ty_arg) in
|
2018-08-07 06:02:12 -07:00
|
|
|
check_partial_application false exp;
|
2015-11-24 15:37:46 -08:00
|
|
|
([Nolabel, Some exp], ty_res)
|
1999-11-30 08:07:38 -08:00
|
|
|
| _ ->
|
2018-04-08 01:51:15 -07:00
|
|
|
let ty = funct.exp_type in
|
2020-04-09 01:54:11 -07:00
|
|
|
type_args [] ty (instance ty) sargs
|
1999-11-30 08:07:38 -08:00
|
|
|
|
2017-12-02 04:44:54 -08:00
|
|
|
and type_construct env loc lid sarg ty_expected_explained attrs =
|
|
|
|
let { ty = ty_expected; explanation } = ty_expected_explained in
|
2019-12-12 07:06:01 -08:00
|
|
|
let expected_type =
|
2012-10-29 00:54:06 -07:00
|
|
|
try
|
2012-11-10 19:46:59 -08:00
|
|
|
let (p0, p,_) = extract_concrete_variant env ty_expected in
|
2018-09-19 07:22:23 -07:00
|
|
|
let principal =
|
|
|
|
(repr ty_expected).level = generic_level || not !Clflags.principal
|
|
|
|
in
|
|
|
|
Some(p0, p, principal)
|
2012-10-29 00:54:06 -07:00
|
|
|
with Not_found -> None
|
|
|
|
in
|
2018-10-12 02:20:21 -07:00
|
|
|
let constrs =
|
|
|
|
Env.lookup_all_constructors ~loc:lid.loc Env.Positive lid.txt env
|
|
|
|
in
|
2013-09-19 00:25:51 -07:00
|
|
|
let constr =
|
2017-12-02 04:44:54 -08:00
|
|
|
wrap_disambiguate "This variant expression is expected to have"
|
|
|
|
ty_expected_explained
|
2019-12-12 07:06:01 -08:00
|
|
|
(Constructor.disambiguate Env.Positive lid env expected_type) constrs
|
2018-10-12 02:20:21 -07:00
|
|
|
in
|
1999-11-30 08:07:38 -08:00
|
|
|
let sargs =
|
|
|
|
match sarg with
|
|
|
|
None -> []
|
2014-04-15 02:53:51 -07:00
|
|
|
| Some {pexp_desc = Pexp_tuple sel} when
|
2015-12-02 06:52:04 -08:00
|
|
|
constr.cstr_arity > 1 || Builtin_attributes.explicit_arity attrs
|
2014-04-15 02:53:51 -07:00
|
|
|
-> sel
|
1999-11-30 08:07:38 -08:00
|
|
|
| Some se -> [se] in
|
|
|
|
if List.length sargs <> constr.cstr_arity then
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(loc, env, Constructor_arity_mismatch
|
2018-04-08 01:51:15 -07:00
|
|
|
(lid.txt, constr.cstr_arity, List.length sargs)));
|
2011-11-24 01:02:48 -08:00
|
|
|
let separate = !Clflags.principal || Env.has_local_constraints env in
|
|
|
|
if separate then (begin_def (); begin_def ());
|
1999-11-30 08:07:38 -08:00
|
|
|
let (ty_args, ty_res) = instance_constructor constr in
|
|
|
|
let texp =
|
2003-04-01 17:32:09 -08:00
|
|
|
re {
|
2013-04-17 02:46:52 -07:00
|
|
|
exp_desc = Texp_construct(lid, constr, []);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = loc; exp_extra = [];
|
2010-10-21 16:59:33 -07:00
|
|
|
exp_type = ty_res;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = attrs;
|
1999-11-30 08:07:38 -08:00
|
|
|
exp_env = env } in
|
2011-11-24 01:02:48 -08:00
|
|
|
if separate then begin
|
2010-10-21 16:59:33 -07:00
|
|
|
end_def ();
|
|
|
|
generalize_structure ty_res;
|
2017-12-02 04:44:54 -08:00
|
|
|
with_explanation explanation (fun () ->
|
2018-07-24 08:17:53 -07:00
|
|
|
unify_exp env {texp with exp_type = instance ty_res}
|
2018-04-08 01:51:15 -07:00
|
|
|
(instance ty_expected));
|
2010-10-21 16:59:33 -07:00
|
|
|
end_def ();
|
|
|
|
List.iter generalize_structure ty_args;
|
|
|
|
generalize_structure ty_res;
|
|
|
|
end;
|
2011-02-23 22:29:26 -08:00
|
|
|
let ty_args0, ty_res =
|
2018-02-12 08:37:35 -08:00
|
|
|
match instance_list (ty_res :: ty_args) with
|
2011-02-23 22:29:26 -08:00
|
|
|
t :: tl -> tl, t
|
|
|
|
| _ -> assert false
|
|
|
|
in
|
2011-11-24 01:02:48 -08:00
|
|
|
let texp = {texp with exp_type = ty_res} in
|
2018-02-12 08:37:35 -08:00
|
|
|
if not separate then unify_exp env texp (instance ty_expected);
|
2014-10-14 08:51:30 -07:00
|
|
|
let recarg =
|
|
|
|
match constr.cstr_inlined with
|
|
|
|
| None -> Rejected
|
|
|
|
| Some _ ->
|
2018-04-08 01:51:15 -07:00
|
|
|
begin match sargs with
|
|
|
|
| [{pexp_desc =
|
|
|
|
Pexp_ident _ |
|
|
|
|
Pexp_record (_, (Some {pexp_desc = Pexp_ident _}| None))}] ->
|
|
|
|
Required
|
|
|
|
| _ ->
|
|
|
|
raise (Error(loc, env, Inlined_record_expected))
|
|
|
|
end
|
2014-10-14 08:51:30 -07:00
|
|
|
in
|
|
|
|
let args =
|
|
|
|
List.map2 (fun e (t,t0) -> type_argument ~recarg env e t t0) sargs
|
2011-02-23 22:29:26 -08:00
|
|
|
(List.combine ty_args ty_args0) in
|
2003-07-02 02:14:35 -07:00
|
|
|
if constr.cstr_private = Private then
|
2019-04-04 05:03:26 -07:00
|
|
|
begin match constr.cstr_tag with
|
|
|
|
| Cstr_extension _ ->
|
|
|
|
raise(Error(loc, env, Private_constructor (constr, ty_res)))
|
|
|
|
| Cstr_constant _ | Cstr_block _ | Cstr_unboxed ->
|
|
|
|
raise (Error(loc, env, Private_type ty_res));
|
|
|
|
end;
|
2013-04-17 02:46:52 -07:00
|
|
|
(* NOTE: shouldn't we call "re" on this final expression? -- AF *)
|
2012-05-31 01:07:31 -07:00
|
|
|
{ texp with
|
2013-04-17 02:46:52 -07:00
|
|
|
exp_desc = Texp_construct(lid, constr, args) }
|
1999-11-30 08:07:38 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Typing of statements (expressions whose values are discarded) *)
|
|
|
|
|
2017-12-02 04:44:54 -08:00
|
|
|
and type_statement ?explanation env sexp =
|
2004-11-28 18:27:25 -08:00
|
|
|
begin_def();
|
|
|
|
let exp = type_exp env sexp in
|
|
|
|
end_def();
|
2016-08-02 17:03:43 -07:00
|
|
|
let ty = expand_head env exp.exp_type and tv = newvar() in
|
|
|
|
if is_Tvar ty && ty.level > tv.level then
|
2019-06-28 05:00:59 -07:00
|
|
|
Location.prerr_warning
|
|
|
|
(final_subexpression exp).exp_loc
|
|
|
|
Warnings.Nonreturning_statement;
|
2009-12-09 01:17:12 -08:00
|
|
|
if !Clflags.strict_sequence then
|
2018-07-24 08:17:53 -07:00
|
|
|
let expected_ty = instance Predef.type_unit in
|
2017-12-02 04:44:54 -08:00
|
|
|
with_explanation explanation (fun () ->
|
|
|
|
unify_exp env exp expected_ty);
|
2016-08-02 17:03:43 -07:00
|
|
|
exp
|
|
|
|
else begin
|
2018-08-07 06:02:12 -07:00
|
|
|
check_partial_application true exp;
|
2016-08-02 17:03:43 -07:00
|
|
|
unify_var env tv ty;
|
|
|
|
exp
|
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2019-09-06 12:53:44 -07:00
|
|
|
and type_unpacks ?in_function env unpacks sbody expected_ty =
|
|
|
|
let ty = newvar() in
|
|
|
|
(* remember original level *)
|
|
|
|
let extended_env, tunpacks =
|
|
|
|
List.fold_left (fun (env, unpacks) (name, loc, uid) ->
|
|
|
|
begin_def ();
|
|
|
|
let context = Typetexp.narrow () in
|
|
|
|
let modl =
|
|
|
|
!type_module env
|
|
|
|
Ast_helper.(
|
|
|
|
Mod.unpack ~loc
|
|
|
|
(Exp.ident ~loc:name.loc (mkloc (Longident.Lident name.txt)
|
|
|
|
name.loc)))
|
|
|
|
in
|
|
|
|
Mtype.lower_nongen ty.level modl.mod_type;
|
|
|
|
let pres =
|
|
|
|
match modl.mod_type with
|
|
|
|
| Mty_alias _ -> Mp_absent
|
|
|
|
| _ -> Mp_present
|
|
|
|
in
|
|
|
|
let scope = create_scope () in
|
|
|
|
let md =
|
|
|
|
{ md_type = modl.mod_type; md_attributes = []; md_loc = name.loc;
|
|
|
|
md_uid = uid; }
|
|
|
|
in
|
|
|
|
let (id, env) =
|
|
|
|
Env.enter_module_declaration ~scope name.txt pres md env
|
|
|
|
in
|
|
|
|
Typetexp.widen context;
|
|
|
|
env, (id, name, pres, modl) :: unpacks
|
|
|
|
) (env, []) unpacks
|
|
|
|
in
|
|
|
|
(* ideally, we should catch Expr_type_clash errors
|
|
|
|
in type_expect triggered by escaping identifiers from the local module
|
|
|
|
and refine them into Scoping_let_module errors
|
|
|
|
*)
|
|
|
|
let body = type_expect ?in_function extended_env sbody expected_ty in
|
|
|
|
let exp_loc = { body.exp_loc with loc_ghost = true } in
|
|
|
|
let exp_attributes = [Ast_helper.Attr.mk (mknoloc "#modulepat") (PStr [])] in
|
|
|
|
List.fold_left (fun body (id, name, pres, modl) ->
|
|
|
|
(* go back to parent level *)
|
|
|
|
end_def ();
|
|
|
|
Ctype.unify_var extended_env ty body.exp_type;
|
|
|
|
re {
|
|
|
|
exp_desc = Texp_letmodule(Some id, { name with txt = Some name.txt },
|
|
|
|
pres, modl, body);
|
|
|
|
exp_loc;
|
|
|
|
exp_attributes;
|
|
|
|
exp_extra = [];
|
|
|
|
exp_type = ty;
|
|
|
|
exp_env = env }
|
|
|
|
) body tunpacks
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Typing of match cases *)
|
2019-09-20 06:23:07 -07:00
|
|
|
and type_cases
|
|
|
|
: type k . k pattern_category ->
|
|
|
|
?in_function:_ -> _ -> _ -> _ -> _ -> _ -> Parsetree.case list ->
|
|
|
|
k case list * partial
|
|
|
|
= fun category ?in_function env ty_arg ty_res partial_flag loc caselist ->
|
2011-11-24 01:02:48 -08:00
|
|
|
(* ty_arg is _fully_ generalized *)
|
2013-04-15 09:23:22 -07:00
|
|
|
let patterns = List.map (fun {pc_lhs=p} -> p) caselist in
|
2015-12-03 00:19:22 -08:00
|
|
|
let contains_polyvars = List.exists contains_polymorphic_variant patterns in
|
2018-03-06 07:09:31 -08:00
|
|
|
let erase_either = contains_polyvars && contains_variant_either ty_arg in
|
|
|
|
let may_contain_gadts = List.exists may_contain_gadts patterns in
|
2013-01-10 23:26:23 -08:00
|
|
|
let ty_arg =
|
2018-03-06 07:09:31 -08:00
|
|
|
if (may_contain_gadts || erase_either) && not !Clflags.principal
|
2013-01-10 23:26:23 -08:00
|
|
|
then correct_levels ty_arg else ty_arg
|
|
|
|
in
|
2016-10-20 07:57:53 -07:00
|
|
|
let rec is_var spat =
|
|
|
|
match spat.ppat_desc with
|
|
|
|
Ppat_any | Ppat_var _ -> true
|
2016-10-27 14:08:17 -07:00
|
|
|
| Ppat_alias (spat, _) -> is_var spat
|
2016-10-20 07:57:53 -07:00
|
|
|
| _ -> false in
|
2016-10-08 04:09:19 -07:00
|
|
|
let needs_exhaust_check =
|
|
|
|
match caselist with
|
|
|
|
[{pc_rhs = {pexp_desc = Pexp_unreachable}}] -> true
|
2016-10-20 07:57:53 -07:00
|
|
|
| [{pc_lhs}] when is_var pc_lhs -> false
|
2016-10-08 04:09:19 -07:00
|
|
|
| _ -> true
|
|
|
|
in
|
2018-02-13 08:07:11 -08:00
|
|
|
let outer_level = get_current_level () in
|
2018-02-12 08:37:35 -08:00
|
|
|
let lev =
|
2018-08-28 09:06:45 -07:00
|
|
|
if may_contain_gadts then begin_def ();
|
|
|
|
get_current_level ()
|
2011-11-24 01:02:48 -08:00
|
|
|
in
|
2018-01-25 02:33:09 -08:00
|
|
|
let take_partial_instance =
|
2020-07-08 01:35:40 -07:00
|
|
|
if erase_either
|
2018-01-25 02:33:09 -08:00
|
|
|
then Some false else None
|
|
|
|
in
|
2018-04-30 09:27:52 -07:00
|
|
|
begin_def (); (* propagation of the argument *)
|
2002-06-03 00:33:48 -07:00
|
|
|
let pattern_force = ref [] in
|
2012-05-30 07:52:37 -07:00
|
|
|
(* Format.printf "@[%i %i@ %a@]@." lev (get_current_level())
|
2010-12-12 18:59:58 -08:00
|
|
|
Printtyp.raw_type_expr ty_arg; *)
|
2018-04-23 03:08:43 -07:00
|
|
|
let half_typed_cases =
|
2000-05-12 11:22:35 -07:00
|
|
|
List.map
|
2020-10-13 00:48:16 -07:00
|
|
|
(fun ({pc_lhs; pc_guard = _; pc_rhs = _} as case) ->
|
2010-10-21 16:59:33 -07:00
|
|
|
if !Clflags.principal then begin_def (); (* propagation of pattern *)
|
2018-02-12 08:37:35 -08:00
|
|
|
begin_def ();
|
|
|
|
let ty_arg = instance ?partial:take_partial_instance ty_arg in
|
|
|
|
end_def ();
|
|
|
|
generalize_structure ty_arg;
|
2018-04-20 06:21:01 -07:00
|
|
|
let (pat, ext_env, force, pvs, unpacks) =
|
2020-10-13 00:48:16 -07:00
|
|
|
type_pattern category ~lev env pc_lhs ty_arg
|
2011-10-25 05:11:06 -07:00
|
|
|
in
|
2002-06-03 00:33:48 -07:00
|
|
|
pattern_force := force @ !pattern_force;
|
2002-04-18 00:27:47 -07:00
|
|
|
let pat =
|
|
|
|
if !Clflags.principal then begin
|
|
|
|
end_def ();
|
2018-05-04 05:33:00 -07:00
|
|
|
iter_pattern_variables_type generalize_structure pvs;
|
2018-02-12 08:37:35 -08:00
|
|
|
{ pat with pat_type = instance pat.pat_type }
|
2002-04-18 00:27:47 -07:00
|
|
|
end else pat
|
|
|
|
in
|
2018-02-13 08:07:11 -08:00
|
|
|
(* Ensure that no ambivalent pattern type escapes its branch *)
|
|
|
|
check_scope_escape pat.pat_loc env outer_level ty_arg;
|
2018-04-23 03:08:43 -07:00
|
|
|
{ typed_pat = pat;
|
|
|
|
pat_type_for_unif = ty_arg;
|
|
|
|
untyped_case = case;
|
|
|
|
branch_env = ext_env;
|
|
|
|
pat_vars = pvs;
|
2018-04-23 03:15:19 -07:00
|
|
|
unpacks;
|
2019-09-20 06:23:07 -07:00
|
|
|
contains_gadt = contains_gadt (as_comp_pattern category pat); }
|
2018-04-23 03:08:43 -07:00
|
|
|
)
|
2000-05-12 11:22:35 -07:00
|
|
|
caselist in
|
2018-04-23 03:08:43 -07:00
|
|
|
let patl = List.map (fun { typed_pat; _ } -> typed_pat) half_typed_cases in
|
2018-04-23 03:15:19 -07:00
|
|
|
let does_contain_gadt =
|
|
|
|
List.exists (fun { contains_gadt; _ } -> contains_gadt) half_typed_cases
|
|
|
|
in
|
2019-07-17 18:11:41 -07:00
|
|
|
let ty_res, do_copy_types =
|
2018-04-23 03:15:19 -07:00
|
|
|
if does_contain_gadt && not !Clflags.principal then
|
2019-07-17 18:11:41 -07:00
|
|
|
correct_levels ty_res, Env.make_copy_of_types env
|
|
|
|
else ty_res, (fun env -> env)
|
2018-04-20 06:30:51 -07:00
|
|
|
in
|
2017-09-07 08:13:34 -07:00
|
|
|
(* Unify all cases (delayed to keep it order-free) *)
|
|
|
|
let ty_arg' = newvar () in
|
|
|
|
let unify_pats ty =
|
2018-04-23 03:08:43 -07:00
|
|
|
List.iter (fun { typed_pat = pat; pat_type_for_unif = pat_ty; _ } ->
|
2019-12-02 11:49:51 -08:00
|
|
|
unify_pat_types pat.pat_loc (ref env) pat_ty ty
|
2018-04-23 03:08:43 -07:00
|
|
|
) half_typed_cases
|
2018-02-12 08:37:35 -08:00
|
|
|
in
|
2017-09-07 08:13:34 -07:00
|
|
|
unify_pats ty_arg';
|
2013-11-12 16:16:03 -08:00
|
|
|
(* Check for polymorphic variants to close *)
|
2003-08-18 01:26:18 -07:00
|
|
|
if List.exists has_variants patl then begin
|
2019-09-20 06:23:07 -07:00
|
|
|
Parmatch.pressure_variants_in_computation_pattern env
|
|
|
|
(List.map (as_comp_pattern category) patl);
|
|
|
|
List.iter finalize_variants patl
|
2003-08-18 01:26:18 -07:00
|
|
|
end;
|
2000-11-06 01:49:27 -08:00
|
|
|
(* `Contaminating' unifications start here *)
|
2002-06-03 00:33:48 -07:00
|
|
|
List.iter (fun f -> f()) !pattern_force;
|
2011-11-24 01:02:48 -08:00
|
|
|
(* Post-processing and generalization *)
|
2018-02-12 08:37:35 -08:00
|
|
|
if take_partial_instance <> None then unify_pats (instance ty_arg);
|
2018-05-04 05:33:00 -07:00
|
|
|
List.iter (fun { pat_vars; _ } ->
|
|
|
|
iter_pattern_variables_type (fun t -> unify_var env (newvar()) t) pat_vars
|
|
|
|
) half_typed_cases;
|
2018-04-30 09:27:52 -07:00
|
|
|
end_def ();
|
|
|
|
generalize ty_arg';
|
2018-05-04 05:33:00 -07:00
|
|
|
List.iter (fun { pat_vars; _ } ->
|
|
|
|
iter_pattern_variables_type generalize pat_vars
|
|
|
|
) half_typed_cases;
|
2011-11-24 01:02:48 -08:00
|
|
|
(* type bodies *)
|
2001-11-21 22:47:29 -08:00
|
|
|
let in_function = if List.length caselist = 1 then in_function else None in
|
2020-10-02 18:48:57 -07:00
|
|
|
let mk_cases interbranch_propagation =
|
2018-04-23 03:08:43 -07:00
|
|
|
List.map
|
|
|
|
(fun { typed_pat = pat; branch_env = ext_env; pat_vars = pvs; unpacks;
|
2018-04-23 03:15:19 -07:00
|
|
|
untyped_case = {pc_lhs = _; pc_guard; pc_rhs};
|
|
|
|
contains_gadt; _ } ->
|
2018-04-20 06:30:51 -07:00
|
|
|
let ext_env =
|
|
|
|
if contains_gadt then
|
2019-07-17 18:11:41 -07:00
|
|
|
do_copy_types ext_env
|
2018-04-20 06:30:51 -07:00
|
|
|
else
|
|
|
|
ext_env
|
|
|
|
in
|
2018-04-20 06:21:01 -07:00
|
|
|
let ext_env =
|
|
|
|
add_pattern_variables ext_env pvs
|
|
|
|
~check:(fun s -> Warnings.Unused_var_strict s)
|
|
|
|
~check_as:(fun s -> Warnings.Unused_var s)
|
|
|
|
in
|
2019-09-06 12:53:44 -07:00
|
|
|
let unpacks =
|
|
|
|
List.map (fun (name, loc) ->
|
|
|
|
name, loc, Uid.mk ~current_unit:(Env.get_unit_name ())
|
|
|
|
) unpacks
|
|
|
|
in
|
2010-11-08 22:23:53 -08:00
|
|
|
let ty_res' =
|
|
|
|
if !Clflags.principal then begin
|
|
|
|
begin_def ();
|
2018-02-12 08:37:35 -08:00
|
|
|
let ty = instance ~partial:true ty_res in
|
2010-11-08 22:23:53 -08:00
|
|
|
end_def ();
|
|
|
|
generalize_structure ty; ty
|
2011-11-24 01:02:48 -08:00
|
|
|
end
|
2020-10-02 18:48:57 -07:00
|
|
|
else if contains_gadt && interbranch_propagation then
|
2018-04-20 06:30:51 -07:00
|
|
|
correct_levels ty_res
|
2011-11-24 01:02:48 -08:00
|
|
|
else ty_res in
|
2013-04-15 09:23:22 -07:00
|
|
|
let guard =
|
|
|
|
match pc_guard with
|
|
|
|
| None -> None
|
|
|
|
| Some scond ->
|
|
|
|
Some
|
2019-09-06 12:53:44 -07:00
|
|
|
(type_unpacks ext_env unpacks scond
|
2018-07-04 07:38:06 -07:00
|
|
|
(mk_expected ~explanation:When_guard Predef.type_bool))
|
2013-04-15 09:23:22 -07:00
|
|
|
in
|
2017-12-02 04:44:54 -08:00
|
|
|
let exp =
|
2019-09-06 12:53:44 -07:00
|
|
|
type_unpacks ?in_function ext_env unpacks pc_rhs (mk_expected ty_res')
|
|
|
|
in
|
2013-04-15 09:23:22 -07:00
|
|
|
{
|
|
|
|
c_lhs = pat;
|
|
|
|
c_guard = guard;
|
2018-02-12 08:37:35 -08:00
|
|
|
c_rhs = {exp with exp_type = instance ty_res'}
|
2013-04-15 09:23:22 -07:00
|
|
|
}
|
|
|
|
)
|
2018-04-23 03:08:43 -07:00
|
|
|
half_typed_cases
|
2003-08-18 01:26:18 -07:00
|
|
|
in
|
2020-10-02 18:48:57 -07:00
|
|
|
let cases =
|
|
|
|
let may_backtrack = does_contain_gadt && not !Clflags.principal in
|
|
|
|
if not may_backtrack then mk_cases false else
|
2020-11-04 18:12:01 -08:00
|
|
|
let state = save_state (ref env) in
|
2020-10-02 18:48:57 -07:00
|
|
|
let has_equation_escape err =
|
|
|
|
match trace_of_error err with
|
|
|
|
Some tr ->
|
|
|
|
List.exists Ctype.Unification_trace.
|
|
|
|
(function Escape {kind=Equation _} -> true | _ -> false) tr
|
|
|
|
| None -> false
|
|
|
|
in
|
|
|
|
try mk_cases false
|
|
|
|
with Error(_,_,err) when has_equation_escape err ->
|
2020-11-04 18:12:01 -08:00
|
|
|
set_state state (ref env);
|
2020-10-02 18:48:57 -07:00
|
|
|
let cases = mk_cases true in
|
|
|
|
let msg =
|
|
|
|
Format.asprintf
|
|
|
|
"@[<v2>@ @[<hov>The return type of this pattern-matching \
|
|
|
|
is ambiguous.@ \
|
|
|
|
Please add a type annotation,@ as the choice of `@[%a@]'@]@]"
|
|
|
|
Printtyp.type_expr ty_res
|
|
|
|
in
|
|
|
|
Location.prerr_warning loc (Warnings.Not_principal msg);
|
|
|
|
cases
|
|
|
|
in
|
2018-04-23 03:15:19 -07:00
|
|
|
if !Clflags.principal || does_contain_gadt then begin
|
2018-02-12 08:37:35 -08:00
|
|
|
let ty_res' = instance ty_res in
|
2013-04-15 09:23:22 -07:00
|
|
|
List.iter (fun c -> unify_exp env c.c_rhs ty_res') cases
|
2010-11-08 22:23:53 -08:00
|
|
|
end;
|
2018-08-28 09:06:45 -07:00
|
|
|
let do_init = may_contain_gadts || needs_exhaust_check in
|
2016-10-19 07:07:28 -07:00
|
|
|
let ty_arg_check =
|
|
|
|
if do_init then
|
|
|
|
(* Hack: use for_saving to copy variables too *)
|
2018-01-24 03:58:53 -08:00
|
|
|
Subst.type_expr (Subst.for_saving Subst.identity) ty_arg'
|
|
|
|
else ty_arg'
|
2016-10-19 07:07:28 -07:00
|
|
|
in
|
2019-09-20 06:23:07 -07:00
|
|
|
let val_cases, exn_cases =
|
|
|
|
match category with
|
|
|
|
| Value -> (cases : value case list), []
|
|
|
|
| Computation -> split_cases env cases in
|
2018-01-15 03:11:06 -08:00
|
|
|
if val_cases = [] && exn_cases <> [] then
|
|
|
|
raise (Error (loc, env, No_value_clauses));
|
2003-08-18 01:26:18 -07:00
|
|
|
let partial =
|
2010-11-08 23:11:08 -08:00
|
|
|
if partial_flag then
|
2018-03-19 04:24:06 -07:00
|
|
|
check_partial ~lev env ty_arg_check loc val_cases
|
2010-11-08 23:11:08 -08:00
|
|
|
else
|
|
|
|
Partial
|
2003-08-18 01:26:18 -07:00
|
|
|
in
|
2018-08-28 09:06:45 -07:00
|
|
|
let unused_check delayed =
|
2018-04-23 03:08:43 -07:00
|
|
|
List.iter (fun { typed_pat; branch_env; _ } ->
|
2019-09-20 06:23:07 -07:00
|
|
|
check_absent_variant branch_env (as_comp_pattern category typed_pat)
|
2018-04-23 03:08:43 -07:00
|
|
|
) half_typed_cases;
|
2018-08-28 09:06:45 -07:00
|
|
|
if delayed then (begin_def (); init_def lev);
|
2018-03-19 04:24:06 -07:00
|
|
|
check_unused ~lev env ty_arg_check val_cases ;
|
2018-01-15 03:11:06 -08:00
|
|
|
check_unused ~lev env Predef.type_exn exn_cases ;
|
2018-08-28 09:06:45 -07:00
|
|
|
if delayed then end_def ();
|
2018-01-15 03:11:06 -08:00
|
|
|
Parmatch.check_ambiguous_bindings val_cases ;
|
|
|
|
Parmatch.check_ambiguous_bindings exn_cases
|
2015-12-03 00:19:22 -08:00
|
|
|
in
|
2018-08-28 09:06:45 -07:00
|
|
|
if contains_polyvars then
|
|
|
|
add_delayed_check (fun () -> unused_check true)
|
2015-12-03 00:19:22 -08:00
|
|
|
else
|
2018-08-28 09:06:45 -07:00
|
|
|
(* Check for unused cases, do not delay because of gadts *)
|
2018-02-12 08:37:35 -08:00
|
|
|
unused_check false;
|
2018-08-28 09:06:45 -07:00
|
|
|
if may_contain_gadts then begin
|
2011-11-24 01:02:48 -08:00
|
|
|
end_def ();
|
|
|
|
(* Ensure that existential types do not escape *)
|
2018-02-12 08:37:35 -08:00
|
|
|
unify_exp_types loc env (instance ty_res) (newvar ()) ;
|
2011-11-24 01:02:48 -08:00
|
|
|
end;
|
2000-11-06 01:49:27 -08:00
|
|
|
cases, partial
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Typing of let bindings *)
|
|
|
|
|
2018-04-21 12:15:13 -07:00
|
|
|
and type_let
|
|
|
|
?(check = fun s -> Warnings.Unused_var s)
|
|
|
|
?(check_strict = fun s -> Warnings.Unused_var_strict s)
|
|
|
|
existential_context
|
2020-10-13 00:48:16 -07:00
|
|
|
env rec_flag spat_sexp_list allow =
|
2013-03-08 06:59:45 -08:00
|
|
|
let open Ast_helper in
|
1995-05-04 03:15:53 -07:00
|
|
|
begin_def();
|
2002-04-18 00:27:47 -07:00
|
|
|
if !Clflags.principal then begin_def ();
|
2011-12-29 09:49:58 -08:00
|
|
|
|
|
|
|
let is_fake_let =
|
|
|
|
match spat_sexp_list with
|
2013-06-03 08:14:19 -07:00
|
|
|
| [{pvb_expr={pexp_desc=Pexp_match(
|
|
|
|
{pexp_desc=Pexp_ident({ txt = Longident.Lident "*opt*"})},_)}}] ->
|
2011-12-29 09:49:58 -08:00
|
|
|
true (* the fake let-declaration introduced by fun ?(x = e) -> ... *)
|
|
|
|
| _ ->
|
|
|
|
false
|
|
|
|
in
|
|
|
|
let check = if is_fake_let then check_strict else check in
|
|
|
|
|
2011-04-18 19:13:52 -07:00
|
|
|
let spatl =
|
|
|
|
List.map
|
2017-09-11 15:05:01 -07:00
|
|
|
(fun {pvb_pat=spat; pvb_expr=sexp; pvb_attributes=attrs} ->
|
|
|
|
attrs,
|
2011-04-18 19:13:52 -07:00
|
|
|
match spat.ppat_desc, sexp.pexp_desc with
|
2011-04-18 19:34:54 -07:00
|
|
|
(Ppat_any | Ppat_constraint _), _ -> spat
|
2013-04-17 05:23:44 -07:00
|
|
|
| _, Pexp_coerce (_, _, sty)
|
|
|
|
| _, Pexp_constraint (_, sty) when !Clflags.principal ->
|
2011-04-18 19:34:54 -07:00
|
|
|
(* propagate type annotation to pattern,
|
|
|
|
to allow it to be generalized in -principal mode *)
|
2013-03-08 06:59:45 -08:00
|
|
|
Pat.constraint_
|
|
|
|
~loc:{spat.ppat_loc with Location.loc_ghost=true}
|
|
|
|
spat
|
|
|
|
sty
|
2011-04-18 19:13:52 -07:00
|
|
|
| _ -> spat)
|
|
|
|
spat_sexp_list in
|
2010-10-18 22:24:36 -07:00
|
|
|
let nvs = List.map (fun _ -> newvar ()) spatl in
|
2018-04-11 10:45:21 -07:00
|
|
|
let (pat_list, new_env, force, pvs, unpacks) =
|
2020-10-13 00:48:16 -07:00
|
|
|
type_pattern_list Value existential_context env spatl nvs allow in
|
2017-09-13 14:50:42 -07:00
|
|
|
let attrs_list = List.map fst spatl in
|
2011-12-21 07:40:54 -08:00
|
|
|
let is_recursive = (rec_flag = Recursive) in
|
2012-02-14 02:00:18 -08:00
|
|
|
(* If recursive, first unify with an approximation of the expression *)
|
2011-12-21 07:40:54 -08:00
|
|
|
if is_recursive then
|
1999-11-30 08:07:38 -08:00
|
|
|
List.iter2
|
2013-06-03 08:14:19 -07:00
|
|
|
(fun pat binding ->
|
2009-10-26 00:11:36 -07:00
|
|
|
let pat =
|
|
|
|
match pat.pat_type.desc with
|
|
|
|
| Tpoly (ty, tl) ->
|
2011-12-27 00:52:45 -08:00
|
|
|
{pat with pat_type =
|
|
|
|
snd (instance_poly ~keep_names:true false tl ty)}
|
2009-10-26 00:11:36 -07:00
|
|
|
| _ -> pat
|
2019-12-02 11:49:51 -08:00
|
|
|
in unify_pat (ref env) pat (type_approx env binding.pvb_expr))
|
1999-11-30 08:07:38 -08:00
|
|
|
pat_list spat_sexp_list;
|
2012-02-14 02:00:18 -08:00
|
|
|
(* Polymorphic variant processing *)
|
|
|
|
List.iter
|
|
|
|
(fun pat ->
|
|
|
|
if has_variants pat then begin
|
|
|
|
Parmatch.pressure_variants env [pat];
|
2019-09-20 06:23:07 -07:00
|
|
|
finalize_variants pat
|
2012-02-14 02:00:18 -08:00
|
|
|
end)
|
|
|
|
pat_list;
|
|
|
|
(* Generalize the structure *)
|
2002-04-18 00:27:47 -07:00
|
|
|
let pat_list =
|
|
|
|
if !Clflags.principal then begin
|
|
|
|
end_def ();
|
2019-03-12 07:37:59 -07:00
|
|
|
iter_pattern_variables_type generalize_structure pvs;
|
|
|
|
List.map (fun pat ->
|
|
|
|
generalize_structure pat.pat_type;
|
|
|
|
{pat with pat_type = instance pat.pat_type}
|
|
|
|
) pat_list
|
|
|
|
end else
|
|
|
|
pat_list
|
|
|
|
in
|
2002-06-03 00:33:48 -07:00
|
|
|
(* Only bind pattern variables after generalizing *)
|
|
|
|
List.iter (fun f -> f()) force;
|
2018-04-11 12:54:00 -07:00
|
|
|
let sexp_is_fun { pvb_expr = sexp; _ } =
|
|
|
|
match sexp.pexp_desc with
|
|
|
|
| Pexp_fun _ | Pexp_function _ -> true
|
|
|
|
| _ -> false
|
|
|
|
in
|
1995-05-04 03:15:53 -07:00
|
|
|
let exp_env =
|
2018-04-11 10:45:21 -07:00
|
|
|
if is_recursive then new_env
|
2018-05-07 14:30:46 -07:00
|
|
|
else if List.for_all sexp_is_fun spat_sexp_list
|
2018-04-11 12:54:00 -07:00
|
|
|
then begin
|
|
|
|
(* Add ghost bindings to help detecting missing "rec" keywords.
|
|
|
|
|
|
|
|
We only add those if the body of the definition is obviously a
|
|
|
|
function. The rationale is that, in other cases, the hint is probably
|
|
|
|
wrong (and the user is using "advanced features" anyway (lazy,
|
|
|
|
recursive values...)).
|
|
|
|
|
|
|
|
[pvb_loc] (below) is the location of the first let-binding (in case of
|
|
|
|
a let .. and ..), and is where the missing "rec" hint suggests to add a
|
|
|
|
"rec" keyword. *)
|
2018-04-11 10:45:21 -07:00
|
|
|
match spat_sexp_list with
|
|
|
|
| {pvb_loc; _} :: _ -> maybe_add_pattern_variables_ghost pvb_loc env pvs
|
|
|
|
| _ -> assert false
|
|
|
|
end
|
|
|
|
else env in
|
2011-12-21 07:40:54 -08:00
|
|
|
|
|
|
|
let current_slot = ref None in
|
2012-05-29 05:30:49 -07:00
|
|
|
let rec_needed = ref false in
|
2017-09-13 14:50:42 -07:00
|
|
|
let warn_about_unused_bindings =
|
|
|
|
List.exists
|
|
|
|
(fun attrs ->
|
|
|
|
Builtin_attributes.warning_scope ~ppwarning:false attrs (fun () ->
|
2017-08-12 13:24:41 -07:00
|
|
|
Warnings.is_active (check "") || Warnings.is_active (check_strict "")
|
|
|
|
|| (is_recursive && (Warnings.is_active Warnings.Unused_rec_flag))))
|
2017-09-13 14:50:42 -07:00
|
|
|
attrs_list
|
2012-05-29 05:30:49 -07:00
|
|
|
in
|
2011-12-21 07:40:54 -08:00
|
|
|
let pat_slot_list =
|
|
|
|
(* Algorithm to detect unused declarations in recursive bindings:
|
|
|
|
- During type checking of the definitions, we capture the 'value_used'
|
|
|
|
events on the bound identifiers and record them in a slot corresponding
|
2012-02-14 02:00:18 -08:00
|
|
|
to the current definition (!current_slot).
|
|
|
|
In effect, this creates a dependency graph between definitions.
|
2011-12-21 07:40:54 -08:00
|
|
|
|
2012-05-29 05:30:49 -07:00
|
|
|
- After type checking the definition (!current_slot = None),
|
2012-02-14 02:00:18 -08:00
|
|
|
when one of the bound identifier is effectively used, we trigger
|
|
|
|
again all the events recorded in the corresponding slot.
|
|
|
|
The effect is to traverse the transitive closure of the graph created
|
2011-12-21 07:40:54 -08:00
|
|
|
in the first step.
|
2011-12-29 09:49:58 -08:00
|
|
|
|
2012-02-14 02:00:18 -08:00
|
|
|
We also keep track of whether *all* variables in a given pattern
|
|
|
|
are unused. If this is the case, for local declarations, the issued
|
|
|
|
warning is 26, not 27.
|
2011-12-21 07:40:54 -08:00
|
|
|
*)
|
2017-09-13 14:50:42 -07:00
|
|
|
List.map2
|
|
|
|
(fun attrs pat ->
|
|
|
|
Builtin_attributes.warning_scope ~ppwarning:false attrs (fun () ->
|
|
|
|
if not warn_about_unused_bindings then pat, None
|
|
|
|
else
|
|
|
|
let some_used = ref false in
|
|
|
|
(* has one of the identifier of this pattern been used? *)
|
|
|
|
let slot = ref [] in
|
|
|
|
List.iter
|
|
|
|
(fun id ->
|
|
|
|
let vd = Env.find_value (Path.Pident id) new_env in
|
2017-08-12 13:24:41 -07:00
|
|
|
(* note: Env.find_value does not trigger the value_used
|
|
|
|
event *)
|
2017-09-13 14:50:42 -07:00
|
|
|
let name = Ident.name id in
|
|
|
|
let used = ref false in
|
|
|
|
if not (name = "" || name.[0] = '_' || name.[0] = '#') then
|
|
|
|
add_delayed_check
|
|
|
|
(fun () ->
|
|
|
|
if not !used then
|
|
|
|
Location.prerr_warning vd.Types.val_loc
|
|
|
|
((if !some_used then check_strict else check) name)
|
|
|
|
);
|
|
|
|
Env.set_value_used_callback
|
2019-08-30 06:28:43 -07:00
|
|
|
vd
|
2017-09-13 14:50:42 -07:00
|
|
|
(fun () ->
|
|
|
|
match !current_slot with
|
|
|
|
| Some slot ->
|
2019-08-30 06:28:43 -07:00
|
|
|
slot := vd.val_uid :: !slot; rec_needed := true
|
2017-09-13 14:50:42 -07:00
|
|
|
| None ->
|
2019-08-30 06:28:43 -07:00
|
|
|
List.iter Env.mark_value_used (get_ref slot);
|
2017-09-13 14:50:42 -07:00
|
|
|
used := true;
|
|
|
|
some_used := true
|
|
|
|
)
|
|
|
|
)
|
|
|
|
(Typedtree.pat_bound_idents pat);
|
|
|
|
pat, Some slot
|
|
|
|
))
|
|
|
|
attrs_list
|
2011-12-21 07:40:54 -08:00
|
|
|
pat_list
|
|
|
|
in
|
1995-05-04 03:15:53 -07:00
|
|
|
let exp_list =
|
1995-11-19 08:53:56 -08:00
|
|
|
List.map2
|
2014-12-27 00:44:50 -08:00
|
|
|
(fun {pvb_expr=sexp; pvb_attributes; _} (pat, slot) ->
|
2011-12-29 09:49:58 -08:00
|
|
|
if is_recursive then current_slot := slot;
|
2009-10-26 00:11:36 -07:00
|
|
|
match pat.pat_type.desc with
|
|
|
|
| Tpoly (ty, tl) ->
|
2011-04-18 19:13:52 -07:00
|
|
|
if !Clflags.principal then begin_def ();
|
2011-12-27 00:52:45 -08:00
|
|
|
let vars, ty' = instance_poly ~keep_names:true true tl ty in
|
2011-04-18 19:13:52 -07:00
|
|
|
if !Clflags.principal then begin
|
|
|
|
end_def ();
|
|
|
|
generalize_structure ty'
|
|
|
|
end;
|
2014-12-27 00:44:50 -08:00
|
|
|
let exp =
|
2019-09-06 12:53:44 -07:00
|
|
|
Builtin_attributes.warning_scope pvb_attributes (fun () ->
|
|
|
|
if rec_flag = Recursive then
|
|
|
|
type_unpacks exp_env unpacks sexp (mk_expected ty')
|
|
|
|
else
|
|
|
|
type_expect exp_env sexp (mk_expected ty')
|
|
|
|
)
|
2014-12-27 00:44:50 -08:00
|
|
|
in
|
2017-02-27 10:09:09 -08:00
|
|
|
exp, Some vars
|
2014-12-27 00:44:50 -08:00
|
|
|
| _ ->
|
2017-02-27 10:09:09 -08:00
|
|
|
let exp =
|
2019-09-06 12:53:44 -07:00
|
|
|
Builtin_attributes.warning_scope pvb_attributes (fun () ->
|
|
|
|
if rec_flag = Recursive then
|
|
|
|
type_unpacks exp_env unpacks sexp (mk_expected pat.pat_type)
|
|
|
|
else
|
|
|
|
type_expect exp_env sexp (mk_expected pat.pat_type))
|
2017-02-27 10:09:09 -08:00
|
|
|
in
|
|
|
|
exp, None)
|
2011-12-21 07:40:54 -08:00
|
|
|
spat_sexp_list pat_slot_list in
|
|
|
|
current_slot := None;
|
2018-09-18 09:14:52 -07:00
|
|
|
if is_recursive && not !rec_needed then begin
|
2016-03-14 09:16:48 -07:00
|
|
|
let {pvb_pat; pvb_attributes} = List.hd spat_sexp_list in
|
|
|
|
(* See PR#6677 *)
|
2017-09-11 15:05:01 -07:00
|
|
|
Builtin_attributes.warning_scope ~ppwarning:false pvb_attributes
|
2016-03-14 09:16:48 -07:00
|
|
|
(fun () ->
|
|
|
|
Location.prerr_warning pvb_pat.ppat_loc Warnings.Unused_rec_flag
|
|
|
|
)
|
|
|
|
end;
|
1995-05-04 03:15:53 -07:00
|
|
|
List.iter2
|
2017-09-11 15:05:01 -07:00
|
|
|
(fun pat (attrs, exp) ->
|
|
|
|
Builtin_attributes.warning_scope ~ppwarning:false attrs
|
|
|
|
(fun () ->
|
|
|
|
ignore(check_partial env pat.pat_type pat.pat_loc
|
|
|
|
[case pat exp])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
pat_list
|
2017-02-27 10:09:09 -08:00
|
|
|
(List.map2 (fun (attrs, _) (e, _) -> attrs, e) spatl exp_list);
|
2019-03-12 07:37:59 -07:00
|
|
|
let pvs = List.map (fun pv -> { pv with pv_type = instance pv.pv_type}) pvs in
|
1995-05-04 03:15:53 -07:00
|
|
|
end_def();
|
1997-03-07 14:00:19 -08:00
|
|
|
List.iter2
|
2017-02-27 10:09:09 -08:00
|
|
|
(fun pat (exp, _) ->
|
2019-04-09 03:28:13 -07:00
|
|
|
if maybe_expansive exp then
|
2019-04-08 23:03:19 -07:00
|
|
|
lower_contravariant env pat.pat_type)
|
1997-03-07 14:00:19 -08:00
|
|
|
pat_list exp_list;
|
2019-03-12 07:37:59 -07:00
|
|
|
iter_pattern_variables_type generalize pvs;
|
2017-02-27 10:09:09 -08:00
|
|
|
List.iter2
|
|
|
|
(fun pat (exp, vars) ->
|
|
|
|
match vars with
|
|
|
|
| None ->
|
|
|
|
(* We generalize expressions even if they are not bound to a variable
|
|
|
|
and do not have an expliclit polymorphic type annotation. This is
|
|
|
|
not needed in general, however those types may be shown by the
|
|
|
|
interactive toplevel, for example:
|
|
|
|
{[
|
|
|
|
let _ = Array.get;;
|
|
|
|
- : 'a array -> int -> 'a = <fun>
|
|
|
|
]}
|
|
|
|
so we do it anyway. *)
|
|
|
|
generalize exp.exp_type
|
|
|
|
| Some vars ->
|
2020-08-27 04:57:11 -07:00
|
|
|
if maybe_expansive exp then
|
|
|
|
lower_contravariant env exp.exp_type;
|
|
|
|
generalize_and_check_univars env "definition" exp pat.pat_type vars)
|
2017-02-27 10:09:09 -08:00
|
|
|
pat_list exp_list;
|
2013-06-03 08:14:19 -07:00
|
|
|
let l = List.combine pat_list exp_list in
|
|
|
|
let l =
|
|
|
|
List.map2
|
2017-02-27 10:09:09 -08:00
|
|
|
(fun (p, (e, _)) pvb ->
|
2014-04-22 08:28:20 -07:00
|
|
|
{vb_pat=p; vb_expr=e; vb_attributes=pvb.pvb_attributes;
|
|
|
|
vb_loc=pvb.pvb_loc;
|
|
|
|
})
|
2013-06-03 08:14:19 -07:00
|
|
|
l spat_sexp_list
|
|
|
|
in
|
2017-09-21 16:04:41 -07:00
|
|
|
if is_recursive then
|
2017-12-02 04:44:54 -08:00
|
|
|
List.iter
|
2017-09-21 16:04:41 -07:00
|
|
|
(fun {vb_pat=pat} -> match pat.pat_desc with
|
|
|
|
Tpat_var _ -> ()
|
|
|
|
| Tpat_alias ({pat_desc=Tpat_any}, _, _) -> ()
|
|
|
|
| _ -> raise(Error(pat.pat_loc, env, Illegal_letrec_pat)))
|
|
|
|
l;
|
2018-08-07 06:02:12 -07:00
|
|
|
List.iter (function
|
|
|
|
| {vb_pat = {pat_desc = Tpat_any; pat_extra; _}; vb_expr; _} ->
|
|
|
|
if not (List.exists (function (Tpat_constraint _, _, _) -> true
|
|
|
|
| _ -> false) pat_extra) then
|
|
|
|
check_partial_application false vb_expr
|
|
|
|
| _ -> ()) l;
|
2013-06-03 08:14:19 -07:00
|
|
|
(l, new_env, unpacks)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2018-07-29 14:29:01 -07:00
|
|
|
and type_andops env sarg sands expected_ty =
|
|
|
|
let rec loop env let_sarg rev_sands expected_ty =
|
|
|
|
match rev_sands with
|
|
|
|
| [] -> type_expect env let_sarg (mk_expected expected_ty), []
|
|
|
|
| { pbop_op = sop; pbop_exp = sexp; pbop_loc = loc; _ } :: rest ->
|
|
|
|
if !Clflags.principal then begin_def ();
|
|
|
|
let op_path, op_desc = type_binding_op_ident env sop in
|
|
|
|
let op_type = instance op_desc.val_type in
|
|
|
|
let ty_arg = newvar () in
|
|
|
|
let ty_rest = newvar () in
|
|
|
|
let ty_result = newvar() in
|
|
|
|
let ty_rest_fun = newty (Tarrow(Nolabel, ty_arg, ty_result, Cok)) in
|
|
|
|
let ty_op = newty (Tarrow(Nolabel, ty_rest, ty_rest_fun, Cok)) in
|
|
|
|
begin try
|
|
|
|
unify env op_type ty_op
|
|
|
|
with Unify trace ->
|
|
|
|
raise(Error(sop.loc, env, Andop_type_clash(sop.txt, trace)))
|
|
|
|
end;
|
|
|
|
if !Clflags.principal then begin
|
|
|
|
end_def ();
|
|
|
|
generalize_structure ty_rest;
|
|
|
|
generalize_structure ty_arg;
|
|
|
|
generalize_structure ty_result
|
|
|
|
end;
|
|
|
|
let let_arg, rest = loop env let_sarg rest ty_rest in
|
|
|
|
let exp = type_expect env sexp (mk_expected ty_arg) in
|
|
|
|
begin try
|
|
|
|
unify env (instance ty_result) (instance expected_ty)
|
|
|
|
with Unify trace ->
|
|
|
|
raise(Error(loc, env, Bindings_type_clash(trace)))
|
|
|
|
end;
|
|
|
|
let andop =
|
|
|
|
{ bop_op_name = sop;
|
|
|
|
bop_op_path = op_path;
|
|
|
|
bop_op_val = op_desc;
|
|
|
|
bop_op_type = op_type;
|
|
|
|
bop_exp = exp;
|
|
|
|
bop_loc = loc }
|
|
|
|
in
|
|
|
|
let_arg, andop :: rest
|
|
|
|
in
|
|
|
|
let let_arg, rev_ands = loop env sarg (List.rev sands) expected_ty in
|
|
|
|
let_arg, List.rev rev_ands
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Typing of toplevel bindings *)
|
|
|
|
|
2020-10-13 00:48:16 -07:00
|
|
|
let type_binding env rec_flag spat_sexp_list =
|
2011-12-29 09:49:58 -08:00
|
|
|
Typetexp.reset_type_variables();
|
2016-03-09 02:40:16 -08:00
|
|
|
let (pat_exp_list, new_env, _unpacks) =
|
2011-12-29 09:49:58 -08:00
|
|
|
type_let
|
|
|
|
~check:(fun s -> Warnings.Unused_value_declaration s)
|
|
|
|
~check_strict:(fun s -> Warnings.Unused_value_declaration s)
|
2018-04-21 12:15:13 -07:00
|
|
|
At_toplevel
|
2020-10-13 00:48:16 -07:00
|
|
|
env rec_flag spat_sexp_list false
|
2011-12-29 09:49:58 -08:00
|
|
|
in
|
|
|
|
(pat_exp_list, new_env)
|
|
|
|
|
2020-10-13 00:48:16 -07:00
|
|
|
let type_let existential_ctx env rec_flag spat_sexp_list =
|
2016-03-09 02:40:16 -08:00
|
|
|
let (pat_exp_list, new_env, _unpacks) =
|
2020-10-13 00:48:16 -07:00
|
|
|
type_let existential_ctx env rec_flag spat_sexp_list false in
|
2010-10-21 16:59:33 -07:00
|
|
|
(pat_exp_list, new_env)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Typing of toplevel expressions *)
|
|
|
|
|
|
|
|
let type_expression env sexp =
|
|
|
|
Typetexp.reset_type_variables();
|
|
|
|
begin_def();
|
|
|
|
let exp = type_exp env sexp in
|
|
|
|
end_def();
|
2019-04-09 03:28:13 -07:00
|
|
|
if maybe_expansive exp then lower_contravariant env exp.exp_type;
|
2016-07-07 02:10:37 -07:00
|
|
|
generalize exp.exp_type;
|
2011-12-26 23:12:06 -08:00
|
|
|
match sexp.pexp_desc with
|
|
|
|
Pexp_ident lid ->
|
2018-10-12 02:20:21 -07:00
|
|
|
let loc = sexp.pexp_loc in
|
2011-12-26 23:12:06 -08:00
|
|
|
(* Special case for keeping type variables when looking-up a variable *)
|
2018-10-12 02:20:21 -07:00
|
|
|
let (_path, desc) = Env.lookup_value ~use:false ~loc lid.txt env in
|
2011-12-26 23:12:06 -08:00
|
|
|
{exp with exp_type = desc.val_type}
|
|
|
|
| _ -> exp
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Error report *)
|
|
|
|
|
2014-12-13 06:46:24 -08:00
|
|
|
let spellcheck ppf unbound_name valid_names =
|
2014-12-13 06:46:22 -08:00
|
|
|
Misc.did_you_mean ppf (fun () ->
|
2014-12-13 06:46:24 -08:00
|
|
|
Misc.spellcheck valid_names unbound_name
|
2014-12-13 06:46:22 -08:00
|
|
|
)
|
|
|
|
|
2014-12-13 06:46:24 -08:00
|
|
|
let spellcheck_idents ppf unbound valid_idents =
|
|
|
|
spellcheck ppf (Ident.name unbound) (List.map Ident.name valid_idents)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
open Format
|
2020-03-03 10:01:42 -08:00
|
|
|
|
|
|
|
let longident = Printtyp.longident
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2019-03-10 07:38:15 -07:00
|
|
|
(* Returns the first diff of the trace *)
|
|
|
|
let type_clash_of_trace trace =
|
|
|
|
Ctype.Unification_trace.(explain trace (fun ~prev:_ -> function
|
|
|
|
| Diff diff -> Some diff
|
|
|
|
| _ -> None
|
|
|
|
))
|
|
|
|
|
2019-03-07 07:36:46 -08:00
|
|
|
(* Hint on type error on integer literals
|
|
|
|
To avoid confusion, it is disabled on float literals
|
|
|
|
and when the expected type is `int` *)
|
2019-04-11 04:54:50 -07:00
|
|
|
let report_literal_type_constraint expected_type const =
|
|
|
|
let const_str = match const with
|
|
|
|
| Const_int n -> Some (Int.to_string n)
|
|
|
|
| Const_int32 n -> Some (Int32.to_string n)
|
|
|
|
| Const_int64 n -> Some (Int64.to_string n)
|
|
|
|
| Const_nativeint n -> Some (Nativeint.to_string n)
|
|
|
|
| _ -> None
|
|
|
|
in
|
|
|
|
let suffix =
|
2019-03-10 07:38:15 -07:00
|
|
|
if Path.same expected_type Predef.path_int32 then
|
2019-04-11 04:54:50 -07:00
|
|
|
Some 'l'
|
2019-03-10 07:38:15 -07:00
|
|
|
else if Path.same expected_type Predef.path_int64 then
|
2019-04-11 04:54:50 -07:00
|
|
|
Some 'L'
|
2019-03-10 07:38:15 -07:00
|
|
|
else if Path.same expected_type Predef.path_nativeint then
|
2019-04-11 04:54:50 -07:00
|
|
|
Some 'n'
|
2019-03-10 07:38:15 -07:00
|
|
|
else if Path.same expected_type Predef.path_float then
|
2019-04-11 04:54:50 -07:00
|
|
|
Some '.'
|
|
|
|
else None
|
2019-03-07 07:36:46 -08:00
|
|
|
in
|
2019-04-11 04:54:50 -07:00
|
|
|
match const_str, suffix with
|
|
|
|
| Some c, Some s -> [ Location.msg "@[Hint: Did you mean `%s%c'?@]" c s ]
|
|
|
|
| _, _ -> []
|
2019-03-07 07:36:46 -08:00
|
|
|
|
2019-04-11 04:54:50 -07:00
|
|
|
let report_literal_type_constraint const = function
|
2019-03-10 07:38:15 -07:00
|
|
|
| Some Unification_trace.
|
|
|
|
{ expected = { t = { desc = Tconstr (typ, [], _) } } } ->
|
2019-04-11 04:54:50 -07:00
|
|
|
report_literal_type_constraint typ const
|
|
|
|
| Some _ | None -> []
|
2019-03-07 07:36:46 -08:00
|
|
|
|
2019-04-11 04:54:50 -07:00
|
|
|
let report_expr_type_clash_hints exp diff =
|
2019-03-07 07:36:46 -08:00
|
|
|
match exp with
|
2019-04-11 04:54:50 -07:00
|
|
|
| Some (Texp_constant const) -> report_literal_type_constraint const diff
|
|
|
|
| _ -> []
|
2019-03-07 07:36:46 -08:00
|
|
|
|
2019-09-20 06:23:07 -07:00
|
|
|
let report_pattern_type_clash_hints
|
|
|
|
(type k) (pat : k pattern_desc option) diff =
|
2019-03-07 11:05:48 -08:00
|
|
|
match pat with
|
2019-04-11 04:54:50 -07:00
|
|
|
| Some (Tpat_constant const) -> report_literal_type_constraint const diff
|
|
|
|
| _ -> []
|
2019-03-07 11:05:48 -08:00
|
|
|
|
2017-12-02 04:44:54 -08:00
|
|
|
let report_type_expected_explanation expl ppf =
|
2019-03-10 07:38:15 -07:00
|
|
|
let because expl_str = fprintf ppf "@ because it is in %s" expl_str in
|
2017-12-02 04:44:54 -08:00
|
|
|
match expl with
|
|
|
|
| If_conditional ->
|
2019-03-10 07:38:15 -07:00
|
|
|
because "the condition of an if-statement"
|
2017-12-02 04:44:54 -08:00
|
|
|
| If_no_else_branch ->
|
2019-03-10 07:38:15 -07:00
|
|
|
because "the result of a conditional with no else branch"
|
2017-12-02 04:44:54 -08:00
|
|
|
| While_loop_conditional ->
|
2019-03-10 07:38:15 -07:00
|
|
|
because "the condition of a while-loop"
|
2017-12-02 04:44:54 -08:00
|
|
|
| While_loop_body ->
|
2019-03-10 07:38:15 -07:00
|
|
|
because "the body of a while-loop"
|
2017-12-02 04:44:54 -08:00
|
|
|
| For_loop_start_index ->
|
2019-03-10 07:38:15 -07:00
|
|
|
because "a for-loop start index"
|
2017-12-02 04:44:54 -08:00
|
|
|
| For_loop_stop_index ->
|
2019-03-10 07:38:15 -07:00
|
|
|
because "a for-loop stop index"
|
2017-12-02 04:44:54 -08:00
|
|
|
| For_loop_body ->
|
2019-03-10 07:38:15 -07:00
|
|
|
because "the body of a for-loop"
|
2017-12-02 04:44:54 -08:00
|
|
|
| Assert_condition ->
|
2019-03-10 07:38:15 -07:00
|
|
|
because "the condition of an assertion"
|
2017-12-02 04:44:54 -08:00
|
|
|
| Sequence_left_hand_side ->
|
2019-03-10 07:38:15 -07:00
|
|
|
because "the left-hand side of a sequence"
|
2018-07-04 07:38:06 -07:00
|
|
|
| When_guard ->
|
2019-03-10 07:38:15 -07:00
|
|
|
because "a when-guard"
|
2017-12-02 04:44:54 -08:00
|
|
|
|
|
|
|
let report_type_expected_explanation_opt expl ppf =
|
|
|
|
match expl with
|
|
|
|
| None -> ()
|
2019-03-10 07:38:15 -07:00
|
|
|
| Some expl -> report_type_expected_explanation expl ppf
|
2017-12-02 04:44:54 -08:00
|
|
|
|
2019-03-10 07:38:15 -07:00
|
|
|
let report_unification_error ~loc ?sub env trace
|
|
|
|
?type_expected_explanation txt1 txt2 =
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.error_of_printer ~loc ?sub (fun ppf () ->
|
2019-03-10 07:38:15 -07:00
|
|
|
Printtyp.report_unification_error ppf env trace
|
|
|
|
?type_expected_explanation txt1 txt2
|
2019-03-10 10:45:10 -07:00
|
|
|
) ()
|
|
|
|
|
|
|
|
let report_error ~loc env = function
|
1995-05-04 03:15:53 -07:00
|
|
|
| Constructor_arity_mismatch(lid, expected, provided) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
2000-03-06 14:12:09 -08:00
|
|
|
"@[The constructor %a@ expects %i argument(s),@ \
|
2009-05-20 04:52:42 -07:00
|
|
|
but is applied here to %i argument(s)@]"
|
2000-03-06 14:12:09 -08:00
|
|
|
longident lid expected provided
|
1996-05-20 09:43:29 -07:00
|
|
|
| Label_mismatch(lid, trace) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
report_unification_error ~loc env trace
|
2000-03-06 14:12:09 -08:00
|
|
|
(function ppf ->
|
2012-11-10 19:46:59 -08:00
|
|
|
fprintf ppf "The record field %a@ belongs to the type"
|
2000-03-16 08:44:21 -08:00
|
|
|
longident lid)
|
2000-03-06 14:12:09 -08:00
|
|
|
(function ppf ->
|
2012-11-10 19:46:59 -08:00
|
|
|
fprintf ppf "but is mixed here with fields of type")
|
2019-03-07 11:05:48 -08:00
|
|
|
| Pattern_type_clash (trace, pat) ->
|
2019-03-10 07:38:15 -07:00
|
|
|
let diff = type_clash_of_trace trace in
|
2019-04-11 04:54:50 -07:00
|
|
|
let sub = report_pattern_type_clash_hints pat diff in
|
|
|
|
Location.error_of_printer ~loc ~sub (fun ppf () ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Printtyp.report_unification_error ppf env trace
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "This pattern matches values of type")
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "but a pattern was expected which matches values of \
|
|
|
|
type");
|
|
|
|
) ()
|
2014-01-27 08:27:05 -08:00
|
|
|
| Or_pattern_type_clash (id, trace) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
report_unification_error ~loc env trace
|
2014-01-27 08:27:05 -08:00
|
|
|
(function ppf ->
|
2015-09-11 04:58:31 -07:00
|
|
|
fprintf ppf "The variable %s on the left-hand side of this \
|
|
|
|
or-pattern has type" (Ident.name id))
|
2014-01-27 08:27:05 -08:00
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "but on the right-hand side it has type")
|
2008-01-11 08:13:18 -08:00
|
|
|
| Multiply_bound_variable name ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
|
|
|
"Variable %s is bound several times in this matching"
|
|
|
|
name
|
2014-12-13 06:46:22 -08:00
|
|
|
| Orpat_vars (id, valid_idents) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.error_of_printer ~loc (fun ppf () ->
|
|
|
|
fprintf ppf
|
|
|
|
"Variable %s must occur on both sides of this | pattern"
|
|
|
|
(Ident.name id);
|
|
|
|
spellcheck_idents ppf id valid_idents
|
|
|
|
) ()
|
2019-03-07 07:36:46 -08:00
|
|
|
| Expr_type_clash (trace, explanation, exp) ->
|
2019-03-10 07:38:15 -07:00
|
|
|
let diff = type_clash_of_trace trace in
|
2019-08-12 02:39:34 -07:00
|
|
|
let sub = report_expr_type_clash_hints exp diff in
|
2019-03-10 07:38:15 -07:00
|
|
|
Location.error_of_printer ~loc ~sub (fun ppf () ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Printtyp.report_unification_error ppf env trace
|
|
|
|
~type_expected_explanation:
|
|
|
|
(report_type_expected_explanation_opt explanation)
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "This expression has type")
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "but an expression was expected of type");
|
|
|
|
) ()
|
1995-05-04 03:15:53 -07:00
|
|
|
| Apply_non_function typ ->
|
1996-04-22 04:15:41 -07:00
|
|
|
begin match (repr typ).desc with
|
1999-11-30 08:07:38 -08:00
|
|
|
Tarrow _ ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
|
|
|
"@[<v>@[<2>This function has type@ %a@]\
|
|
|
|
@ @[It is applied to too many arguments;@ %s@]@]"
|
2020-03-03 10:01:42 -08:00
|
|
|
Printtyp.type_expr typ "maybe you forgot a `;'.";
|
1995-05-04 03:15:53 -07:00
|
|
|
| _ ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc "@[<v>@[<2>This expression has type@ %a@]@ %s@]"
|
2020-03-03 10:01:42 -08:00
|
|
|
Printtyp.type_expr typ
|
2012-05-28 14:57:08 -07:00
|
|
|
"This is not a function; it cannot be applied."
|
1995-05-04 03:15:53 -07:00
|
|
|
end
|
2020-04-17 07:43:38 -07:00
|
|
|
| Apply_wrong_label (l, ty, extra_info) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
let print_label ppf = function
|
2014-12-22 00:45:55 -08:00
|
|
|
| Nolabel -> fprintf ppf "without label"
|
2019-03-10 10:45:10 -07:00
|
|
|
| l -> fprintf ppf "with label %s" (prefixed_label_name l)
|
2001-12-25 19:50:22 -08:00
|
|
|
in
|
2020-04-17 07:43:38 -07:00
|
|
|
let extra_info =
|
|
|
|
if not extra_info then
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
[ Location.msg
|
|
|
|
"Since OCaml 4.11, optional arguments do not commute when \
|
|
|
|
-nolabels is given" ]
|
|
|
|
in
|
|
|
|
Location.errorf ~loc ~sub:extra_info
|
2009-05-20 04:52:42 -07:00
|
|
|
"@[<v>@[<2>The function applied to this argument has type@ %a@]@.\
|
2019-03-10 10:45:10 -07:00
|
|
|
This argument cannot be applied %a@]"
|
2020-03-03 10:01:42 -08:00
|
|
|
Printtyp.type_expr ty print_label l
|
2012-11-14 08:59:33 -08:00
|
|
|
| Label_multiply_defined s ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc "The record field label %s is defined several times"
|
|
|
|
s
|
2001-06-28 18:46:46 -07:00
|
|
|
| Label_missing labels ->
|
2012-05-31 01:07:31 -07:00
|
|
|
let print_labels ppf =
|
|
|
|
List.iter (fun lbl -> fprintf ppf "@ %s" (Ident.name lbl)) in
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc "@[<hov>Some record fields are undefined:%a@]"
|
2001-06-28 18:46:46 -07:00
|
|
|
print_labels labels
|
1995-05-04 03:15:53 -07:00
|
|
|
| Label_not_mutable lid ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc "The record field %a is not mutable" longident lid
|
2019-12-12 12:01:42 -08:00
|
|
|
| Wrong_name (eorp, ty_expected, { type_path; kind; name; valid_names; }) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.error_of_printer ~loc (fun ppf () ->
|
2020-03-03 10:01:42 -08:00
|
|
|
Printtyp.wrap_printing_env ~error:true env (fun () ->
|
|
|
|
let { ty; explanation } = ty_expected in
|
|
|
|
if Path.is_constructor_typath type_path then begin
|
|
|
|
fprintf ppf
|
|
|
|
"@[The field %s is not part of the record \
|
|
|
|
argument for the %a constructor@]"
|
|
|
|
name.txt
|
2020-03-03 10:07:43 -08:00
|
|
|
Printtyp.type_path type_path;
|
2020-03-03 10:01:42 -08:00
|
|
|
end else begin
|
|
|
|
fprintf ppf
|
|
|
|
"@[@[<2>%s type@ %a%t@]@ \
|
|
|
|
The %s %s does not belong to type %a@]"
|
|
|
|
eorp Printtyp.type_expr ty
|
|
|
|
(report_type_expected_explanation_opt explanation)
|
|
|
|
(Datatype_kind.label_name kind)
|
2020-03-03 10:07:43 -08:00
|
|
|
name.txt (*kind*) Printtyp.type_path type_path;
|
2020-03-03 10:01:42 -08:00
|
|
|
end;
|
|
|
|
spellcheck ppf name.txt valid_names
|
|
|
|
)) ()
|
2012-11-10 19:46:59 -08:00
|
|
|
| Name_type_mismatch (kind, lid, tp, tpl) ->
|
2019-12-14 04:49:28 -08:00
|
|
|
let type_name = Datatype_kind.type_name kind in
|
|
|
|
let name = Datatype_kind.label_name kind in
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.error_of_printer ~loc (fun ppf () ->
|
2020-03-03 10:01:42 -08:00
|
|
|
Printtyp.report_ambiguous_type_error ppf env tp tpl
|
2019-03-10 10:45:10 -07:00
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "The %s %a@ belongs to the %s type"
|
2019-12-12 06:09:35 -08:00
|
|
|
name longident lid type_name)
|
2019-03-10 10:45:10 -07:00
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "The %s %a@ belongs to one of the following %s types:"
|
2019-12-12 06:09:35 -08:00
|
|
|
name longident lid type_name)
|
2019-03-10 10:45:10 -07:00
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "but a %s was expected belonging to the %s type"
|
2019-12-12 06:09:35 -08:00
|
|
|
name type_name)
|
2019-03-10 10:45:10 -07:00
|
|
|
) ()
|
2014-05-12 08:37:29 -07:00
|
|
|
| Invalid_format msg ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc "%s" msg
|
2014-12-13 06:46:27 -08:00
|
|
|
| Undefined_method (ty, me, valid_methods) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.error_of_printer ~loc (fun ppf () ->
|
2020-03-03 10:01:42 -08:00
|
|
|
Printtyp.wrap_printing_env ~error:true env (fun () ->
|
|
|
|
fprintf ppf
|
|
|
|
"@[<v>@[This expression has type@;<1 2>%a@]@,\
|
|
|
|
It has no method %s@]" Printtyp.type_expr ty me;
|
|
|
|
begin match valid_methods with
|
|
|
|
| None -> ()
|
|
|
|
| Some valid_methods -> spellcheck ppf me valid_methods
|
|
|
|
end
|
|
|
|
)) ()
|
2014-12-13 06:46:27 -08:00
|
|
|
| Undefined_inherited_method (me, valid_methods) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.error_of_printer ~loc (fun ppf () ->
|
|
|
|
fprintf ppf "This expression has no method %s" me;
|
|
|
|
spellcheck ppf me valid_methods;
|
|
|
|
) ()
|
1996-04-22 04:15:41 -07:00
|
|
|
| Virtual_class cl ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc "Cannot instantiate the virtual class %a"
|
2003-02-27 22:59:19 -08:00
|
|
|
longident cl
|
2014-12-13 06:46:29 -08:00
|
|
|
| Unbound_instance_variable (var, valid_vars) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.error_of_printer ~loc (fun ppf () ->
|
|
|
|
fprintf ppf "Unbound instance variable %s" var;
|
|
|
|
spellcheck ppf var valid_vars;
|
|
|
|
) ()
|
2018-10-12 02:20:21 -07:00
|
|
|
| Instance_variable_not_mutable v ->
|
|
|
|
Location.errorf ~loc "The instance variable %s is not mutable" v
|
1996-05-26 06:42:34 -07:00
|
|
|
| Not_subtype(tr1, tr2) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.error_of_printer ~loc (fun ppf () ->
|
2020-03-03 10:01:42 -08:00
|
|
|
Printtyp.report_subtyping_error ppf env tr1 "is not a subtype of" tr2
|
2019-03-10 10:45:10 -07:00
|
|
|
) ()
|
1996-04-22 04:15:41 -07:00
|
|
|
| Outside_class ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
|
|
|
"This object duplication occurs outside a method definition"
|
1996-04-22 04:15:41 -07:00
|
|
|
| Value_multiply_overridden v ->
|
2019-03-10 07:38:15 -07:00
|
|
|
Location.errorf ~loc
|
|
|
|
"The instance variable %s is overridden several times"
|
|
|
|
v
|
2002-06-12 02:52:08 -07:00
|
|
|
| Coercion_failure (ty, ty', trace, b) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.error_of_printer ~loc (fun ppf () ->
|
|
|
|
Printtyp.report_unification_error ppf env trace
|
|
|
|
(function ppf ->
|
2020-03-03 10:01:42 -08:00
|
|
|
let ty, ty' = Printtyp.prepare_expansion (ty, ty') in
|
2019-03-10 07:38:15 -07:00
|
|
|
fprintf ppf "This expression cannot be coerced to type@;<1 2>%a;@ \
|
|
|
|
it has type"
|
2020-03-03 10:01:42 -08:00
|
|
|
(Printtyp.type_expansion ty) ty')
|
2019-03-10 10:45:10 -07:00
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "but is here used with type");
|
|
|
|
if b then
|
|
|
|
fprintf ppf ".@.@[<hov>%s@ %s@ %s@]"
|
|
|
|
"This simple coercion was not fully general."
|
|
|
|
"Hint: Consider using a fully explicit coercion"
|
|
|
|
"of the form: `(foo : ty1 :> ty2)'."
|
|
|
|
) ()
|
2017-12-02 04:44:54 -08:00
|
|
|
| Too_many_arguments (in_function, ty, explanation) ->
|
2001-11-21 22:47:29 -08:00
|
|
|
if in_function then begin
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
|
|
|
"This function expects too many arguments,@ \
|
|
|
|
it should have type@ %a%t"
|
2020-03-03 10:01:42 -08:00
|
|
|
Printtyp.type_expr ty
|
2017-12-02 04:44:54 -08:00
|
|
|
(report_type_expected_explanation_opt explanation)
|
2001-11-21 22:47:29 -08:00
|
|
|
end else begin
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
|
|
|
"This expression should not be a function,@ \
|
|
|
|
the expected type is@ %a%t"
|
2020-03-03 10:01:42 -08:00
|
|
|
Printtyp.type_expr ty
|
2017-12-02 04:44:54 -08:00
|
|
|
(report_type_expected_explanation_opt explanation)
|
2001-11-21 22:47:29 -08:00
|
|
|
end
|
2017-12-02 04:44:54 -08:00
|
|
|
| Abstract_wrong_label (l, ty, explanation) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
let label_mark = function
|
2014-12-22 00:45:55 -08:00
|
|
|
| Nolabel -> "but its first argument is not labelled"
|
2015-09-11 04:58:31 -07:00
|
|
|
| l -> sprintf "but its first argument is labelled %s"
|
|
|
|
(prefixed_label_name l) in
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
|
|
|
"@[<v>@[<2>This function should have type@ %a%t@]@,%s@]"
|
2020-03-03 10:01:42 -08:00
|
|
|
Printtyp.type_expr ty
|
2017-12-02 04:44:54 -08:00
|
|
|
(report_type_expected_explanation_opt explanation)
|
|
|
|
(label_mark l)
|
1998-02-26 04:54:44 -08:00
|
|
|
| Scoping_let_module(id, ty) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
|
|
|
"This `let module' expression has type@ %a@ \
|
|
|
|
In this type, the locally bound module name %s escapes its scope"
|
2020-03-03 10:01:42 -08:00
|
|
|
Printtyp.type_expr ty id
|
2003-05-01 15:22:37 -07:00
|
|
|
| Private_type ty ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc "Cannot create values of the private type %a"
|
2020-03-03 10:01:42 -08:00
|
|
|
Printtyp.type_expr ty
|
2003-07-02 02:14:35 -07:00
|
|
|
| Private_label (lid, ty) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc "Cannot assign field %a of the private type %a"
|
2020-03-03 10:01:42 -08:00
|
|
|
longident lid Printtyp.type_expr ty
|
2019-04-04 05:03:26 -07:00
|
|
|
| Private_constructor (constr, ty) ->
|
|
|
|
Location.errorf ~loc
|
|
|
|
"Cannot use private constructor %s to create values of type %a"
|
2020-03-03 10:01:42 -08:00
|
|
|
constr.cstr_name Printtyp.type_expr ty
|
2000-02-21 19:08:08 -08:00
|
|
|
| Not_a_variant_type lid ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc "The type %a@ is not a variant type" longident lid
|
2001-04-19 01:34:21 -07:00
|
|
|
| Incoherent_label_order ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
|
|
|
"This function is applied to arguments@ \
|
|
|
|
in an order different from other calls.@ \
|
|
|
|
This is only allowed when the real type is known."
|
2002-04-18 00:27:47 -07:00
|
|
|
| Less_general (kind, trace) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
report_unification_error ~loc env trace
|
2002-04-18 00:27:47 -07:00
|
|
|
(fun ppf -> fprintf ppf "This %s has type" kind)
|
|
|
|
(fun ppf -> fprintf ppf "which is less general than")
|
2010-10-21 16:59:33 -07:00
|
|
|
| Modules_not_allowed ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc "Modules are not allowed in this pattern."
|
2010-10-21 16:59:33 -07:00
|
|
|
| Cannot_infer_signature ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
2010-10-21 16:59:33 -07:00
|
|
|
"The signature for this packaged module couldn't be inferred."
|
|
|
|
| Not_a_packed_module ty ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
2010-10-21 16:59:33 -07:00
|
|
|
"This expression is packed module, but the expected type is@ %a"
|
2020-03-03 10:01:42 -08:00
|
|
|
Printtyp.type_expr ty
|
2019-03-10 10:45:10 -07:00
|
|
|
| Unexpected_existential (reason, name, types) ->
|
|
|
|
let reason_str =
|
|
|
|
match reason with
|
|
|
|
| In_class_args ->
|
|
|
|
"Existential types are not allowed in class arguments"
|
|
|
|
| In_class_def ->
|
|
|
|
"Existential types are not allowed in bindings inside \
|
|
|
|
class definition"
|
|
|
|
| In_self_pattern ->
|
|
|
|
"Existential types are not allowed in self patterns"
|
|
|
|
| At_toplevel ->
|
|
|
|
"Existential types are not allowed in toplevel bindings"
|
|
|
|
| In_group ->
|
|
|
|
"Existential types are not allowed in \"let ... and ...\" bindings"
|
|
|
|
| In_rec ->
|
|
|
|
"Existential types are not allowed in recursive bindings"
|
|
|
|
| With_attributes ->
|
|
|
|
"Existential types are not allowed in presence of attributes"
|
|
|
|
in
|
|
|
|
begin match List.find (fun ty -> ty <> "$" ^ name) types with
|
|
|
|
| example ->
|
|
|
|
Location.errorf ~loc
|
|
|
|
"%s,@ but this pattern introduces the existential type %s."
|
|
|
|
reason_str example
|
|
|
|
| exception Not_found ->
|
|
|
|
Location.errorf ~loc
|
|
|
|
"%s,@ but the constructor %s introduces existential types."
|
|
|
|
reason_str name
|
|
|
|
end
|
2013-04-16 08:34:09 -07:00
|
|
|
| Invalid_interval ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
|
|
|
"@[Only character intervals are supported in patterns.@]"
|
2013-12-02 10:00:18 -08:00
|
|
|
| Invalid_for_loop_index ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
2014-01-22 17:58:37 -08:00
|
|
|
"@[Invalid for-loop index: only variables and _ are allowed.@]"
|
2014-05-05 04:49:37 -07:00
|
|
|
| No_value_clauses ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
2014-05-05 04:49:37 -07:00
|
|
|
"None of the patterns in this 'match' expression match values."
|
2018-01-15 03:11:06 -08:00
|
|
|
| Exception_pattern_disallowed ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
2018-01-15 03:11:06 -08:00
|
|
|
"@[Exception patterns are not allowed in this position.@]"
|
|
|
|
| Mixed_value_and_exception_patterns_under_guard ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
2018-01-15 03:11:06 -08:00
|
|
|
"@[Mixing value and exception patterns under when-guards is not \
|
|
|
|
supported.@]"
|
2014-10-14 08:51:30 -07:00
|
|
|
| Inlined_record_escape ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
2014-10-14 08:51:30 -07:00
|
|
|
"@[This form is not allowed as the type of the inlined record could \
|
|
|
|
escape.@]"
|
2016-03-21 14:41:25 -07:00
|
|
|
| Inlined_record_expected ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
2016-03-21 14:41:25 -07:00
|
|
|
"@[This constructor expects an inlined record argument.@]"
|
2015-10-14 18:55:52 -07:00
|
|
|
| Unrefuted_pattern pat ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
2015-10-14 18:55:52 -07:00
|
|
|
"@[%s@ %s@ %a@]"
|
|
|
|
"This match case could not be refuted."
|
2015-10-23 07:36:56 -07:00
|
|
|
"Here is an example of a value that would reach it:"
|
2017-07-07 03:02:37 -07:00
|
|
|
Printpat.top_pretty pat
|
2015-10-30 04:37:07 -07:00
|
|
|
| Invalid_extension_constructor_payload ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
2015-10-30 04:37:07 -07:00
|
|
|
"Invalid [%%extension_constructor] payload, a constructor is expected."
|
|
|
|
| Not_an_extension_constructor ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
2015-10-30 04:37:07 -07:00
|
|
|
"This constructor is not an extension constructor."
|
2015-04-18 20:07:32 -07:00
|
|
|
| Literal_overflow ty ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
|
|
|
"Integer literal exceeds the range of representable integers of type %s"
|
|
|
|
ty
|
2015-04-18 20:07:32 -07:00
|
|
|
| Unknown_literal (n, m) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc "Unknown modifier '%c' for literal %s%c" m n m
|
2017-09-21 16:04:41 -07:00
|
|
|
| Illegal_letrec_pat ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
2017-09-21 16:04:41 -07:00
|
|
|
"Only variables are allowed as left-hand side of `let rec'"
|
|
|
|
| Illegal_letrec_expr ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
2017-09-21 16:04:41 -07:00
|
|
|
"This kind of expression is not allowed as right-hand side of `let rec'"
|
|
|
|
| Illegal_class_expr ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Location.errorf ~loc
|
|
|
|
"This kind of recursive class expression is not allowed"
|
2018-07-29 14:29:01 -07:00
|
|
|
| Letop_type_clash(name, trace) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
report_unification_error ~loc env trace
|
2018-07-29 14:29:01 -07:00
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "The operator %s has type" name)
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "but it was expected to have type")
|
|
|
|
| Andop_type_clash(name, trace) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
report_unification_error ~loc env trace
|
2018-07-29 14:29:01 -07:00
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "The operator %s has type" name)
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "but it was expected to have type")
|
|
|
|
| Bindings_type_clash(trace) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
report_unification_error ~loc env trace
|
2018-07-29 14:29:01 -07:00
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "These bindings have type")
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "but bindings were expected of type")
|
2011-12-21 07:40:54 -08:00
|
|
|
|
2019-03-10 10:45:10 -07:00
|
|
|
let report_error ~loc env err =
|
2020-03-03 10:07:43 -08:00
|
|
|
Printtyp.wrap_printing_env ~error:true env
|
|
|
|
(fun () -> report_error ~loc env err)
|
2012-01-22 23:59:45 -08:00
|
|
|
|
2013-09-11 09:08:00 -07:00
|
|
|
let () =
|
|
|
|
Location.register_error_of_exn
|
|
|
|
(function
|
|
|
|
| Error (loc, env, err) ->
|
2019-03-10 10:45:10 -07:00
|
|
|
Some (report_error ~loc env err)
|
2014-05-07 01:26:17 -07:00
|
|
|
| Error_forward err ->
|
|
|
|
Some err
|
2013-09-11 09:08:00 -07:00
|
|
|
| _ ->
|
|
|
|
None
|
|
|
|
)
|
|
|
|
|
2011-12-21 07:40:54 -08:00
|
|
|
let () =
|
2019-01-27 03:27:08 -08:00
|
|
|
Persistent_env.add_delayed_check_forward := add_delayed_check;
|
|
|
|
Env.add_delayed_check_forward := add_delayed_check;
|
|
|
|
()
|
2014-10-14 08:51:30 -07:00
|
|
|
|
|
|
|
(* drop ?recarg argument from the external API *)
|
|
|
|
let type_expect ?in_function env e ty = type_expect ?in_function env e ty
|
|
|
|
let type_exp env e = type_exp env e
|
|
|
|
let type_argument env e t1 t2 = type_argument env e t1 t2
|