1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//! CST → HIR lowering for the native `.brink` surface's **declaration and
//! module-skeleton layer** (`docs/b0-sequencing.md` §B0.6, issue #1175).
//!
//! Scope, precisely: `flow`/`fn` declaration heads (→ `Knot`/`Stitch`,
//! params), `var`/`const`/`flags`/`struct`/`extern` (→ their HIR decl
//! nodes), `use`/`import` (→ `Import`). **Bodies are out of scope** — every
//! `Knot`/`Stitch`'s `body` is the empty stub `Block::default()`; prose
//! lowering is B0.7, code-statement lowering is B0.8. The `SymbolManifest`
//! is never hand-built here — [`crate::symbols::project_manifest`] (B0.4)
//! derives it from the `HirFile` this module produces, exactly like the ink
//! frontend's own [`crate::hir::lower::lower`] does. That is the whole
//! point of this slice: a second, independent frontend producing HIR that
//! satisfies the same admission contract (`docs/hir-admission-contract.md`)
//! the ink frontend does, without hand-building a manifest or reaching for
//! ink's `AstPtr`-based provenance.
//!
//! # Judgment calls (flagged for the coordinator / issue #1106)
//!
//! 1. **Crate home + pipeline plug-in shape.** This lowering lives inside
//! `brink-ir` (a new `brink-syntax-native` dependency, sibling module to
//! `hir::lower`) rather than a new crate or inside
//! `brink-syntax-native` itself. Rationale: `brink-ir`'s own doc
//! ("HIR ... and lowering") already frames it as the home for
//! *whichever* frontend produces the CST, and it's the crate that owns
//! [`crate::symbols::project_manifest`] and [`crate::provenance`] this
//! lowering depends on — putting the lowering here avoids a dependency
//! cycle (`brink-syntax-native` would otherwise need to depend on
//! `brink-ir`, which owns nothing native-specific). **Not wired** into
//! `brink-db`'s `parse_query`/`lowered_query` (F-B's admission point) —
//! B0.6's gate is "native HIR passes admission," tested by calling this
//! module directly (mirrors `b03_admission_corpus.rs`'s own direct
//! `brink_ir::hir::lower` + `validate_admission` calls, bypassing
//! `brink-db` entirely). Dialect dispatch at the `brink-db` seam,
//! `.brink` file-extension registration, and project-layer discovery
//! are explicitly B0.9/B0.10 per `docs/b0-sequencing.md` — wiring them
//! here would be scope creep this slice didn't need to take on to meet
//! its own exit criterion.
//! 2. **The `NativeProvenanceResolver` shape** ([`provenance`]) is a
//! byte-for-byte mirror of [`crate::hir::InkProvenanceResolver`] against
//! `brink-syntax-native`'s own `SyntaxKind`/`SyntaxNode` types — two
//! independent implementations of the same trait, no shared resolution
//! code between frontends (per Q1(b)'s "the ink frontend keeps `AstPtr`
//! *behind* its resolver; native supplies its own"). The small
//! duplication (the walk-up-to-matching-range algorithm) was judged
//! cheaper and more honest than factoring out a generic version that
//! would need to abstract over two unrelated rowan `Language`s for a
//! ~25-line function.
//! 3. **Body-deferral strategy**: every container's `body` is
//! `Block::default()` unconditionally — not a per-content-line
//! diagnostic. A `flow`/`fn`'s body can contain prose, choices, diverts,
//! conditionals, tags, annotations — all B0.7/B0.8 territory — and
//! diagnosing every such line individually would be noise, not signal
//! (the whole subsystem is deferred, not any one construct within it).
//! This is distinct from judgment call #4 below: constructs *at the
//! declaration layer* B0.6 owns but can't yet place get a diagnostic
//! each; content *inside* a deferred body does not.
//! 4. **Deferred-construct diagnostics** (all E129 unless noted): a `fn`
//! nested below top level (no HIR container carries `is_function` below
//! `Knot`); a `flow` nested three levels deep (E130, the Q4(b) fence);
//! a `module { … }` block (no HIR "module container" node exists —
//! `HirFile.module` is a single file-identity fact, not a recursive
//! container; contents are flattened into the enclosing scope and the
//! diagnostic says so); any other body-line construct reaching top-level
//! declaration position (content/tags/choices/diverts/conditionals with
//! no enclosing `flow`/`fn` — a native file has no top-level body ground
//! the way ink's root weave does; the only top-level "entry" spelling is
//! the `flow main()` naming convention (`entry_root_content`, ruled
//! 2026-07-21), not literal content or a divert at file-root position —
//! so such content has nowhere to go and must not vanish silently);
//! `struct`/`extern`/
//! `use`/`import` declared below the flattened top-level scope (nested
//! inside a `flow`/`fn` body) — ink restricts these four kinds to
//! top-level-only (D6), and the native grammar's shared `item()`
//! dispatch means the parser *can* produce them at body position even
//! though nothing downstream can use them there yet. Also the prose
//! block elements the grammar gained in issue #1715 — scene headings
//! and their header-scoped stitch bodies, cues, compact cues,
//! parentheticals, and a `flow` header's trailing per-flow `#tag`s
//! (`container::report_header_tags`). Their attachment is issue
//! #1717's slice and the per-flow tag API is #474's, so every one of
//! these shapes parses cleanly and is reported here rather than being
//! read as ordinary prose or dropped. **One exception since issue
//! #1838**: a scene heading claimed by a natural-notation
//! `@[element(claims = "…")]` handler lowers to one call on it
//! ([`element`]); an *unclaimed* heading still reports.
//! 5. **Decl-level directive/annotation channel — the annotation half is
//! now wired.** `@[effects(…)]` above a `flow`/`fn` populates
//! `effects_assertion` on the resulting `Knot`/`Stitch` (issue #1563,
//! [`annotation`]), and `@[allow(Exxx, …)]` above any declaration
//! populates `HirFile::allow_scopes` (issue #1161) — the source-level
//! diagnostic-suppression channel. [`annotation`] also owns the
//! channel's erasure/diagnosis
//! chokepoint so an unknown (`E111`) or misplaced (`E112`) annotation is
//! loud instead of blanket-`E129`. `visibility` is now wired too (issue
//! #1582, RULED 2026-08-03): a leading `pub` keyword (`decl::eat_pub`,
//! `ast::FlowDecl::is_pub` and its six siblings) sets
//! `Some(VisibilityMark::Public)`; absent, `None` — the ratified
//! 2026-07-23 Private default, unchanged. `is_local` remains `false` on
//! every decl node: no native syntax exists for it yet. `///`/`//!` docs
//! ARE
//! wired (B0.6b, `docs/decision-log.md` 2026-07-20, `doc_comment`) —
//! they were judgment call 5 in the B0.6 report but shipped as their
//! own ruled slice rather than staying deferred alongside the rest. The
//! file-level `@[was("old::path")]` *module* rename record is now wired
//! too (issue #1286, [`module`]) — see judgment call #7.
//! 6. **`import name;`'s semantics** (B0.5's own Finding #3 explicitly left
//! this to B0.6): lowered as the *qualified* form of ink's `Import`
//! (`module` = the whole `::`-joined path, `items` empty, `bare: false`
//! — "brings only the module name into scope"), matching a
//! single-segment `use module;`. A multi-segment `use path::item;`
//! instead names an *item* of `path` (issue #1581), so its leaf lands in
//! `items`. `use`'s one shape with no `Import` equivalent (recursive
//! nested groups — plus aliasing a single-segment *module* name) gets
//! E129 rather than a lossy guess — see [`import`]'s module doc.
//! 7. **Native module *identity* is filesystem-derived, not stamped here.**
//! `HirFile.module.name` stays empty for every native file: unlike ink's
//! `#@module(name)` tag (a per-file directive), native module identity is
//! charter-ruled **filesystem-derived** (NF-3: "path on disk = path in
//! language") — a project-layer fact `module_map_query` owns, not
//! something a single-file lowering can determine. What a native file
//! *can* author is the module's **rename record**: a file-level
//! `@[was("old::path")]` now populates `HirFile.module.was` (issue #1286,
//! [`module`]) so a moved file's old `DefinitionId`s still resolve. Such a
//! file therefore carries `Some(ModuleDecl { name: "", was: Some(…) })` —
//! the name is deliberately empty (see [`module`]'s doc); a file with no
//! `@[was]` still carries `None`. A nested `module name { … }` *block* is
//! a different concept (charter §13.2's declared sub-modules) — see
//! judgment call #4.
use SyntaxKind as N;
use ;
use ;
use crateFileId;
use crate;
use crate::;
// Issue #2351: `hir::classify`'s node-aware entry points need to select the
// SAME sub-node `try_claim` matches against, for the SAME reason `try_claim`
// itself does — a copy of `candidate`'s match arms would re-diverge, which
// is exactly the bug #2351 exists to close. Crate-internal only
// (`pub(crate)`): no external consumer of `brink-ir` selects a claim
// candidate itself.
pub use candidate;
// Issue #2351 review finding: `classify_node_compiled` needs the same
// "does this node carry a trailing tag extension" answer `try_claim`'s own
// attach-mode decline (`element.rs`'s `is_attach && !extra_tags.is_empty()`
// gate) is built on, without re-deriving `element_extras`'s per-kind child
// selection (and without triggering its diagnostic side effects — see
// `has_trailing_tags`'s own doc). `pub(crate)` for the same reason
// `candidate` is: crate-internal only.
pub use has_trailing_tags;
/// Lower a complete native source file to HIR.
///
/// Produces the same `(HirFile, SymbolManifest, Vec<Diagnostic>)` triple
/// contract §1.1 requires of *any* frontend — the manifest is
/// [`project_manifest`]'s pure projection of the just-built `HirFile`
/// (B0.4), exactly as [`crate::hir::lower::lower`] does for ink. `Knot`/
/// `Stitch` bodies are always the empty stub; see the module doc's judgment
/// call #3. `root_content` is the one exception: see [`entry_root_content`]
/// for the `flow main()` entry convention.
///
/// A thin wrapper over [`lower_with_conventions`] with no injected
/// handler set — this signature stays exactly what it has always been so
/// none of this crate's ~40 existing call sites need to change; issue
/// #2289's injection point is additive, opted into only by calling
/// [`lower_with_conventions`] directly.
/// Lower a complete native source file to HIR, optionally merging in the
/// project's configured conventions module's declared `@[convention]`
/// handlers (issue #2289, correcting the file-local claiming defect the
/// 2026-08-05 ruling names — see [`element`]'s own module doc, "Cross-file
/// claiming reach").
///
/// `external: None` is byte-identical to [`lower`] — this function IS
/// `lower`'s implementation, not a parallel path that could drift from
/// it. `external` is every `ClaimHandlerDecl` the project's conventions
/// module declares, resolved and ordered by the caller
/// (`brink_db::queries::analysis::external_claim_handlers_query`) — this
/// crate has no project database of its own to resolve "which file is the
/// conventions module" itself.
/// The native story-entry convention (maintainer-ruled 2026-07-21,
/// `docs/decision-log.md`): **a top-level `flow main()` is a native
/// story's default standalone entry point.** Mirrors ink's own "root
/// content is the entry" model (the ink frontend's
/// [`crate::hir::lower::lower`] populates `root_content` from the file's
/// literal top-level weave body) using the same `Divert`/`Block` HIR — no
/// new entry mechanism, no new HIR node. When a top-level, non-function,
/// zero-parameter `flow` named `main` exists, `root_content` becomes a
/// single synthesized `Divert` to it. Any other top-level `flow`/`fn`
/// remains host-entry-only (effects-spec §10 "play from here") — a project
/// with no `main` has an empty `root_content`, which is not an error, only
/// "no standalone entry point".
///
/// The synthesized `Divert` carries `ptr: None` — it is not backed by any
/// real `DIVERT_STMT` source node (nothing is written at file-root
/// position; the convention is implied by the flow's name), so there is no
/// honest provenance to attach, unlike every other `Divert` this frontend
/// produces. The target `Path`'s `Name` reuses `main`'s own declared name
/// token — real source provenance, not a fabricated range.
///
/// A `main` with parameters is deliberately **not** matched: a bare entry
/// divert cannot supply arguments, and silently dropping required params
/// would be a silent data drop (a standing rule, `CLAUDE.md` "Flag silent
/// data drops"). Such a file simply has no synthesized entry — `main` is
/// still an ordinary flow, reachable as a host entry point.
/// Walk one "flattened top-level scope" — `SOURCE_FILE`'s direct children,
/// or (recursively) a `MODULE_DECL`'s body, since native has no HIR
/// "module container" node yet (judgment call #4) and the flat-hoist
/// posture (D6) extends to declared containers, not just globals, once a
/// module block is entered. `var`/`const`/`flags` are handled by the
/// separate whole-tree pass in [`lower`] and skipped here.
/// Unifies `ast::FlowDecl`/`ast::FnDecl` for [`container`]'s shared
/// top-level lowering — the two node kinds have identical accessor shapes
/// (`name_token`/`param_list`/`body`) but no common trait in
/// `brink-syntax-native` (mirroring its own `ast_node!` macro's
/// one-struct-per-kind pattern, not worth a trait for two variants).
pub