Skip to content
Open
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 changes/external-unused-attribute-once.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Report an unused attribute on a relative `@module` external once instead of twice.
1 change: 1 addition & 0 deletions changes/external-warning-attribute.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `@warning` on an external, or a `@@warning` anywhere before it in the file, now applies to the warnings about its FFI attributes, such as an unused `@as`.
1 change: 1 addition & 0 deletions changes/inline-bigint-let.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Inline `@inline let` bigint constants like other literals, instead of warning that `@inline` cannot appear there.
1 change: 1 addition & 0 deletions changes/resolve-ffi-externals.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Keep resolved FFI specifications out of the parsetree, which now holds an external's primitive string as written. The type checker resolves each external once.
2 changes: 1 addition & 1 deletion compiler/core/lam_compile_external_call.ml
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types ~(prim_name : string)
E.new_ fn args)
| ( (Decl_send _ | Decl_get _ | Decl_set _ | Decl_get_index | Decl_set_index),
Some Module_itself ) ->
assert false (* rejected during digestion *)
assert false (* rejected during resolution *)
| Decl_val {name}, _ when decl.effective_arity = 0 ->
(* a global value *)
let e = translate_scoped_module_val named_module name scopes in
Expand Down
15 changes: 4 additions & 11 deletions compiler/frontend/ast_attributes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,10 @@ let prim_to_be_encoded (name : string) = not (first_char_special name)
They are not considered externals, they are part of the language
*)

let rs_externals (attrs : t) (pval_prim : Parsetree.primitive_repr option) =
match (attrs, pval_prim) with
| _, (None | Some (Prim_ffi _ | Prim_inline_const _)) -> false
(* [None] is a [val]; an already-digested external is not processed again *)
| [], Some (Prim_name name) ->
(* Not any attribute found *)
prim_to_be_encoded name
| _, Some (Prim_name name) ->
Ext_list.exists_fst attrs (fun ({txt} : string Asttypes.loc) ->
external_attrs |> Array.exists (fun (x : string) -> txt = x))
|| prim_to_be_encoded name
let is_ffi_external (attrs : t) (prim_name : string) =
Ext_list.exists_fst attrs (fun ({txt} : string Asttypes.loc) ->
external_attrs |> Array.exists (fun (x : string) -> txt = x))
|| prim_to_be_encoded prim_name

let is_inline : attr -> bool = fun ({txt}, _) -> txt = "inline"

Expand Down
4 changes: 3 additions & 1 deletion compiler/frontend/ast_attributes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ val process_derive_type : t -> derive_attr * t

val internal_expansive : attr

val rs_externals : t -> Parsetree.primitive_repr option -> bool
val is_ffi_external : t -> string -> bool
(** Whether an [external] with these attributes and primitive name is an FFI
external, as opposed to a compiler intrinsic. *)

val is_gentype : attr -> bool

Expand Down
8 changes: 4 additions & 4 deletions compiler/frontend/ast_exp_handle_external.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ let raw_expression loc ~is_function (exp : Parsetree.expression) =
{
exp with
pexp_desc =
Ast_external_mk.local_external_apply loc
~pval_prim:(Prim_name "#raw_expr") ~pval_type:(raw_type ()) [exp];
Ast_external_mk.local_external_apply loc ~pval_prim:"#raw_expr"
~pval_type:(raw_type ()) [exp];
pexp_attributes =
(match !is_function with
| None -> exp.pexp_attributes
Expand Down Expand Up @@ -72,8 +72,8 @@ let raw_structure loc (exp : Parsetree.expression) =
{
exp with
pexp_desc =
Ast_external_mk.local_external_apply loc
~pval_prim:(Prim_name "#raw_stmt") ~pval_type:(raw_type ()) [exp];
Ast_external_mk.local_external_apply loc ~pval_prim:"#raw_stmt"
~pval_type:(raw_type ()) [exp];
}

let handle_raw loc payload =
Expand Down
111 changes: 38 additions & 73 deletions compiler/frontend/ast_external.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,77 +22,42 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

let handle_external_in_sig (self : Ast_mapper.mapper)
(prim : Parsetree.value_description) (sigi : Parsetree.signature_item) :
Parsetree.signature_item =
let loc = prim.pval_loc in
let pval_type = self.typ self prim.pval_type in
let pval_attributes = self.attributes self prim.pval_attributes in
match prim.pval_prim with
| None -> Location.raise_errorf ~loc "empty primitive string"
| Some (Prim_ffi _ | Prim_inline_const _) ->
Location.raise_errorf ~loc "external declaration already processed"
| Some (Prim_name v) -> (
match
Ast_external_process.handle_attributes_as_prim loc pval_type
pval_attributes v
with
| {pval_type; pval_prim; pval_attributes; no_inline_cross_module} ->
(* The resolver behind [Primitive.resolve_external], which the type checker
calls once per declaration: an [@inline] constant, an FFI external with its
specification, or an intrinsic. An FFI external's errors and warnings are
reported here, including unused attributes on its resolved form. *)
let resolve (value_desc : Parsetree.value_description) (prim_name : string) :
Primitive.resolved_external =
if prim_name = Ast_external_mk.inline_const_prim then
match Ast_external_mk.inline_const_of_declaration value_desc with
| Some c ->
{
sigi with
psig_desc =
Psig_value
{
prim with
pval_type;
pval_prim =
(if no_inline_cross_module then None else Some pval_prim);
pval_attributes;
};
})

let handle_external_in_stru (self : Ast_mapper.mapper)
(prim : Parsetree.value_description) (str : Parsetree.structure_item) :
Parsetree.structure_item =
let loc = prim.pval_loc in
let pval_type = self.typ self prim.pval_type in
let pval_attributes = self.attributes self prim.pval_attributes in
match prim.pval_prim with
| None -> Location.raise_errorf ~loc "empty primitive string"
| Some (Prim_ffi _ | Prim_inline_const _) ->
Location.raise_errorf ~loc "external declaration already processed"
| Some (Prim_name v) -> (
match
Ast_external_process.handle_attributes_as_prim loc pval_type
pval_attributes v
with
| {pval_type; pval_prim; pval_attributes; no_inline_cross_module} ->
let external_result =
{
str with
pstr_desc =
Pstr_primitive
{prim with pval_type; pval_prim = Some pval_prim; pval_attributes};
}
in
if not no_inline_cross_module then external_result
else
let open Ast_helper in
Str.include_ ~loc
(Incl.mk ~loc
(Mod.constraint_ ~loc
(Mod.structure ~loc [external_result])
(Mty.signature ~loc
[
{
psig_desc =
Psig_value
{
prim with
pval_type;
pval_prim = None;
pval_attributes;
};
psig_loc = loc;
};
]))))
resolved_type = value_desc.pval_type;
resolved_attributes = [];
resolved_name = "";
resolved_kind = Kind_inline_const c;
}
| None ->
Location.raise_errorf ~loc:value_desc.pval_loc
"\"%s\" is reserved for %@inline constants" prim_name
else if Ast_attributes.is_ffi_external value_desc.pval_attributes prim_name
then (
let {Ast_external_process.pval_type; spec; pval_attributes} =
Ast_external_process.resolve value_desc.pval_loc value_desc.pval_type
value_desc.pval_attributes prim_name
in
Bs_ast_invariant.emit_external_warnings_on_resolved_external value_desc
~pval_type ~pval_attributes;
{
resolved_type = pval_type;
resolved_attributes = pval_attributes;
resolved_name = prim_name;
resolved_kind = Kind_external spec;
})
else
{
resolved_type = value_desc.pval_type;
resolved_attributes = value_desc.pval_attributes;
resolved_name = prim_name;
resolved_kind = Kind_intrinsic;
}
16 changes: 5 additions & 11 deletions compiler/frontend/ast_external.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

val handle_external_in_sig :
Ast_mapper.mapper ->
Parsetree.value_description ->
Parsetree.signature_item ->
Parsetree.signature_item

val handle_external_in_stru :
Ast_mapper.mapper ->
Parsetree.value_description ->
Parsetree.structure_item ->
Parsetree.structure_item
val resolve :
Parsetree.value_description -> string -> Primitive.resolved_external
(** The resolver behind [Primitive.resolve_external]: an [@inline] constant,
an FFI external with its specification, or an intrinsic. Reports an FFI
external's errors and warnings. *)
50 changes: 37 additions & 13 deletions compiler/frontend/ast_external_mk.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. *)

let local_external_apply loc ~(pval_prim : Parsetree.primitive_repr)
let local_external_apply loc ~(pval_prim : string)
~(pval_type : Parsetree.core_type) (args : Parsetree.expression list) :
Parsetree.expression_desc =
let local_module_name = "J" and local_fun_name = "unsafe_expr" in
Expand All @@ -45,18 +45,42 @@ let local_external_apply loc ~(pval_prim : Parsetree.primitive_repr)
{txt = Ldot (Lident local_module_name, local_fun_name); loc})
(Ext_list.map args (fun x -> (Asttypes.Nolabel, x))) )

let inline_const (c : External_ffi_types.inline_const) :
Parsetree.primitive_repr =
Prim_inline_const c
let inline_const_prim = "#rescript-inline"

let inline_string semantic = inline_const (Const_string semantic)
let inline_const_of_expression (expression : Parsetree.expression) :
External_ffi_types.inline_const option =
match (Ast_payload.unwrap_braces expression).pexp_desc with
| Pexp_constant (Pconst_string _)
| Pexp_template {source_segments = [_]; values = []} -> (
match Ast_payload.semantic_string_of_expression expression with
| Some semantic -> Some (Const_string semantic)
| None -> assert false)
| Pexp_constant (Pconst_integer (s, None)) ->
Some (Const_int (Int32.of_string s))
| Pexp_constant (Pconst_integer (s, Some 'n')) ->
let positive, digits = Bigint_utils.parse_bigint s in
Some (Const_bigint {positive; digits})
| Pexp_constant (Pconst_float (s, None)) -> Some (Const_float s)
| Pexp_construct ({txt = Lident (("true" | "false") as txt)}, {txt = []}) ->
Some (Const_bool (txt = "true"))
| _ -> None

let inline_bool b = inline_const (Const_bool b)
let inline_const_declaration ~(attr_loc : Location.t)
(value_desc : Parsetree.value_description)
(expression : Parsetree.expression) : Parsetree.value_description =
{
value_desc with
pval_prim = Some inline_const_prim;
pval_jsx_component = None;
pval_attributes =
[
( {txt = "inline"; loc = attr_loc},
PStr [Ast_helper.Str.eval ~loc:expression.pexp_loc expression] );
];
}

let inline_int i = inline_const (Const_int i)

let inline_bigint s =
let positive, digits = Bigint_utils.parse_bigint s in
inline_const (Const_bigint {positive; digits})

let inline_float s = inline_const (Const_float s)
let inline_const_of_declaration (value_desc : Parsetree.value_description) =
match value_desc.pval_attributes with
| [({txt = "inline"}, PStr [{pstr_desc = Pstr_eval (expression, _)}])] ->
inline_const_of_expression expression
| _ -> None
26 changes: 19 additions & 7 deletions compiler/frontend/ast_external_mk.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

val local_external_apply :
Location.t ->
pval_prim:Parsetree.primitive_repr ->
pval_prim:string ->
pval_type:Parsetree.core_type ->
Parsetree.expression list ->
Parsetree.expression_desc
Expand All @@ -39,12 +39,24 @@ val local_external_apply :
]}
*)

val inline_string : string -> Parsetree.primitive_repr
val inline_const_prim : string
(** The primitive string of an [@inline] constant's declaration *)

val inline_bool : bool -> Parsetree.primitive_repr
val inline_const_of_expression :
Parsetree.expression -> External_ffi_types.inline_const option
(** The constant an [@inline] literal denotes, if it is one an [@inline]
value can carry *)

val inline_int : int32 -> Parsetree.primitive_repr
val inline_const_declaration :
attr_loc:Location.t ->
Parsetree.value_description ->
Parsetree.expression ->
Parsetree.value_description
(** [inline_const_declaration ~attr_loc value_desc literal] turns [value_desc]
into the [external] the type checker resolves to an inline constant: its
primitive is [inline_const_prim] and its only attribute is
[@inline(literal)]. *)

val inline_bigint : string -> Parsetree.primitive_repr

val inline_float : string -> Parsetree.primitive_repr
val inline_const_of_declaration :
Parsetree.value_description -> External_ffi_types.inline_const option
(** The constant of a declaration built by [inline_const_declaration] *)
48 changes: 18 additions & 30 deletions compiler/frontend/ast_external_process.ml
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,6 @@ let check_return_wrapper loc (wrapper : External_ffi_types.return_wrapper)
if Ast_core_type.is_user_option result_type then wrapper
else Bs_syntaxerr.err loc Expect_opt_in_bs_return_to_opt

type response = {
pval_type: Parsetree.core_type;
pval_prim: Parsetree.primitive_repr;
pval_attributes: Parsetree.attributes;
no_inline_cross_module: bool;
}

(* A fixed argument means what [@as(value) _] means, so decode it with the
same reader. *)
let fixed_arg_type
Expand Down Expand Up @@ -906,10 +899,15 @@ let external_decl_of_non_obj (loc : Location.t) (st : external_desc)
| {get_name = Some _} ->
Location.raise_errorf ~loc "Attribute found that conflicts with %@get"

type resolution = {
pval_type: Parsetree.core_type;
spec: External_ffi_types.t;
pval_attributes: Parsetree.attributes;
}

(** Note that the passed [type_annotation] is already processed by visitor pattern before*)
let handle_attributes (loc : Location.t) (type_annotation : Parsetree.core_type)
(prim_attributes : Ast_attributes.t) (prim_name : string) :
Parsetree.core_type * External_ffi_types.t * Parsetree.attributes * bool =
let resolve (loc : Location.t) (type_annotation : Parsetree.core_type)
(prim_attributes : Ast_attributes.t) (prim_name : string) : resolution =
let prim_name_with_source = {name = prim_name; source = External} in
let result_type, arg_types_ty =
(* Note this assumes external type is syntatic (no abstraction)*)
Expand All @@ -925,7 +923,7 @@ let handle_attributes (loc : Location.t) (type_annotation : Parsetree.core_type)
let _arity, new_type, spec =
process_obj loc external_desc prim_name arg_types_ty result_type
in
(new_type, spec, unused_attrs, false)
{pval_type = new_type; spec; pval_attributes = unused_attrs}
else
let splice = external_desc.splice in
let arg_type_specs, args, arg_type_specs_length =
Expand Down Expand Up @@ -1015,28 +1013,18 @@ let handle_attributes (loc : Location.t) (type_annotation : Parsetree.core_type)
external_decl_of_non_obj loc external_desc prim_name_with_source
arg_type_specs_length arg_type_specs
in
let relative = External_ffi_types.check_decl ~loc decl ~prim_name in
External_ffi_types.check_decl ~loc decl ~prim_name;
(* result type can not be labeled *)
(* currently we don't process attributes of
return type, in the future we may *)
let return_wrapper =
check_return_wrapper loc external_desc.return_wrapper result_type
in
( (match args with
| [] -> result_type
| _ -> Ast_helper.Typ.arrow ~loc args result_type),
External_ffi_types.ffi_bs arg_type_specs return_wrapper decl,
unused_attrs,
relative )

let handle_attributes_as_prim (pval_loc : Location.t) (typ : Ast_core_type.t)
(attrs : Ast_attributes.t) (prim_name : string) : response =
let pval_type, ffi, pval_attributes, no_inline_cross_module =
handle_attributes pval_loc typ attrs prim_name
in
{
pval_type;
pval_prim = Prim_ffi {name = prim_name; spec = ffi};
pval_attributes;
no_inline_cross_module;
}
{
pval_type =
(match args with
| [] -> result_type
| _ -> Ast_helper.Typ.arrow ~loc args result_type);
spec = External_ffi_types.ffi_bs arg_type_specs return_wrapper decl;
pval_attributes = unused_attrs;
}
Loading
Loading