Skip to content

Regression in pattern matching optional fields. #7440

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

Merged
merged 1 commit into from
May 8, 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 @@ -31,6 +31,7 @@
- Fix field flattening optimization to avoid creating unnecessary copies of allocating constants. https://github.com/rescript-lang/rescript-compiler/pull/7421
- Fix leading comments removed when braces inside JSX contains `let` assignment. https://github.com/rescript-lang/rescript/pull/7424
- Fix JSON escaping in code editor analysis: JSON was not always escaped properly, which prevented code actions from being available in certain situations https://github.com/rescript-lang/rescript/pull/7435
- Fix regression in pattern matching for optional fields containing variants. https://github.com/rescript-lang/rescript/pull/7440

#### :house: Internal

Expand Down
6 changes: 3 additions & 3 deletions compiler/ml/matching.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2413,7 +2413,7 @@ let combine_array names loc arg partial ctx def (len_lambda_list, total1, _pats)

(* Insertion of debugging events *)

let[@inline] event_branch _repr lam = lam
(* let[@inline] event_branch _repr lam = lam *)

(*
This exception is raised when the compiler cannot produce code
Expand Down Expand Up @@ -2602,8 +2602,8 @@ let rec compile_match repr partial ctx m =
| {cases = ([], action) :: rem} ->
if is_guarded action then
let lambda, total = compile_match None partial ctx {m with cases = rem} in
(event_branch repr (patch_guarded lambda action), total)
else (event_branch repr action, jumps_empty)
(patch_guarded lambda action, total)
else (action, jumps_empty)
| {args = (arg, str) :: argl} ->
let v, newarg = arg_to_var arg m.cases in
let first_match, rem =
Expand Down
5 changes: 0 additions & 5 deletions compiler/ml/parmatch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -553,11 +553,6 @@ let all_record_args lbls =
in
match cdecl with
| None -> x
| Some cstr
when Ast_untagged_variants.is_nullary_variant cstr.cd_args ->
let _, tag = Ast_untagged_variants.get_cstr_loc_tag cstr in
if Ast_untagged_variants.tag_can_be_undefined tag then x
else (id, lbl, pat_construct, o)
| Some cstr -> (
match
Ast_untagged_variants.get_block_type ~env:pat.pat_env cstr
Expand Down
6 changes: 5 additions & 1 deletion tests/tests/src/pattern_match_json.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ function decodeGroup(group) {
}

function decodeNull(x) {
let tmp = x.field;
let match = x.field;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has changed because dicts always add optional fields, and in this case it contains a json value, and one of the possible json values is undefined.

if (match === undefined) {
return "no";
}
let tmp = Primitive_option.valFromOption(match);
if (tmp === null) {
return "yes it's null";
} else {
Expand Down
60 changes: 60 additions & 0 deletions tests/tests/src/pm_opt_fields_regression.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Generated by ReScript, PLEASE EDIT WITH CARE


function bad1(schema) {
if (schema.format !== undefined) {
return "int32";
} else {
return "default";
}
}

function good1(schema) {
if (schema.format !== undefined) {
return "int32";
} else {
return "default";
}
}

let SingleFormatCase = {
bad1: bad1,
good1: good1
};

function bad2(schema) {
let match = schema.format;
if (match !== undefined) {
if (match === "Int32") {
return "int32";
} else {
return "dd";
}
} else {
return "default";
}
}

function good2(schema) {
let match = schema.format;
if (match !== undefined) {
if (match === "Int32") {
return "int32";
} else {
return "dd";
}
} else {
return "default";
}
}

let MultipleFormatCase = {
bad2: bad2,
good2: good2
};

export {
SingleFormatCase,
MultipleFormatCase,
}
/* No side effect */
41 changes: 41 additions & 0 deletions tests/tests/src/pm_opt_fields_regression.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module SingleFormatCase = {
type format1 = Int32

type schema = {format?: format1}

let bad1 = schema => {
switch schema {
| {format: Int32} => "int32"
| _ => "default"
}
}

let good1 = schema => {
switch schema {
| {format: _} => "int32"
| _ => "default"
}
}
}

module MultipleFormatCase = {
type format2 = Int32 | DD

type schema = {format?: format2}

let bad2 = schema => {
switch schema {
| {format: Int32} => "int32"
| {format: DD} => "dd"
| _ => "default"
}
}

let good2 = schema => {
switch schema {
| {format: Int32} => "int32"
| {format: _} => "dd"
| _ => "default"
}
}
}