PEP 709: Merge symbols from an inlined comprehension into the parent scope. Matches symtable.c inline_comprehension().
(
parent_symbols: &mut SymbolMap,
comp: &SymbolTable,
comp_free: &mut IndexSet<String>,
inlined_cells: &mut IndexSet<String>,
parent_type: CompilerScope,
)
| 340 | /// PEP 709: Merge symbols from an inlined comprehension into the parent scope. |
| 341 | /// Matches symtable.c inline_comprehension(). |
| 342 | fn inline_comprehension( |
| 343 | parent_symbols: &mut SymbolMap, |
| 344 | comp: &SymbolTable, |
| 345 | comp_free: &mut IndexSet<String>, |
| 346 | inlined_cells: &mut IndexSet<String>, |
| 347 | parent_type: CompilerScope, |
| 348 | ) { |
| 349 | for (name, sub_symbol) in &comp.symbols { |
| 350 | // Skip the .0 parameter |
| 351 | if sub_symbol.flags.contains(SymbolFlags::PARAMETER) { |
| 352 | continue; |
| 353 | } |
| 354 | |
| 355 | // Track inlined cells |
| 356 | if sub_symbol.scope == SymbolScope::Cell |
| 357 | || sub_symbol.flags.contains(SymbolFlags::COMP_CELL) |
| 358 | { |
| 359 | inlined_cells.insert(name.clone()); |
| 360 | } |
| 361 | |
| 362 | // Handle __class__ in ClassBlock |
| 363 | let scope = if sub_symbol.scope == SymbolScope::Free |
| 364 | && parent_type == CompilerScope::Class |
| 365 | && name == "__class__" |
| 366 | { |
| 367 | comp_free.swap_remove(name); |
| 368 | SymbolScope::GlobalImplicit |
| 369 | } else { |
| 370 | sub_symbol.scope |
| 371 | }; |
| 372 | |
| 373 | if let Some(existing) = parent_symbols.get(name) { |
| 374 | // Name exists in parent |
| 375 | if existing.is_bound() && parent_type != CompilerScope::Class { |
| 376 | // Check if the name is free in any child of the comprehension |
| 377 | let is_free_in_child = comp.sub_tables.iter().any(|child| { |
| 378 | child |
| 379 | .symbols |
| 380 | .get(name) |
| 381 | .is_some_and(|s| s.scope == SymbolScope::Free) |
| 382 | }); |
| 383 | if !is_free_in_child { |
| 384 | comp_free.swap_remove(name); |
| 385 | } |
| 386 | } |
| 387 | } else { |
| 388 | // Name doesn't exist in parent, copy from comprehension. |
| 389 | // Reset scope to Unknown so analyze_symbol will resolve it |
| 390 | // in the parent's context. |
| 391 | let mut symbol = sub_symbol.clone(); |
| 392 | symbol.scope = if sub_symbol.is_bound() { |
| 393 | SymbolScope::Unknown |
| 394 | } else { |
| 395 | scope |
| 396 | }; |
| 397 | parent_symbols.insert(name.clone(), symbol); |
| 398 | } |
| 399 | } |
no test coverage detected