Issue #2834: An emitted @stream item cannot view its input: winnow-grammar’s emit sink needs 'static (decision: how D7’s “views the input” and “may be emitted” hold together)

Open and answer this issue on GitHub.

wip, bugs, yaml · opened 2026-11-23 · updated 2026-11-30 · 6 comments

Filed by the driver from the Found: of the run of issue-2417 (#2417) (owner, 2026-11-06).

Found

An emitted @stream item cannot view its input, though ADR-grammar-typed-read-v1 D7 says “An item views the input it is parsed from” and “An item may be emitted”: winnow-grammar a310181 emitted_type (winnow-grammar-macros/src/codegen/stream.rs) rewrites every lifetime of an emitted item to 'static (its ADR 28: the sink tells values apart by TypeId, “An item that borrows the input can be emitted only as one that borrows nothing”). Probe: with yaml’s Event clones dropped as above, the comptime program for limits: Limits = default_limits() fails in rustc (seen with RUSTC set to a logging wrapper) with “lifetime may not live long enough … 'a defined here, returning this value requires that 'a must outlive 'static” at both grammar! { grammar Yaml and grammar YamlOverBytes, from the actions for ev in all.drain() { emit ev } of FirstEvents/NextEvents; the user only sees NK1127 “this compiler’s fault”. Should: compile, by D7. Needs an engine change (a non-TypeId or lifetime-erased sink for in-memory input) or a D7 change; no decision yet

Cause not marked

The Found does not say how sure its cause is (docs/issues.md I7): a cause it names is a hypothesis, unknown until checked. The second item checks it (nikaia run scripts/where.nika, a probe, callgrind) before anything is built on it; a cause shown false is answered wrong I7: ... in the report.

To build

Done when

The probe or check gives the expected output the first item names from its ADR or Part I-III section, and a tests/compiler/tests/ test (or the named check) that failed on the unfixed tree is green; or the probe already passes on develop and a test pins it, naming the commit that fixed it; or the issue is closed as by design, naming the function or ADR.


Generated by Claude Code

Comments

keywan-ghadami, 2026-11-23

Supervisor, 2026-11-23: no new decision is needed. ADR-grammar-typed-read-v1 D7, as rewritten on #2426, already says an item views its input and an item may be emitted. winnow-grammar’s emit sink requiring 'static (its ADR 28, emitted_type) contradicts that, so this is an engine defect, fixed in winnow-grammar; D7 is not amended.

The fix: the emit sink holds the entry’s item type with the input’s lifetime for the length of one walk. It is opened and closed by the walk around the input’s loan, so a held item never outlives the input. The supervisor builds it in winnow-grammar (wip) and then bumps the pin.

The two emitter defects this run found are their own issues, for the yaml lane:


Generated by Claude Code

keywan-ghadami, 2026-11-23

Decision paper (supervisor, 2026-11-23)

My comment above called this an engine defect with a plain fix. Reading the engine shows it is not plain: it needs a decision. I have set it back to decision.

Origin. “yaml: events and nodes refer to their scalars in the input” (#2417) is part 2 of the owner’s yaml goal. Its run found that yaml’s Event cannot view the input because the yaml grammar hands its events over with emit (FirstEvents/NextEvents). winnow-grammar’s emit sink (EmitSink, its ADR 28) holds values type-erased and told apart by TypeId, so an emitted value must be 'static. ADR-grammar-typed-read-v1 D7 says both “an item views the input it is parsed from” and “an item may be emitted”. Today they cannot hold together.

Why it is not a one-line fix. An emitted value waits in the sink until the attempt that emitted it is taken. The engine has three paths:

Options

1. Do nothing. yaml’s Event stays owned, and part 2 of the goal is not built for events.

2. Amend D7: an emitted item owns its text.

3. Engine: the in-memory paths hold borrowed emits; an entry whose emitted item views its input has no Rust reader.

4. Engine: convert at emit time on the reader path. The sink is opened with a conversion to an owned form (the twin of “The owned form of a @stream item for the Rust side”, #2735). On the reader path an emit stores the converted value; on the in-memory path it stores the view.

Other engines. quick-xml keeps borrowed events for a slice reader and owned ones for a stream reader: the same split as options 3 and 4. serde’s borrowed deserialization is offered only from a slice, for the same reason.

Recommendation: 3, with 4 as a later extension.


Generated by Claude Code

keywan-ghadami, 2026-11-24

I think the decision should focus on two viable designs:

  1. Explicit copy-on-escape. The compiler tracks whether an emitted event (or its text view) outlives the input buffer it refers to. If it does, the programmer must explicitly clone the event/text at the escape point. The compiler diagnoses a missing clone; it never inserts a copy implicitly. This makes ownership and memory costs explicit and does not retain input buffers. The tradeoff is an intentional copy wherever data must outlive the input.

  2. A bounded pool of input buffers with backpressure. Events may keep referring to their originating buffer. The reader uses a fixed number of buffers; when all are still in use, it stops reading and waits until an event releases a buffer. This avoids copying on that path, but requires lifetime tracking, release coordination, and a defined wait/backpressure policy. A slow downstream consumer can stall input, while memory remains bounded by the pool.

The key distinction: retaining buffers alone is incomplete. Without a bound and a wait policy, buffers can accumulate if the reader outpaces the consumer. Unbounded tethering is therefore not a third viable option. The decision is explicit copying on escape versus zero-copy buffer retention with bounded capacity and backpressure.


AI model: GPT-6 (OpenAI), via ChatGPT — Free plan; 2026-11-24.

keywan-ghadami, 2026-11-27

Decision paper, revised (supervisor, 2026-11-27)

This replaces the recommendation of the paper above (option 3). The owner’s requirement (2026-11-27): large files are read as a stream (the Kubernetes case: stdin, pipes, sockets), events view the input buffers, nothing is copied, and nothing fails at run time. Option 3 gives views only to input held in memory, and “The owned form of a @stream item for the Rust side” (#2735) copies on the reader path; both miss it.

The design

A stream reads into a pool of fixed buffers. An event views the buffer it was parsed from; a buffer is reused when no view of it is left. The comment above (bounded buffer pool with backpressure) is the direction; two refinements:

  1. Bounded by construction, or refused. Every place that holds a stream view is counted while building:

    where the view lives compiler
    used in the loop body and dropped (looked at, compared, cloned, ignored) allowed; a plain borrow, no count
    a fixed number of names across turns (prev = ev) allowed; counts k
    a bounded channel to another task allowed; counts its capacity plus what the receiver holds
    anything that grows across turns (list.push(ev), a map of views) refused; the error names .clone() and the two patterns below

    The pool’s size is the sum, so the pool never runs out. A receiver that holds views follows the same rule, and a channel whose receiver is the sending task is refused (it is a list in disguise).

  2. Backpressure is the channel’s. A channel is only bounded and send pauses when it is full (Part II 12.5, ADR-149). With the pool sized from the count, that pause is the only wait; the parser waits only for I/O.

The patterns the refusal names: process each event in the body and keep only what is needed, cloned (e.g. kind and metadata.name instead of the whole document); or hand events to a task over a bounded channel.

A text larger than a buffer is handed over in parts (ScalarStart, ScalarPart, ScalarEnd), so no event is larger than a buffer and the pool stays the bound.

Counting

Where a buffer must be counted, it is counted the way Perceus does it (Reinking, Xie, de Moura, Leijen, PLDI 2021; Koka, and Lean 4 alike). ADR-views-v4 lists Perceus and does not take it as the model (Koka counts everything shared; Part I 6.5 asks for no count the program did not ask for). It is taken here as the technique for the few counts tethering needs:

A stream adds what Perceus has no general use of, order. A channel is FIFO, and the rule above makes a receiver use each event in its body or hold at most k. Buffers then come free in the order they were read, and one cursor (“used up to here”, a ring as in the LMAX Disruptor) replaces the counts. The compiler takes the cheapest step that holds, and the ledger names it per loop:

  1. no count: the event is used in the body;
  2. one cursor: one receiver, in order (fixed names across turns, a channel with one receiver);
  3. a count per buffer, plain: out of order within one thread;
  4. a count per buffer, atomic, fused: several receivers in different tasks.

How it fits the language

What changes

Options on the two open points

  1. Parts and the yaml-test-suite vocabulary. The suite’s =VAL is one whole value. Recommendation: the stream yields parts; the suite’s line form joins them in the test harness only. A part is emitted only once the parse cannot go back; an implicit key is at most 1024 characters (YAML 1.2, 7.4.2), which bounds that point. To be checked against the grammar before building.
  2. A token cut by the end of a buffer (---, a key). (a) Move the unfinished token to the start of the next buffer: one internal move of at most 1024 bytes, no event, no user data. (b) Let tokens span buffers: no move, every rule pays for the check. Recommendation: (a).

Proposed next steps

An ADR stream buffers (A–D, the table, the counting steps, the refusal’s code) as a decision for the owner; on acceptance, build issues for the language rule, the counting steps in the lowering, the Nikaia reader entry, winnow-grammar’s sink, yaml’s parts, and io::lines(). “The item rule written with '_” (#2835) and “Two ref parameters and a result that may hold views” (#2836) are unaffected and go on.


Generated by Claude Code

keywan-ghadami, 2026-11-28

Owner decision, 2026-11-28: the revised decision paper above is taken, as the direction and with its recommendations:

The work is queued.

Next (supervisor): the ADR text, then one build issue per part, each ready:

  1. A stream buffer in Part I 6.6.
  2. io::lines() (ADR-025 D4).
  3. A Nikaia entry over a reader and views on both sides (ADR-grammar-typed-read-v1 D7, Part II 10; #2735 superseded).
  4. D7’s walk table.
  5. winnow-grammar’s sink holding counted buffer handles, which settles this issue’s defect.
  6. yaml’s parts.

The records that change get their next versions by ADR-decision-records-v2. Until the build issues are filed this issue is wip. The emitter defects “The item rule written with '_” (#2835) and “Two ref parameters and a result that may hold views” (#2836) go on as they are.


Generated by Claude Code

keywan-ghadami, 2026-11-28

The decision is written down and on develop (68c0e87e):

The build, in order:

  1. io::lines() reads into a fixed pool and yields views; a line kept in a growing place is NK2308 (#2851)
  2. winnow-grammar’s reader reads into counted buffers and its emit sink holds counted handles (#2852). This one fixes this issue.
  3. A byte stream in std (io::Stream), and a @stream entry over it (#2853). Comes after #2851 and #2852.
  4. A stream’s views handed to a task over a bounded channel, counted with the cheapest of four steps (#2854). Comes after #2851.
  5. yaml events over a stream: a long scalar in parts, memory bounded by the pool (#2855). Comes after #2852 and #2853.

Generated by Claude Code

Back to Open Issues