Archived/OCaml Errors
OCaml Errors
This page provides code examples which cause various OCaml compiler errors. The filenames refer to the OCaml source files in which the errors are defined. The internal error type definitions are used to refer to the errors. Please add examples to the various issues listed here as you come across them.
typing/typecore.ml
Unbound_value of Longident.t
# x;; Unbound value x
Unbound_constructor of Longident.t
# X;; Unbound constructor X
Unbound_label of Longident.t Polymorphic_label of Longident.t Constructor_arity_mismatch of Longident.t * int * int
# type x = A of int;; type x = A of int # A;; The constructor A expects 1 argument(s), but is here applied to 0 argument(s)
Label_mismatch of Longident.t * (type_expr * type_expr) list Pattern_type_clash of (type_expr * type_expr) list
# let f x = match (x:int) with | true -> 1 | false -> 0;; This pattern matches values of type bool but is here used to match values of type int
Multiply_bound_variable of string Orpat_vars of Ident.t Expr_type_clash of (type_expr * type_expr) list
# true+1;; This expression has type bool but is here used with type int
Apply_non_function of type_expr
# let f x = 2 in f 2 3;; This function is applied to too many arguments, maybe you forgot a `;'
# let f = 2 in f 3;; This expression is not a function, it cannot be applied
Apply_wrong_label of label * type_expr
# let f ~x () = x+1 in f ~y:3 ();; Expecting function has type x:int -> int This argument cannot be applied with label ~y
# let f ~x ~x = 3+x in f 3;; Expecting function has type x:'a -> x:int -> int This argument cannot be applied without label
Label_multiply_defined of Longident.t Label_missing of string list Label_not_mutable of Longident.t Incomplete_format of string
# Printf.printf "%";; Premature end of format string ``"%"''
Bad_conversion of string * int * char Undefined_method of type_expr * string
# class c = object method m = 3 end;; class c : object method m : int end # let p = new c;; val p : c = <obj> # p#fred;; This expression has type c It has no method fred
Undefined_inherited_method of string Unbound_class of Longident.t
# let p = new fred;; Unbound class fred
Virtual_class of Longident.t
# class virtual c = object method virtual m : int end;; class virtual c : object method virtual m : int end # let p = new c;; One cannot create instances of the virtual class c
Private_type of type_expr Private_label of Longident.t * type_expr Unbound_instance_variable of string
# class c = object method setx = x <- x + 3 end;; Unbound instance variable x
Instance_variable_not_mutable of string
# class c = object val x = 3 method setx = x <- x + 3 end;; The instance variable x is not mutable
Not_subtype of (type_expr * type_expr) list * (type_expr * type_expr) list
# class c = object method f () = 4 end;; class c : object method f : unit -> int end # class d = object inherit c method g () = 3 end;; class d : object method f : unit -> int method g : unit -> int end # let p = new c in (p : c :> d);; Type c = < f : unit -> int > is not a subtype of type d = < f : unit -> int; g : unit -> int >
Outside_class
Value_multiply_overridden of string
Coercion_failure of type_expr * type_expr * (type_expr * type_expr) list * bool
Too_many_arguments of bool * type_expr
Abstract_wrong_label of label * type_expr
Scoping_let_module of string * type_expr
# let module M = struct type t = C let x = C end in M.x;; This `let module' expression has type M.t In this type, the locally bound module name M escapes its scope
Masked_instance_variable of Longident.t
Not_a_variant_type of Longident.t
Incoherent_label_order
Less_general of string * (type_expr * type_expr) list
typing/typeclass.ml
Unconsistent_constraint of (type_expr * type_expr) list
Field_type_mismatch of string * string * (type_expr * type_expr) list
Structure_expected of class_type
Cannot_apply of class_type
Apply_wrong_label of label
Pattern_type_clash of type_expr
# type t = Jim;; type t = Jim # class c = object (x : t) end;; This pattern cannot match self: it only matches values of type t
Repeated_parameter
Unbound_class of Longident.t
Unbound_class_2 of Longident.t
Unbound_class_type of Longident.t
Unbound_class_type_2 of Longident.t
Abbrev_type_clash of type_expr * type_expr * type_expr
Constructor_type_mismatch of string * (type_expr * type_expr) list
Virtual_class of bool * string list * string list
# class c = object method virtual f : int end;; This class should be virtual. The following methods are undefined : f
Parameter_arity_mismatch of Longident.t * int * int
Parameter_mismatch of (type_expr * type_expr) list
Bad_parameters of Ident.t * type_expr * type_expr
Class_match_failure of Ctype.class_match_failure list
Unbound_val of string
Unbound_type_var of (formatter -> unit) * Ctype.closed_class_failure
# class c = object method f x = true end;; The method f has type 'a -> bool where 'a is unbound
# class ['b] c (x : 'a) = object method f = x end;; The method f has type 'b where 'b is unbound
Make_nongen_seltype of type_expr
Non_generalizable_class of Ident.t * Types.class_declaration
Cannot_coerce_self of type_expr
Non_collapsable_conjunction of Ident.t * Types.class_declaration * (type_expr * type_expr) list
Final_self_clash of (type_expr * type_expr) list
Mutability_mismatch of string * mutable_flag
typing/typedecl.ml
Repeated_parameter
Duplicate_constructor of string
# type t = Jim | Jim;; Two constructors are named Jim
Too_many_constructors
Duplicate_label of string
Recursive_abbrev of string
Definition_mismatch of type_expr
Constraint_failed of type_expr * type_expr
Unconsistent_constraint of (type_expr * type_expr) list
Type_clash of (type_expr * type_expr) list
Parameters_differ of Path.t * type_expr * type_expr
Null_arity_external
Missing_native_external
Unbound_type_var of type_expr * type_declaration
Unbound_exception of Longident.t
Not_an_exception of Longident.t
Bad_variance of int * (bool * bool) * (bool * bool)
# module M = struct type -'a t end;; module M : sig type -'a t end # module N = struct type +'a t = 'a M.t end;; In this definition, expected parameter variances are not satisfied. The 1st type parameter was expected to be covariant, but it is contravariant# (* with over 10 type parameters *)
module M = struct type ('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k, -'l) t end;; module M : sig type ('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k, -'l) t end # module N = struct type ('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k, +'l) t = ('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k, 'l) M.t end;; In this definition, expected parameter variances are not satisfied. The 12th type parameter was expected to be covariant, but it is contravariant
Unavailable_type_constructor of Path.t
Bad_fixed_type of string
# type t = private int;; This fixed type is not an object or variant
typing/typemod.ml
Unbound_module of Longident.t
Unbound_modtype of Longident.t
Cannot_apply of module_type
Not_included of Includemod.error list
Cannot_eliminate_dependency of module_type
Signature_expected
Structure_expected of module_type
With_no_component of Longident.t
With_mismatch of Longident.t * Includemod.error list
Repeated_name of string * string
Non_generalizable of type_expr
Non_generalizable_class of Ident.t * class_declaration
Non_generalizable_module of module_type
Implementation_is_required of string
Interface_not_compiled of string
typing/typetexp.ml
Unbound_type_variable of string
# type t = Jim of 'a;; Unbound type parameter 'a
Unbound_type_constructor of Longident.t
Unbound_type_constructor_2 of Path.t
Type_arity_mismatch of Longident.t * int * int
Bound_type_variable of string
Recursive_type
Unbound_class of Longident.t
Unbound_row_variable of Longident.t
Type_mismatch of (type_expr * type_expr) list
Alias_type_mismatch of (type_expr * type_expr) list
Present_has_conjunction of string
Present_has_no_type of string
Constructor_mismatch of type_expr * type_expr
Not_a_variant of type_expr
Variant_tags of string * string
Invalid_variable_name of string
# type t = '_a list;; The type variable name '_a is not allowed in programs
However, the compiler can use this kind of variables to specify the type of polymorphic variables which become monomorphic after their first usage (it is a bit tricky ...):
# let f = List.sort compare;; val f : '_a list -> '_a list = <fun> # f [2;1;3];; - : int list = [1; 2; 3] # f;; - : int list -> int list = <fun>
Cannot_quantify of string * type_expr
Unification error explanations
Errors relating to unification failure invoke the report_unification_error function from typing/printtyp.ml. In certain circumstances this function prints one of the following explanations.
Self type cannot escape its class
The type constructor t would escape its scope
# let x = ref [];; val x : '_a list ref = {contents = []} # type t = C;; type t = C # x := [C];; The type constructor t would escape its scope
The universal variable v would escape its scope
Self type cannot be unified with a closed object type
Types for method m are incompatible
The first object type has no method m
The second object type has no method m
These two variant types have no intersection
The first variant type does not allow tag(s) t
The second variant type does not allow tag(s) t
Types for tag `t are incompatible