Skip to content

Commit

Permalink
Remove lazy keyword (rescript-lang#6342)
Browse files Browse the repository at this point in the history
* remove lazy keyword

* fix contributing doc stating old ocaml ver

* sync test output

* change CHANGELOG
  • Loading branch information
namenu authored and JonoPrest committed Mar 21, 2024
1 parent 86bcb02 commit 6b21f0b
Show file tree
Hide file tree
Showing 88 changed files with 241 additions and 842 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@

- Fix compiler crash when reexporting tagged template literal externals. https://github.com/rescript-lang/rescript-compiler/pull/6645

#### :boom: Breaking Change

- `lazy` syntax is no longer supported. If you're using it, use `Lazy` module or `React.lazy_` instead. https://github.com/rescript-lang/rescript-compiler/pull/6342

# 11.1.0-rc.2

#### :rocket: New Feature
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Make sure you have [opam](https://opam.ocaml.org/doc/Install.html) installed on
opam init

# Any recent OCaml version works as a development compiler
opam switch create 4.14.1 # can also create local switch with opam switch create . 4.14.1
opam switch create 5.1.1 # can also create local switch with opam switch create

# Install dev dependencies from OPAM
opam install . --deps-only
Expand Down
2 changes: 1 addition & 1 deletion jscomp/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(dirs bsb bsb_exe bsb_helper bsb_helper_exe bsc cmij common core depends ext
frontend gentype jsoo js_parser ml napkin ounit_tests syntax)
frontend gentype jsoo js_parser ml ounit_tests syntax)

(env
(dev
Expand Down
2 changes: 1 addition & 1 deletion jscomp/runtime/caml_module.res
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ let init_mod = (loc: (string, int, int), shape: shape) => {
let rec loop = (shape: shape, struct_: Obj.t, idx) =>
switch shape {
| Function => set_field(struct_, idx, Obj.magic(undef_module))
| Lazy => set_field(struct_, idx, Obj.magic(lazy undef_module))
| Lazy => set_field(struct_, idx, Obj.magic(undef_module))
| Class =>
set_field(
struct_,
Expand Down
17 changes: 17 additions & 0 deletions jscomp/stdlib-406/camlinternalLazy.res
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type t<'a> = {
%%private(external fnToVal: ((. unit) => 'a) => 'a = "%identity")
%%private(external valToFn: 'a => (. unit) => 'a = "%identity")
%%private(external castToConcrete: lazy_t<'a> => t<'a> = "%identity")
%%private(external castFromConcrete: t<'a> => lazy_t<'a> = "%identity")

let is_val = (type a, l: lazy_t<a>): bool => castToConcrete(l).tag

Expand Down Expand Up @@ -90,3 +91,19 @@ let force_val = (type a, lzv: lazy_t<a>): a => {
force_val_lazy_block(lzv)
}
}

let from_fun = (type a, closure: (. unit) => a): lazy_t<a> => {
let blk = {
tag: false,
value: fnToVal(closure),
}
castFromConcrete(blk)
}

let from_val = (type a, value: a): lazy_t<a> => {
let blk = {
tag: true,
value: value,
}
castFromConcrete(blk)
}
4 changes: 4 additions & 0 deletions jscomp/stdlib-406/camlinternalLazy.resi
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ let force: lazy_t<'a> => 'a
let force_val: lazy_t<'a> => 'a

let is_val: lazy_t<'a> => bool

let from_fun: ((. unit) => 'a) => lazy_t<'a>

let from_val: 'a => lazy_t<'a>
2 changes: 1 addition & 1 deletion jscomp/stdlib-406/hashtbl.res
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ let randomized = ref(randomized_default)
let randomize = () => randomized := true
let is_randomized = () => randomized.contents

let prng = lazy Random.State.make_self_init()
let prng = Lazy.from_fun(() => Random.State.make_self_init())

/* Creating a fresh, empty table */

Expand Down
4 changes: 2 additions & 2 deletions jscomp/stdlib-406/lazy.res
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ external force: t<'a> => 'a = "%lazy_force"

let force_val = CamlinternalLazy.force_val

let from_fun = f => lazy f()
let from_fun = f => CamlinternalLazy.from_fun((. ) => f())

let from_val = v => lazy v
let from_val = v => CamlinternalLazy.from_val(v)

let is_val = CamlinternalLazy.is_val

Expand Down
8 changes: 4 additions & 4 deletions jscomp/stdlib-406/stream.res
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ let iapp = (i, s) => Some({count: 0, data: Sapp(data(i), data(s))})
let icons = (i, s) => Some({count: 0, data: Scons(i, data(s))})
let ising = i => Some({count: 0, data: Scons(i, Sempty)})

let lapp = (f, s) => Some({count: 0, data: Slazy(lazy Sapp(data(f()), data(s)))})
let lapp = (f, s) => Some({count: 0, data: Slazy(Lazy.from_fun(() => Sapp(data(f()), data(s))))})

let lcons = (f, s) => Some({count: 0, data: Slazy(lazy Scons(f(), data(s)))})
let lsing = f => Some({count: 0, data: Slazy(lazy Scons(f(), Sempty))})
let lcons = (f, s) => Some({count: 0, data: Slazy(Lazy.from_fun(() => Scons(f(), data(s))))})
let lsing = f => Some({count: 0, data: Slazy(Lazy.from_fun(() => Scons(f(), Sempty)))})

let sempty = None
let slazy = f => Some({count: 0, data: Slazy(lazy data(f()))})
let slazy = f => Some({count: 0, data: Slazy(Lazy.from_fun(() => data(f())))})

/* For debugging use */

Expand Down
10 changes: 0 additions & 10 deletions jscomp/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1108,11 +1108,6 @@ let rec parsePattern ?(alias = true) ?(or_ = true) p =
let pat = parsePattern ~alias:false ~or_:false p in
let loc = mkLoc startPos p.prevEndPos in
Ast_helper.Pat.exception_ ~loc ~attrs pat
| Lazy ->
Parser.next p;
let pat = parsePattern ~alias:false ~or_:false p in
let loc = mkLoc startPos p.prevEndPos in
Ast_helper.Pat.lazy_ ~loc ~attrs pat
| List ->
Parser.next p;
parseListPattern ~startPos ~attrs p
Expand Down Expand Up @@ -2127,11 +2122,6 @@ and parseOperandExpr ~context p =
let () = attrs := [] in
parseAsyncArrowExpression ~arrowAttrs p
| Await -> parseAwaitExpression p
| Lazy ->
Parser.next p;
let expr = parseUnaryExpr p in
let loc = mkLoc startPos p.prevEndPos in
Ast_helper.Exp.lazy_ ~loc expr
| Try -> parseTryExpression p
| If -> parseIfOrIfLetExpression p
| For -> parseForExpression p
Expand Down
18 changes: 9 additions & 9 deletions jscomp/syntax/src/res_grammar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ let isSignatureItemStart = function

let isAtomicPatternStart = function
| Token.Int _ | String _ | Codepoint _ | Backtick | Lparen | Lbracket | Lbrace
| Underscore | Lident _ | Uident _ | List | Exception | Lazy | Percent ->
| Underscore | Lident _ | Uident _ | List | Exception | Percent ->
true
| _ -> false

Expand All @@ -148,9 +148,9 @@ let isAtomicTypExprStart = function

let isExprStart = function
| Token.Assert | At | Await | Backtick | Bang | Codepoint _ | False | Float _
| For | Hash | If | Int _ | Lazy | Lbrace | Lbracket | LessThan | Lident _
| List | Lparen | Minus | MinusDot | Module | Percent | Plus | PlusDot
| String _ | Switch | True | Try | Uident _ | Underscore (* _ => doThings() *)
| For | Hash | If | Int _ | Lbrace | Lbracket | LessThan | Lident _ | List
| Lparen | Minus | MinusDot | Module | Percent | Plus | PlusDot | String _
| Switch | True | Try | Uident _ | Underscore (* _ => doThings() *)
| While ->
true
| _ -> false
Expand All @@ -169,7 +169,7 @@ let isStructureItemStart = function
let isPatternStart = function
| Token.Int _ | Float _ | String _ | Codepoint _ | Backtick | True | False
| Minus | Plus | Lparen | Lbracket | Lbrace | List | Underscore | Lident _
| Uident _ | Hash | Exception | Lazy | Percent | Module | At ->
| Uident _ | Hash | Exception | Percent | Module | At ->
true
| _ -> false

Expand Down Expand Up @@ -257,10 +257,10 @@ let isJsxChildStart = isAtomicExprStart

let isBlockExprStart = function
| Token.Assert | At | Await | Backtick | Bang | Codepoint _ | Exception
| False | Float _ | For | Forwardslash | Hash | If | Int _ | Lazy | Lbrace
| Lbracket | LessThan | Let | Lident _ | List | Lparen | Minus | MinusDot
| Module | Open | Percent | Plus | PlusDot | String _ | Switch | True | Try
| Uident _ | Underscore | While ->
| False | Float _ | For | Forwardslash | Hash | If | Int _ | Lbrace | Lbracket
| LessThan | Let | Lident _ | List | Lparen | Minus | MinusDot | Module | Open
| Percent | Plus | PlusDot | String _ | Switch | True | Try | Uident _
| Underscore | While ->
true
| _ -> false

Expand Down
7 changes: 2 additions & 5 deletions jscomp/syntax/src/res_token.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ type t =
| Hash
| HashEqual
| Assert
| Lazy
| Tilde
| Question
| If
Expand Down Expand Up @@ -166,7 +165,6 @@ let toString = function
| AsteriskDot -> "*."
| Exponentiation -> "**"
| Assert -> "assert"
| Lazy -> "lazy"
| Tilde -> "tilde"
| Question -> "?"
| If -> "if"
Expand Down Expand Up @@ -222,7 +220,6 @@ let keywordTable = function
| "if" -> If
| "in" -> In
| "include" -> Include
| "lazy" -> Lazy
| "let" -> Let
| "list{" -> List
| "module" -> Module
Expand All @@ -242,8 +239,8 @@ let keywordTable = function

let isKeyword = function
| Await | And | As | Assert | Constraint | Else | Exception | External | False
| For | If | In | Include | Land | Lazy | Let | List | Lor | Module | Mutable
| Of | Open | Private | Rec | Switch | True | Try | Typ | When | While ->
| For | If | In | Include | Land | Let | List | Lor | Module | Mutable | Of
| Open | Private | Rec | Switch | True | Try | Typ | When | While ->
true
| _ -> false

Expand Down
10 changes: 5 additions & 5 deletions jscomp/syntax/tests/idempotency/covid-19charts.com/src/Data.res
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type value =
let dataWithGrowth =
Map.entries(data)
|> Js.Array.map(((countryId, dataPoints)) => {
let data = lazy {
let data = Lazy.from_fun(() => {
let countryDataWithGrowth = Map.empty()
let _ = Js.Array.reduce((prevRecord, day) => {
let record = Map.get(dataPoints, day)
Expand All @@ -72,7 +72,7 @@ let dataWithGrowth =
Some(record)
}, None, days)
countryDataWithGrowth
}
})
(countryId, data)
})
|> Belt.Map.String.fromArray
Expand All @@ -92,7 +92,7 @@ let calendar: t = Js.Array.mapi((day, index) => {
Belt.HashMap.String.set(
values,
Map.get(locations, countryId).name,
lazy Map.get(Belt.Map.String.getExn(dataWithGrowth, countryId) |> Lazy.force, day),
Lazy.from_fun(() => Map.get(Belt.Map.String.getExn(dataWithGrowth, countryId) |> Lazy.force, day)),
),
countryIds,
)
Expand Down Expand Up @@ -140,7 +140,7 @@ let getValue = (dataType, dataItem) => getValueFromRecord(dataType, getRecord(da

let alignToDay0 = (dataType, threshold) => {
let data = Belt.Map.String.mapU(dataWithGrowth, (. dataPoints) =>
lazy {
Lazy.from_fun(() => {
let dataPoints = Lazy.force(dataPoints)
Map.entries(dataPoints)
|> Js.Array.map(((date, value)) => (Map.get(dayToIndex, date), value))
Expand All @@ -149,7 +149,7 @@ let alignToDay0 = (dataType, threshold) => {
|> Js.Array.filter(value => getValue(dataType, value) >= threshold)
|> Js.Array.mapi((value, index) => (index, value))
|> Belt.Map.Int.fromArray
}
})
)

Array.init(Js.Array.length(days), day => {
Expand Down
2 changes: 1 addition & 1 deletion jscomp/syntax/tests/idempotency/genType/src/Arnold.res
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ let traverseAst = (~valueBindingsTable) => {
valueBindings |> List.iter((vb: Typedtree.value_binding) =>
switch vb.vb_pat.pat_desc {
| Tpat_var(id, {loc: {loc_start: pos}}) =>
let callees = lazy FindFunctionsCalled.findCallees(vb.vb_expr)
let callees = Lazy.from_fun(() => FindFunctionsCalled.findCallees(vb.vb_expr))
Hashtbl.replace(valueBindingsTable, Ident.name(id), (pos, vb.vb_expr, callees))
| _ => ()
}
Expand Down
4 changes: 2 additions & 2 deletions jscomp/syntax/tests/idempotency/genType/src/DeadValue.res
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ let whiteListSideEffects = list{
"Int64.one",
"String.length",
}
let whiteTableSideEffects = lazy {
let whiteTableSideEffects = Lazy.from_fun(() => {
let tbl = Hashtbl.create(11)

whiteListSideEffects |> List.iter(s => Hashtbl.add(tbl, s, ()))
tbl
}
})

let pathIsWhitelistedForSideEffects = path =>
switch path |> Path.flatten {
Expand Down
2 changes: 1 addition & 1 deletion jscomp/syntax/tests/idempotency/genType/src/Log_.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Color = {
let color_enabled = lazy Unix.isatty(Unix.stdout)
let color_enabled = Lazy.from_fun(() => Unix.isatty(Unix.stdout))
let forceColor = ref(false)

let get_color_enabled = () => forceColor.contents || Lazy.force(color_enabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ type resolver = {
}

let createLazyResolver = (~config, ~extensions, ~excludeFile) => {
lazyFind: lazy {
lazyFind: Lazy.from_fun(() => {
let (moduleNameMap, bsDependenciesFileMap) = sourcedirsJsonToMap(
~config,
~extensions,
Expand All @@ -210,7 +210,7 @@ let createLazyResolver = (~config, ~extensions, ~excludeFile) => {
moduleName |> find(~bsDependencies=true, ~map=bsDependenciesFileMap)
| res => res
}
},
}),
}

let apply = (~resolver, ~useBsDependencies, moduleName) =>
Expand Down
2 changes: 0 additions & 2 deletions jscomp/syntax/tests/parsing/grammar/expressions/arrow.res
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ let f = ((a, b)) => a + b
let f = ((a, b), (c, d)) => a + b + c + d
let f = (exception Terminate) => ()
let f = (exception Terminate, exception Exit) => ()
let f = (lazy x) => ()
let f = (lazy x, lazy y) => ()
let f = (list{}) => ()
let f = (list{x, ...xs}) => x + xs->Belt.List.length

Expand Down
2 changes: 0 additions & 2 deletions jscomp/syntax/tests/parsing/grammar/expressions/binary.res
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ let x = z |> switch z {| _ => false}
let x = z |> @attr switch z {| _ => false}
let x = z |> assert(z)
let x = z |> @attr assert(z)
let x = z |> lazy z
let x = z |> @attr lazy z
let x = z |> try sideEffect() catch { | _ => f() }
let x = z |> @attr try sideEffect() catch { | _ => f() }
let x = z |> for i in 0 to 10 { () }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ let f (a, b) = a + b
let f (a, b) (c, d) = ((a + b) + c) + d
let f exception Terminate = ()
let f exception Terminate exception Exit = ()
let f (lazy x) = ()
let f (lazy x) (lazy y) = ()
let f [] = ()
let f (x::xs) = x + (xs |. Belt.List.length)
let f (x : int) (y : int) = x + y
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ let x = z |> (match z with | _ -> false)
let x = z |> ((match z with | _ -> false)[@attr ])
let x = z |> (assert z)
let x = z |> ((assert z)[@attr ])
let x = z |> (lazy z)
let x = z |> ((lazy z)[@attr ])
let x = z |> (try sideEffect () with | _ -> f ())
let x = z |> ((try sideEffect () with | _ -> f ())[@attr ])
let x = z |> for i = 0 to 10 do () done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ let aTuple = (1, 2)
let aRecord = { name = {js|steve|js}; age = 30 }
let blockExpression = ((let a = 1 in let b = 2 in a + b)[@res.braces ])
let assertSmthing = assert true
let lazyThing = lazy true
let jsx =
((div ~className:(({js|cx|js})[@res.namedArgLoc ]) ~children:[foo] ())
[@JSX ])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ let blockExpression = ({

let assertSmthing = (assert(true))

let lazyThing = (lazy true)

let jsx = (<div className="cx"> foo </div>)

let ifExpr = (if true {
Expand Down
2 changes: 0 additions & 2 deletions jscomp/syntax/tests/parsing/grammar/pattern/constant.res
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ switch science {
| -4.15 as x => true
| -4.15 | +4.15 => true
| (-3.14 : float) => true
| lazy 5.678 => true
| exception 19.34 => true
| _ => false
}
Expand All @@ -99,7 +98,6 @@ switch literal {
| `literal` as x => true
| `literal` | `literal` => true
| (`literal` : string) => true
| lazy `literal` => true
| exception `literal` => true
| _ => false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ let (-1)..(-1.) = x
| (-4.15) as x -> true
| (-4.15)|4.15 -> true
| ((-3.14) : float) -> true
| (lazy 5.678) -> true
| exception 19.34 -> true
| _ -> false
;;match literal with
Expand All @@ -92,7 +91,6 @@ let (-1)..(-1.) = x
| (({js|literal|js})[@res.template ])|(({js|literal|js})[@res.template ])
-> true
| ((({js|literal|js})[@res.template ]) : string) -> true
| (lazy (({js|literal|js})[@res.template ])) -> true
| exception (({js|literal|js})[@res.template ]) -> true
| _ -> false
let (({js|literal constant|js})[@res.template ]) = x
Expand Down
Loading

0 comments on commit 6b21f0b

Please sign in to comment.