Fix panic verifying generic HOF with generic closure argument (#108)#109
Merged
Conversation
) 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
Contributor
There was a problem hiding this comment.
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_defto extract the closure’s ownClosure(def_id, closure_args)arguments from the firstFnTraitgeneric arg, and pass only the parent-generic prefix todef_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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
$(cat <<'EOF'
Summary
Root cause:
resolve_fn_defwas returning the fullFnOnce::call_oncegeneric args[ClosureType, ArgsTuple]as the generic args fordef_ty_with_args(closure_def_id, ...). When the closure is defined inside a generic function (e.g.call_apply<T>), its own type parameterTisType::Param(0).def_ty_with_argsthen calledinstantiate_ty_paramswith those args, mappingT → 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 — sowith_value_varproducedvalue_var = Nonewhile the refinement still containedRefinedTypeVar::Value, triggering theunwrap()panic atrty.rs:1440.Fix (
src/analyze/basic_block.rs): one-line change inresolve_fn_def. The closure's own type args are already stored inside theClosure(def_id, closure_args)kind of the first FnTrait generic arg. Usingclosure_argsinstead of the FnTrait call'sargsgives the correctT → i32instantiation.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 produceUnsat, confirming the verification actually runs rather than being silently skipped.Closes #108.
EOF
)
Generated by Claude Code