Skip to content

feat(ffi): take what converts into an Into target through a chain of From implementations - #248

Merged
kixelated merged 14 commits into
mainfrom
quest/m3/ffi-from-chains
Oct 30, 2026
Merged

kixelated merged 14 commits into
mainfrom
quest/m3/ffi-from-chains

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Completes quest/m3/ffi-from-chains.md.

Problem

impl Into<X>, where X does not map, followed only one level of From implementations. A source parameter bounded by Into<Y> with an unmapped Y stopped there. The parameter then fell back to String, Bytes, or Vec<u8>, which is the #197 bug class: for example, api_sample_hex rejected a payload that is not UTF-8.

Approach

  • sources in ffi/src/collect.rs walks breadth-first. It follows each Into<Y> bound of a From source parameter whose Y does not map, through hop (the old one-level body). Each (target, outlives) pair is visited once, which ends cycles.
  • A chain longer than the crate's recursion_limit, past which rustc proves no conversion either, is refused as growing conversion, with a note at the impl. The pinned nightly accepts impls such as From<U: Into<Wrap<Vec<T>>>> for Wrap<T>, so without this the walk hung. A finite expansion that stops matching still exports.
  • Lifetimes: after matching an impl's self type to the target, hop reads the inference context's region constraints. It pins to 'static each impl lifetime the target makes 'static (or that a lifetime the export makes 'static covers), closes over 'b: 'a clauses, and erases the rest. A followed target keeps those 'statics. A leaf borrow is dropped when it names 'static, or when any level must outlive the call. A lifetime-bearing intermediate is followed, not dropped.
  • Ambiguity: two chain sources that C passes as one buffer but that hold different values, such as String and Vec<u8>, are refused as ambiguous conversion, with a note at the other impl. instantiate returns both new refusals directly instead of falling back to each.
  • Fixtures:
    • api (differential against native Rust in C, C++, and Kotlin):
      • two-hop sample_hex and three-hop queued_hex (middle level 'static), both with a payload that is not UTF-8
      • a Tick/Tock cycle with a Label exit
      • rooted_name: an owned record behind a 'static intermediate
      • stored_text: a &str its 'static target does not borrow for
      • layer_name: a finite expansion
    • reject: call (ambiguous), grows (growing), and holder, lender, outlived, paired, pinner (borrows some level demands outlive the call)
    • Each new case failed, hung, or exported wrongly on the head before its fix.

Choices made without asking

  • "Same foreign signature" means the same C buffer, and "different sources" means different values. &str, String, and Box<str> are forms of one value and convert alike, so they are not ambiguous. Counting them as ambiguous refused moq-net's broadcast::Producer::create_track and reserve_track (impl Into<Arc<str>>). The maintainer confirmed this narrowing.
  • The rule applies to one-level chains too, so one-hop and multi-hop behave the same.

Impact

  • Public API: the C, C++, and Kotlin types of parameters that convert through a chain. Exporter refusals gain ambiguous conversion and growing conversion.
  • just ffi census for moq: no movement. With main's exporter at 3c4fb50, every package's census is identical. No moq parameter goes through two levels; impl Into<Timed<Bytes>> is one.
  • Consumer: none needs it yet; the quest anticipated moq parameters going through two levels. The maintainer asked for it now.

Alternatives

  • Refusing an impl reused at a larger target: rejected after review. It refused finite expansions.
  • Aligning lifetimes by zipping walk()s: rejected after review. walk() deduplicates, so repeated target lifetimes misalign.
  • Refusing every same-signature pair, including &str/String: regresses moq (above).

Follow-ups

  • None.

Review

Codex (gpt-6.1-sol, medium), quest review --base origin/main. The latest round is clean (0 findings) on head dcaf1e9.

First session (rounds 1 to 4):

  • Round 1: a 'static intermediate lost on erasure: fixed (holder). Expanding chains: wrongly refuted with system rustc 1.98.
  • Round 2: a lifetime instantiated to 'static through the target: fixed (lender). Expanding chains hang on the pinned nightly: fixed (grows).
  • Round 3: a leaf whose own level must outlive the call was borrowed: fixed (pinner).
  • Round 4 (design checkpoint): intermediate targets with a lifetime were over-refused. The maintainer chose to continue; fixed (rooted_name).

Second session (restarted by the merge of #249):

  • Round 1: finite expansion refused: fixed with the recursion-limit bound (layer_name). Untied source under a 'static target over-refused: fixed with lifetime pinning (stored_text).
  • Round 2: 'b: 'a not propagated: fixed (outlived).
  • Round 3: walk() zip misaligns repeated lifetimes: fixed with region constraints (paired).
  • Round 4: clean.

Head 34bd5fa is dcaf1e9 plus a conflict-free merge of origin/main (6c3fd5a, ts and quest files only), so the review carries over. Local checks: just ci passes on 34bd5fa; just ffi moq passes on dcaf1e9.

Merge of main at cbf8ee08417c3fc6fa0f76bec146b5f8c58a73fc: git merged it without conflicts and it touches no ffi/ file; it only refreshes the base so GitHub can merge, with no change to this PR's diff since 34bd5fae, so it is a recorded skip, too trivial to review.

(written by Claude Opus 5.5)

kixelated and others added 5 commits October 29, 2026 19:44
…From implementations

An `impl Into<X>` whose `X` does not map follows each `Into<Y>` bound of a
`From` source parameter whose `Y` does not map either, through any number of
levels, carrying whether a level must outlive the call and visiting each
target once so a cycle ends. Two chain sources that C passes as one buffer but
that hold different values, such as `String` and `Vec<u8>`, are refused as an
ambiguous conversion.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… target names 'static

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…get ties to 'static

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…call

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Into targets through a chain of From implementations feat(ffi): take what converts into an Into target through a chain of From implementations Oct 30, 2026
@kixelated

Copy link
Copy Markdown
Collaborator Author

Decision (maintainer, round 4 checkpoint): continue with option 1. Apply the borrow filter only to a chain's leaves, carry the outlives requirement into followed targets, and add an api case with an owned record behind a lifetime-carrying intermediate, reproducing the over-refusal first. The narrowing of "same foreign signature" to different values in one C buffer stands and stays listed in the PR body.

(written by Claude Opus 5.5)

kixelated and others added 7 commits October 29, 2026 21:59
# Conflicts:
#	quest/m3/ffi-from-chains.md
… filtering only borrowed leaves

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…etimes the target ties to 'static

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s 'static

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…, not by walk position

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 05:48
@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 40 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4eed7a8a-19ce-49ab-9b22-bcc40264e4b7
Commits

Reviewing files that changed from the base of the PR and between 31ba4c2 and cbf8ee0.

Files selected for processing: 14
  • ffi/README.md
  • ffi/fixtures/api/examples/transcript.rs
  • ffi/fixtures/api/ffi.toml
  • ffi/fixtures/api/src/lib.rs
  • ffi/fixtures/reject/expected.txt
  • ffi/fixtures/reject/ffi.toml
  • ffi/fixtures/reject/src/lib.rs
  • ffi/src/collect.rs
  • ffi/src/ir.rs
  • ffi/tests/api.c
  • ffi/tests/api.cpp
  • ffi/tests/api.kt
  • quest/m3/README.md
  • quest/m3/ffi-from-chains.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Automated review by review (OpenAI)

Reviewed commit 34bd5fae511c18b0ecc4ccc21d74340a9a0c47a4.

No actionable bugs found. The breadth-first walk follows unmapped Into-bound targets rather than assuming arbitrary From conversions compose; visited target/outlives pairs terminate cycles, and candidate validation still checks the original export's bounds. Static lifetime constraints are carried through intermediate targets, while unrelated borrowed inputs remain usable. The fixtures cover non-UTF-8 chains, owned intermediates, cycles with an exit, finite expansion, growing-chain refusal, lifetime rejection and ambiguous buffer sources across the C/C++/Kotlin transcripts.

Direction: extending the existing source discovery is preferable to adding consumer-specific conversion configuration. The explicit growing/ambiguous refusals avoid silently falling back to a different parameter representation. The documented string-form equivalence is treated as the chosen API policy.

Verification limits: GitHub-only static review of the implementation, surrounding candidate selection and fixtures, plus an independent read-only pass. I did not run the exporter, differential suites, census or reported local checks, and do not claim they passed.

(Written by OpenAI)

@kixelated
kixelated merged commit 7edb4e5 into main Oct 30, 2026
1 check passed
@kixelated
kixelated deleted the quest/m3/ffi-from-chains branch October 30, 2026 05:59
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.

1 participant