Skip to content

Fix issue where build error of newtype escaping its scope #6000

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

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 2 additions & 1 deletion jscomp/test/build.ninja

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions jscomp/test/newtype_props_component_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';


function Newtype_props_component_test$C1(props) {
return props.foo;
}

var C1 = {
make: Newtype_props_component_test$C1
};

function Newtype_props_component_test$C2(props) {
return props.foo;
}

var C2 = {
make: Newtype_props_component_test$C2
};

exports.C1 = C1;
exports.C2 = C2;
/* No side effect */
23 changes: 23 additions & 0 deletions jscomp/test/newtype_props_component_test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@@bs.config({
flags: ["-bs-jsx", "4"],
})

module type T = {
type t
}

module C1 = {
@react.component
let make = (type a, ~foo: module(T with type t = a)) => {
module T = unpack(foo)
foo
}
}

module C2 = {
@react.component
let make = (type a, ~foo) => {
module T = unpack(foo: T with type t = a)
foo
}
}
49 changes: 18 additions & 31 deletions res_syntax/src/reactjs_jsx_v4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,12 @@ let makeTypeDeclsWithCoreType propsName loc coreType typVars =
]

(* type props<'x, 'y, ...> = { x: 'x, y?: 'y, ... } *)
let makePropsRecordType ~coreTypeOfAttr ~typVarsOfCoreType propsName loc
let makePropsRecordTypeDecls ~coreTypeOfAttr ~typVarsOfCoreType propsName loc
namedTypeList =
Str.type_ Nonrecursive
(match coreTypeOfAttr with
| None -> makeTypeDecls propsName loc namedTypeList
| Some coreType ->
makeTypeDeclsWithCoreType propsName loc coreType typVarsOfCoreType)
match coreTypeOfAttr with
| None -> makeTypeDecls propsName loc namedTypeList
| Some coreType ->
makeTypeDeclsWithCoreType propsName loc coreType typVarsOfCoreType

(* type props<'x, 'y, ...> = { x: 'x, y?: 'y, ... } *)
let makePropsRecordTypeSig ~coreTypeOfAttr ~typVarsOfCoreType propsName loc
Expand Down Expand Up @@ -687,18 +686,7 @@ let rec recursivelyTransformNamedArgsForMake mapper expr args newtypes coreType
(Some coreType)
| _ -> (args, newtypes, coreType)

let newtypeToVar newtype type_ =
let var_desc = Ptyp_var ("type-" ^ newtype) in
let typ (mapper : Ast_mapper.mapper) typ =
match typ.ptyp_desc with
| Ptyp_constr ({txt = Lident name}, _) when name = newtype ->
{typ with ptyp_desc = var_desc}
| _ -> Ast_mapper.default_mapper.typ mapper typ
in
let mapper = {Ast_mapper.default_mapper with typ} in
mapper.typ mapper type_

let argToType ~newtypes ~(typeConstraints : core_type option) types
let argToType ~(typeConstraints : core_type option) types
((name, default, {ppat_attributes = attrs}, _alias, loc, type_) :
arg_label * expression option * pattern * label * 'loc * core_type option)
=
Expand All @@ -710,13 +698,11 @@ let argToType ~newtypes ~(typeConstraints : core_type option) types
in
let typeConst = Option.bind typeConstraints (getType name) in
let type_ =
List.fold_left
(fun type_ newtype ->
match (type_, typeConst) with
| _, Some typ | Some typ, None -> Some (newtypeToVar newtype.txt typ)
| _ -> None)
type_ newtypes
match (type_, typeConst) with
| _, Some typ | Some typ, None -> Some typ
| _ -> None
in

match (type_, name, default) with
| Some type_, name, _ when isOptional name ->
(true, getLabel name, attrs, loc, type_) :: types
Expand Down Expand Up @@ -811,8 +797,9 @@ let transformStructureItem ~config mapper item =
in
(* type props<'x, 'y> = { x: 'x, y?: 'y, ... } *)
let propsRecordType =
makePropsRecordType ~coreTypeOfAttr ~typVarsOfCoreType "props"
pstr_loc namedTypeList
Str.type_ Nonrecursive
(makePropsRecordTypeDecls ~coreTypeOfAttr ~typVarsOfCoreType "props"
pstr_loc namedTypeList)
in
(* can't be an arrow because it will defensively uncurry *)
let newExternalType =
Expand Down Expand Up @@ -1033,9 +1020,7 @@ let transformStructureItem ~config mapper item =
[] [] None
in
let namedTypeList =
List.fold_left
(argToType ~newtypes ~typeConstraints)
[] namedArgList
List.fold_left (argToType ~typeConstraints) [] namedArgList
in
let vbMatch (name, default, _, alias, loc, _) =
let label = getLabel name in
Expand Down Expand Up @@ -1076,8 +1061,10 @@ let transformStructureItem ~config mapper item =
in
(* type props = { ... } *)
let propsRecordType =
makePropsRecordType ~coreTypeOfAttr ~typVarsOfCoreType "props"
pstr_loc namedTypeList
Str.type_ Nonrecursive
(List.map (fun label -> Type.mk label) newtypes
@ makePropsRecordTypeDecls ~coreTypeOfAttr ~typVarsOfCoreType
"props" pstr_loc namedTypeList)
in
let innerExpression =
Exp.apply
Expand Down
13 changes: 8 additions & 5 deletions res_syntax/tests/ppx/react/expected/firstClassModules.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ module Select = {
type key
type t
}
type props<'model, 'selected, 'onChange, 'items> = {
type key
and a
Copy link
Collaborator

Choose a reason for hiding this comment

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

This declares a new type a in the current scope. Will error if a type called a is already in scope. Eg defined just above.
The (type a) should be confined to the body of make.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think so too. I want to avoid to add type declaration. But I don't have a clear idea to fix this:

module type T = {
  type t
}

module V4A2 = {
  type props<'foo> = {foo: 'foo}

  let make = (type a, {foo, _}: props<'foo>) => {
    module T = unpack(foo: T with type t = a)
    foo
  }
  let make = {
    let \"Newtype$V4A2" = (props: props<_>) => make(props)

    \"Newtype$V4A2"
  }
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

If it compiles OK, should be good.
Might be worth testing a couple of examples by hand and see that it compiles OK, before changing the ppx.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That : props<'foo> should be : props<_> I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

That : props<'foo> should be : props<_> I think.

Yes, It works. But How can JSX4 know that foo is going be unpack in the body expression?

// original code
module V4A2 = {
  @react.component
  let make = (type a, ~foo) => {
    module T = unpack(foo: T with type t = a)
    foo
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

The current ppx passes the core_type of argument to props type. Do you mean passing no parameter to props type?

@react.component
let make = (~a: int) => React.int(a)

// (current) transformed to
type props<'a> = {a: 'a}
let make = ({a, _}: props<int>) => React.int(a)
                         ^^^^^

// (new?)
type props<'a> = {a: 'a}
let make = ({a, _}: props<_>) => React.int(a)
                         ^^^^^

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah no that should stay the same.
I think above you want let make = (type a, ... : props<a>


and props<'model, 'selected, 'onChange, 'items> = {
model: 'model,
selected: 'selected,
onChange: 'onChange,
Expand All @@ -67,10 +70,10 @@ module Select = {
@react.component
let make = (
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess this should be let make = (type a, ...

{model, selected, onChange, items, _}: props<
module(T with type t = '\"type-a" and type key = '\"type-key"),
option<'\"type-key">,
option<'\"type-key"> => unit,
array<'\"type-a">,
module(T with type t = a and type key = key),
option<key>,
option<key> => unit,
array<a>,
>,
) => {
let _ = (model, selected, onChange, items)
Expand Down
65 changes: 60 additions & 5 deletions res_syntax/tests/ppx/react/expected/newtype.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ module V3 = {
@@jsxConfig({version: 4, mode: "classic"})

module V4C = {
type props<'a, 'b, 'c> = {a: 'a, b: 'b, c: 'c}
type a

and props<'a, 'b, 'c> = {a: 'a, b: 'b, c: 'c}

@react.component
let make = ({a, b, c, _}: props<'\"type-a", array<option<[#Foo('\"type-a")]>>, 'a>) =>
let make = ({a, b, c, _}: props<a, array<option<[#Foo(a)]>>, 'a>) =>
ReactDOM.createDOMElementVariadic("div", [])
let make = {
let \"Newtype$V4C" = (props: props<_>) => make(props)
Expand All @@ -39,14 +41,67 @@ module V4C = {
@@jsxConfig({version: 4, mode: "automatic"})

module V4A = {
type props<'a, 'b, 'c> = {a: 'a, b: 'b, c: 'c}
type a

and props<'a, 'b, 'c> = {a: 'a, b: 'b, c: 'c}

@react.component
let make = ({a, b, c, _}: props<'\"type-a", array<option<[#Foo('\"type-a")]>>, 'a>) =>
ReactDOM.jsx("div", {})
let make = ({a, b, c, _}: props<a, array<option<[#Foo(a)]>>, 'a>) => ReactDOM.jsx("div", {})
let make = {
let \"Newtype$V4A" = (props: props<_>) => make(props)

\"Newtype$V4A"
}
}

module V4A2 = {
type b
and a

and props<'a, 'b, 'c> = {a: 'a, b: 'b, c: 'c}

@react.component let make = ({a, b, c, _}: props<a, array<b>, 'a>) => ReactDOM.jsx("div", {})
let make = {
let \"Newtype$V4A2" = (props: props<_>) => make(props)

\"Newtype$V4A2"
}
}

module type T = {
type t
}

module V4A1 = {
type a

and props<'foo> = {foo: 'foo}

@react.component
let make = ({foo, _}: props<module(T with type t = a)>) => {
module T = unpack(foo)
ReactDOM.jsx("div", {})
}
let make = {
let \"Newtype$V4A1" = (props: props<_>) => make(props)

\"Newtype$V4A1"
}
}

module V4A2 = {
type a

and props<'foo> = {foo: 'foo}

@react.component
let make = ({foo, _}: props<'foo>) => {
module T = unpack(foo: T with type t = a)
foo
}
let make = {
let \"Newtype$V4A2" = (props: props<_>) => make(props)

\"Newtype$V4A2"
}
}
13 changes: 8 additions & 5 deletions res_syntax/tests/ppx/react/expected/typeConstraint.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ module V3 = {
@@jsxConfig({version: 4, mode: "classic"})

module V4C = {
type props<'a, 'b> = {a: 'a, b: 'b}
type a

and props<'a, 'b> = {a: 'a, b: 'b}

@react.component
let make = ({a, b, _}: props<'\"type-a", '\"type-a">) =>
ReactDOM.createDOMElementVariadic("div", [])
let make = ({a, b, _}: props<a, a>) => ReactDOM.createDOMElementVariadic("div", [])
let make = {
let \"TypeConstraint$V4C" = (props: props<_>) => make(props)

Expand All @@ -32,9 +33,11 @@ module V4C = {
@@jsxConfig({version: 4, mode: "automatic"})

module V4A = {
type props<'a, 'b> = {a: 'a, b: 'b}
type a

and props<'a, 'b> = {a: 'a, b: 'b}

@react.component let make = ({a, b, _}: props<'\"type-a", '\"type-a">) => ReactDOM.jsx("div", {})
@react.component let make = ({a, b, _}: props<a, a>) => ReactDOM.jsx("div", {})
let make = {
let \"TypeConstraint$V4A" = (props: props<_>) => make(props)

Expand Down
25 changes: 25 additions & 0 deletions res_syntax/tests/ppx/react/newtype.res
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,28 @@ module V4A = {
@react.component
let make = (type a, ~a: a, ~b: array<option<[#Foo(a)]>>, ~c: 'a, _) => <div />
}

module V4A2 = {
@react.component
let make = (type a b, ~a:a, ~b:array<b>, ~c:'a) => <div />
}

module type T = {
type t
}

module V4A1 = {
@react.component
let make = (type a, ~foo: module(T with type t = a)) => {
module T = unpack(foo)
<div />
}
}

module V4A2 = {
@react.component
let make = (type a, ~foo) => {
module T = unpack(foo: T with type t = a)
foo
}
}