Skip to content

Commit

Permalink
fix(compiler): Correct in_function state when typing record propert…
Browse files Browse the repository at this point in the history
…ies (#2205)
  • Loading branch information
spotandjake authored Nov 21, 2024
1 parent d0038d3 commit d9dd274
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
15 changes: 10 additions & 5 deletions compiler/src/typed/typecore.re
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,8 @@ and type_expect_ =
loc,
closed,
env,
(e, k) => k(type_label_exp(true, env, loc, ty_record, e)),
(e, k) =>
k(type_label_exp(~in_function, true, env, loc, ty_record, e)),
opath,
es,
),
Expand Down Expand Up @@ -1422,6 +1423,7 @@ and type_expect_ =
});
| PExpConstruct(cstr, arg) =>
type_construct(
~in_function,
env,
loc,
cstr,
Expand Down Expand Up @@ -2098,7 +2100,8 @@ and type_application = (~in_function=?, ~loc, env, funct, sargs) => {
(ordered_labels, omitted_args @ typed_args, instance(env, ty_ret));
}

and type_construct = (env, loc, lid, sarg, ty_expected_explained, attrs) => {
and type_construct =
(~in_function, env, loc, lid, sarg, ty_expected_explained, attrs) => {
let {ty: ty_expected, explanation} = ty_expected_explained;
let (sargs, is_record_cstr) =
switch (sarg) {
Expand Down Expand Up @@ -2215,7 +2218,7 @@ and type_construct = (env, loc, lid, sarg, ty_expected_explained, attrs) => {
let args =
List.map2(
(sarg, (ty_arg, ty_arg0)) =>
type_argument(~recarg, env, sarg, ty_arg, ty_arg0),
type_argument(~in_function?, ~recarg, env, sarg, ty_arg, ty_arg0),
sargs,
List.combine(ty_args, ty_args0),
);
Expand Down Expand Up @@ -2725,7 +2728,8 @@ and type_label_access = (env, srecord, lid) => {
(record, label, opath);
}

and type_label_exp = (create, env, loc, ty_expected, (lid, label, sarg)) => {
and type_label_exp =
(~in_function, create, env, loc, ty_expected, (lid, label, sarg)) => {
/* Here also ty_expected may be at generic_level */
begin_def();
let separate = Env.has_local_constraints(env);
Expand Down Expand Up @@ -2758,7 +2762,8 @@ and type_label_exp = (create, env, loc, ty_expected, (lid, label, sarg)) => {
} else {
Some(Btype.snapshot());
};
let arg = type_argument(env, sarg, ty_arg, instance(env, ty_arg));
let arg =
type_argument(~in_function?, env, sarg, ty_arg, instance(env, ty_arg));
end_def();
try(
{
Expand Down
28 changes: 28 additions & 0 deletions compiler/test/suites/returns.re
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,32 @@ describe("early return", ({test, testSkip}) => {
|},
"7\n",
);
assertRun(
"regression_2204_1",
{|
record Test {
a: Number,
}
let test = () => {
{ a: if (true) return 1 else 0, }
return -1
}
print(test())
|},
"1\n",
);
assertRun(
"regression_2204_2",
{|
record Test {
a: Number,
}
let test = () => {
Ok({ a: if (true) return 1 else 0, })
return -1
}
print(test())
|},
"1\n",
);
});

0 comments on commit d9dd274

Please sign in to comment.