diff --git a/changes/inline-const-diagnostics.fix.md b/changes/inline-const-diagnostics.fix.md new file mode 100644 index 0000000000..33bd44af01 --- /dev/null +++ b/changes/inline-const-diagnostics.fix.md @@ -0,0 +1,3 @@ +- Report an `@inline` constant in a signature whose declared type can't hold it, such as `@inline("x") let x: int`. Code using such a signature compiled to wrong JavaScript. The constant's own type, an alias of it (private or not) and an abstract type are still accepted. +- Warn about an `@inline` value in a signature without a constant it can inline, such as `@inline let x: int`, which had no effect. +- Treat `@inline(never)` on a constant like on a function: the value is no longer inlined, and the attribute is reported as misplaced. A payload that is neither a constant nor `always` is reported as an illegal payload, and so is a constant payload that differs from the value, as in `@inline(3) let x = 4`. diff --git a/compiler/typing/ast_external_mk.ml b/compiler/typing/ast_external_mk.ml index 33ede319a8..6193060ba4 100644 --- a/compiler/typing/ast_external_mk.ml +++ b/compiler/typing/ast_external_mk.ml @@ -73,6 +73,16 @@ let inline_const_of_expression (expression : Parsetree.expression) : Some (Const_bool (txt = "true")) | _ -> None +(* Whether two constants are the same value, whatever their spelling: + [1.0] and [1.00], or [0x10n] and [16n]. *) +let same_inline_const (a : External_ffi_types.inline_const) + (b : External_ffi_types.inline_const) = + match (a, b) with + | Const_float a, Const_float b -> float_of_string a = float_of_string b + | Const_bigint a, Const_bigint b -> + Bigint_utils.compare (a.positive, a.digits) (b.positive, b.digits) = 0 + | _ -> a = b + (* A signature's [@inline(constant)] declaration *) let inline_const_of_attributes (attributes : Parsetree.attributes) = match Ast_attributes.has_inline_payload attributes with @@ -82,6 +92,20 @@ let inline_const_of_attributes (attributes : Parsetree.attributes) = | None -> None) | Some _ | None -> None +(* In a structure, [@inline] and [@inline(always)] make a constant, as they + make a function always inlined, and so does a constant payload, which + repeats the value as a signature writes it: [@inline("x") let x = "x"]. + Any other payload, [never] included, leaves an ordinary value, which + Translattribute then reports as for a function. *) +let constant_payload ((_, payload) : Parsetree.attribute) = + match payload with + | PStr [] -> true + | PStr [{pstr_desc = Pstr_eval (expression, [])}] -> ( + match (Ast_payload.unwrap_braces expression).pexp_desc with + | Pexp_ident {txt = Lident "always"} -> true + | _ -> Option.is_some (inline_const_of_expression expression)) + | _ -> false + (* The shape of a structure's [@inline let x = e] or [@inline let x: t = e], with its name, annotation and [@inline] attribute. [e] still has to be a constant. A recursive binding, a pattern, or a [type a.] or ['a.] @@ -101,7 +125,8 @@ let inline_binding (rec_flag : Asttypes.rec_flag) | _ -> None in match (shape, Ast_attributes.has_inline_payload pvb_attributes) with - | Some (name, annotation), Some attr -> Some (vb, name, annotation, attr) + | Some (name, annotation), Some attr when constant_payload attr -> + Some (vb, name, annotation, attr) | _ -> None) | _ -> None @@ -119,6 +144,15 @@ let inline_const_of_binding rec_flag bindings = match inline_const_of_expression pvb_expr with | None -> None | Some const -> + (match attr with + | {loc}, PStr [{pstr_desc = Pstr_eval (payload, [])}] -> ( + match inline_const_of_expression payload with + | Some payload_const when not (same_inline_const payload_const const) -> + Loc_report.prerr_warning loc + (Warnings.Attribute_payload + ("inline", "It differs from the value, which is used instead")) + | Some _ | None -> ()) + | _ -> ()); let pval_type = match (annotation, const) with | Some annotation, _ -> annotation diff --git a/compiler/typing/typedecl.ml b/compiler/typing/typedecl.ml index 429ee4f53c..ef13c7a69b 100644 --- a/compiler/typing/typedecl.ml +++ b/compiler/typing/typedecl.ml @@ -1945,6 +1945,44 @@ let inline_const_prim valdecl attr const = List.filter (fun a -> a != attr) valdecl.pval_attributes, Some ("", Primitive.Kind_inline_const const) ) +let inline_const_path (const : External_ffi_types.inline_const) = + match const with + | Const_string _ -> Predef.path_string + | Const_int _ -> Predef.path_int + | Const_float _ -> Predef.path_float + | Const_bool _ -> Predef.path_bool + | Const_bigint _ -> Predef.path_bigint + +(* A signature's [@inline(constant)] declaration may give the constant its own + type, possibly through an alias, private or not, or an abstract type that the + implementation defines. Another built-in type, a type variable or a + concrete type cannot hold the constant. *) +let check_signature_inline_const env (pval_type : Parsetree.core_type) ty const + = + let literal_path = inline_const_path const in + let is_builtin = function + | Path.Pident id -> + List.exists (fun (_, id') -> Ident.same id id') Predef.builtin_idents + | _ -> false + in + let fits = + match (Ctype.expand_head_opt env ty).desc with + | Tconstr (path, _, _) when Path.same path literal_path -> true + | Tconstr (path, _, _) when is_builtin path -> false + | Tconstr (path, _, _) -> ( + match Env.find_type path env with + | {type_kind = Type_abstract; type_manifest = None} -> true + | _ -> false + | exception Not_found -> false) + | _ -> false + in + if not fits then + Loc_report.raise_errorf ~loc:pval_type.ptyp_loc + "This @inline constant has type %a, but it is declared with type %a." + Printtyp.type_expr + (Ctype.newconstr literal_path []) + Printtyp.type_expr ty + (* Translate a value declaration. [inline] is the constant of an [@inline] [let] in a structure; a signature gives the constant as the attribute's payload. *) @@ -1957,7 +1995,17 @@ let transl_value_decl ?inline env loc valdecl = Ast_external_mk.inline_const_of_attributes valdecl.pval_attributes with | Some (attr, const) -> inline_const_prim valdecl attr const - | None -> (valdecl.pval_type, valdecl.pval_attributes, None)) + | None -> + (match Ast_attributes.has_inline_payload valdecl.pval_attributes with + | Some ({loc}, _) -> + (* In a signature, [@inline] only declares a constant. *) + Loc_report.prerr_warning loc + (Warnings.Attribute_payload + ( "inline", + "In a signature, it must be a string, int, float, bool or \ + bigint literal, such as @inline(3)" )) + | None -> ()); + (valdecl.pval_type, valdecl.pval_attributes, None)) | None, Some txt -> let { Primitive.resolved_type; @@ -1978,16 +2026,7 @@ let transl_value_decl ?inline env loc valdecl = literal's type, and an alias is kept. The literal's type is a fresh node: unifying [Predef]'s shared one would change it for the rest of the unit. *) - let literal_type = - Ctype.newconstr - (match const with - | Const_string _ -> Predef.path_string - | Const_int _ -> Predef.path_int - | Const_float _ -> Predef.path_float - | Const_bool _ -> Predef.path_bool - | Const_bigint _ -> Predef.path_bigint) - [] - in + let literal_type = Ctype.newconstr (inline_const_path const) [] in Typetexp.reset_type_variables (); Ctype.begin_def (); let cty = Typetexp.transl_simple_type env false pval_type in @@ -2004,6 +2043,10 @@ let transl_value_decl ?inline env loc valdecl = cty in let ty = cty.ctyp_type in + (match (inline, prim) with + | None, Some (_, Primitive.Kind_inline_const const) -> + check_signature_inline_const env pval_type ty const + | _ -> ()); (* The typed tree keeps the attributes as written: only the JSX transform reads the component annotation, and a declaration it leaves alone keeps the attribute, as it always has. *) diff --git a/packages/@rescript/runtime/Stdlib_Int.resi b/packages/@rescript/runtime/Stdlib_Int.resi index 0867758915..0cb467ad79 100644 --- a/packages/@rescript/runtime/Stdlib_Int.resi +++ b/packages/@rescript/runtime/Stdlib_Int.resi @@ -26,7 +26,6 @@ on MDN. Console.log(Int.Constants.minValue) ``` */ - @inline let minValue: int /** @@ -40,7 +39,6 @@ on MDN. Console.log(Int.Constants.maxValue) ``` */ - @inline let maxValue: int } diff --git a/tests/error_tests/single/expected/inline_const_payload.res.expected b/tests/error_tests/single/expected/inline_const_payload.res.expected new file mode 100644 index 0000000000..e2eed38bdc --- /dev/null +++ b/tests/error_tests/single/expected/inline_const_payload.res.expected @@ -0,0 +1,37 @@ + + Warning number 47 + /.../fixtures/inline_const_payload.res:2:1-7 + + 1 │ // A constant payload that differs from the value: a constant, with a wa + │ rning. + 2 │ @inline(3) let x = 4 + 3 │ // `never`: an ordinary value. + 4 │ @inline(never) let y = 5 + + illegal payload for attribute @inline. +It differs from the value, which is used instead + + + Warning number 47 + /.../fixtures/inline_const_payload.res:6:1-7 + + 4 │ @inline(never) let y = 5 + 5 │ // Not a constant: an ordinary value. + 6 │ @inline(sometimes) let z = 6 + 7 │ // The value repeated as the payload: a constant, without a warning. + 8 │ @inline(7) let w = 7 + + illegal payload for attribute @inline. +It must be either empty, 'always' or 'never' + + + Warning number 53 + /.../fixtures/inline_const_payload.res:4:1-24 + + 2 │ @inline(3) let x = 4 + 3 │ // `never`: an ordinary value. + 4 │ @inline(never) let y = 5 + 5 │ // Not a constant: an ordinary value. + 6 │ @inline(sometimes) let z = 6 + + the @inline attribute cannot appear in this context \ No newline at end of file diff --git a/tests/error_tests/single/expected/inline_const_signature_other_builtin.res.expected b/tests/error_tests/single/expected/inline_const_signature_other_builtin.res.expected new file mode 100644 index 0000000000..8ba9dbbdd8 --- /dev/null +++ b/tests/error_tests/single/expected/inline_const_signature_other_builtin.res.expected @@ -0,0 +1,10 @@ + + We've found a bug for you! + /.../fixtures/inline_const_signature_other_builtin.res:2:23-25 + + 1 │ module type S = { + 2 │ @inline("x") let x: int + 3 │ } + 4 │ + + This @inline constant has type string, but it is declared with type int. \ No newline at end of file diff --git a/tests/error_tests/single/expected/inline_const_signature_type_variable.res.expected b/tests/error_tests/single/expected/inline_const_signature_type_variable.res.expected new file mode 100644 index 0000000000..e1c86399f8 --- /dev/null +++ b/tests/error_tests/single/expected/inline_const_signature_type_variable.res.expected @@ -0,0 +1,10 @@ + + We've found a bug for you! + /.../fixtures/inline_const_signature_type_variable.res:2:22 + + 1 │ module type S = { + 2 │ @inline(1) let x: 'a + 3 │ } + 4 │ + + This @inline constant has type int, but it is declared with type 'a. \ No newline at end of file diff --git a/tests/error_tests/single/expected/inline_const_signature_variant.res.expected b/tests/error_tests/single/expected/inline_const_signature_variant.res.expected new file mode 100644 index 0000000000..62768d7da3 --- /dev/null +++ b/tests/error_tests/single/expected/inline_const_signature_variant.res.expected @@ -0,0 +1,11 @@ + + We've found a bug for you! + /.../fixtures/inline_const_signature_variant.res:3:21 + + 1 │ module type S = { + 2 │ type t = A | B + 3 │ @inline(1) let x: t + 4 │ } + 5 │ + + This @inline constant has type int, but it is declared with type t. \ No newline at end of file diff --git a/tests/error_tests/single/expected/inline_const_signature_without_constant.res.expected b/tests/error_tests/single/expected/inline_const_signature_without_constant.res.expected new file mode 100644 index 0000000000..1f7b216301 --- /dev/null +++ b/tests/error_tests/single/expected/inline_const_signature_without_constant.res.expected @@ -0,0 +1,25 @@ + + Warning number 47 + /.../fixtures/inline_const_signature_without_constant.res:3:3-9 + + 1 │ // In a signature, @inline must carry the constant. + 2 │ module type S = { + 3 │ @inline(never) let q: int + 4 │ @inline let r: int + 5 │ } + + illegal payload for attribute @inline. +In a signature, it must be a string, int, float, bool or bigint literal, such as @inline(3) + + + Warning number 47 + /.../fixtures/inline_const_signature_without_constant.res:4:3-9 + + 2 │ module type S = { + 3 │ @inline(never) let q: int + 4 │ @inline let r: int + 5 │ } + 6 │ + + illegal payload for attribute @inline. +In a signature, it must be a string, int, float, bool or bigint literal, such as @inline(3) \ No newline at end of file diff --git a/tests/error_tests/single/fixtures/inline_const_payload.res b/tests/error_tests/single/fixtures/inline_const_payload.res new file mode 100644 index 0000000000..a4df113fd9 --- /dev/null +++ b/tests/error_tests/single/fixtures/inline_const_payload.res @@ -0,0 +1,11 @@ +// A constant payload that differs from the value: a constant, with a warning. +@inline(3) let x = 4 +// `never`: an ordinary value. +@inline(never) let y = 5 +// Not a constant: an ordinary value. +@inline(sometimes) let z = 6 +// The value repeated as the payload: a constant, without a warning. +@inline(7) let w = 7 +// The same value spelled differently: no warning either. +@inline(1.0) let v = 1.00 +let use = (x, y, z, w, v) diff --git a/tests/error_tests/single/fixtures/inline_const_signature_other_builtin.res b/tests/error_tests/single/fixtures/inline_const_signature_other_builtin.res new file mode 100644 index 0000000000..64633a8bb5 --- /dev/null +++ b/tests/error_tests/single/fixtures/inline_const_signature_other_builtin.res @@ -0,0 +1,3 @@ +module type S = { + @inline("x") let x: int +} diff --git a/tests/error_tests/single/fixtures/inline_const_signature_type_variable.res b/tests/error_tests/single/fixtures/inline_const_signature_type_variable.res new file mode 100644 index 0000000000..c8d7c74952 --- /dev/null +++ b/tests/error_tests/single/fixtures/inline_const_signature_type_variable.res @@ -0,0 +1,3 @@ +module type S = { + @inline(1) let x: 'a +} diff --git a/tests/error_tests/single/fixtures/inline_const_signature_variant.res b/tests/error_tests/single/fixtures/inline_const_signature_variant.res new file mode 100644 index 0000000000..91d75fe9ec --- /dev/null +++ b/tests/error_tests/single/fixtures/inline_const_signature_variant.res @@ -0,0 +1,4 @@ +module type S = { + type t = A | B + @inline(1) let x: t +} diff --git a/tests/error_tests/single/fixtures/inline_const_signature_without_constant.res b/tests/error_tests/single/fixtures/inline_const_signature_without_constant.res new file mode 100644 index 0000000000..02e9e6740a --- /dev/null +++ b/tests/error_tests/single/fixtures/inline_const_signature_without_constant.res @@ -0,0 +1,5 @@ +// In a signature, @inline must carry the constant. +module type S = { + @inline(never) let q: int + @inline let r: int +} diff --git a/tests/tests/src/inline_const_signature.mjs b/tests/tests/src/inline_const_signature.mjs new file mode 100644 index 0000000000..37adaa1e28 --- /dev/null +++ b/tests/tests/src/inline_const_signature.mjs @@ -0,0 +1,26 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +let M = {}; + +let Encoding = {}; + +let uses = [ + 1, + 2, + 3, + 97 +]; + +let hidden = 4; + +let encoding = "ascii"; + +export { + M, + uses, + hidden, + Encoding, + encoding, +} +/* No side effect */ diff --git a/tests/tests/src/inline_const_signature.res b/tests/tests/src/inline_const_signature.res new file mode 100644 index 0000000000..2f3df89b34 --- /dev/null +++ b/tests/tests/src/inline_const_signature.res @@ -0,0 +1,38 @@ +// The declared types a signature's @inline constant may have: its own, an +// alias of it, private or not, and an abstract type. +module type S = { + type id = int + type priv = private int + type abstract + @inline(1) let own: int + @inline(2) let aliased: id + @inline(3) let privateAlias: priv + @inline(4) let hidden: abstract + @inline(97) let letter: char +} + +module M: S = { + type id = int + type priv = int + type abstract = int + @inline let own = 1 + @inline let aliased = 2 + @inline let privateAlias = 3 + @inline let hidden = 4 + @inline let letter = 97 +} + +let uses = (M.own, M.aliased, M.privateAlias :> int, M.letter) +let hidden = M.hidden + +// An implementation may repeat the constant as the payload, as a signature +// writes it, behind a private type (as in rescript-nodejs). +module Encoding: { + type t = private string + @inline("ascii") let ascii: t +} = { + type t = string + @inline("ascii") let ascii = "ascii" +} + +let encoding = (Encoding.ascii :> string)