Skip to content
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

Fix issue with type environments and unified ops. #7277

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

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

# 12.0.0-alpha.8

Expand Down
36 changes: 18 additions & 18 deletions compiler/ml/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3379,22 +3379,22 @@ and translate_unified_ops (env : Env.t) (funct : Typedtree.expression)
let result_type =
match (lhs_type.desc, specialization) with
| Tconstr (path, _, _), _ when Path.same path Predef.path_int ->
Predef.type_int
instance_def Predef.type_int
| Tconstr (path, _, _), {bool = Some _}
when Path.same path Predef.path_bool ->
Predef.type_bool
instance_def Predef.type_bool
| Tconstr (path, _, _), {float = Some _}
when Path.same path Predef.path_float ->
Predef.type_float
instance_def Predef.type_float
| Tconstr (path, _, _), {bigint = Some _}
when Path.same path Predef.path_bigint ->
Predef.type_bigint
instance_def Predef.type_bigint
| Tconstr (path, _, _), {string = Some _}
when Path.same path Predef.path_string ->
Predef.type_string
instance_def Predef.type_string
| _ ->
unify env lhs_type Predef.type_int;
Predef.type_int
unify env lhs_type (instance_def Predef.type_int);
instance_def Predef.type_int
in
let targs = [(to_noloc lhs_label, Some lhs)] in
Some (targs, result_type)
Expand All @@ -3409,50 +3409,50 @@ and translate_unified_ops (env : Env.t) (funct : Typedtree.expression)
match (lhs_type.desc, specialization) with
| Tconstr (path, _, _), _ when Path.same path Predef.path_int ->
let rhs = type_expect env rhs_expr Predef.type_int in
(lhs, rhs, Predef.type_int)
(lhs, rhs, instance_def Predef.type_int)
| Tconstr (path, _, _), {bool = Some _}
when Path.same path Predef.path_bool ->
let rhs = type_expect env rhs_expr Predef.type_bool in
(lhs, rhs, Predef.type_bool)
(lhs, rhs, instance_def Predef.type_bool)
| Tconstr (path, _, _), {float = Some _}
when Path.same path Predef.path_float ->
let rhs = type_expect env rhs_expr Predef.type_float in
(lhs, rhs, Predef.type_float)
(lhs, rhs, instance_def Predef.type_float)
| Tconstr (path, _, _), {bigint = Some _}
when Path.same path Predef.path_bigint ->
let rhs = type_expect env rhs_expr Predef.type_bigint in
(lhs, rhs, Predef.type_bigint)
(lhs, rhs, instance_def Predef.type_bigint)
| Tconstr (path, _, _), {string = Some _}
when Path.same path Predef.path_string ->
let rhs = type_expect env rhs_expr Predef.type_string in
(lhs, rhs, Predef.type_string)
(lhs, rhs, instance_def Predef.type_string)
| _ -> (
(* Rule 2. Try unifying to rhs *)
match (rhs_type.desc, specialization) with
| Tconstr (path, _, _), _ when Path.same path Predef.path_int ->
let lhs = type_expect env lhs_expr Predef.type_int in
(lhs, rhs, Predef.type_int)
(lhs, rhs, instance_def Predef.type_int)
| Tconstr (path, _, _), {bool = Some _}
when Path.same path Predef.path_bool ->
let lhs = type_expect env lhs_expr Predef.type_bool in
(lhs, rhs, Predef.type_bool)
(lhs, rhs, instance_def Predef.type_bool)
| Tconstr (path, _, _), {float = Some _}
when Path.same path Predef.path_float ->
let lhs = type_expect env lhs_expr Predef.type_float in
(lhs, rhs, Predef.type_float)
(lhs, rhs, instance_def Predef.type_float)
| Tconstr (path, _, _), {bigint = Some _}
when Path.same path Predef.path_bigint ->
let lhs = type_expect env lhs_expr Predef.type_bigint in
(lhs, rhs, Predef.type_bigint)
(lhs, rhs, instance_def Predef.type_bigint)
| Tconstr (path, _, _), {string = Some _}
when Path.same path Predef.path_string ->
let lhs = type_expect env lhs_expr Predef.type_string in
(lhs, rhs, Predef.type_string)
(lhs, rhs, instance_def Predef.type_string)
| _ ->
(* Rule 3. Fallback to int *)
let lhs = type_expect env lhs_expr Predef.type_int in
let rhs = type_expect env rhs_expr Predef.type_int in
(lhs, rhs, Predef.type_int))
(lhs, rhs, instance_def Predef.type_int))
in
let targs =
[(to_noloc lhs_label, Some lhs); (to_noloc rhs_label, Some rhs)]
Expand Down
18 changes: 18 additions & 0 deletions tests/tests/src/EnvUnifiedOps.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Generated by ReScript, PLEASE EDIT WITH CARE


function n(x) {
return x + 1 | 0;
}

let X = {
n: n
};

let z = 3;

export {
X,
z,
}
/* No side effect */
6 changes: 6 additions & 0 deletions tests/tests/src/EnvUnifiedOps.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module X = {
type t = int
let n: t => t = x => x + 1
}

let z: X.t = 3
Loading