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(compiler): Correct type approximation on recursive functions #2154

Merged
merged 4 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
29 changes: 26 additions & 3 deletions compiler/src/typed/typecore.re
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,15 @@ let rec approx_type = (env, sty) =>
| PTyArrow(args, ret) =>
newty(
TTyArrow(
List.map(x => (x.ptyp_arg_label, newvar()), args),
List.map(
x => {
switch (x.ptyp_arg_label) {
| Default(_) => (x.ptyp_arg_label, type_option(newvar()))
| _ => (x.ptyp_arg_label, newvar())
}
},
args,
),
approx_type(env, ret),
TComOk,
),
Expand Down Expand Up @@ -635,7 +643,14 @@ let rec type_approx = (env, sexp: Parsetree.expression) =>
| PExpLambda(args, e) =>
newty(
TTyArrow(
List.map(x => (x.pla_label, newvar()), args),
List.map(
x =>
switch (x.pla_label) {
| Default(_) => (x.pla_label, type_option(newvar()))
| _ => (x.pla_label, newvar())
},
args,
),
type_approx(env, e),
TComOk,
),
Expand Down Expand Up @@ -1862,7 +1877,15 @@ and type_application = (~in_function=?, ~loc, env, funct, sargs) => {
let (ty_args, ty_ret) =
switch (ty_fun.desc) {
| TTyVar(_) =>
let t_args = List.map(arg => (arg.paa_label, newvar()), sargs)
let t_args =
List.map(
arg =>
switch (arg.paa_label) {
| Default(_) => (arg.paa_label, type_option(newvar()))
| _ => (arg.paa_label, newvar())
},
sargs,
)
and t_ret = newvar();
unify(
env,
Expand Down
14 changes: 14 additions & 0 deletions compiler/test/suites/functions.re
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,20 @@ truc()|},
|},
"999\n",
);
assertRun(
"default_args7",
{|
let rec pExp = () => exp(p=0)
and exp = (p=0) => 0

let parse = (s: String) => {
exp(p=0)
}

print(parse("abc"))
|},
"0\n",
);

assertRun(
"labeled_args_typecheck1",
Expand Down
Loading