diff --git a/changes/jsx-preserve-unboxed-props.fix.md b/changes/jsx-preserve-unboxed-props.fix.md new file mode 100644 index 0000000000..c85a27b7cc --- /dev/null +++ b/changes/jsx-preserve-unboxed-props.fix.md @@ -0,0 +1 @@ +- Fix JSX preserve mode spreading the props of a component whose props type is an `@unboxed` record. https://github.com/rescript-lang/rescript/pull/8847 diff --git a/compiler/ml/translcore.ml b/compiler/ml/translcore.ml index 4d044c9316..0ebf6dcc68 100644 --- a/compiler/ml/translcore.ml +++ b/compiler/ml/translcore.ml @@ -1060,6 +1060,12 @@ let record_copies_fields Array.length fields < 20 && not (Array.exists (fun (lbl, _, _) -> lbl.lbl_optional) fields) +let is_unboxed_record env ty = + match Ctype.extract_concrete_typedecl env ty with + | _, _, {type_kind = Type_record (_, Record_unboxed _)} -> true + | _ -> false + | exception Not_found -> false + let jsx_props_of ~multi (props : expression) = match props.exp_desc with | Texp_record @@ -1101,6 +1107,13 @@ let jsx_props_of ~multi (props : expression) = match jsx_children ~multi lbl child with | Some children -> record (Some children) | None -> None)) + (* an unboxed record is the value of its field, not an object; inline + records are never the props *) + | Texp_record + {representation = Record_unboxed _ | Record_inlined _ | Record_extension} + -> + None + | _ when is_unboxed_record props.exp_env props.exp_type -> None | _ -> Some (Jsx_props_spread props) (* [None] when the call doesn't have the shape the PPX produces; it then diff --git a/tests/build_tests/jsx_preserve_semantics/cases/Unboxed.res b/tests/build_tests/jsx_preserve_semantics/cases/Unboxed.res new file mode 100644 index 0000000000..d93465d2ca --- /dev/null +++ b/tests/build_tests/jsx_preserve_semantics/cases/Unboxed.res @@ -0,0 +1,23 @@ +// An unboxed props record is the value of its field, not an object + +%%raw(` +function lowerUnboxed(props) { return null } +`) + +@unboxed type props = {title: string} + +module Comp = { + @react.componentWithProps + let make = (props: props) =>
+} +module Lower = { + @val external make: React.component