Give closures concretely typed captures #4915
All Vouch review confidence checks passed
Residual risk 0.25 = Change Risk (0.48) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 504 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.372
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #4915"
general
weight: 0.35
KristofferC
A closure's type has one type parameter per captured variable, so a closure whose captured variable is inferred as a
Union(e.g.Union{Nothing, String}) has a type that is only known at run time: building it takes a call toapply_typeat run time. Natively that works, through Julia's runtime. A code generator that compiles Julia without the runtime, in a closed world (e.g. to WebAssembly), cannot build a type at run time, so these closures don't compile there. This PR makes the captures concrete at the 11 such closures in Pkg's own code, with no behaviour change: anIntsentinel, an explicit element type, a typeassert to the type already known at that point, or the value wrapped inSome(a concrete type) and unwrapped inside the closure.| Function | Captured variable (inferred type) | Change |
|---|---|---|
|
Resolve.build_eq_classes1!|pref_idx::Union{Int, Nothing}|0for "none" (version indices start at 1) ||
Operations.download_source|server_registry_info::Union{Nothing, Tuple{…}}| captured asSome{…}, unwrapped in the closure ||
Types.handle_repo_add!|repo_source::Union{Nothing, String}|repo_source::String(asserted non-nothingabove, and already asserted::Stringat its uses) ||
Registry.download_registries|url::Union{Nothing, String}| captured asSome{…}||
Operations.add|added, aSetwhose element type depends on the values |Set{Union{Nothing, UUID}}||
Operations.print_compat|names, a comprehension ofpkg.name|Union{Nothing, String}[…]||
PlatformEngines.download|progress::Union{Nothing, …},interrupt::Union{Event, Nothing}| thearg_writeclosure moves into a function,request_to, so the call is union-split ||
Operations.download_artifacts|pkg_uuids(as inadd) |Set{Union{Nothing, UUID}}||
Operations.download_artifacts(selector closure) |server_registry_info(as above),io(ctx.io, abstractly typed) |Some{…};ctx.ioread inside ||
Operations.sandbox|sandbox_project::Union{Nothing, String}|projectfile_pathwithoutstrictalways returns aString: asserted ||
Operations.collect_project|project_file::Union{Nothing, String}, used assomething(project_file, path)| thatStringcomputed once and captured |The sites were found by scanning Pkg's inferred code (
code_typed(…; optimize = false)of every specialization of Pkg's methods after loading Pkg and precompiling the enclosing functions) for a closure type built with a non-constantapply_type. On this branch the scan finds 5 left, which this PR does not change: 3 in the vendored Resolver (Clauses.jl:426,Problem.jl:157,SAT.jl:664), which follows its upstream, and 2 ininstall_collected_artifacts!, whose download jobs are values of aDict{SHA1, Function}, so the capturedret/fare abstractly typed by design.A typeassert to a
Union, or a declaredx::Union{…}, does not help: the closure's type parameter is stilltypeof(x).juliac --trim=safejuliac --trim=safe(JuliaC 0.3.12, Julia 1.13.2) accepts such a closure: a 6-line program with aUnion{Nothing, Int}capture builds with no verifier error and runs, since native code builds the closure type through the runtime. So a juliac probe does not measure this change, and none is given here.Tests
test/misc.jl, "closures capture concretely typed values": 9 checks, one per enclosing function, that its typed code builds no closure from a type computed at run time. They cover 10 of the 11 closures; the one not covered is indownload_registries, inside another closure. The 9 checks fail on master (7eabf057d) and pass on this branch.Pkg.test(test_args = ["misc", "resolve", "registry", "platformengines", "artifacts", "sandbox", "repo", "add"])passes 1903/1903 on this branch (Julia 1.15.0-DEV.235 0d77de6d2c, General from the package server).🤖 Generated with Claude Code