Skip to content

Commit 7b601f2

Browse files
committed
Fix issue with type environments and unified ops.
Fixes #7275
1 parent c527775 commit 7b601f2

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
- Fix async context checking for module await. https://github.com/rescript-lang/rescript/pull/7271
2222
- Fix `%external` extension. https://github.com/rescript-lang/rescript/pull/7272
23+
- Fix issue with type environment for unified ops. https://github.com/rescript-lang/rescript/pull/7277
2324

2425
# 12.0.0-alpha.8
2526

compiler/ml/typecore.ml

+18-18
Original file line numberDiff line numberDiff line change
@@ -3379,22 +3379,22 @@ and translate_unified_ops (env : Env.t) (funct : Typedtree.expression)
33793379
let result_type =
33803380
match (lhs_type.desc, specialization) with
33813381
| Tconstr (path, _, _), _ when Path.same path Predef.path_int ->
3382-
Predef.type_int
3382+
instance_def Predef.type_int
33833383
| Tconstr (path, _, _), {bool = Some _}
33843384
when Path.same path Predef.path_bool ->
3385-
Predef.type_bool
3385+
instance_def Predef.type_bool
33863386
| Tconstr (path, _, _), {float = Some _}
33873387
when Path.same path Predef.path_float ->
3388-
Predef.type_float
3388+
instance_def Predef.type_float
33893389
| Tconstr (path, _, _), {bigint = Some _}
33903390
when Path.same path Predef.path_bigint ->
3391-
Predef.type_bigint
3391+
instance_def Predef.type_bigint
33923392
| Tconstr (path, _, _), {string = Some _}
33933393
when Path.same path Predef.path_string ->
3394-
Predef.type_string
3394+
instance_def Predef.type_string
33953395
| _ ->
3396-
unify env lhs_type Predef.type_int;
3397-
Predef.type_int
3396+
unify env lhs_type (instance_def Predef.type_int);
3397+
instance_def Predef.type_int
33983398
in
33993399
let targs = [(to_noloc lhs_label, Some lhs)] in
34003400
Some (targs, result_type)
@@ -3409,50 +3409,50 @@ and translate_unified_ops (env : Env.t) (funct : Typedtree.expression)
34093409
match (lhs_type.desc, specialization) with
34103410
| Tconstr (path, _, _), _ when Path.same path Predef.path_int ->
34113411
let rhs = type_expect env rhs_expr Predef.type_int in
3412-
(lhs, rhs, Predef.type_int)
3412+
(lhs, rhs, instance_def Predef.type_int)
34133413
| Tconstr (path, _, _), {bool = Some _}
34143414
when Path.same path Predef.path_bool ->
34153415
let rhs = type_expect env rhs_expr Predef.type_bool in
3416-
(lhs, rhs, Predef.type_bool)
3416+
(lhs, rhs, instance_def Predef.type_bool)
34173417
| Tconstr (path, _, _), {float = Some _}
34183418
when Path.same path Predef.path_float ->
34193419
let rhs = type_expect env rhs_expr Predef.type_float in
3420-
(lhs, rhs, Predef.type_float)
3420+
(lhs, rhs, instance_def Predef.type_float)
34213421
| Tconstr (path, _, _), {bigint = Some _}
34223422
when Path.same path Predef.path_bigint ->
34233423
let rhs = type_expect env rhs_expr Predef.type_bigint in
3424-
(lhs, rhs, Predef.type_bigint)
3424+
(lhs, rhs, instance_def Predef.type_bigint)
34253425
| Tconstr (path, _, _), {string = Some _}
34263426
when Path.same path Predef.path_string ->
34273427
let rhs = type_expect env rhs_expr Predef.type_string in
3428-
(lhs, rhs, Predef.type_string)
3428+
(lhs, rhs, instance_def Predef.type_string)
34293429
| _ -> (
34303430
(* Rule 2. Try unifying to rhs *)
34313431
match (rhs_type.desc, specialization) with
34323432
| Tconstr (path, _, _), _ when Path.same path Predef.path_int ->
34333433
let lhs = type_expect env lhs_expr Predef.type_int in
3434-
(lhs, rhs, Predef.type_int)
3434+
(lhs, rhs, instance_def Predef.type_int)
34353435
| Tconstr (path, _, _), {bool = Some _}
34363436
when Path.same path Predef.path_bool ->
34373437
let lhs = type_expect env lhs_expr Predef.type_bool in
3438-
(lhs, rhs, Predef.type_bool)
3438+
(lhs, rhs, instance_def Predef.type_bool)
34393439
| Tconstr (path, _, _), {float = Some _}
34403440
when Path.same path Predef.path_float ->
34413441
let lhs = type_expect env lhs_expr Predef.type_float in
3442-
(lhs, rhs, Predef.type_float)
3442+
(lhs, rhs, instance_def Predef.type_float)
34433443
| Tconstr (path, _, _), {bigint = Some _}
34443444
when Path.same path Predef.path_bigint ->
34453445
let lhs = type_expect env lhs_expr Predef.type_bigint in
3446-
(lhs, rhs, Predef.type_bigint)
3446+
(lhs, rhs, instance_def Predef.type_bigint)
34473447
| Tconstr (path, _, _), {string = Some _}
34483448
when Path.same path Predef.path_string ->
34493449
let lhs = type_expect env lhs_expr Predef.type_string in
3450-
(lhs, rhs, Predef.type_string)
3450+
(lhs, rhs, instance_def Predef.type_string)
34513451
| _ ->
34523452
(* Rule 3. Fallback to int *)
34533453
let lhs = type_expect env lhs_expr Predef.type_int in
34543454
let rhs = type_expect env rhs_expr Predef.type_int in
3455-
(lhs, rhs, Predef.type_int))
3455+
(lhs, rhs, instance_def Predef.type_int))
34563456
in
34573457
let targs =
34583458
[(to_noloc lhs_label, Some lhs); (to_noloc rhs_label, Some rhs)]

tests/tests/src/EnvUnifiedOps.mjs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
4+
function n(x) {
5+
return x + 1 | 0;
6+
}
7+
8+
let X = {
9+
n: n
10+
};
11+
12+
let z = 3;
13+
14+
export {
15+
X,
16+
z,
17+
}
18+
/* No side effect */

tests/tests/src/EnvUnifiedOps.res

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module X = {
2+
type t = int
3+
let n: t => t = x => x + 1
4+
}
5+
6+
let z: X.t = 3

0 commit comments

Comments
 (0)