From ac21673cb0f752a701e930ae973bbf4cbf98e551 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sun, 29 Nov 2026 09:08:11 +0000 Subject: [PATCH] Warn when other modules can't inline an @inline function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Why: An exported `@inline` function stores its body for other modules only when it can: it must be closed, not async, without a directive, and must not call a relative-path external. Otherwise other modules called it, and nothing said so. Warning 113, on by default, now names the reason. It is a warning, not an error, since the function is still inlined in its own module. Diagnostic added (exported @inline function that isn't stored): Example: let rec helper = x => x > 10 ? x : helper(x + 1) @inline let usesHelper = x => helper(x) + 1 @inline let asyncOne = async x => x Before (diagnostic): none After (diagnostic): Warning number 113 This @inline function is inlined only in this module, not in modules that use it: it uses helper, a value of this module. Warning number 113 This @inline function is inlined only in this module, not in modules that use it: it is async. Review: The warning is reported while the .cmj is built, after type checking. Do `-warn-error +113`, `@warning("-113")` and `@warnerror("+113")` still apply? Yes. The check for fatal warnings now runs after the .cmj is built, and the type checker records the warning state of each `@inline` value binding, whose `@inline` may be on the binding or on its function (`let f = {@inline x => …}`), and of each `@inline` module binding, whose `@inline` may be below a module type constraint. Other bindings aren't recorded, which keeps the cost away from the many bindings without `@inline`. The state holds the binding's `@warning` and `@warnerror`, those on its expression and a `@@warning` above it, under the binding's identifier (`Inline_not_stored`). Warning 113 is reported in that state, so a silenced value in a nested module doesn't silence another of the same name. Review: Does it fire for functions an interface hides? No. Only exported values are considered, so an `@inline` helper the `.resi` leaves out is inlined in its module without a warning. Functions in nested modules and `@inline let rec` are not covered either. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D Signed-off-by: Christoph Knittel --- changes/warning-inline-not-stored.feature.md | 1 + compiler/backend/lam_compile_main.ml | 3 +- compiler/backend/lam_stats_export.ml | 97 +++++++----- compiler/typing/inline_not_stored.ml | 38 +++++ compiler/typing/inline_not_stored.mli | 13 ++ compiler/typing/typemod.ml | 29 ++++ compiler/typing/warnings.ml | 9 +- compiler/typing/warnings.mli | 1 + .../cross_module_relative_external/input.js | 10 +- .../rescript.json | 2 +- .../src/ffi/Hidden.res | 3 +- .../inline_warning_as_error/.gitignore | 2 + .../projects/inline_warning_as_error/input.js | 34 ++++ .../inline_warning_as_error/rescript.json | 6 + .../inline_warning_as_error/src/Limits.res | 4 + ...warning_113_inline_not_stored.res.expected | 59 +++++++ .../warning_113_inline_warnerror.res.expected | 51 ++++++ .../warning_113_inline_not_stored.res | 47 ++++++ .../fixtures/warning_113_inline_warnerror.res | 15 ++ tests/tests/src/inline_static_exit_test.mjs | 148 ++---------------- tests/tests/src/inline_static_exit_test.res | 3 + tests/tests/src/inline_static_exit_test.resi | 1 + 22 files changed, 405 insertions(+), 171 deletions(-) create mode 100644 changes/warning-inline-not-stored.feature.md create mode 100644 compiler/typing/inline_not_stored.ml create mode 100644 compiler/typing/inline_not_stored.mli create mode 100644 tests/build_tests/projects/inline_warning_as_error/.gitignore create mode 100644 tests/build_tests/projects/inline_warning_as_error/input.js create mode 100644 tests/build_tests/projects/inline_warning_as_error/rescript.json create mode 100644 tests/build_tests/projects/inline_warning_as_error/src/Limits.res create mode 100644 tests/error_tests/single/expected/warning_113_inline_not_stored.res.expected create mode 100644 tests/error_tests/single/expected/warning_113_inline_warnerror.res.expected create mode 100644 tests/error_tests/single/fixtures/warning_113_inline_not_stored.res create mode 100644 tests/error_tests/single/fixtures/warning_113_inline_warnerror.res create mode 100644 tests/tests/src/inline_static_exit_test.resi diff --git a/changes/warning-inline-not-stored.feature.md b/changes/warning-inline-not-stored.feature.md new file mode 100644 index 0000000000..a1a60e8168 --- /dev/null +++ b/changes/warning-inline-not-stored.feature.md @@ -0,0 +1 @@ +- Add warning 113, on by default, for an exported `@inline` function that other modules can't inline, with the reason: it uses another value of its module, it's async or has a directive, or it calls an external with a relative path. It's still inlined in its own module. Turn it off for one binding with `@warning("-113")`. diff --git a/compiler/backend/lam_compile_main.ml b/compiler/backend/lam_compile_main.ml index 32fa834b17..9bb2998f13 100644 --- a/compiler/backend/lam_compile_main.ml +++ b/compiler/backend/lam_compile_main.ml @@ -459,7 +459,6 @@ let compile ?on_optimized_lambda (output_prefix : string) | 0 -> Lam_module_ident.compare_same_name id1 id2 | c -> c) in - Warnings.check_fatal (); let effect_ = Lam_stats_export.get_dependent_module_effect maybe_pure external_module_ids in @@ -467,6 +466,8 @@ let compile ?on_optimized_lambda (output_prefix : string) Lam_stats_export.export_to_cmj meta effect_ export_map (Ext_filename.case output_prefix) in + (* After the export, which reports warning 113. *) + Warnings.check_fatal (); if not !((Clflags.current ()).dont_write_files) then Js_cmj_format.to_file ~check_exists:(not !((Js_config.current ()).force_cmj)) diff --git a/compiler/backend/lam_stats_export.ml b/compiler/backend/lam_stats_export.ml index 298d73d4de..968a6fe02b 100644 --- a/compiler/backend/lam_stats_export.ml +++ b/compiler/backend/lam_stats_export.ml @@ -73,42 +73,69 @@ let values_of_export (meta : Lam_stats.t) (export_map : Lambda.t Map_ident.t) : | None -> optlam | Some lambda -> ( - if calls_relative_external lambda then None - else - match lambda with - | Lfunction ({attr = {inline = Always_inline}} as fn) -> - (* An [@inline] function or functor: other modules inline its - body too, with or without -bs-cross-module-opt, when they - can. It must be closed, since the body is copied without - its environment. *) - if - Lambda.lfunction_can_be_inlined fn - && Lam_closure.is_closed lambda - then optlam - else None - | _ when not !((Js_config.current ()).cross_module_inline) -> None - | _ when not (Lam_analysis.safe_to_inline lambda) -> - (* when inlining a non function, we have to be very careful, + match lambda with + | Lfunction + { + attr = {inline = Always_inline; async; directive; is_a_functor}; + loc; + } -> ( + (* An [@inline] function or functor: other modules inline its + body too, when they can. It must be closed, since the body is + copied without its environment. *) + let not_stored = + if async then Some "it is async" + else if Option.is_some directive then Some "it has a directive" + else if calls_relative_external lambda then + Some "it calls an external with a path relative to this file" + else if Lam_closure.is_closed lambda then None + else + (* The first variable that [Lam_closure.is_closed] objects + to: other modules are global and fine to name. *) + match + Map_ident.fold + (fun id _ found -> + match found with + | None when not (Ident.global id) -> Some id + | _ -> found) + (Lam_closure.free_variables Set_ident.empty Map_ident.empty + lambda) + None + with + | Some id -> + Some ("it uses " ^ Ident.name id ^ ", a value of this module") + | None -> None + in + match not_stored with + | None -> optlam + | Some reason -> + Inline_not_stored.report x (fun () -> + Loc_report.prerr_warning loc + (Warnings.Inline_not_stored + ((if is_a_functor then "functor" else "function"), reason))); + None) + | _ when calls_relative_external lambda -> None + | _ when not !((Js_config.current ()).cross_module_inline) -> None + | _ when not (Lam_analysis.safe_to_inline lambda) -> + (* when inlining a non function, we have to be very careful, only truly immutable values can be inlined *) - None - | Lfunction {attr = {is_a_functor = true}} -> - if Lam_closure.is_closed lambda then optlam else None - | _ -> - let lam_size = - Lam_analysis.size_upto ~limit:Lam_analysis.small_inline_size - lambda - in - let free_variables = - Lam_closure.free_variables Set_ident.empty Map_ident.empty - lambda - in - if - lam_size < Lam_analysis.small_inline_size - && Map_ident.is_empty free_variables - then ( - Ir_log.dwarn ~__POS__ "%s recorded for inlining @." x.name; - optlam) - else None) + None + | Lfunction {attr = {is_a_functor = true}} -> + if Lam_closure.is_closed lambda then optlam else None + | _ -> + let lam_size = + Lam_analysis.size_upto ~limit:Lam_analysis.small_inline_size + lambda + in + let free_variables = + Lam_closure.free_variables Set_ident.empty Map_ident.empty lambda + in + if + lam_size < Lam_analysis.small_inline_size + && Map_ident.is_empty free_variables + then ( + Ir_log.dwarn ~__POS__ "%s recorded for inlining @." x.name; + optlam) + else None) in match (arity, persistent_closed_lambda) with | Single Arity_na, (None | Some (Lconst Const_module_alias)) -> acc diff --git a/compiler/typing/inline_not_stored.ml b/compiler/typing/inline_not_stored.ml new file mode 100644 index 0000000000..c2b1d57e54 --- /dev/null +++ b/compiler/typing/inline_not_stored.ml @@ -0,0 +1,38 @@ +(* Warning 113 is reported where the .cmj is built, after type checking, + when the warning scopes of a binding and of [@@warning] are gone. The type + checker records here the warning state of each [@inline] binding, whose + [@inline] may be on the binding or on its expression, so the warning is + reported, or not, and made an error, or not, as it would have been there. *) +let states_key = + Request_local.new_key (fun () : (Ident.t, Warnings.state) Hashtbl.t -> + Hashtbl.create 1) + +let has_inline (attributes : Parsetree.attributes) = + List.exists + (fun (({txt}, _) : Parsetree.attribute) -> txt = "inline") + attributes + +let record_binding (attributes : Parsetree.attributes) + (inner_attributes : Parsetree.attributes) ids = + if has_inline attributes || has_inline inner_attributes then + (* Type checking applied these scopes already and reported what they + report, such as [@ppwarning] or a malformed [@warning]; here they are + only read. *) + let state = + Warnings.without_warnings (fun () -> + Builtin_attributes.warning_scope ~ppwarning:false attributes + (fun () -> + Builtin_attributes.warning_scope ~ppwarning:false inner_attributes + Warnings.backup)) + in + List.iter + (fun id -> Hashtbl.replace (Domain.DLS.get states_key) id state) + (ids ()) + +let report (id : Ident.t) report = + match Hashtbl.find_opt (Domain.DLS.get states_key) id with + | None -> report () + | Some state -> + let outside = Warnings.backup () in + Warnings.restore state; + Fun.protect ~finally:(fun () -> Warnings.restore outside) report diff --git a/compiler/typing/inline_not_stored.mli b/compiler/typing/inline_not_stored.mli new file mode 100644 index 0000000000..e23acbedcd --- /dev/null +++ b/compiler/typing/inline_not_stored.mli @@ -0,0 +1,13 @@ +(* The warning state of the bindings of [@inline] values: their [@warning] + and [@warnerror], and a [@@warning] above them. *) + +(* [record_binding attributes inner_attributes ids] records, if the binding + or its expression is [@inline], the warning state inside the binding's + [attributes] and those of its expression for the identifiers [ids ()] + binds. *) +val record_binding : + Parsetree.attributes -> Parsetree.attributes -> (unit -> Ident.t list) -> unit + +(* [report id f] runs [f], which reports warning 113 for [id], in the + warning state of [id]'s binding. *) +val report : Ident.t -> (unit -> unit) -> unit diff --git a/compiler/typing/typemod.ml b/compiler/typing/typemod.ml index cf516274cd..92ae8cebdb 100644 --- a/compiler/typing/typemod.ml +++ b/compiler/typing/typemod.ml @@ -1469,6 +1469,24 @@ and type_structure funct_body anchor env sstr = let defs, newenv = Typecore.type_binding ~context:None env rec_flag sdefs in + (* Warning 113 is reported after type checking, outside the warning + scopes of the binding and of a [@@warning] above it, so record the + warning state of [@inline] bindings here. *) + List.iter + (fun (vb : value_binding) -> + (* A type constraint keeps its attributes in [exp_extra], the + outermost first. [warning_scope] applies a list from its end, + so the innermost attributes go first. *) + let expression_attributes = + match vb.vb_expr.exp_extra with + | [] -> vb.vb_expr.exp_attributes + | extra -> + vb.vb_expr.exp_attributes + @ List.concat_map (fun (_, _, attrs) -> attrs) (List.rev extra) + in + Inline_not_stored.record_binding vb.vb_attributes + expression_attributes (fun () -> pat_bound_idents vb.vb_pat)) + defs; let () = if rec_flag = Recursive then Rec_check.check_recursive_bindings defs in @@ -1582,6 +1600,17 @@ and type_structure funct_body anchor env sstr = check_name check_module names name; let id = Ident.create name.txt in (* create early for PR#6752 *) + (* An [@inline] functor gets warning 113 like a function. Its [@inline] + may be below module type constraints. *) + let rec module_attributes (smod : Parsetree.module_expr) = + match smod.pmod_desc with + | Pmod_constraint (inner, _) -> + (* The innermost first, as for expressions. *) + module_attributes inner @ smod.pmod_attributes + | _ -> smod.pmod_attributes + in + Inline_not_stored.record_binding attrs (module_attributes smodl) + (fun () -> [id]); let modl = Builtin_attributes.warning_scope attrs (fun () -> type_module ~alias:true true funct_body diff --git a/compiler/typing/warnings.ml b/compiler/typing/warnings.ml index 3c151753cf..6d98ad2126 100644 --- a/compiler/typing/warnings.ml +++ b/compiler/typing/warnings.ml @@ -75,6 +75,7 @@ type t = | Bs_todo of string option (* 110 *) | Bs_private_record_mutation of string (* 111 *) | Bs_record_rest_optional_overlap of string list (* 112 *) + | Inline_not_stored of string * string (* 113 *) (* If you remove a warning, leave a hole in the numbering. NEVER change the numbers of existing warnings. @@ -128,8 +129,9 @@ let number = function | Bs_todo _ -> 110 | Bs_private_record_mutation _ -> 111 | Bs_record_rest_optional_overlap _ -> 112 + | Inline_not_stored _ -> 113 -let last_warning_number = 112 +let last_warning_number = 113 let letter_all = let rec loop i = if i = 0 then [] else i :: loop (i - 1) in @@ -463,6 +465,10 @@ let message = function ^ "\n\n\ \ This code is not implemented yet and will crash at runtime. Make sure \ you implement this before running the code." + | Inline_not_stored (kind, reason) -> + "This @inline " ^ kind + ^ " is inlined only in this module, not in modules that use it: " ^ reason + ^ "." | Bs_private_record_mutation field_name -> Printf.sprintf "Mutation of private record field %S." field_name @@ -563,6 +569,7 @@ let descriptions = (110, "Todo found"); (111, "Mutation of private record field"); (112, "Record rest pattern will always be empty"); + (113, "@inline function inlined only in its own module"); ] let help_warnings () = diff --git a/compiler/typing/warnings.mli b/compiler/typing/warnings.mli index 6be1a5b366..71d9e4c865 100644 --- a/compiler/typing/warnings.mli +++ b/compiler/typing/warnings.mli @@ -68,6 +68,7 @@ type t = | Bs_todo of string option (* 110 *) | Bs_private_record_mutation of string (* 111 *) | Bs_record_rest_optional_overlap of string list (* 112 *) + | Inline_not_stored of string * string (* 113 *) val parse_options : bool -> string -> unit diff --git a/tests/build_tests/projects/cross_module_relative_external/input.js b/tests/build_tests/projects/cross_module_relative_external/input.js index 402a508014..727bfefb73 100644 --- a/tests/build_tests/projects/cross_module_relative_external/input.js +++ b/tests/build_tests/projects/cross_module_relative_external/input.js @@ -7,11 +7,19 @@ import * as assert from "node:assert"; import * as fs from "node:fs/promises"; import * as path from "node:path"; +import { stripVTControlCharacters } from "node:util"; import { setup } from "#dev/process"; const { execBuildOrThrow, execClean } = setup(import.meta.dirname); -await execBuildOrThrow(); +const { stdout, stderr } = await execBuildOrThrow(); +const output = stripVTControlCharacters(stdout + stderr); + +assert.match( + output, + /Warning number 113.*Hidden\.res.*calls an external with a path relative to this file/s, + "warning 113 reports that Hidden.wrapped stays in its module", +); const app = await fs.readFile( path.join(import.meta.dirname, "src", "app", "App.mjs"), diff --git a/tests/build_tests/projects/cross_module_relative_external/rescript.json b/tests/build_tests/projects/cross_module_relative_external/rescript.json index 17c3ec773e..db37e2a201 100644 --- a/tests/build_tests/projects/cross_module_relative_external/rescript.json +++ b/tests/build_tests/projects/cross_module_relative_external/rescript.json @@ -10,5 +10,5 @@ }, "suffix": ".mjs", "compiler-flags": ["-bs-cross-module-opt"], - "warnings": { "error": true } + "warnings": { "error": "A-113" } } diff --git a/tests/build_tests/projects/cross_module_relative_external/src/ffi/Hidden.res b/tests/build_tests/projects/cross_module_relative_external/src/ffi/Hidden.res index b8360cdc1c..0d478a79bf 100644 --- a/tests/build_tests/projects/cross_module_relative_external/src/ffi/Hidden.res +++ b/tests/build_tests/projects/cross_module_relative_external/src/ffi/Hidden.res @@ -1,4 +1,5 @@ -// A path relative to this file: the call must stay in this module. +// A path relative to this file: the call must stay in this module, which +// warning 113 reports (checked by input.js). @module("./helper.js") external raw: int => int = "raw" @inline let wrapped = x => raw(x) + 1 diff --git a/tests/build_tests/projects/inline_warning_as_error/.gitignore b/tests/build_tests/projects/inline_warning_as_error/.gitignore new file mode 100644 index 0000000000..b39db7b277 --- /dev/null +++ b/tests/build_tests/projects/inline_warning_as_error/.gitignore @@ -0,0 +1,2 @@ +lib +src/**/*.mjs diff --git a/tests/build_tests/projects/inline_warning_as_error/input.js b/tests/build_tests/projects/inline_warning_as_error/input.js new file mode 100644 index 0000000000..3288dae1bd --- /dev/null +++ b/tests/build_tests/projects/inline_warning_as_error/input.js @@ -0,0 +1,34 @@ +// @ts-check + +// Warning 113 is reported while the .cmj is built, after type checking. As an +// error, it must still fail the build. + +import * as assert from "node:assert"; +import { stripVTControlCharacters } from "node:util"; +import { setup } from "#dev/process"; + +const { execBuild, execClean } = setup(import.meta.dirname); + +const lines = (/** @type {{stderr: string}} */ out) => + stripVTControlCharacters(out.stderr) + .split("\n") + .map(s => s.trim()); + +await execClean(); + +const warning = await execBuild(); +assert.equal(warning.status, 0, warning.stdout + warning.stderr); +assert.ok( + lines(warning).includes("Warning number 113"), + warning.stdout + warning.stderr, +); + +await execClean(); +const error = await execBuild(["--warn-error", "+113"]); +assert.notEqual(error.status, 0, "warning 113 as an error fails the build"); +assert.ok( + lines(error).includes("Warning number 113 (configured as error)"), + error.stdout + error.stderr, +); + +await execClean(); diff --git a/tests/build_tests/projects/inline_warning_as_error/rescript.json b/tests/build_tests/projects/inline_warning_as_error/rescript.json new file mode 100644 index 0000000000..34c2d07976 --- /dev/null +++ b/tests/build_tests/projects/inline_warning_as_error/rescript.json @@ -0,0 +1,6 @@ +{ + "name": "inline_warning_as_error", + "sources": ["src"], + "package-specs": { "module": "esmodule", "in-source": true }, + "suffix": ".mjs" +} diff --git a/tests/build_tests/projects/inline_warning_as_error/src/Limits.res b/tests/build_tests/projects/inline_warning_as_error/src/Limits.res new file mode 100644 index 0000000000..666189a8cc --- /dev/null +++ b/tests/build_tests/projects/inline_warning_as_error/src/Limits.res @@ -0,0 +1,4 @@ +let limit = "abc" + +// Other modules can't inline this: it uses `limit`. +@inline let withLimit = x => x ++ limit diff --git a/tests/error_tests/single/expected/warning_113_inline_not_stored.res.expected b/tests/error_tests/single/expected/warning_113_inline_not_stored.res.expected new file mode 100644 index 0000000000..5d6b259c3c --- /dev/null +++ b/tests/error_tests/single/expected/warning_113_inline_not_stored.res.expected @@ -0,0 +1,59 @@ + + Warning number 113 + /.../fixtures/warning_113_inline_not_stored.res:3:26-43 + + 1 │ // Exported @inline functions that other modules cannot inline. + 2 │ let rec helper = x => x > 10 ? x : helper(x + 1) + 3 │ @inline let usesHelper = x => helper(x) + 1 + 4 │ @inline let asyncOne = async x => x + 5 │ @inline let withDirective = @directive("'use memo'") x => x + + This @inline function is inlined only in this module, not in modules that use it: it uses helper, a value of this module. + + + Warning number 113 + /.../fixtures/warning_113_inline_not_stored.res:4:30-35 + + 2 │ let rec helper = x => x > 10 ? x : helper(x + 1) + 3 │ @inline let usesHelper = x => helper(x) + 1 + 4 │ @inline let asyncOne = async x => x + 5 │ @inline let withDirective = @directive("'use memo'") x => x + 6 │ @module("./h.js") external raw: int => int = "raw" + + This @inline function is inlined only in this module, not in modules that use it: it is async. + + + Warning number 113 + /.../fixtures/warning_113_inline_not_stored.res:5:54-59 + + 3 │ @inline let usesHelper = x => helper(x) + 1 + 4 │ @inline let asyncOne = async x => x + 5 │ @inline let withDirective = @directive("'use memo'") x => x + 6 │ @module("./h.js") external raw: int => int = "raw" + 7 │ @inline let relative = x => raw(x) + + This @inline function is inlined only in this module, not in modules that use it: it has a directive. + + + Warning number 113 + /.../fixtures/warning_113_inline_not_stored.res:7:24-34 + + 5 │ @inline let withDirective = @directive("'use memo'") x => x + 6 │ @module("./h.js") external raw: int => int = "raw" + 7 │ @inline let relative = x => raw(x) + 8 │ + 9 │ // Silenced where it is bound: no warning. + + This @inline function is inlined only in this module, not in modules that use it: it calls an external with a path relative to this file. + + + Warning number 113 + /.../fixtures/warning_113_inline_not_stored.res:26:24-41 + + 24 │ } + 25 │ + 26 │ @inline let shadowed = x => helper(x) - 1 + 27 │ + 28 │ // A functor silenced where it is bound: no warning. + + This @inline function is inlined only in this module, not in modules that use it: it uses helper, a value of this module. \ No newline at end of file diff --git a/tests/error_tests/single/expected/warning_113_inline_warnerror.res.expected b/tests/error_tests/single/expected/warning_113_inline_warnerror.res.expected new file mode 100644 index 0000000000..ec4431bd45 --- /dev/null +++ b/tests/error_tests/single/expected/warning_113_inline_warnerror.res.expected @@ -0,0 +1,51 @@ + + Warning number 113 (configured as error) + /.../fixtures/warning_113_inline_warnerror.res:4:45-62 + + 2 │ // for @inline on the function, and for a functor. + 3 │ let rec helper = x => x > 10 ? x : helper(x + 1) + 4 │ @warnerror("+113") @inline let usesHelper = x => helper(x) + 1 + 5 │ @warnerror("+113") let onFunction = {@inline x => helper(x) + 2} + 6 │ @inline let constrained = (@warnerror("+113") x => helper(x) + 3: int => + │ int) + + This @inline function is inlined only in this module, not in modules that use it: it uses helper, a value of this module. + + + Warning number 113 (configured as error) + /.../fixtures/warning_113_inline_warnerror.res:5:46-63 + + 3 │ let rec helper = x => x > 10 ? x : helper(x + 1) + 4 │ @warnerror("+113") @inline let usesHelper = x => helper(x) + 1 + 5 │ @warnerror("+113") let onFunction = {@inline x => helper(x) + 2} + 6 │ @inline let constrained = (@warnerror("+113") x => helper(x) + 3: int => + │ int) + 7 │ + + This @inline function is inlined only in this module, not in modules that use it: it uses helper, a value of this module. + + + Warning number 113 (configured as error) + /.../fixtures/warning_113_inline_warnerror.res:6:47-64 + + 4 │ @warnerror("+113") @inline let usesHelper = x => helper(x) + 1 + 5 │ @warnerror("+113") let onFunction = {@inline x => helper(x) + 2} + 6 │ @inline let constrained = (@warnerror("+113") x => helper(x) + 3: int => + │  int) + 7 │ + 8 │ module type S = { + + This @inline function is inlined only in this module, not in modules that use it: it uses helper, a value of this module. + + + Warning number 113 (configured as error) + /.../fixtures/warning_113_inline_warnerror.res:13:15-15:1 + + 11 │ + 12 │ @warnerror("+113") @inline + 13 │ module Make = (X: S) => { + 14 │  let w = helper(X.v) + 15 │ } + 16 │ + + This @inline functor is inlined only in this module, not in modules that use it: it uses helper, a value of this module. \ No newline at end of file diff --git a/tests/error_tests/single/fixtures/warning_113_inline_not_stored.res b/tests/error_tests/single/fixtures/warning_113_inline_not_stored.res new file mode 100644 index 0000000000..53e86371e5 --- /dev/null +++ b/tests/error_tests/single/fixtures/warning_113_inline_not_stored.res @@ -0,0 +1,47 @@ +// Exported @inline functions that other modules cannot inline. +let rec helper = x => x > 10 ? x : helper(x + 1) +@inline let usesHelper = x => helper(x) + 1 +@inline let asyncOne = async x => x +@inline let withDirective = @directive("'use memo'") x => x +@module("./h.js") external raw: int => int = "raw" +@inline let relative = x => raw(x) + +// Silenced where it is bound: no warning. +@warning("-113") @inline let silenced = x => helper(x) + +// @inline on the function, silenced on the binding or the function: no +// warning. +@warning("-113") let onFunction = {@inline x => helper(x)} +let onFunctionToo = {@warning("-113") @inline x => helper(x)} + +// Silenced on a type-constrained function: no warning. +@inline let constrained = (@warning("-113") x => helper(x): int => int) + +// Silenced in a nested module only: the top-level value of the same name +// still warns. +module Nested = { + @warning("-113") @inline let shadowed = x => helper(x) +} + +@inline let shadowed = x => helper(x) - 1 + +// A functor silenced where it is bound: no warning. +module type S = { + let v: int +} + +@warning("-113") @inline +module Silenced = (X: S) => { + let w = helper(X.v) +} + +// The same with the @inline on the functor, below a module type constraint. +@warning("-113") +module Constrained: (X: S) => { + let w: int +} = (@inline X: S) => { + let w = helper(X.v) +} + +// Stored for other modules: no warning. +@inline let closed = x => x + 1 diff --git a/tests/error_tests/single/fixtures/warning_113_inline_warnerror.res b/tests/error_tests/single/fixtures/warning_113_inline_warnerror.res new file mode 100644 index 0000000000..f7b076a7fa --- /dev/null +++ b/tests/error_tests/single/fixtures/warning_113_inline_warnerror.res @@ -0,0 +1,15 @@ +// A binding's @warnerror makes warning 113 an error for that value, +// for @inline on the function, and for a functor. +let rec helper = x => x > 10 ? x : helper(x + 1) +@warnerror("+113") @inline let usesHelper = x => helper(x) + 1 +@warnerror("+113") let onFunction = {@inline x => helper(x) + 2} +@inline let constrained = (@warnerror("+113") x => helper(x) + 3: int => int) + +module type S = { + let v: int +} + +@warnerror("+113") @inline +module Make = (X: S) => { + let w = helper(X.v) +} diff --git a/tests/tests/src/inline_static_exit_test.mjs b/tests/tests/src/inline_static_exit_test.mjs index 22133552bd..03e4d4926b 100644 --- a/tests/tests/src/inline_static_exit_test.mjs +++ b/tests/tests/src/inline_static_exit_test.mjs @@ -12,17 +12,6 @@ function big(s) { return s + "!" + s; } -function pick(x, y, flag) { - if (x !== 0 && y !== 0) { - return "other"; - } - if (flag) { - return "t"; - } else { - return big("a") + big("b"); - } -} - let constant = "t"; let x = id(0); @@ -85,36 +74,6 @@ if (exit$2 === 1) { computedLast = flag$2 ? "t" : big("a") + big("b"); } -function nested(x, y, z, w, flag) { - let exit = 0; - if (x !== 0) { - if (y !== 0) { - return "outer"; - } - exit = 1; - } else { - exit = 1; - } - if (exit === 1) { - let exit$1 = 0; - if (z !== 0) { - if (w !== 0) { - return "inner"; - } - exit$1 = 2; - } else { - exit$1 = 2; - } - if (exit$1 === 2) { - if (flag) { - return "t"; - } else { - return big("a") + big("b"); - } - } - } -} - let nestedConstant = "t"; let x$3 = id(0); @@ -291,28 +250,6 @@ if (exit$10 === 1) { safeRaised = flag$7 ? "t" : big("a") + big("b"); } -function bound(x, y, flag) { - let exit = 0; - let v; - if (x !== 0) { - if (y === 5) { - v = x; - exit = 2; - } - } else { - v = y; - exit = 2; - } - if (exit === 2 && flag) { - return big(v.toString()); - } - if (x !== 0 && y !== 0) { - return "other"; - } else { - return "zero"; - } -} - let boundConstant = big((5).toString()); let x$8 = id(0); @@ -429,28 +366,8 @@ if (exit$15 === 1) { boundLast = x$10 !== 0 && y$8 !== 0 ? "other" : "zero"; } -function Make(X) { - let r; - let exit = 0; - if (X.v !== 0 && X.w !== 0) { - r = "other"; - } else { - exit = 1; - } - if (exit === 1) { - r = X.flag ? "t" : big("a") + big("b"); - } - return { - r: r - }; -} - let r = "t"; -let First = { - r: r -}; - let v$3 = id(0); let w$3 = id(5); @@ -471,65 +388,34 @@ if (exit$17 === 1) { r$1 = flag$11 ? "t" : big("a") + big("b"); } -let Second = { - r: r$1 -}; - Mocha.describe("Inline_static_exit_test", () => { Mocha.test("two inlined copies", () => { - Test_utils.eq("File \"inline_static_exit_test.res\", line 124, characters 7-14", constant, "t"); - Test_utils.eq("File \"inline_static_exit_test.res\", line 125, characters 7-14", computed, "a!ab!b"); - Test_utils.eq("File \"inline_static_exit_test.res\", line 126, characters 7-14", computedOther, "other"); - Test_utils.eq("File \"inline_static_exit_test.res\", line 127, characters 7-14", computedLast, "t"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 127, characters 7-14", constant, "t"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 128, characters 7-14", computed, "a!ab!b"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 129, characters 7-14", computedOther, "other"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 130, characters 7-14", computedLast, "t"); }); Mocha.test("nested exits in inlined copies", () => { - Test_utils.eq("File \"inline_static_exit_test.res\", line 131, characters 7-14", nestedConstant, "t"); - Test_utils.eq("File \"inline_static_exit_test.res\", line 132, characters 7-14", nestedComputed, "a!ab!b"); - Test_utils.eq("File \"inline_static_exit_test.res\", line 133, characters 7-14", nestedInner, "inner"); - Test_utils.eq("File \"inline_static_exit_test.res\", line 134, characters 7-14", nestedOuter, "outer"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 134, characters 7-14", nestedConstant, "t"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 135, characters 7-14", nestedComputed, "a!ab!b"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 136, characters 7-14", nestedInner, "inner"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 137, characters 7-14", nestedOuter, "outer"); }); Mocha.test("inlined copies with an exception case", () => { - Test_utils.eq("File \"inline_static_exit_test.res\", line 138, characters 7-14", safeConstant, "t"); - Test_utils.eq("File \"inline_static_exit_test.res\", line 139, characters 7-14", safeComputed, "a!ab!b"); - Test_utils.eq("File \"inline_static_exit_test.res\", line 140, characters 7-14", safeRaised, "foo"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 141, characters 7-14", safeConstant, "t"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 142, characters 7-14", safeComputed, "a!ab!b"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 143, characters 7-14", safeRaised, "foo"); }); Mocha.test("inlined copies of an or-pattern binding a variable", () => { - Test_utils.eq("File \"inline_static_exit_test.res\", line 144, characters 7-14", boundConstant, "5!5"); - Test_utils.eq("File \"inline_static_exit_test.res\", line 145, characters 7-14", boundComputed, "zero"); - Test_utils.eq("File \"inline_static_exit_test.res\", line 146, characters 7-14", boundOther, "3!3"); - Test_utils.eq("File \"inline_static_exit_test.res\", line 147, characters 7-14", boundLast, "other"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 147, characters 7-14", boundConstant, "5!5"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 148, characters 7-14", boundComputed, "zero"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 149, characters 7-14", boundOther, "3!3"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 150, characters 7-14", boundLast, "other"); }); Mocha.test("functor applied twice", () => { - Test_utils.eq("File \"inline_static_exit_test.res\", line 151, characters 7-14", r, "t"); - Test_utils.eq("File \"inline_static_exit_test.res\", line 152, characters 7-14", r$1, "a!ab!b"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 154, characters 7-14", r, "t"); + Test_utils.eq("File \"inline_static_exit_test.res\", line 155, characters 7-14", r$1, "a!ab!b"); }); }); -export { - id, - big, - pick, - constant, - computed, - computedOther, - computedLast, - nested, - nestedConstant, - nestedComputed, - nestedInner, - nestedOuter, - Foo, - g, - safeConstant, - safeComputed, - safeRaised, - bound, - boundConstant, - boundComputed, - boundOther, - boundLast, - Make, - First, - Second, -} /* x Not a pure module */ diff --git a/tests/tests/src/inline_static_exit_test.res b/tests/tests/src/inline_static_exit_test.res index 10a2a6f22c..c0d43ecccb 100644 --- a/tests/tests/src/inline_static_exit_test.res +++ b/tests/tests/src/inline_static_exit_test.res @@ -1,3 +1,6 @@ +// Inlining within this module only: the @inline functions below use `big`, +// which keeps them out of other modules, so the interface exports nothing. + open Mocha open Test_utils diff --git a/tests/tests/src/inline_static_exit_test.resi b/tests/tests/src/inline_static_exit_test.resi new file mode 100644 index 0000000000..e264af1e3a --- /dev/null +++ b/tests/tests/src/inline_static_exit_test.resi @@ -0,0 +1 @@ +// Nothing is exported: the @inline functions are inlined only in this module.