Skip to content

Fix panic verifying generic HOF with generic closure argument (#108)#109

Merged
coord-e merged 3 commits into
mainfrom
claude/kind-brahmagupta-Akh4l
Jun 7, 2026
Merged

Fix panic verifying generic HOF with generic closure argument (#108)#109
coord-e merged 3 commits into
mainfrom
claude/kind-brahmagupta-Akh4l

Conversation

@coord-e
Copy link
Copy Markdown
Owner

@coord-e coord-e commented Jun 7, 2026

$(cat <<'EOF'

Summary

  • Root cause: resolve_fn_def was returning the full FnOnce::call_once generic args [ClosureType, ArgsTuple] as the generic args for def_ty_with_args(closure_def_id, ...). When the closure is defined inside a generic function (e.g. call_apply<T>), its own type parameter T is Type::Param(0). def_ty_with_args then called instantiate_ty_params with those args, mapping T → unit (the closure model for a non-capturing closure) instead of the correct concrete type. After that wrong substitution the arg-tuple type (own T,) collapsed to (own unit,) — a singleton sort — so with_value_var produced value_var = None while the refinement still contained RefinedTypeVar::Value, triggering the unwrap() panic at rty.rs:1440.

  • Fix (src/analyze/basic_block.rs): one-line change in resolve_fn_def. The closure's own type args are already stored inside the Closure(def_id, closure_args) kind of the first FnTrait generic arg. Using closure_args instead of the FnTrait call's args gives the correct T → i32 instantiation.

Test plan

  • tests/ui/pass/issue_108.rs — exact reproduction from the issue; previously panicked, now verifies cleanly.
  • tests/ui/fail/issue_108.rs — same shape with a wrong assertion (r == 4); should produce Unsat, confirming the verification actually runs rather than being silently skipped.

Closes #108.
EOF
)


Generated by Claude Code

claude added 3 commits June 7, 2026 08:08
)

resolve_fn_def returns the closure's own generic args (extracted from
the closure type itself) instead of the full FnTrait call's generic args.

The bug: when calling f(x) inside apply<T, F: FnOnce(T)->T>, the MIR
call is FnOnce::call_once(f, (x,)) whose generic args are [ClosureType,
(T,)]. resolve_fn_def was forwarding these unmodified as the generic args
for def_ty_with_args(closure_def_id, ...). def_ty_with_args then calls
instantiate_ty_params with those args, mapping Type::Param(0) (= T from
call_apply<T>) to the closure model type () instead of the correct i32.
After that wrong substitution the arg tuple type became (own (),) whose
sort is singleton, so with_value_var produced value_var = None while the
refinement still contained RefinedTypeVar::Value — causing the unwrap panic.

The closure's correct type args are stored in the Closure kind of the
first FnTrait arg (args.type_at(0) = Closure(def_id, closure_args)).
Using closure_args instead of args gives the right instantiation.

Fixes #108.
Verifies that the generic HOF / generic closure shape produces a correct
Unsat result when the assertion is wrong, not just that it no longer panics.
When a closure is called via an Fn-trait method, `resolve_fn_def` was
returning the closure's full internal type args (which include upvars,
return type, fn signature binder, etc.). `def_ty_with_args` would then
pass these opaque types to `type_builder.build()`, triggering
`not implemented` panics on internal Rust types like `i8`.

Only the parent generics (inherited from the enclosing generic function)
are meaningful to `def_ty_with_args`; slice to `parent_count` to drop
the closure-internal portion.

https://claude.ai/code/session_01FSUujExLmDgAHHW49m72ZN
@coord-e coord-e marked this pull request as ready for review June 7, 2026 08:21
@coord-e coord-e requested a review from Copilot June 7, 2026 08:22
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a verifier panic when analyzing generic higher-order functions that call a generic-typed closure argument via FnOnce::call_once/RustCall. The change corrects how the closure’s generic arguments are derived so the closure body/type is instantiated with the parent function’s generics (e.g. T -> i32) instead of the FnOnce::call_once method’s generics (which previously caused an incorrect T -> unit substitution and later unwrap() panic).

Changes:

  • Update resolve_fn_def to extract the closure’s own Closure(def_id, closure_args) arguments from the first FnTrait generic arg, and pass only the parent-generic prefix to def_ty_with_args.
  • Add a UI pass test that previously triggered the panic and now verifies cleanly.
  • Add a UI fail test with the same shape that should report Unsat, ensuring verification actually runs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/analyze/basic_block.rs Fixes FnTrait closure call resolution by using the closure’s stored args (parent-generic prefix) instead of FnOnce::call_once args when instantiating the closure def type.
tests/ui/pass/issue_108.rs Regression test reproducing the original panic scenario; should now pass verification.
tests/ui/fail/issue_108.rs Negative test ensuring the verifier runs and reports Unsat for an incorrect assertion in the same pattern.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@coord-e coord-e merged commit 699b6c5 into main Jun 7, 2026
7 checks passed
@coord-e coord-e deleted the claude/kind-brahmagupta-Akh4l branch June 7, 2026 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic verifying a generic function whose closure argument has a generic parameter type

3 participants