@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).
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
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.
develop first: the run that wrote it may have used a binary older than the tip, so it may be fixed already (#1822: #1788 was filed from a probe a fix under #1754 had already made pass). A probe that passes there names the commit that fixed it (git log -400 --grep by topic, since a fix often lands under another issue’s number), and the run builds only a tests/compiler/tests/ test that pins it (docs/issues.md I13).std defect with a probe (nikaia run scripts/probe.nika -- SNIPPET; a lowered file that differs, with an uncached lower-std of a git archive HEAD copy, NIKAIA_LOWER_CACHE_SALT fresh and --sysroot COPY/crates, its lowered/ diffed against the copy’s, never the shared working tree’s, #1793), a defect in docs/, guide/, website/ or scripts/ (a stale example, a wrong rule) with the command or check that shows it; a behaviour the code does on purpose (name the function or ADR) closes it as by design. The Found line may lack them (#1710): the commit message, or the report’s Body: when closed, gives the probe or check with its expected output, the ADR or Part I-III section that says so, and its actual output (docs/issues.md I5, I6), and the cause, file and function, traced or suspected (I7).std defect in the compiler, a stale page example or rule in its page or docs/ file, a script defect in the script. Add a test in tests/compiler/tests/ unless a check that already runs turns red on it (name it: pre-push’s page-examples for guide/ and website/, check-adr-refs.nika for a docs citation).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
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:
'_ (#2835);ref parameters and a result that may hold views (#2836).Generated by Claude Code
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:
parse_<rule>_slice_each, the yaml benchmark and the Nikaia for walk): the input never moves. A held value that views it is sound.G::<rule>_reader): the input arrives in windows, and a read during an attempt can move the window. A held value that views the window would dangle. This path is why ADR 28 required 'static._state.emit(v)). The bound cannot differ by path without splitting that code.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.
Event is both emitted and the item of events(docs). A field is tethered or owned per type (ADR-views-v4 D5), so the in-memory path would copy too. The benchmark does not move. This is option 1 in effect.3. Engine: the in-memory paths hold borrowed emits; an entry whose emitted item views its input has no Rust reader.
'static form, as today. push accepts the item with its real lifetimes and erases them; that is unsafe, and the invariant is documented and checked under Miri._reader, _reader_each) for a #[stream] rule whose item type has lifetime parameters. On the Nikaia side, NK1345 extends to “a reader of an entry whose items view and are emitted”.events takes a value).unsafe block in winnow-grammar. D7’s Rust reader form is not offered for such an entry until option 4 exists. That narrows D7 for one case and is written into it.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.
unsafe and of the reader for a case nobody uses yet.'_ (#2835), and two ref parameters with a result that may hold views (#2836).Generated by Claude Code
I think the decision should focus on two viable designs:
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.
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.
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.
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:
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).
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.
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:
.clone() in the help. The new refusal is the same rule for a new kind of buffer.send already exist.throws (NK2701, ADR-025): an input error is a result, not a system failure. Parse errors and yaml’s Limits (ADR-yaml-1 D8) stay input errors.--tethers already reports where buffers live.io::lines() yields owned text “because a stream’s bytes are gone once consumed”. The pool contradicts that premise; io::lines() follows the same rule, or two rules stand for one thing.String or Bytes today, and the reader is the Rust side’s with an owned form. A Nikaia entry over a reader (Yaml::events(stdin)) is added, items are views on both sides, and D7’s rejection of “a reference-counted slice of the input” is lifted for the stream buffer. #2735 is superseded.'static without copying its text and without unsafe, on both paths. That settles this issue’s defect.=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.---, 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).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
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:
io::lines() (ADR-025 D4).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
The decision is written down and on develop (68c0e87e):
NK2308. The pool’s size is the counted sum, backpressure comes from a bounded channel, and nothing fails at run time.@stream entry over a byte stream, and the emit sink holds counted handles. v1 is moved to history.io::lines() yields views of stream buffers.NK2308 row in each.The build, in order:
Generated by Claude Code