Skip to content

Commit 2bcdff7

Browse files
committed
PR comments
1 parent e33decf commit 2bcdff7

11 files changed

+56
-32
lines changed

compiler/ml/error_message_utils.ml

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ type type_clash_context =
9999
operator: string;
100100
is_constant: string option;
101101
}
102-
| FunctionArgument of {optional: bool}
102+
| FunctionArgument of {optional: bool; name: string option}
103103
| Statement of type_clash_statement
104104
| ForLoopCondition
105105
@@ -138,11 +138,17 @@ let error_type_text ppf type_clash_context =
138138
139139
let error_expected_type_text ppf type_clash_context =
140140
match type_clash_context with
141-
| Some (FunctionArgument {optional}) ->
142-
fprintf ppf "But this%s function argument is expecting:"
141+
| Some (FunctionArgument {optional; name}) ->
142+
fprintf ppf "But this%s function argument"
143143
(match optional with
144144
| false -> ""
145-
| true -> " optional")
145+
| true -> " optional");
146+
147+
(match name with
148+
| Some name -> fprintf ppf " @{<info>~%s@}" name
149+
| None -> ());
150+
151+
fprintf ppf " is expecting:"
146152
| Some ComparisonOperator ->
147153
fprintf ppf "But it's being compared to something of type:"
148154
| Some SwitchReturn -> fprintf ppf "But this switch is expected to return:"
@@ -158,21 +164,21 @@ let error_expected_type_text ppf type_clash_context =
158164
fprintf ppf "But this @{<info>if@} statement is expected to return:"
159165
| Some ArrayValue ->
160166
fprintf ppf "But this array is expected to have items of type:"
161-
| Some (SetRecordField _) -> fprintf ppf "But this record field is of type:"
167+
| Some (SetRecordField _) -> fprintf ppf "But the record field is of type:"
162168
| Some
163169
(RecordField {field_name = "children"; jsx = Some {jsx_type = `Fragment}})
164170
->
165-
fprintf ppf "But children of JSX fragments is expected to have type:"
171+
fprintf ppf "But children of JSX fragments are expected to have type:"
166172
| Some
167173
(RecordField
168174
{field_name = "children"; jsx = Some {jsx_type = `CustomComponent}}) ->
169175
fprintf ppf
170-
"But children passed to this component is expected to have type:"
176+
"But children passed to this component are expected to have type:"
171177
| Some (RecordField {field_name; jsx = Some _}) ->
172-
fprintf ppf "But this component prop @{<info>%s@} is expected to have type:"
178+
fprintf ppf "But the component prop @{<info>%s@} is expected to have type:"
173179
field_name
174180
| Some (RecordField {field_name}) ->
175-
fprintf ppf "But this record field @{<info>%s@} is expected to have type:"
181+
fprintf ppf "But the record field @{<info>%s@} is expected to have type:"
176182
field_name
177183
| Some (Statement FunctionCall) -> fprintf ppf "But it's expected to return:"
178184
| Some (MathOperator {operator}) ->
@@ -429,9 +435,8 @@ let print_extra_type_clash_help ~extract_concrete_typedecl ~env loc ppf
429435
@,\
430436
@{<info>%s@} is an optional record field, and you're passing an \
431437
optional value to it.@,\
432-
Optional fields expect you to pass the concrete value, not an option, \
433-
when passed directly.\n\
434-
\ @,\
438+
Optional fields expect you to pass the concrete value, not an option.\n\
439+
\ @,\
435440
Possible solutions: @,\
436441
- Unwrap the option and pass a concrete value directly@,\
437442
- If you really do want to pass the optional value, prepend the value \
@@ -447,8 +452,8 @@ let print_extra_type_clash_help ~extract_concrete_typedecl ~env loc ppf
447452
@,\
448453
You're passing an optional value into an optional function argument.@,\
449454
Optional function arguments expect you to pass the concrete value, not \
450-
an option, when passed directly.\n\
451-
\ @,\
455+
an option.\n\
456+
\ @,\
452457
Possible solutions: @,\
453458
- Unwrap the option and pass a concrete value directly@,\
454459
- If you really do want to pass the optional value, prepend the value \
@@ -510,9 +515,9 @@ let type_clash_context_from_function sexp sfunct =
510515
Some (MathOperator {for_float = true; operator; is_constant})
511516
| Pexp_ident {txt = Lident (("/" | "*" | "+" | "-") as operator)} ->
512517
Some (MathOperator {for_float = false; operator; is_constant})
513-
| _ -> Some (FunctionArgument {optional = false})
518+
| _ -> None
514519
515-
let type_clash_context_for_function_argument type_clash_context sarg0 =
520+
let type_clash_context_for_function_argument ~label type_clash_context sarg0 =
516521
match type_clash_context with
517522
| Some (MathOperator {for_float; operator}) ->
518523
Some
@@ -527,7 +532,16 @@ let type_clash_context_for_function_argument type_clash_context sarg0 =
527532
Some txt
528533
| _ -> None);
529534
})
530-
| None -> Some (FunctionArgument {optional = false})
535+
| None ->
536+
Some
537+
(FunctionArgument
538+
{
539+
optional = false;
540+
name =
541+
(match label with
542+
| Asttypes.Nolabel -> None
543+
| Optional {txt = l} | Labelled {txt = l} -> Some l);
544+
})
531545
| type_clash_context -> type_clash_context
532546
533547
let type_clash_context_maybe_option ty_expected ty_res =

compiler/ml/typecore.ml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,12 +3644,22 @@ and type_application ~context total_app env funct (sargs : sargs) :
36443644
(if (not optional) || is_optional_loc l' then fun () ->
36453645
type_argument
36463646
~context:
3647-
(type_clash_context_for_function_argument context sarg0)
3647+
(type_clash_context_for_function_argument ~label:l' context
3648+
sarg0)
36483649
env sarg0 ty ty0
36493650
else fun () ->
36503651
option_some
36513652
(type_argument
3652-
~context:(Some (FunctionArgument {optional = true}))
3653+
~context:
3654+
(Some
3655+
(FunctionArgument
3656+
{
3657+
optional = true;
3658+
name =
3659+
(match l' with
3660+
| Nolabel -> None
3661+
| Optional l | Labelled l -> Some l.txt);
3662+
}))
36533663
env sarg0
36543664
(extract_option_type env ty)
36553665
(extract_option_type env ty0))) )

tests/build_tests/super_errors/expected/inline_types_record_type_params.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
15 ┆ otherExtra: Some({test: true, anotherInlined: {record: true}}),
1010

1111
This has type: int
12-
But this record field age is expected to have type: string
12+
But the record field age is expected to have type: string
1313

1414
You can convert int to string with Int.toString.

tests/build_tests/super_errors/expected/jsx_custom_component_children.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
25 │
99

1010
This has type: float
11-
But children passed to this component is expected to have type:
11+
But children passed to this component are expected to have type:
1212
React.element (defined as Jsx.element)
1313

1414
In JSX, all content must be JSX elements. You can convert float to a JSX element with React.float.

tests/build_tests/super_errors/expected/jsx_custom_component_optional.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
32 │
99

1010
This has type: string
11-
But this component prop someOpt is expected to have type: float
11+
But the component prop someOpt is expected to have type: float
1212

1313
You can convert string to float with Float.fromString.

tests/build_tests/super_errors/expected/jsx_type_mismatch_float.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
18 │
99

1010
This has type: float
11-
But children of JSX fragments is expected to have type:
11+
But children of JSX fragments are expected to have type:
1212
React.element (defined as Jsx.element)
1313

1414
In JSX, all content must be JSX elements. You can convert float to a JSX element with React.float.

tests/build_tests/super_errors/expected/jsx_type_mismatch_int.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
18 │
99

1010
This has type: int
11-
But children of JSX fragments is expected to have type:
11+
But children of JSX fragments are expected to have type:
1212
React.element (defined as Jsx.element)
1313

1414
In JSX, all content must be JSX elements. You can convert int to a JSX element with React.int.

tests/build_tests/super_errors/expected/jsx_type_mismatch_string.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
18 │
99

1010
This has type: string
11-
But children of JSX fragments is expected to have type:
11+
But children of JSX fragments are expected to have type:
1212
React.element (defined as Jsx.element)
1313

1414
In JSX, all content must be JSX elements. You can convert string to a JSX element with React.string.

tests/build_tests/super_errors/expected/optional_fn_argument_pass_option.res.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
6 │
99

1010
This has type: option<int>
11-
But this optional function argument is expecting: int
11+
But this optional function argument ~x is expecting: int
1212

1313
You're passing an optional value into an optional function argument.
14-
Optional function arguments expect you to pass the concrete value, not an option, when passed directly.
15-
14+
Optional function arguments expect you to pass the concrete value, not an option.
15+
1616
Possible solutions:
1717
- Unwrap the option and pass a concrete value directly
1818
- If you really do want to pass the optional value, prepend the value with ? to show you want to pass the option, like: ?t

tests/build_tests/super_errors/expected/optional_record_field_pass_option.res.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
5 │
99

1010
This has type: option<bool>
11-
But this record field test is expected to have type: bool
11+
But the record field test is expected to have type: bool
1212

1313
test is an optional record field, and you're passing an optional value to it.
14-
Optional fields expect you to pass the concrete value, not an option, when passed directly.
15-
14+
Optional fields expect you to pass the concrete value, not an option.
15+
1616
Possible solutions:
1717
- Unwrap the option and pass a concrete value directly
1818
- If you really do want to pass the optional value, prepend the value with ? to show you want to pass the option, like: {test: ?t}

tests/build_tests/super_errors/expected/set_record_field_type_match.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
12 │
99

1010
You're assigning something to this field that has type: int
11-
But this record field is of type: string
11+
But the record field is of type: string
1212

1313
You can convert int to string with Int.toString.

0 commit comments

Comments
 (0)