← Back to Pull requests

Give closures concretely typed captures #4915

Open
unknown opened this pull request with 504 lines diff across 6 files
View on GitHub
unknown unknown commented on Nov 06, 2026 17:28 UTC
Author

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 to apply_type at 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: an Int sentinel, an explicit element type, a typeassert to the type already known at that point, or the value wrapped in Some (a concrete type) and unwrapped inside the closure.

| Function | Captured variable (inferred type) | Change |
|---|---|---|
| Resolve.build_eq_classes1! | pref_idx::Union{Int, Nothing} | 0 for "none" (version indices start at 1) |
| Operations.download_source | server_registry_info::Union{Nothing, Tuple{…}} | captured as Some{…}, unwrapped in the closure |
| Types.handle_repo_add! | repo_source::Union{Nothing, String} | repo_source::String (asserted non-nothing above, and already asserted ::String at its uses) |
| Registry.download_registries | url::Union{Nothing, String} | captured as Some{…} |
| Operations.add | added, a Set whose element type depends on the values | Set{Union{Nothing, UUID}} |
| Operations.print_compat | names, a comprehension of pkg.name | Union{Nothing, String}[…] |
| PlatformEngines.download | progress::Union{Nothing, …}, interrupt::Union{Event, Nothing} | the arg_write closure moves into a function, request_to, so the call is union-split |
| Operations.download_artifacts | pkg_uuids (as in add) | Set{Union{Nothing, UUID}} |
| Operations.download_artifacts (selector closure) | server_registry_info (as above), io (ctx.io, abstractly typed) | Some{…}; ctx.io read inside |
| Operations.sandbox | sandbox_project::Union{Nothing, String} | projectfile_path without strict always returns a String: asserted |
| Operations.collect_project | project_file::Union{Nothing, String}, used as something(project_file, path) | that String computed 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-constant apply_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 in install_collected_artifacts!, whose download jobs are values of a Dict{SHA1, Function}, so the captured ret/f are abstractly typed by design.

A typeassert to a Union, or a declared x::Union{…}, does not help: the closure's type parameter is still typeof(x).

juliac --trim=safe

juliac --trim=safe (JuliaC 0.3.12, Julia 1.13.2) accepts such a closure: a 6-line program with a Union{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

  • New testset in 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 in download_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).

  • Runic 1.4 reports no changes on the modified files.
  • 🤖 Generated with Claude Code

    Diff: 504 lines (+378 / -126) across 6 files Reviewer: KristofferC Review duration: 35273m
    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
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.6% (MEDIUM) · Higher = larger blast radius
    47.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.4% · Safe to merge
    25.4%