@@ -2,23 +2,48 @@ open Printtyp
2
2
3
3
let fprintf = Format. fprintf
4
4
5
-
6
- let pp_component_type_not_generalizable_pre ppf =
7
- fprintf ppf " @[<v>\
8
- @[@{<info>Is this a ReasonReact reducerComponent or component with retained props?@}@ \
9
- If so, you can fix it by doing one of these:@]@,\
10
- @[- Defining the component's `make` function@]@,\
11
- @[- Annotating the state once with a type, wherever it's used (e.g. render)@]@,\
12
- @[- Annotating actions (in e.g. reducer)@]@,\
13
- @[- Annotating retained props, if any@]@,@,\
14
- @[@{<info>Here's the original error message@}@]\
15
- @]@,"
16
-
17
- let pp_component_type_not_generalizable_post ppf () =
18
- fprintf ppf
19
- " @[This happens when the type system senses there's a mutation/side-effect,@ in combination with a polymorphic value.@,\
20
- Using or annotating that value usually solves it.@ \
21
- More info:@ https://realworldocaml.org/v1/en/html/imperative-programming-1.html#side-effects-and-weak-polymorphism@]"
5
+ let non_generalizable_msg ppf ~result ~is_module print_fallback_msg =
6
+ let contain_vs_be =
7
+ if is_module then " This module seems to contain" else " This seems to be" in
8
+ match result with
9
+ | (_ , true , _ ) :: _ ->
10
+ fprintf ppf " @[<v>\
11
+ @{<info>%s a ReasonReact reducerComponentWithRetainedProps?@}@ \
12
+ The retained props feature is deprecated.@ \
13
+ Please use a regular @{<info>reducerComponent@} and keep the props you want to retain in state.\
14
+ @]"
15
+ contain_vs_be
16
+ | (true , _ , _ ) :: _ ->
17
+ fprintf ppf " @[<v>\
18
+ @[\
19
+ @{<info>%s a ReasonReact reducerComponent?@}@ \
20
+ We don't have@ all@ the@ type@ info@ for@ its@ @{<info>state@}.@ \
21
+ Make sure you've done the following: @]@,@,\
22
+ @[- Define the component `make` function@]@,\
23
+ @[- Define `reducer` in that `make` body@]@,\
24
+ @[- Annotate reducer's second parameter (state) with the desired state type\
25
+ @]\
26
+ @]"
27
+ contain_vs_be
28
+ | (_ , _ , true ) :: _ ->
29
+ fprintf ppf " @[<v>\
30
+ @[\
31
+ @{<info>%s a ReasonReact reducerComponent?@}@ \
32
+ We don't have@ all@ the@ type@ info@ for@ its@ @{<info>action@}.@ \
33
+ Make sure you've done the following: @]@,@,\
34
+ @[- Define the component `make` function@]@,\
35
+ @[- Define `reducer` in that `make` body@]@,\
36
+ @[- Annotate reducer's first parameter (action) with the desired action type\
37
+ @]\
38
+ @]"
39
+ contain_vs_be
40
+ | _ ->
41
+ fprintf ppf
42
+ " %a@,@,\
43
+ @[This happens when the type system senses there's a mutation/side-effect,@ in combination with a polymorphic value.@,\
44
+ @{<info>Using or annotating that value usually solves it.@}@ \
45
+ More info:@ https://realworldocaml.org/v1/en/html/imperative-programming-1.html#side-effects-and-weak-polymorphism@]"
46
+ print_fallback_msg ()
22
47
23
48
(* taken from https://github.com/BuckleScript/ocaml/blob/d4144647d1bf9bc7dc3aadc24c25a7efa3a67915/typing/typemod.ml#L1754 *)
24
49
(* modified branches are commented *)
@@ -57,14 +82,14 @@ let report_error ppf = Typemod.(function
57
82
| Non_generalizable typ ->
58
83
(* modified *)
59
84
fprintf ppf " @[<v>" ;
60
- if Super_reason_react. type_is_component_spec typ then begin
61
- pp_component_type_not_generalizable_pre ppf
62
- end ;
63
- fprintf ppf
64
- " @[This expression's type contains type variables that can't be generalized:@,@{<error>%a@}@]@,@,\
65
- %a "
66
- type_scheme typ
67
- pp_component_type_not_generalizable_post ( ) ;
85
+ non_generalizable_msg
86
+ ppf
87
+ ~result: ([ Super_reason_react. component_spec_weak_type_variables typ])
88
+ ~is_module: false
89
+ ( fun ppf () ->
90
+ fprintf ppf
91
+ " @[This expression's type contains type variables that can't be generalized:@,@{<error>%a@}@] "
92
+ type_scheme typ );
68
93
fprintf ppf " @]"
69
94
| Non_generalizable_class (id , desc ) ->
70
95
fprintf ppf
@@ -74,15 +99,15 @@ let report_error ppf = Typemod.(function
74
99
| Non_generalizable_module mty ->
75
100
(* modified *)
76
101
fprintf ppf " @[<v>" ;
77
- if Super_reason_react. module_type_is_component_spec mty then begin
78
- pp_component_type_not_generalizable_pre ppf
79
- end ;
80
- fprintf ppf
81
- " @[The type of this module contains type variables that cannot be generalized:@,@{<error>%a@}@]@,@,\
82
- %a "
83
- modtype mty
84
- pp_component_type_not_generalizable_post ( ) ;
85
- fprintf ppf " @]"
102
+ non_generalizable_msg
103
+ ppf
104
+ ~result: ( Super_reason_react. component_spec_weak_type_variables_in_module_type mty)
105
+ ~is_module: true
106
+ ( fun ppf () ->
107
+ fprintf ppf
108
+ " @[The type of this module contains type variables that cannot be generalized:@,@{<error>%a@}@] "
109
+ modtype mty );
110
+ fprintf ppf " @]"
86
111
| Implementation_is_required intf_name ->
87
112
fprintf ppf
88
113
" @[The interface %a@ declares values, not just types.@ \
0 commit comments