|
| 1 | +open Printtyp |
| 2 | + |
| 3 | +let fprintf = Format.fprintf |
| 4 | + |
| 5 | +(* taken from https://github.com/BuckleScript/ocaml/blob/d4144647d1bf9bc7dc3aadc24c25a7efa3a67915/typing/typemod.ml#L1754 *) |
| 6 | +(* modified branches are commented *) |
| 7 | +let report_error ppf = Typemod.(function |
| 8 | + Cannot_apply mty -> |
| 9 | + fprintf ppf |
| 10 | + "@[This module is not a functor; it has type@ %a@]" modtype mty |
| 11 | + | Not_included errs -> |
| 12 | + fprintf ppf |
| 13 | + "@[<v>Signature mismatch:@ %a@]" Includemod.report_error errs |
| 14 | + | Cannot_eliminate_dependency mty -> |
| 15 | + fprintf ppf |
| 16 | + "@[This functor has type@ %a@ \ |
| 17 | + The parameter cannot be eliminated in the result type.@ \ |
| 18 | + Please bind the argument to a module identifier.@]" modtype mty |
| 19 | + | Signature_expected -> fprintf ppf "This module type is not a signature" |
| 20 | + | Structure_expected mty -> |
| 21 | + fprintf ppf |
| 22 | + "@[This module is not a structure; it has type@ %a" modtype mty |
| 23 | + | With_no_component lid -> |
| 24 | + fprintf ppf |
| 25 | + "@[The signature constrained by `with' has no component named %a@]" |
| 26 | + longident lid |
| 27 | + | With_mismatch(lid, explanation) -> |
| 28 | + fprintf ppf |
| 29 | + "@[<v>\ |
| 30 | + @[In this `with' constraint, the new definition of %a@ \ |
| 31 | + does not match its original definition@ \ |
| 32 | + in the constrained signature:@]@ \ |
| 33 | + %a@]" |
| 34 | + longident lid Includemod.report_error explanation |
| 35 | + | Repeated_name(kind, name) -> |
| 36 | + fprintf ppf |
| 37 | + "@[Multiple definition of the %s name %s.@ \ |
| 38 | + Names must be unique in a given structure or signature.@]" kind name |
| 39 | + | Non_generalizable typ -> |
| 40 | + (* modified *) |
| 41 | + fprintf ppf "@["; |
| 42 | + if Super_reason_react.type_is_component_spec typ then begin |
| 43 | + fprintf ppf "@[<v>\ |
| 44 | + @[@{<info>Is this a ReasonReact component with state or retained props?@}@ If so, this error will disappear after:@]@,\ |
| 45 | + @[- Defining the component's `make` function@]@,\ |
| 46 | + @[- Using the state once or annotating it with a type where it's used (e.g. render)@]\ |
| 47 | + @[- Do the same for retained props, if any@]@,@,\ |
| 48 | + @[@{<info>Here's the original error message@}@]\ |
| 49 | + @]@," |
| 50 | + end; |
| 51 | + fprintf ppf |
| 52 | + "@[<v>@[This expression's type contains type variables that can't be generalized:@,@{<error>%a@}@]@,@,\ |
| 53 | + @[This happens when the type system senses there's a mutation/side-effect, in combination with a polymorphic value.@,\ |
| 54 | + Using or annotating that value usually solves it.@ \ |
| 55 | + More info:@ https://realworldocaml.org/v1/en/html/imperative-programming-1.html#side-effects-and-weak-polymorphism@]@]" |
| 56 | + type_scheme |
| 57 | + typ; |
| 58 | + fprintf ppf "@]" |
| 59 | + | Non_generalizable_class (id, desc) -> |
| 60 | + fprintf ppf |
| 61 | + "@[The type of this class,@ %a,@ \ |
| 62 | + contains type variables that cannot be generalized@]" |
| 63 | + (class_declaration id) desc |
| 64 | + | Non_generalizable_module mty -> |
| 65 | + fprintf ppf |
| 66 | + "@[The type of this module,@ %a,@ \ |
| 67 | + contains type variables that cannot be generalized@]" modtype mty; |
| 68 | + | Implementation_is_required intf_name -> |
| 69 | + fprintf ppf |
| 70 | + "@[The interface %a@ declares values, not just types.@ \ |
| 71 | + An implementation must be provided.@]" |
| 72 | + Location.print_filename intf_name |
| 73 | + | Interface_not_compiled intf_name -> |
| 74 | + fprintf ppf |
| 75 | + "@[Could not find the .cmi file for interface@ %a.@]" |
| 76 | + Location.print_filename intf_name |
| 77 | + | Not_allowed_in_functor_body -> |
| 78 | + fprintf ppf |
| 79 | + "@[This expression creates fresh types.@ %s@]" |
| 80 | + "It is not allowed inside applicative functors." |
| 81 | + | With_need_typeconstr -> |
| 82 | + fprintf ppf |
| 83 | + "Only type constructors with identical parameters can be substituted." |
| 84 | + | Not_a_packed_module ty -> |
| 85 | + fprintf ppf |
| 86 | + "This expression is not a packed module. It has type@ %a" |
| 87 | + type_expr ty |
| 88 | + | Incomplete_packed_module ty -> |
| 89 | + fprintf ppf |
| 90 | + "The type of this packed module contains variables:@ %a" |
| 91 | + type_expr ty |
| 92 | + | Scoping_pack (lid, ty) -> |
| 93 | + fprintf ppf |
| 94 | + "The type %a in this module cannot be exported.@ " longident lid; |
| 95 | + fprintf ppf |
| 96 | + "Its type contains local dependencies:@ %a" type_expr ty |
| 97 | + | Recursive_module_require_explicit_type -> |
| 98 | + fprintf ppf "Recursive modules require an explicit module type." |
| 99 | + | Apply_generative -> |
| 100 | + fprintf ppf "This is a generative functor. It can only be applied to ()" |
| 101 | +) |
| 102 | + |
| 103 | +let report_error env ppf err = |
| 104 | + Printtyp.wrap_printing_env env (fun () -> report_error ppf err) |
| 105 | + |
| 106 | +(* This will be called in super_main. This is how you'd override the default error printer from the compiler & register new error_of_exn handlers *) |
| 107 | +let setup () = |
| 108 | + Location.register_error_of_exn |
| 109 | + (function |
| 110 | + | Typemod.Error (loc, env, err) -> |
| 111 | + Some (Super_location.error_of_printer loc (report_error env) err) |
| 112 | + | Typemod.Error_forward err -> |
| 113 | + Some err |
| 114 | + | _ -> |
| 115 | + None |
| 116 | + ) |
0 commit comments