Repository navigation
refactor(ffi): infer instances in one pass before typing - #188
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Inference walks every signature the export maps, at every choice its bounds allow, before any type is declared, so mapping a signature never discovers an instance. A function inferred at an instance keeps its lent closure and foreign callback parameters, and a concrete instance only a trait impl names, such as Wrap<u32>: Seed<u64>, is inferred. Drops the pending/named bookkeeping and the rerun of collection per inferred round. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0fb182d0b3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 0fb182d
Two actionable P1 issues are already independently reported; I agree with them and am referencing rather than duplicating inline comments:
- Bound non-ADT instance growth, ffi/src/collect.rs:436–438: a seeded Grow returning Grow<(T,)> keeps admitting deeper tuples. The ADT-repeat guard does not bound tuple/array/reference nesting, and inference now expands even instances typing would refuse. Add a terminating structural admission rule and a regression for these constructors.
- Preserve callback-only inference, ffi/src/collect.rs:502: before declarations, a callback trait with no Rust implementors makes instantiate fail and the signature disappear. Typing later accepts that callback and can panic on its newly encountered return instance. Infer the kept callback alternative independently of the declaration map and cover a callback with no Rust implementors.
Direction: separating inference from mapping is a useful simplification, and sharing instantiation/bound checks avoids duplicated choice rules. The remaining requirement is that this earlier pass be both terminating and a conservative superset of typing; the new closure/implementor and Ping/Pong fixtures do not cover the two cases above. No additional actionable findings.
Verification: static inspection of the collector, glue/IR changes and fixtures only. I did not run tests or independently verify the reported moq export comparison.
…oices while inferring Inference walks only the members of an instance whose arguments could map, so a method wrapping its argument in a tuple no longer grows instances forever. Before traits are declared, a parameter one exported trait bounds may stay for a foreign implementation, so a signature taking a callback no Rust type implements still names its instances. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review (Written by Claude Opus 5.5) |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e199039a6e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: e199039
Compared with my last review at 0fb182d (one fix commit; unchanged base).
P1 still open, incomplete fix: recursively check exported argument instances before expanding members. In ffi/src/collect.rs:503–507, arguable accepts any exported ADT without checking its arguments. With exported Wrap<T> and Grow<T>, a seed Grow<Wrap<u32>>, and impl<T> Grow<Wrap<T>> { pub fn grow(self) -> Grow<Wrap<(T,)>> { todo!() } }, inference still admits Grow<Wrap<(u32,)>>, then Grow<Wrap<((u32,),)>>, indefinitely. Every outer argument is an exported Wrap, so it passes this filter, and no individual type contains a repeated ADT for admit to reject. Although Wrap<(u32,)> would eventually be refused, typing is never reached. This is a remaining case of the earlier growth finding, not a separate new bug. Recursively establish argument eligibility (including referenced instances' arguments) and add this wrapped-tuple regression alongside direct Grow<(T,)>.
The callback-only inference panic is addressed: structural foreign recognition preserves the parameter during inference, while taken retains declaration validation during typing. The new keeps fixture exercises the previously missing return instance through C/C++ and the native transcript.
Direction: retaining a separate inference pass and narrowing member expansion is sensible, but the termination guard must follow nested argument dependencies, not just the outer constructor. No other actionable findings in this update.
Verification: static inspection of the fix and regression changes only; I did not run tests or execute the wrapped-tuple reproduction.
🛡️ Codex Security Review · Automatically triggeredSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
…apping its fields Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review (Written by Claude Opus 5.5) |
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 29d81a1
Compared with my last review at e199039: one fix commit, unchanged base.
- Fixed: the independently reported field-settling panic.
ffi/src/collect.rs:770–774now marks unsupported argument instances refused beforesettlevisits fields, and the newShell<(u32,)>/Core<u8>reject fixture covers this case. - Still open, not new: the wrapped-tuple inference-growth P1 from my previous review. The renamed
unarguablestill checks only the outer ADT (ffi/src/collect.rs:507–511), so the exportedWrapcase remains unbounded. Recursively validate argument eligibility and add that regression. The callback-only panic remains addressed.
Direction: sharing the early eligibility check between inference and declaration is a focused fix with little extra complexity. No new actionable bugs found in this delta; the existing termination issue remains.
Verification: static inspection of the collector delta, surrounding declaration/settling logic and reject fixture/expectations only. I did not run tests or execute a reproduction.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 29d81a110e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
🛡️ Codex Security Review · Automatically triggeredSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
… at the first refusal An instance typing meets that inference did not admit is now refused at the item's span as `instance not inferred`, with a note at the generic type, so a gap in inference shows in the census instead of crashing the exporter. A candidate typing tries and finds uninferred is rejected. `instantiate` returns at its first refusal, as inference does, so it maps no later parameter's candidates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review (Written by Claude Opus 5.5) |
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: b53d8ea
Compared with my last review at 29d81a1, excluding the merged main changes (base now a4fb96f).
Fixed: the independently reported candidate-selection panic after a refused instantiation. ffi/src/collect.rs:1664–1665,1684–1685 now stops at the first refusal; 2478–2485 turns a missed instance into a diagnostic instead of panicking. The new const-generic probe reject fixture covers the reported ordering.
Still open, not new: the wrapped-tuple inference-growth P1. unarguable at ffi/src/collect.rs:507–511 remains shallow, so exported Wrap arguments can still hide unbounded tuple growth. Recursively validate those argument dependencies and cover the wrapped case. Earlier callback-only and field-settling panic fixes remain intact.
Direction: fail-fast instantiation is simpler than accumulating errors while continuing speculative mapping, and a source-spanned refusal is a reasonable fallback for missed inference. No new actionable bugs found in this delta; the existing termination issue remains.
Verification: static inspection of the FFI delta, surrounding selection/refusal paths and fixture expectations only. I did not run tests or reproductions; merged TypeScript changes were not re-reviewed.
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
🛡️ Codex Security Review · Automatically triggeredSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
|
Merging at b53d8ea, which Codex reviewed clean in round 4, with a direct squash merge (the land queue is not set up yet).
(Written by Claude Opus 5.5) |
#184 and #188 merged apart: #188 added apply, report, wrapped, and keeps to the api fixture's native transcript and its C and C++ tests, while #184's Kotlin test predates them, so `just ffi test` fails on main. Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Restructures the exporter's instance inference into one pass that runs before typing, so the cases Codex kept finding on #152 follow from the design instead of being patched one by one. It finishes the quest it opened,
quest/m1/ffi-export/inference.md, and deletes it.Consumer: moq. Its export is byte-identical to main's, and its census changes as listed below. The two round-4 cases from #152 now export, and the api fixture calls each one from C and C++.
Design
Before this PR, mapping a signature discovered instances. When
ty()met an instance not yet inferred, it recorded it as pending, andnaming()kept it if that was the signature's only refusal. Collection then reran from scratch. So inference saw only what typing reached:Wrap<u32>failedeach's mapping check before any signature was typed.One pass,
infer, after the walk and beforedeclare. It reads rustc's types only. No mapping outcome feeds back into it.pub fnandconst, and each trait's methods.pubfields, and the inherent methods and constants that apply to it. An object's private fields are not part of its API.instantiatethat typing uses, withinferringset:choosefixes takes its ranked candidates, up to the first that names no export. Typing takes a later one only when an earlier export is refused.Fntrait bounds stays a parameter. Closures are kept, not typed.impl Seed<u64> for Wrap<u32>).exclude.(), a primitive,String, or an export's type, asargrequires).declarerefuses any other instance astype argumentwhen it first declares it, before mapping any of its members. The set is sorted by path, so it does not depend on crate or item order, and the shortest-unique names stay as decided.(), primitives,String, or export types add inputs.admitrejects an instance holding an instance of its own type at any depth. So on any path from such an instance to its leaves, each generic type appears at most once, depth is bounded by the number of generic types, and leaves come from a finite set ((), primitives,String, the non-generic exported types). Those instances are finite, and so are the ones their signatures name. The reject fixture'sGrow<T>::grow -> Grow<(T,)>admitsGrow<(u32,)>, which is refused astuple, and goes no further. The reject fixture'sPing<T>::pong -> Pong<Ping<T>>andPong<T>::ping -> Ping<Pong<T>>admitPong<Ping<u8>>. They stop atPing<Pong<Ping<u8>>>, which holds aPinginside aPing.declareanddefineread the set and never add to it. The mapping filters (a type that does not map, a refused callback trait) only choose among the instantiations inference took.instantiatereturns at its first refusal in both modes, so typing maps no candidate after an error that inference stopped at. A generic type unreachable from its crate's root is now refused as such, not as "instance no export names".instance not inferred, with a note at the generic type, and a candidate it tries and finds uninferred is rejected. A gap shows up in the census as that construct and never crashes the exporter. Today no crate in moq's census reports it. Rounds 1 to 3 of review each found such a gap, and one is now a refusal, not a panic.signaturemaps them. An instance's turbofish names such a parameter_(Instance::argsis nowVec<Option<Type>>), and Rust infers it from the argument.holdsused to check every clause with chosen arguments substituted, against the item's identity environment. That environment states each bound only at the item's own parameters. So a clause naming another parameter, such asF: Fn(T)atT = u32orP: IntoRendition<E, _>atE = Extra, could never hold.holdsnow skips clauses that still name a parameter, asappliesalready did, and the final check runs on every instantiation.unmet bound ...), not "generic parameters no choice meets the bounds of".instance-keysquest asked about: moq-mux'sjson_snapshot<T: Serialize>now takesString. moq's config still excludes it, and the quest's text says so.Reuse.
rustc_monomorphize::collectorispub(crate). It roots at non-generic items and walks MIR bodies for the concrete uses they make, bounded by the recursion limit. It cannot choose arguments for a public generic item that no body uses, which is the whole problem here. The pass keeps reusing rustc's trait solver for every bound, andtrait_impls_offor implementors.Choice made, for review
The instance set now depends only on the signatures, not on what maps. So an instance named only by a function refused for another reason still infers, and then exports or is refused itself. Fixing a mapping gap later never changes the instance set or any instance's name. Removing the typing feedback that would avoid this was the point of the redesign. Effect on moq's census (the export is unchanged):
Report/Snapshot<String>instances, named bypublisher/viewer, whichHealthrefusesPending<…>instances, refused as objects nothing returnsjson::{Snapshot,Stream,Consumer}<String>, fromjson_snapshot/json_streamatStringTimedinstancesfailover::Attempt<…>instances, refused as objects nothing returnsChanges
ffi/src/collect.rs:inferruns the fixpoint, throughnamed/instantiated.members,shown,spelled, andunmetare split out ofmethods,functions,each, andholds, so the pass and typing share them.lent,maps, andnames_exportare new.Instantiation::keptreplacesimpls, andsignaturemaps the kept parameters.pending,named,naming(),UNNAMEDand its note, and collection's inner rerun loop.ffi/src/ir.rs,ffi/src/glue.rs: a kept declared parameter isNone, spelled_.apply<T, F: Fn(T) -> u32>(Pair<T>, F),report<T, L: Log>(Pair<T>, L), andWrap<T>with onlyimpl Seed<u64> for Wrap<u32>, each called from C, C++, and the native transcript.keeps(impl Log) -> Boxed<u8>, a callback no Rust type implements on a signature naming an instance nothing else names.Ping/Pong,Grow,Shell<(u32,)>, whose field names an instance nothing else does, andprobe<const N: usize>(impl Into<Probe<u8>>), refused at its const parameter before its other candidate is mapped.ffi/README.md: the inference paragraph.quest/m1/ffi-export/instance-keys.md: theT: Serializefinding.Net lines (merge base to head):
ffi/src+388 −240. Everything, fixtures and tests included: +543 −261.Public API: additions only. New C/C++ functions appear where a crate has these shapes (the api fixture's
api_apply_u32,api_report_u32,api_wrap_u32, and*_wrap_u32seed instances). Existing names in the api fixture and moq are unchanged.Instance::argsis crate-internal. No MIGRATE entry.Checks
just check,just ffi test(17 tests), andjust ffi moq(C and C++) pass.just checkreran on the merged head; main touched onlyts/.apply(type parameter T),report<u32>(type parameter L), andWrap(generic type).Ping/Pong,Grow,Shell, andProberefusals and the line shifts they cause. One existing message changed:Cell<Vec<String>>is now refused astype argument, like every instance refused for its arguments.instantiatein typing mode mapped a candidate after a const refusal and hit theunreachable!. Reproduced. The maintainer chose to keep the pass, replace the panic with theinstance not inferredrefusal, and stopinstantiateat its first refusal. Fixed in b53d8ea's parent.Checkpassed on b53d8ea.Follow-ups
json_snapshot/json_streamnow chooseString. The JSON shape stays with theinstance-keysquest.🤖 Generated with Claude Code