pythonrs 0.1.15

Python as a fusevm frontend: lexer, parser, and compiler to fusevm::Chunk on a PyHost heap — the first compiled standalone Python runtime
Documentation
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
//! The declaration checks of CPython's symbol table (`Python/symtable.c`).
//!
//! CPython builds a symbol table for the whole module before it generates any
//! code, visiting statements in source order and OR-ing flags into each name as
//! it goes. A `global` or `nonlocal` statement then looks at the flags its names
//! ALREADY carry in the current scope (`symtable_visit_stmt`, `Global_kind` /
//! `Nonlocal_kind`): a parameter, a use, an annotation or an assignment seen
//! earlier makes the declaration a `SyntaxError`, pointed at the declaring
//! statement. Once every block is built, the analysis pass (`analyze_name`)
//! rejects a name declared both `global` and `nonlocal` in one scope.
//!
//! What counts as "earlier" follows the table's own scoping: a nested `def`,
//! `class`, `lambda` or comprehension body is a block of its own, so a name
//! used only there flags nothing here. What is evaluated in the enclosing scope
//! still does — decorators, default values, base classes, the first iterable of
//! a comprehension, and the target of a `:=` inside a comprehension (PEP 572
//! binds it in the enclosing function). Annotations are not: under PEP 649
//! they are compiled into an annotation scope of their own. An `import` sets
//! `DEF_IMPORT`, which the declaration check does not test, so `import x`
//! followed by `global x` is accepted, as it is by CPython.

use crate::ast::*;
use crate::parser::{at_pos, SYNTAX_FIELD};
use indexmap::IndexMap;

/// `DEF_PARAM`: bound as a parameter of the current function.
const PARAM: u8 = 1;
/// `USE`: read in the current scope.
const USE: u8 = 2;
/// `DEF_ANNOT`: the target of an annotated assignment.
const ANNOT: u8 = 4;
/// `DEF_LOCAL`: bound in the current scope (assignment, `for`, `with … as`,
/// `except … as`, `del`, `def`, `class`, a pattern capture, `:=`).
const LOCAL: u8 = 8;
/// `DEF_GLOBAL`: declared `global`.
const GLOBAL: u8 = 16;
/// `DEF_NONLOCAL`: declared `nonlocal`.
const NONLOCAL: u8 = 32;

/// One block of the table: the flags of each name in first-seen order, and
/// where each name was first declared `global` or `nonlocal` (the location
/// the analysis pass reports a conflict at).
#[derive(Default)]
struct Block {
    flags: IndexMap<String, u8>,
    directives: IndexMap<String, (u32, u32, u32, u32)>,
}

impl Block {
    fn add(&mut self, name: &str, flag: u8) {
        *self.flags.entry(name.to_string()).or_insert(0) |= flag;
    }
}

/// Run the checks over a module. The first declaration error in source order
/// wins; failing that, the first `global`/`nonlocal` conflict in the order the
/// analysis pass visits blocks (a block before the blocks nested in it).
/// Answers the `__future__` features the module turns on.
pub fn check(stmts: &[Stmt]) -> Result<FutureFeatures, String> {
    // `_PyFuture_FromAST` runs before the symbol table is built, so its
    // errors win over every declaration error.
    let features = future_parse(stmts)?;
    let mut table = Table {
        future: features.location,
        ..Table::default()
    };
    let order = table.open();
    let mut module = Block::default();
    table.body(stmts, &mut module)?;
    table.close(module, order);
    table.analysis.sort_by_key(|(order, _)| *order);
    match table.analysis.into_iter().next() {
        Some((_, e)) => Err(e),
        None => Ok(features),
    }
}

/// What the `from __future__` imports heading a module turn on.
#[derive(Default, Clone, Copy)]
pub struct FutureFeatures {
    /// `ff_location`: the extent of the last of them.
    location: Option<(u32, u32, u32, u32)>,
    /// PEP 563 `annotations`: annotations are kept as strings.
    pub annotations: bool,
}

#[derive(Default)]
struct Table {
    /// How many blocks have been opened, which numbers them in the order the
    /// analysis pass would reach them.
    opened: usize,
    /// Analysis-pass errors, tagged with the number of the block they are in.
    analysis: Vec<(usize, String)>,
    /// `ff_location`: the extent of the last `from __future__` import at the
    /// head of the module, `None` when there is none.
    future: Option<(u32, u32, u32, u32)>,
}

/// The features `future_check_features` (`Python/future.c`) accepts.
const FUTURE_FEATURES: &[&str] = &[
    "nested_scopes",
    "generators",
    "division",
    "absolute_import",
    "with_statement",
    "print_function",
    "unicode_literals",
    "barry_as_FLUFL",
    "generator_stop",
    "annotations",
];

/// `future_parse`: check the `from __future__` imports at the head of the
/// module — after an optional docstring, before any other statement — and
/// return the extent of the last one. A name `__future__` does not define
/// is `future feature X is not defined` (`braces` is `not a chance`), at
/// that name.
fn future_parse(stmts: &[Stmt]) -> Result<FutureFeatures, String> {
    let skip = usize::from(crate::compiler::docstring(stmts).is_some());
    let mut features = FutureFeatures::default();
    for s in &stmts[skip..] {
        let StmtKind::ImportFrom {
            module: Some(module),
            names,
            level: 0,
        } = &s.kind
        else {
            break;
        };
        if module != "__future__" {
            break;
        }
        for alias in names {
            if alias.name == "annotations" {
                features.annotations = true;
            }
            if FUTURE_FEATURES.contains(&alias.name.as_str()) {
                continue;
            }
            let msg = if alias.name == "braces" {
                "SyntaxError: not a chance".to_string()
            } else {
                // `UNDEFINED_FUTURE_FEATURE` formats the name with `%.100s`:
                // at most 100 UTF-8 bytes, a split character replaced.
                let bytes = alias.name.as_bytes();
                let name = String::from_utf8_lossy(&bytes[..bytes.len().min(100)]);
                format!("SyntaxError: future feature {name} is not defined")
            };
            return Err(match alias.span {
                Some(span) => symtable_error(&msg, span),
                None => msg,
            });
        }
        features.location = s.span;
    }
    Ok(features)
}

impl Table {
    /// Number a new block. Blocks are numbered as they are opened, so a block
    /// precedes the blocks nested in it, which is the order the analysis pass
    /// visits them in.
    fn open(&mut self) -> usize {
        self.opened += 1;
        self.opened - 1
    }

    /// Finish block number `order`: record its first `global`/`nonlocal`
    /// conflict.
    fn close(&mut self, block: Block, order: usize) {
        let conflict = block
            .flags
            .iter()
            .find(|(_, f)| **f & GLOBAL != 0 && **f & NONLOCAL != 0);
        if let Some((name, _)) = conflict {
            let span = block.directives[name];
            let msg = format!("SyntaxError: name '{name}' is nonlocal and global");
            self.analysis.push((order, symtable_error(&msg, span)));
        }
    }

    /// Visit a nested block's body with a fresh table, `params` pre-flagged.
    fn nested(&mut self, params: &[&str], body: &[Stmt]) -> Result<(), String> {
        let order = self.open();
        let mut block = Block::default();
        for p in params {
            block.add(p, PARAM);
        }
        self.body(body, &mut block)?;
        self.close(block, order);
        Ok(())
    }

    fn body(&mut self, stmts: &[Stmt], b: &mut Block) -> Result<(), String> {
        for s in stmts {
            self.stmt(s, b)?;
        }
        Ok(())
    }

    fn stmt(&mut self, s: &Stmt, b: &mut Block) -> Result<(), String> {
        crate::stack::enter_compile()?;
        match &s.kind {
            StmtKind::Expr(e) => self.expr(e, b)?,
            // The value is a scope of its own, evaluated lazily.
            StmtKind::TypeAlias { name, .. } => b.add(name, LOCAL),
            StmtKind::Assign { targets, value } => {
                for t in targets {
                    self.store(t, b)?;
                }
                self.expr(value, b)?;
            }
            StmtKind::AugAssign { target, value, .. } => {
                self.store(target, b)?;
                self.expr(value, b)?;
            }
            StmtKind::AnnAssign { target, value, .. } => {
                match target.unspanned() {
                    Expr::Name(n) => {
                        let flag = if value.is_some() {
                            ANNOT | LOCAL
                        } else {
                            ANNOT
                        };
                        b.add(n, flag);
                    }
                    _ => self.store(target, b)?,
                }
                if let Some(v) = value {
                    self.expr(v, b)?;
                }
            }
            StmtKind::If { test, body, orelse } | StmtKind::While { test, body, orelse } => {
                self.expr(test, b)?;
                self.body(body, b)?;
                self.body(orelse, b)?;
            }
            StmtKind::For {
                target,
                iter,
                body,
                orelse,
                ..
            } => {
                self.store(target, b)?;
                self.expr(iter, b)?;
                self.body(body, b)?;
                self.body(orelse, b)?;
            }
            StmtKind::With { items, body, .. } => {
                for it in items {
                    self.expr(&it.context, b)?;
                    if let Some(v) = &it.vars {
                        self.store(v, b)?;
                    }
                }
                self.body(body, b)?;
            }
            StmtKind::FuncDef {
                name,
                params,
                body,
                decorators,
                ..
            } => {
                for d in decorators {
                    self.expr(d, b)?;
                }
                self.defaults(params, b)?;
                b.add(name, LOCAL);
                let names = param_names(params);
                self.nested(&names, body)?;
            }
            StmtKind::ClassDef {
                name,
                bases,
                keywords,
                body,
                decorators,
            } => {
                for d in decorators {
                    self.expr(d, b)?;
                }
                for e in bases {
                    self.expr(e, b)?;
                }
                for k in keywords {
                    self.expr(&k.value, b)?;
                }
                b.add(name, LOCAL);
                self.nested(&[], body)?;
            }
            StmtKind::Return(Some(e)) => self.expr(e, b)?,
            StmtKind::Delete(targets) => {
                for t in targets {
                    self.store(t, b)?;
                }
            }
            StmtKind::Global(names) => self.declare(names, GLOBAL, s.span, b)?,
            StmtKind::Nonlocal(names) => self.declare(names, NONLOCAL, s.span, b)?,
            StmtKind::Raise { exc, cause } => {
                for e in [exc, cause].into_iter().flatten() {
                    self.expr(e, b)?;
                }
            }
            StmtKind::Try {
                body,
                handlers,
                orelse,
                finalbody,
            } => {
                self.body(body, b)?;
                for h in handlers {
                    if let Some(t) = &h.typ {
                        self.expr(t, b)?;
                    }
                    if let Some(n) = &h.name {
                        b.add(n, LOCAL);
                    }
                    self.body(&h.body, b)?;
                }
                self.body(orelse, b)?;
                self.body(finalbody, b)?;
            }
            StmtKind::Assert { test, msg } => {
                self.expr(test, b)?;
                if let Some(m) = msg {
                    self.expr(m, b)?;
                }
            }
            StmtKind::Match { subject, cases } => {
                self.expr(subject, b)?;
                for c in cases {
                    self.pattern(&c.pattern, b)?;
                    if let Some(g) = &c.guard {
                        self.expr(g, b)?;
                    }
                    self.body(&c.body, b)?;
                }
            }
            StmtKind::ImportFrom { module, level, .. } => {
                self.check_import_from(module.as_deref(), *level, s.span)?
            }
            StmtKind::Return(None)
            | StmtKind::Pass
            | StmtKind::Break
            | StmtKind::Continue
            | StmtKind::Import(_) => {}
        }
        Ok(())
    }

    /// `check_import_from`: a `from __future__` import anywhere after the ones
    /// heading the module — in any block — is refused at the statement.
    fn check_import_from(
        &self,
        module: Option<&str>,
        level: usize,
        span: Option<(u32, u32, u32, u32)>,
    ) -> Result<(), String> {
        let (Some("__future__"), 0, Some(span)) = (module, level, span) else {
            return Ok(());
        };
        // An absent `ff_location` is all -1s, which every statement follows.
        let late = match self.future {
            None => true,
            Some((line, _, end_line, end_col)) => {
                span.0 > line || (span.0 == end_line && span.1 > end_col)
            }
        };
        if late {
            return Err(symtable_error(
                "SyntaxError: from __future__ imports must occur at the beginning of the file",
                span,
            ));
        }
        Ok(())
    }

    /// `global` / `nonlocal`: each name must not already be a parameter, used,
    /// annotated or bound in this block.
    fn declare(
        &mut self,
        names: &[String],
        kind: u8,
        span: Option<(u32, u32, u32, u32)>,
        b: &mut Block,
    ) -> Result<(), String> {
        let word = if kind == GLOBAL { "global" } else { "nonlocal" };
        for n in names {
            let cur = b.flags.get(n).copied().unwrap_or(0);
            let msg = if cur & PARAM != 0 {
                Some(format!("name '{n}' is parameter and {word}"))
            } else if cur & USE != 0 {
                Some(format!("name '{n}' is used prior to {word} declaration"))
            } else if cur & ANNOT != 0 {
                Some(format!("annotated name '{n}' can't be {word}"))
            } else if cur & LOCAL != 0 {
                Some(format!(
                    "name '{n}' is assigned to before {word} declaration"
                ))
            } else {
                None
            };
            if let (Some(msg), Some(span)) = (msg, span) {
                return Err(symtable_error(&format!("SyntaxError: {msg}"), span));
            }
            b.add(n, kind);
            if let Some(span) = span {
                b.directives.entry(n.clone()).or_insert(span);
            }
        }
        Ok(())
    }

    /// The default values of a `def` or `lambda`, evaluated where it stands.
    fn defaults(&mut self, params: &Params, b: &mut Block) -> Result<(), String> {
        for d in &params.defaults {
            self.expr(d, b)?;
        }
        for d in params.kwonly_defaults.iter().flatten() {
            self.expr(d, b)?;
        }
        Ok(())
    }

    /// An assignment or `del` target: a name is bound, and the object of an
    /// attribute or subscript target is read.
    fn store(&mut self, t: &Expr, b: &mut Block) -> Result<(), String> {
        crate::stack::enter_compile()?;
        match t.unspanned() {
            Expr::Name(n) => b.add(n, LOCAL),
            Expr::Tuple(xs) | Expr::List(xs) => {
                for x in xs {
                    self.store(x, b)?;
                }
            }
            Expr::Starred(x) => self.store(x, b)?,
            other => self.expr(other, b)?,
        }
        Ok(())
    }

    fn pattern(&mut self, p: &Pattern, b: &mut Block) -> Result<(), String> {
        crate::stack::enter_compile()?;
        match &p.kind {
            PatternKind::Wildcard | PatternKind::Star(None) => {}
            PatternKind::Capture(n) | PatternKind::Star(Some(n)) => b.add(n, LOCAL),
            PatternKind::Value(e) => self.expr(e, b)?,
            PatternKind::Or(alts) => {
                for a in alts {
                    self.pattern(a, b)?;
                }
            }
            PatternKind::As(inner, n) => {
                self.pattern(inner, b)?;
                b.add(n, LOCAL);
            }
            PatternKind::Sequence { elems, .. } => {
                for e in elems {
                    self.pattern(e, b)?;
                }
            }
            PatternKind::Mapping { keys, rest } => {
                for (k, v) in keys {
                    self.expr(k, b)?;
                    self.pattern(v, b)?;
                }
                if let Some(r) = rest {
                    b.add(r, LOCAL);
                }
            }
            PatternKind::Class { cls, pos, kw } => {
                self.expr(cls, b)?;
                for p in pos {
                    self.pattern(p, b)?;
                }
                for (_, p) in kw {
                    self.pattern(p, b)?;
                }
            }
        }
        Ok(())
    }

    /// An expression evaluated in this block.
    fn expr(&mut self, e: &Expr, b: &mut Block) -> Result<(), String> {
        crate::stack::enter_compile()?;
        match e.unspanned() {
            Expr::Name(n) => b.add(n, USE),
            Expr::NamedExpr(target, value) => {
                self.store(target, b)?;
                self.expr(value, b)?;
            }
            // The body is a block of its own, and one that can hold no
            // declaration; only the defaults are evaluated here.
            Expr::Lambda { params, .. } => self.defaults(params, b)?,
            Expr::ListComp(..) | Expr::SetComp(..) | Expr::GenExp(..) | Expr::DictComp(..) => {
                let (elts, comps) = comprehension_parts(e.unspanned());
                self.comprehension(&elts, comps, b)?;
                no_yield_in_comprehension(e.unspanned())?;
            }
            Expr::List(xs) | Expr::Tuple(xs) | Expr::Set(xs) | Expr::BoolOp(_, xs) => {
                for x in xs {
                    self.expr(x, b)?;
                }
            }
            Expr::Dict(pairs) => {
                for (k, v) in pairs {
                    if let Some(k) = k {
                        self.expr(k, b)?;
                    }
                    self.expr(v, b)?;
                }
            }
            Expr::Starred(x)
            | Expr::UnaryOp(_, x)
            | Expr::YieldFrom(x)
            | Expr::Await(x)
            | Expr::Attribute(x, _)
            | Expr::Yield(Some(x)) => self.expr(x, b)?,
            Expr::BinOp(_, l, r) | Expr::Subscript(l, r) => {
                self.expr(l, b)?;
                self.expr(r, b)?;
            }
            Expr::Compare(l, rest) => {
                self.expr(l, b)?;
                for (_, x) in rest {
                    self.expr(x, b)?;
                }
            }
            Expr::IfExp { test, body, orelse } => {
                self.expr(test, b)?;
                self.expr(body, b)?;
                self.expr(orelse, b)?;
            }
            Expr::Call {
                func,
                args,
                keywords,
            } => {
                self.expr(func, b)?;
                for a in args {
                    self.expr(a, b)?;
                }
                for k in keywords {
                    self.expr(&k.value, b)?;
                }
            }
            Expr::Slice { lo, hi, step } => {
                for x in [lo, hi, step].into_iter().flatten() {
                    self.expr(x, b)?;
                }
            }
            Expr::FString(parts) | Expr::TString(parts) => self.fstring(parts, b)?,
            _ => {}
        }
        Ok(())
    }

    fn fstring(&mut self, parts: &[FStrPart], b: &mut Block) -> Result<(), String> {
        for p in parts {
            if let FStrPart::Expr { expr, spec, .. } = p {
                self.expr(expr, b)?;
                self.fstring(spec, b)?;
            }
        }
        Ok(())
    }

    /// A comprehension: its first iterable is evaluated here and everything
    /// else runs in the comprehension's own block — which can hold no
    /// declaration — except that a `:=` target in it binds in this one.
    fn comprehension(
        &mut self,
        elts: &[&Expr],
        comps: &[Comprehension],
        b: &mut Block,
    ) -> Result<(), String> {
        let mut targets = Vec::new();
        for (i, c) in comps.iter().enumerate() {
            if i == 0 {
                self.expr(&c.iter, b)?;
            } else {
                walrus_targets(&c.iter, &mut targets);
            }
            for cond in &c.ifs {
                walrus_targets(cond, &mut targets);
            }
        }
        for e in elts {
            walrus_targets(e, &mut targets);
        }
        for t in targets {
            b.add(&t, LOCAL);
        }
        Ok(())
    }
}

/// The value expressions and the clauses of a comprehension.
fn comprehension_parts(e: &Expr) -> (Vec<&Expr>, &[Comprehension]) {
    match e {
        Expr::ListComp(elt, comps) | Expr::SetComp(elt, comps) | Expr::GenExp(elt, comps) => {
            (vec![&**elt], comps)
        }
        Expr::DictComp(k, v, comps) => (vec![&**k, &**v], comps),
        _ => (Vec::new(), &[]),
    }
}

/// A comprehension's body is a function block of its own, and not one that
/// may suspend: a `yield` there is `symtable_raise_if_comprehension_block`'s
/// error, named for the innermost comprehension holding it. Its first iterable
/// belongs to the enclosing block and is checked there; a `lambda` is a block
/// of its own.
fn no_yield_in_comprehension(comp: &Expr) -> Result<(), String> {
    let kind = match comp {
        Expr::ListComp(..) => "list comprehension",
        Expr::SetComp(..) => "set comprehension",
        Expr::DictComp(..) => "dict comprehension",
        _ => "generator expression",
    };
    let (elts, comps) = comprehension_parts(comp);
    let mut parts: Vec<&Expr> = elts;
    for (i, c) in comps.iter().enumerate() {
        if i > 0 {
            parts.push(&c.iter);
        }
        parts.push(&c.target);
        parts.extend(c.ifs.iter());
    }
    parts.into_iter().try_for_each(|p| yield_in(p, kind))
}

fn yield_in(e: &Expr, kind: &str) -> Result<(), String> {
    crate::stack::enter_compile()?;
    match e.unspanned() {
        Expr::Yield(_) | Expr::YieldFrom(_) => {
            let msg = format!("SyntaxError: 'yield' inside {kind}");
            let sp = e.span();
            if !sp.is_some() {
                return Err(msg);
            }
            Err(symtable_error(
                &msg,
                (sp.line, sp.start + 1, sp.end_line, sp.end + 1),
            ))
        }
        Expr::Lambda { .. } => Ok(()),
        inner
        @ (Expr::ListComp(..) | Expr::SetComp(..) | Expr::GenExp(..) | Expr::DictComp(..)) => {
            // The nested comprehension's first iterable is evaluated in THIS
            // one; the rest is its own block.
            let (_, comps) = comprehension_parts(inner);
            if let Some(first) = comps.first() {
                yield_in(&first.iter, kind)?;
            }
            no_yield_in_comprehension(inner)
        }
        other => crate::compiler::expr_children(other)
            .into_iter()
            .try_for_each(|c| yield_in(c, kind)),
    }
}

/// The `:=` targets inside `e`, nested comprehensions included (their targets
/// bind in the same enclosing function) but not `lambda` bodies.
fn walrus_targets(e: &Expr, out: &mut Vec<String>) {
    if crate::stack::compile_overflowed() {
        return;
    }
    let e = e.unspanned();
    if let Expr::NamedExpr(t, _) = e {
        if let Expr::Name(n) = t.unspanned() {
            out.push(n.clone());
        }
    }
    if matches!(e, Expr::Lambda { .. }) {
        return;
    }
    for child in crate::compiler::expr_children(e) {
        walrus_targets(child, out);
    }
}

/// Every name a parameter list binds, in order.
fn param_names(p: &Params) -> Vec<&str> {
    let mut out: Vec<&str> = p.names.iter().map(String::as_str).collect();
    if let Some(s) = p.star.as_deref().filter(|s| !s.is_empty()) {
        out.push(s);
    }
    out.extend(p.kwonly.iter().map(String::as_str));
    if let Some(k) = p.kwargs.as_deref() {
        out.push(k);
    }
    out
}

/// A symbol-table `SyntaxError` at `span`: it carries the position as
/// attributes only (`args == (msg,)`) and no source text of its own — see
/// `SyntaxPos::bare_args` and `SyntaxPos::no_source`. `span` is in
/// characters; the compile entry turns it into the byte columns CPython
/// reports ([`crate::parser::with_byte_columns`]).
pub(crate) fn symtable_error(
    msg: &str,
    (line, col, end_line, end_col): (u32, u32, u32, u32),
) -> String {
    format!(
        "{}{SYNTAX_FIELD}nosrc=1{SYNTAX_FIELD}bare=1{}",
        at_pos(msg, line, col as i64, end_line, end_col as i64),
        crate::parser::CHAR_COLUMNS
    )
}