-
Notifications
You must be signed in to change notification settings - Fork 471
Error for braced single identifier to record #7806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2256,6 +2256,18 @@ let rec type_exp ~context ?recarg env sexp = | |
*) | ||
|
||
and type_expect ~context ?in_function ?recarg env sexp ty_expected = | ||
(* Special errors for braced identifiers passed to records *) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use example pattern, or else "braced identifier" is a bit difficult to understand for the comment reader. |
||
let context = | ||
match sexp.pexp_desc with | ||
| Pexp_ident _ -> | ||
if | ||
sexp.pexp_attributes | ||
|> List.exists (fun (attr, _) -> attr.txt = "res.braces") | ||
&& is_record_type ~extract_concrete_typedecl ~env ty_expected | ||
then Some Error_message_utils.BracedIdent | ||
else context | ||
| _ -> context | ||
in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cristianoc I've tried to guard this lookup as much as possible, but do you think it's worth seeing if this processing could be moved to only happen if there's an actual type error? I guess I could catch and reraise. |
||
let previous_saved_types = Cmt_format.get_saved_types () in | ||
let exp = | ||
Builtin_attributes.warning_scope sexp.pexp_attributes (fun () -> | ||
|
@@ -3394,7 +3406,9 @@ and type_label_exp ~call_context create env loc ty_expected | |
(lid, label, {arg with exp_type = instance env arg.exp_type}, opt) | ||
|
||
and type_argument ~context ?recarg env sarg ty_expected' ty_expected = | ||
print_endline "hey1"; | ||
let texp = type_expect ~context ?recarg env sarg ty_expected' in | ||
print_endline "hey"; | ||
unify_exp ~context env texp ty_expected; | ||
texp | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
[1;31mWe've found a bug for you![0m | ||
[36m/.../fixtures/pass_braced_identifier_to_record.res[0m:[2m4:15-25[0m | ||
|
||
2 [2m│[0m let householdId = "12" | ||
3 [2m│[0m | ||
[1;31m4[0m [2m│[0m let ff: xx = {[1;31mhouseholdId[0m} | ||
5 [2m│[0m | ||
|
||
This has type: [1;31mstring[0m | ||
But it's expected to have type: [1;33mxx[0m | ||
|
||
You might have meant to pass this as a record, but wrote it as a block. | ||
Braces with a single identifier counts as a block, not a record with a single (punned) field. | ||
|
||
Possible solutions: | ||
- Return the expected record from the block | ||
- Write out the full record with field and value, like: [1;33m{householdId: householdId} | ||
[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
[1;31mWe've found a bug for you![0m | ||
[36m/.../fixtures/pass_braced_identifier_to_record_fn_argument.res[0m:[2m8:15-25[0m | ||
|
||
6 [2m│[0m | ||
7 [2m│[0m let householdId = "12" | ||
[1;31m8[0m [2m│[0m let ff = doX({[1;31mhouseholdId[0m}) | ||
9 [2m│[0m | ||
|
||
This has type: [1;31mstring[0m | ||
But it's expected to have type: [1;33mxx[0m | ||
|
||
You might have meant to pass this as a record, but wrote it as a block. | ||
Braces with a single identifier counts as a block, not a record with a single (punned) field. | ||
|
||
Possible solutions: | ||
- Return the expected record from the block | ||
- Write out the full record with field and value, like: [1;33m{householdId: householdId} | ||
[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type xx = {householdId: string} | ||
let householdId = "12" | ||
|
||
let ff: xx = {householdId} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
type xx = {householdId: string} | ||
|
||
let doX = ({householdId}) => { | ||
householdId | ||
} | ||
|
||
let householdId = "12" | ||
let ff = doX({householdId}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think writing out the record fully without punning will be the most common fix, so maybe we should show it first in the list of possible solutions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mediremi good point. Done in the latest commit.