Skip to content

Remove remaining "bs." attributes #6643

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 14 commits into from
Feb 20, 2024
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
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@

# 12.0.0-alpha.1 (Unreleased)

#### :house: Internal

- Build with OCaml 5.1.1. https://github.com/rescript-lang/rescript-compiler/pull/6641

#### :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
- Remove handling of attributes with `bs.` prefix (`@bs.as` -> `@as` etc.). https://github.com/rescript-lang/rescript-compiler/pull/6643
- Remove obsolete `@bs.open` feature. https://github.com/rescript-lang/rescript-compiler/pull/6629

#### :house: Internal

- Build with OCaml 5.1.1. https://github.com/rescript-lang/rescript-compiler/pull/6641

# 11.1.0-rc.2

Expand Down
2 changes: 1 addition & 1 deletion jscomp/bsc/rescript_compiler_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ let buckle_script_flags : (string * Bsc_args.spec * string) array =



(** parse flags in bs.config *)
(** parse flags in config *)
let file_level_flags_handler (e : Parsetree.expression option) =
match e with
| None -> ()
Expand Down
2 changes: 1 addition & 1 deletion jscomp/build_tests/cmd/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type bla

external foo : bla = "foo.react" [@@module]

external bar : unit -> bla = "bar" [@@bs.val] [@@module "foo.react"]
external bar : unit -> bla = "bar" [@@val] [@@module "foo.react"]

let c = foo

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@@warning("-39")
// https://github.com/rescript-lang/rescript-compiler/issues/4511
/*
[@bs.config {
[@config {
flags : [|"-dsource"|]
}];
*/
Expand Down
8 changes: 4 additions & 4 deletions jscomp/core/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Curry.__N o
]}
Another use case:
{[
external f : ('a -> 'b [@bs.uncurry]) -> unit
external f : ('a -> 'b [@uncurry]) -> unit

f g (* The compiler will do such converison internally*)
]}
Expand Down Expand Up @@ -173,12 +173,12 @@ We can simply do inlining, it may have side efffect in `b0`, `b1`, our optimizer

Maybe in the future, we should lift the restriction about `variadic` (delegate to `slow` mode when we can not resolve it statically, my personal expereince is that people will complain about why it fails to compile more than why it is slow in some corner cases)

Note this also interacts with `[@bs.uncurry]`
Note this also interacts with `[@uncurry]`

for example

```ocaml
external filter : 'a array -> ('a -> bool [@bs.uncurry]) -> 'a array = "filter"
external filter : 'a array -> ('a -> bool [@uncurry]) -> 'a array = "filter"
[@@send]

let f xs =
Expand Down Expand Up @@ -211,7 +211,7 @@ if (typeof x === "undefined"){

```ocaml
external of_char : char -> string = "String.fromCharCode"
[@@bs.val]
[@@val]
```

We introduced `#` so that we can do some optimizations.
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1514,7 +1514,7 @@ and compile_prim (prim_info : Lam.prim_info)
compile_sequand l r lambda_cxt
| { primitive = Psequor; args = [ l; r ] } -> compile_sequor l r lambda_cxt
| { primitive = Pdebugger; _ } ->
(* [%bs.debugger] guarantees that the expression does not matter
(* [%debugger] guarantees that the expression does not matter
TODO: make it even safer *)
Js_output.output_of_block_and_expression lambda_cxt.continuation
S.debugger_block E.unit
Expand Down
16 changes: 6 additions & 10 deletions jscomp/frontend/ast_attributes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ let process_attributes_rev (attrs : t) : attr_kind * t =
match (txt, st) with
| "bs", (Nothing | Uncurry _) ->
(Uncurry attr, acc) (* TODO: warn unused/duplicated attribute *)
| ("bs.this" | "this"), (Nothing | Meth_callback _) ->
(Meth_callback attr, acc)
| "this", (Nothing | Meth_callback _) -> (Meth_callback attr, acc)
| "meth", (Nothing | Method _) -> (Method attr, acc)
| ("bs" | "bs.this" | "this"), _ ->
Bs_syntaxerr.err loc Conflict_bs_bs_this_bs_meth
| ("bs" | "this"), _ -> Bs_syntaxerr.err loc Conflict_bs_bs_this_bs_meth
| _, _ -> (st, attr :: acc))

let process_bs (attrs : t) =
Expand Down Expand Up @@ -150,8 +148,7 @@ let rs_externals (attrs : t) pval_prim =
prims_to_be_encoded pval_prim
| _, _ ->
Ext_list.exists_fst attrs (fun ({txt} : string Asttypes.loc) ->
Ext_string.starts_with txt "bs."
|| Ext_array.exists external_attrs (fun (x : string) -> txt = x))
Ext_array.exists external_attrs (fun (x : string) -> txt = x))
|| prims_to_be_encoded pval_prim

let is_inline : attr -> bool = fun ({txt}, _) -> txt = "inline"
Expand Down Expand Up @@ -221,12 +218,11 @@ let iter_process_bs_string_int_unwrap_uncurry (attrs : t) =
in
Ext_list.iter attrs (fun (({txt; loc = _}, (payload : _)) as attr) ->
match txt with
| "bs.string" | "string" -> assign `String attr
| "string" -> assign `String attr
| "int" -> assign `Int attr
| "ignore" -> assign `Ignore attr
| "bs.unwrap" | "unwrap" -> assign `Unwrap attr
| "bs.uncurry" | "uncurry" ->
assign (`Uncurry (Ast_payload.is_single_int payload)) attr
| "unwrap" -> assign `Unwrap attr
| "uncurry" -> assign (`Uncurry (Ast_payload.is_single_int payload)) attr
| _ -> ());
!st

Expand Down
14 changes: 4 additions & 10 deletions jscomp/frontend/ast_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ let process_directives str =
let rec iter_on_bs_config_str (x : Parsetree.structure) =
match x with
| [] -> ()
| {
pstr_desc =
Pstr_attribute (({txt = "bs.config" | "config"; loc}, payload) as attr);
}
:: _ ->
| {pstr_desc = Pstr_attribute (({txt = "config"; loc}, payload) as attr)} :: _
->
Bs_ast_invariant.mark_used_bs_attribute attr;
Ext_list.iter
(Ast_payload.ident_or_record_as_config loc payload)
Expand All @@ -76,11 +73,8 @@ let process_str str =
let rec iter_on_bs_config_sig (x : Parsetree.signature) =
match x with
| [] -> ()
| {
psig_desc =
Psig_attribute (({txt = "bs.config" | "config"; loc}, payload) as attr);
}
:: _ ->
| {psig_desc = Psig_attribute (({txt = "config"; loc}, payload) as attr)} :: _
->
Bs_ast_invariant.mark_used_bs_attribute attr;
Ext_list.iter
(Ast_payload.ident_or_record_as_config loc payload)
Expand Down
11 changes: 5 additions & 6 deletions jscomp/frontend/ast_exp_extension.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ let handle_extension e (self : Bs_ast_mapper.mapper)
(({txt; loc}, payload) : Parsetree.extension) =
match txt with
| "ffi" -> Ast_exp_handle_external.handle_ffi ~loc ~payload
| "bs.raw" | "raw" ->
Ast_exp_handle_external.handle_raw ~kind:Raw_exp loc payload
| "bs.re" | "re" ->
| "raw" -> Ast_exp_handle_external.handle_raw ~kind:Raw_exp loc payload
| "re" ->
Exp.constraint_ ~loc
(Ast_exp_handle_external.handle_raw ~kind:Raw_re loc payload)
(Ast_comb.to_js_re_type loc)
| "bs.external" | "external" -> (
| "external" -> (
match Ast_payload.as_ident payload with
| Some {txt = Lident x} ->
Ast_exp_handle_external.handle_external loc x
Expand All @@ -43,7 +42,7 @@ let handle_extension e (self : Bs_ast_mapper.mapper)
*)
| None | Some _ ->
Location.raise_errorf ~loc "external expects a single identifier")
| "bs.time" | "time" -> (
| "time" -> (
match payload with
| PStr [{pstr_desc = Pstr_eval (e, _)}] ->
let locString =
Expand All @@ -69,7 +68,7 @@ let handle_extension e (self : Bs_ast_mapper.mapper)
(Exp.ident ~loc {loc; txt = Lident "timed"})))
| _ ->
Location.raise_errorf ~loc "expect a boolean expression in the payload")
| "bs.debugger" | "debugger" ->
| "debugger" ->
{e with pexp_desc = Ast_exp_handle_external.handle_debugger loc payload}
| "obj" -> (
match payload with
Expand Down
5 changes: 2 additions & 3 deletions jscomp/frontend/ast_external_process.ml
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string)
else
let action () =
match txt with
| "bs.val" | "val" ->
| "val" ->
if no_arguments then
{st with val_name = Some (name_from_payload_or_prim ~loc payload)}
else
Expand Down Expand Up @@ -418,8 +418,7 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string)
with Not_handled_external_attribute -> (attr :: attrs, st))

let has_bs_uncurry (attrs : Ast_attributes.t) =
Ext_list.exists_fst attrs (fun {txt; loc = _} ->
txt = "bs.uncurry" || txt = "uncurry")
Ext_list.exists_fst attrs (fun {txt; loc = _} -> txt = "uncurry")

let check_return_wrapper loc (wrapper : External_ffi_types.return_wrapper)
result_type =
Expand Down
2 changes: 1 addition & 1 deletion jscomp/frontend/ast_typ_uncurry.mli
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ val to_uncurry_type : uncurry_type_gen

val to_method_callback_type : uncurry_type_gen
(** syntax:
{[ 'obj -> int -> int [@bs.this] ]}
{[ 'obj -> int -> int [@this] ]}
*)
2 changes: 1 addition & 1 deletion jscomp/frontend/ast_uncurry_gen.mli
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ val to_method_callback :
Parsetree.expression ->
Parsetree.expression_desc
(** syntax:
{[fun [@bs.this] obj pat pat1 -> body]}
{[fun [@this] obj pat pat1 -> body]}
*)
11 changes: 5 additions & 6 deletions jscomp/frontend/bs_ast_invariant.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
it may fail third party ppxes
*)
let is_bs_attribute txt =
Copy link
Member Author

@cknitt cknitt Feb 18, 2024

Choose a reason for hiding this comment

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

This was not invoked for the non-bs attributes and therefore the unused attribute check was not working for those, see #6636.

Some more investigation needed though as adding "as" causes tests to fail. This should be done in a separate PR.

let len = String.length txt in
len >= 2
(*TODO: check the stringing padding rule, this preciate may not be needed *)
&& String.unsafe_get txt 0 = 'b'
&& String.unsafe_get txt 1 = 's'
&& (len = 2 || String.unsafe_get txt 2 = '.')
match txt with
(* TODO #6636: | "as "| "int" *)
| "bs" | "config" | "ignore" | "optional" | "string" | "uncurry" | "unwrap" ->
true
| _ -> false

let used_attributes : string Asttypes.loc Hash_set_poly.t =
Hash_set_poly.create 16
Expand Down
6 changes: 3 additions & 3 deletions jscomp/frontend/bs_builtin_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
(**
1. extension point
{[
[%bs.raw{| blabla |}]
[%raw{| blabla |}]
]}
will be desugared into
{[
Expand Down Expand Up @@ -467,7 +467,7 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
Pstr_value
(Nonrecursive, [{pvb_pat; pvb_expr; pvb_attributes; pvb_loc}]);
})
| Pstr_attribute ({txt = "bs.config" | "config"}, _) -> str
| Pstr_attribute ({txt = "config"}, _) -> str
| _ -> default_mapper.structure_item self str

let local_module_name =
Expand Down Expand Up @@ -514,7 +514,7 @@ let rec structure_mapper ~await_context (self : mapper) (stru : Ast_structure.t)
| [] -> []
| item :: rest -> (
match item.pstr_desc with
| Pstr_extension (({txt = "bs.raw" | "raw"; loc}, payload), _attrs) ->
| Pstr_extension (({txt = "raw"; loc}, payload), _attrs) ->
Ast_exp_handle_external.handle_raw_structure loc payload
:: structure_mapper ~await_context self rest
(* | Pstr_extension (({txt = "i"}, _),_)
Expand Down
3 changes: 1 addition & 2 deletions jscomp/frontend/external_arg_spec.mli
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ type attr =
| Poly_var of {descr: (string * string) list option}
| Int of (string * int) list (* ([`a | `b ] [@int])*)
| Arg_cst of cst
| Fn_uncurry_arity of
int (* annotated with [@bs.uncurry ] or [@bs.uncurry 2]*)
| Fn_uncurry_arity of int (* annotated with [@uncurry ] or [@uncurry 2]*)
(* maybe we can improve it as a combination of {!Asttypes.constant} and tuple *)
| Extern_unit
| Nothing
Expand Down
2 changes: 1 addition & 1 deletion jscomp/gentype/Annotation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let tagIsGenType s = s = "genType" || s = "gentype"
let tagIsGenTypeAs s = s = "genType.as" || s = "gentype.as"
let tagIsAs s = s = "as"
let tagIsInt s = s = "int"
let tagIsString s = s = "bs.string" || s = "string"
let tagIsString s = s = "string"

let tagIsTag s = s = "tag"

Expand Down
6 changes: 3 additions & 3 deletions jscomp/ml/ast_payload.mli
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ val assert_strings : Location.t -> t -> string list
(** as a record or empty
it will accept

{[ [@@@bs.config ]]}
{[ [@@@config ]]}
or
{[ [@@@bs.config no_export ] ]}
{[ [@@@config no_export ] ]}
or
{[ [@@@bs.config { property .. } ]]}
{[ [@@@config { property .. } ]]}
Note that we only
{[
{ flat_property}
Expand Down
2 changes: 1 addition & 1 deletion jscomp/ml/oprint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ and print_simple_out_type ppf =
| Otyp_constr (Oide_dot (Oide_dot (Oide_ident "Js_OO", "Callback" ), _),
[tyl])
->
fprintf ppf "@[<0>(%a@ [@bs.this])@]" print_out_type_1 tyl
fprintf ppf "@[<0>(%a@ [@this])@]" print_out_type_1 tyl
| Otyp_constr (id, tyl) ->
pp_open_box ppf 0;
print_typargs ppf tyl;
Expand Down
2 changes: 1 addition & 1 deletion jscomp/others/belt_List.res
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
```
*/

@@bs.config({flags: ["-bs-noassertfalse"]})
@@config({flags: ["-bs-noassertfalse"]})

type t<'a> = list<'a>

Expand Down
2 changes: 1 addition & 1 deletion jscomp/others/belt_internalAVLset.res
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
@@bs.config({flags: ["-bs-noassertfalse"]})
@@config({flags: ["-bs-noassertfalse"]})
type rec node<'value> = {
@as("v") mutable value: 'value,
@as("h") mutable height: int,
Expand Down
2 changes: 1 addition & 1 deletion jscomp/others/belt_internalAVLtree.res
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

/* Almost rewritten by authors of ReScript */

@@bs.config({flags: ["-bs-noassertfalse"]})
@@config({flags: ["-bs-noassertfalse"]})
type rec node<'k, 'v> = {
@as("k") mutable key: 'k,
@as("v") mutable value: 'v,
Expand Down
2 changes: 1 addition & 1 deletion jscomp/others/belt_internalMapInt.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@@bs.config({flags: ["-bs-noassertfalse"]})
@@config({flags: ["-bs-noassertfalse"]})

type key = int

Expand Down
2 changes: 1 addition & 1 deletion jscomp/others/belt_internalMapString.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@@bs.config({flags: ["-bs-noassertfalse"]})
@@config({flags: ["-bs-noassertfalse"]})

type key = string

Expand Down
2 changes: 1 addition & 1 deletion jscomp/others/internal_map.cppo.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@@bs.config({flags: ["-bs-noassertfalse"]})
@@config({flags: ["-bs-noassertfalse"]})

#ifdef TYPE_STRING
type key = string
Expand Down
11 changes: 5 additions & 6 deletions jscomp/others/js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

[@@@bs.config {flags = [|"-unboxed-types"; "-w"; "-49"|]}]
[@@@config {flags = [|"-unboxed-types"; "-w"; "-49"|]}]
(* DESIGN:
- It does not have any code, all its code will be inlined so that
there will never be
Expand Down Expand Up @@ -141,14 +141,13 @@ external log : 'a -> unit = "log"
[@@val] [@@scope "console"]
(** Equivalent to console.log any value. *)

external log2 : 'a -> 'b -> unit = "log" [@@bs.val] [@@scope "console"]
external log3 : 'a -> 'b -> 'c -> unit = "log" [@@bs.val] [@@scope "console"]
external log2 : 'a -> 'b -> unit = "log" [@@val] [@@scope "console"]
external log3 : 'a -> 'b -> 'c -> unit = "log" [@@val] [@@scope "console"]

external log4 : 'a -> 'b -> 'c -> 'd -> unit = "log"
[@@bs.val] [@@scope "console"]
external log4 : 'a -> 'b -> 'c -> 'd -> unit = "log" [@@val] [@@scope "console"]

external logMany : 'a array -> unit = "log"
[@@bs.val] [@@scope "console"] [@@variadic]
[@@val] [@@scope "console"] [@@variadic]
(** A convenience function to console.log more than 4 arguments *)

external eqNull : 'a -> 'a null -> bool = "%bs_equal_null"
Expand Down
2 changes: 1 addition & 1 deletion jscomp/others/js_OO.res
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

@@bs.config({flags: ["-unboxed-types"]})
@@config({flags: ["-unboxed-types"]})

external unsafe_to_method: 'a => 'a = "#fn_method"

Expand Down
Loading