@@ -303,6 +303,40 @@ let maybe_pessimise_type env ty =
303
303
else
304
304
(env, ty)
305
305
306
+ let type_index ~read env p ty_have ty_expect enforced reason =
307
+ let log_name =
308
+ if read then
309
+ " array_get/type_index"
310
+ else
311
+ " assign_array_set/type_index"
312
+ in
313
+ Typing_log. (
314
+ log_with_level env " typing" ~level: 1 (fun () ->
315
+ log_types
316
+ (Pos_or_decl. of_raw_pos p)
317
+ env
318
+ [
319
+ Log_head
320
+ ( log_name,
321
+ [
322
+ Log_type (" ty_have" , ty_have);
323
+ Log_type (" ty_expect" , ty_expect);
324
+ ] );
325
+ ]));
326
+ let (env, ty_err_opt) =
327
+ Typing_coercion. coerce_type
328
+ ~coerce_for_op: true
329
+ p
330
+ reason
331
+ env
332
+ ty_have
333
+ ty_expect
334
+ enforced
335
+ Typing_error.Callback. index_type_mismatch
336
+ in
337
+ let ty_mismatch = mk_ty_mismatch_res ty_have ty_expect ty_err_opt in
338
+ ((env, ty_err_opt), ty_mismatch)
339
+
306
340
(* * Typing of array-get like expressions; [ty1] is the type of the expression
307
341
into which we are indexing (the 'collection'), [e2] is the index expression
308
342
and [ty2] is the type of that expression.
@@ -409,34 +443,6 @@ let rec array_get
409
443
e2
410
444
ty2
411
445
in
412
- let type_index env p ty_have ty_expect enforced_expect reason =
413
- Typing_log. (
414
- log_with_level env " typing" ~level: 1 (fun () ->
415
- log_types
416
- (Pos_or_decl. of_raw_pos p)
417
- env
418
- [
419
- Log_head
420
- ( " array_get/type_index" ,
421
- [
422
- Log_type (" ty_have" , ty_have);
423
- Log_type (" ty_expect" , ty_expect);
424
- ] );
425
- ]));
426
- let (env, ty_err_opt) =
427
- Typing_coercion. coerce_type
428
- ~coerce_for_op: true
429
- p
430
- reason
431
- env
432
- ty_have
433
- ty_expect
434
- enforced_expect
435
- Typing_error.Callback. index_type_mismatch
436
- in
437
- let ty_mismatch = mk_ty_mismatch_res ty_have ty_expect ty_err_opt in
438
- ((env, ty_err_opt), ty_mismatch)
439
- in
440
446
let got_dynamic () =
441
447
let tv = MakeType. dynamic r in
442
448
let (env, idx_ty_err_opt) =
@@ -465,7 +471,14 @@ let rec array_get
465
471
let (_, p2, _) = e2 in
466
472
let ty1 = MakeType. int (Reason. idx_vector p2) in
467
473
let ((env, ty_err_opt), idx_err_res) =
468
- type_index env expr_pos ty2 ty1 Enforced (Reason. index_class cn)
474
+ type_index
475
+ ~read: true
476
+ env
477
+ expr_pos
478
+ ty2
479
+ ty1
480
+ Enforced
481
+ (Reason. index_class cn)
469
482
in
470
483
Option. iter ty_err_opt ~f: (Typing_error_utils. add_typing_error ~env );
471
484
(env, (ty, dflt_arr_res, idx_err_res))
@@ -504,7 +517,14 @@ let rec array_get
504
517
if String. equal cn SN.Collections. cMap then
505
518
let (env, k) = Env. expand_type env k in
506
519
let (env, k) = maybe_pessimise_type env k in
507
- type_index env expr_pos ty2 k Enforced (Reason. index_class cn)
520
+ type_index
521
+ ~read: true
522
+ env
523
+ expr_pos
524
+ ty2
525
+ k
526
+ Enforced
527
+ (Reason. index_class cn)
508
528
else
509
529
let (env, res) = check_arraykey_index_read env expr_pos ty1 ty2 in
510
530
((env, None ), res)
@@ -553,7 +573,14 @@ let rec array_get
553
573
let (_, p2, _) = e2 in
554
574
let ty1 = MakeType. int (Reason. idx (p2, r)) in
555
575
let ((env, ty_err1), idx_err_res) =
556
- type_index env expr_pos ty2 ty1 Enforced (Reason. index_class cn)
576
+ type_index
577
+ ~read: true
578
+ env
579
+ expr_pos
580
+ ty2
581
+ ty1
582
+ Enforced
583
+ (Reason. index_class cn)
557
584
in
558
585
Option. iter ty_err1 ~f: (Typing_error_utils. add_typing_error ~env );
559
586
(env, (ty, dflt_arr_res, idx_err_res))
@@ -587,7 +614,7 @@ let rec array_get
587
614
let (_, p2, _) = e2 in
588
615
let ty1 = MakeType. int (Reason. idx (p2, r)) in
589
616
let ((env, ty_err1), idx_err_res) =
590
- type_index env expr_pos ty2 ty1 Enforced Reason. index_array
617
+ type_index ~read: true env expr_pos ty2 ty1 Enforced Reason. index_array
591
618
in
592
619
Option. iter ty_err1 ~f: (Typing_error_utils. add_typing_error ~env );
593
620
(env, (ty, dflt_arr_res, idx_err_res))
@@ -1190,21 +1217,6 @@ let assign_array_get ~array_pos ~expr_pos ur env ty1 (key : Nast.expr) tkey ty2
1190
1217
@@ Primary. Array_get_arity
1191
1218
{ pos = expr_pos; name; decl_pos = Reason. to_pos r })
1192
1219
in
1193
- let type_index env p ty_have ty_expect enforced reason =
1194
- let (env, ty_err_opt) =
1195
- Typing_coercion. coerce_type
1196
- ~coerce_for_op: true
1197
- p
1198
- reason
1199
- env
1200
- ty_have
1201
- ty_expect
1202
- enforced
1203
- Typing_error.Callback. index_type_mismatch
1204
- in
1205
- let ty_mismatch = mk_ty_mismatch_res ty_have ty_expect ty_err_opt in
1206
- ((env, ty_err_opt), ty_mismatch)
1207
- in
1208
1220
let got_dynamic () =
1209
1221
let tv = MakeType. dynamic r in
1210
1222
let (env, ty_err1) =
@@ -1236,7 +1248,14 @@ let assign_array_get ~array_pos ~expr_pos ur env ty1 (key : Nast.expr) tkey ty2
1236
1248
let (_, p, _) = key in
1237
1249
let tk = MakeType. int (Reason. idx_vector p) in
1238
1250
let ((env, ty_err1), idx_err) =
1239
- type_index env expr_pos tkey tk Enforced (Reason. index_class cn)
1251
+ type_index
1252
+ ~read: false
1253
+ env
1254
+ expr_pos
1255
+ tkey
1256
+ tk
1257
+ Enforced
1258
+ (Reason. index_class cn)
1240
1259
in
1241
1260
let (env, ty_err2) =
1242
1261
Typing_ops. sub_type
@@ -1264,7 +1283,14 @@ let assign_array_get ~array_pos ~expr_pos ur env ty1 (key : Nast.expr) tkey ty2
1264
1283
let (_, p, _) = key in
1265
1284
let tk = MakeType. int (Reason. idx_vector p) in
1266
1285
let ((env, ty_err1), idx_err) =
1267
- type_index env expr_pos tkey tk Enforced (Reason. index_class cn)
1286
+ type_index
1287
+ ~read: false
1288
+ env
1289
+ expr_pos
1290
+ tkey
1291
+ tk
1292
+ Enforced
1293
+ (Reason. index_class cn)
1268
1294
in
1269
1295
let (env, tv) = maybe_make_supportdyn r env ~supportdyn tv in
1270
1296
let (env, tv') = Typing_union. union env tv ty2 in
@@ -1301,7 +1327,14 @@ let assign_array_get ~array_pos ~expr_pos ur env ty1 (key : Nast.expr) tkey ty2
1301
1327
(env, Unenforced , tk)
1302
1328
in
1303
1329
let ((env, ty_err1), idx_err2) =
1304
- type_index env expr_pos tkey tk enforced (Reason. index_class cn)
1330
+ type_index
1331
+ ~read: false
1332
+ env
1333
+ expr_pos
1334
+ tkey
1335
+ tk
1336
+ enforced
1337
+ (Reason. index_class cn)
1305
1338
in
1306
1339
let idx_err =
1307
1340
match (idx_err1, idx_err2) with
@@ -1442,7 +1475,14 @@ let assign_array_get ~array_pos ~expr_pos ur env ty1 (key : Nast.expr) tkey ty2
1442
1475
let tk = MakeType. int (Reason. idx (p, r)) in
1443
1476
let tv = MakeType. string (Reason. witness expr_pos) in
1444
1477
let ((env, ty_err1), idx_err) =
1445
- type_index env expr_pos tkey tk Enforced Reason. index_array
1478
+ type_index
1479
+ ~read: false
1480
+ env
1481
+ expr_pos
1482
+ tkey
1483
+ tk
1484
+ Enforced
1485
+ Reason. index_array
1446
1486
in
1447
1487
let (env, tv') = maybe_pessimise_type env tv in
1448
1488
let (env, ty_err2) =
0 commit comments