shuck-semantic 0.0.32

Semantic analysis model for shell scripts with scopes, bindings, and dataflow
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
use rustc_hash::{FxHashMap, FxHashSet};
use shuck_ast::{Name, Span};

use crate::{
    Binding, BindingAttributes, BindingId, BindingKind, DeclarationBuiltin, ReferenceKind, ScopeId,
    ScopeKind, SemanticModel,
};

/// Options for nonpersistent assignment analysis.
#[derive(Debug, Clone, Default)]
pub struct NonpersistentAssignmentAnalysisOptions {
    /// Whether Bash pipeline side effects should be treated as persistent.
    pub suppress_bash_pipefail_pipeline_side_effects: bool,
    /// Names that callers do not want analyzed for this effect.
    pub ignored_names: Vec<Name>,
}

/// Command envelope used by nonpersistent assignment analysis for command-local reset behavior.
#[derive(Debug, Clone)]
pub struct NonpersistentAssignmentCommandContext {
    /// Full command span.
    pub span: Span,
    /// Names assigned in this command's assignment prefix.
    pub prefix_reset_names: Vec<Name>,
}

/// Extra linter- or embedding-provided read that should participate in this semantic relation.
#[derive(Debug, Clone)]
pub struct NonpersistentAssignmentExtraRead {
    /// Read name.
    pub name: Name,
    /// Span to report as the later read/use.
    pub span: Span,
    /// Scope where the read occurs.
    pub scope: ScopeId,
}

/// Inputs for nonpersistent assignment analysis.
#[derive(Debug, Clone, Default)]
pub struct NonpersistentAssignmentAnalysisContext {
    /// Analysis behavior options.
    pub options: NonpersistentAssignmentAnalysisOptions,
    /// Command envelopes, used to suppress same-command resets and extend reset ranges.
    pub commands: Vec<NonpersistentAssignmentCommandContext>,
    /// Additional runtime reads not represented as normal semantic references.
    pub extra_reads: Vec<NonpersistentAssignmentExtraRead>,
}

/// Result of nonpersistent assignment analysis.
#[derive(Debug, Clone, Default)]
pub struct NonpersistentAssignmentAnalysis {
    /// Effects where a nonpersistent assignment is observed by a later use.
    pub effects: Vec<NonpersistentAssignmentEffect>,
}

/// A nonpersistent assignment paired with a later use that may still see the outer value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NonpersistentAssignmentEffect {
    /// Variable name involved in the effect.
    pub name: Name,
    /// Binding made inside a nonpersistent scope.
    pub assignment_binding: BindingId,
    /// Semantic assignment span for the binding.
    pub assignment_span: Span,
    /// Span of the later use.
    pub later_use_span: Span,
    /// Source category of the later use.
    pub later_use_kind: NonpersistentLaterUseKind,
}

/// Source category for a later use in nonpersistent assignment analysis.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NonpersistentLaterUseKind {
    /// A normal semantic reference.
    Reference,
    /// A synthetic read introduced by semantic modeling.
    SyntheticRead,
    /// A binding that reads the previous value, such as append or arithmetic assignment.
    Binding,
    /// A caller-provided runtime read.
    ExtraRead,
}

#[derive(Debug, Clone, Copy)]
struct CandidateNonpersistentAssignment {
    binding_id: BindingId,
    effective_local: bool,
    enclosing_function_scope: Option<ScopeId>,
    assignment_span: Span,
    subshell_start: usize,
    subshell_end: usize,
}

#[derive(Debug, Clone, Copy)]
struct NonpersistentScopeSpan {
    span: Span,
}

#[derive(Debug, Clone, Copy)]
struct PersistentReset {
    offset: usize,
    command_index: Option<usize>,
    command_end_offset: usize,
}

#[derive(Debug, Clone, Default)]
struct CommandOffsetLookup {
    entries: Vec<CommandOffsetLookupEntry>,
}

#[derive(Debug, Clone, Copy)]
struct CommandOffsetLookupEntry {
    offset: usize,
    index: usize,
}

impl SemanticModel {
    /// Analyze assignments whose effects do not persist outside a subshell-like scope but are used
    /// later as though they had.
    pub fn analyze_nonpersistent_assignments(
        &self,
        context: &NonpersistentAssignmentAnalysisContext,
    ) -> NonpersistentAssignmentAnalysis {
        let scope_spans_by_id = self
            .scopes()
            .iter()
            .map(|scope| (scope.id, scope.span))
            .collect::<FxHashMap<_, _>>();
        let ignored_names = context
            .options
            .ignored_names
            .iter()
            .collect::<FxHashSet<_>>();
        let mut candidate_bindings_by_scope: FxHashMap<
            (Name, usize, usize),
            CandidateNonpersistentAssignment,
        > = FxHashMap::default();
        let mut persistent_reset_offsets_by_name: FxHashMap<Name, Vec<usize>> =
            FxHashMap::default();
        let mut command_query_offsets = Vec::new();
        let mut relevant_references = Vec::new();
        let mut relevant_synthetic_reads = Vec::new();

        for binding in self.bindings() {
            if !is_nonpersistent_assignment_candidate(binding.kind, binding.attributes) {
                continue;
            }
            if ignored_names.contains(&binding.name) {
                continue;
            }

            let Some(nonpersistent_scope) = nonpersistent_scope_span_for_assignment(
                self,
                binding.scope,
                &scope_spans_by_id,
                context.options.suppress_bash_pipefail_pipeline_side_effects,
            ) else {
                continue;
            };

            candidate_bindings_by_scope
                .entry((
                    binding.name.clone(),
                    nonpersistent_scope.span.start.offset,
                    nonpersistent_scope.span.end.offset,
                ))
                .or_insert(CandidateNonpersistentAssignment {
                    binding_id: binding.id,
                    effective_local: binding_effectively_targets_local(self, binding),
                    enclosing_function_scope: self.enclosing_function_scope(binding.scope),
                    assignment_span: binding.span,
                    subshell_start: nonpersistent_scope.span.start.offset,
                    subshell_end: nonpersistent_scope.span.end.offset,
                });
        }

        let mut candidate_bindings_by_name: FxHashMap<Name, Vec<CandidateNonpersistentAssignment>> =
            FxHashMap::default();
        for ((name, _, _), candidate) in candidate_bindings_by_scope {
            candidate_bindings_by_name
                .entry(name)
                .or_default()
                .push(candidate);
        }
        for candidates in candidate_bindings_by_name.values_mut() {
            candidates.sort_by_key(|candidate| {
                (
                    candidate.subshell_end,
                    candidate.assignment_span.start.offset,
                    candidate.assignment_span.end.offset,
                )
            });
        }

        for binding in self.bindings() {
            if !is_persistent_subshell_reset_binding(binding.kind, binding.attributes) {
                continue;
            }
            if ignored_names.contains(&binding.name) {
                continue;
            }
            persistent_reset_offsets_by_name
                .entry(binding.name.clone())
                .or_default()
                .push(binding.span.start.offset);
            command_query_offsets.push(binding.span.start.offset);
        }

        for reference in self.references() {
            if matches!(reference.kind, ReferenceKind::DeclarationName) {
                continue;
            }
            if ignored_names.contains(&reference.name) {
                continue;
            }
            if candidate_bindings_by_name.contains_key(&reference.name) {
                command_query_offsets.push(reference.span.start.offset);
                relevant_references.push(reference);
            }
        }

        for synthetic_read in self.synthetic_reads() {
            if ignored_names.contains(synthetic_read.name()) {
                continue;
            }
            if candidate_bindings_by_name.contains_key(synthetic_read.name()) {
                command_query_offsets.push(synthetic_read.span().start.offset);
                relevant_synthetic_reads.push(synthetic_read);
            }
        }

        for read in &context.extra_reads {
            if ignored_names.contains(&read.name) {
                continue;
            }
            if candidate_bindings_by_name.contains_key(&read.name) {
                command_query_offsets.push(read.span.start.offset);
            }
        }

        let command_offsets = build_command_offset_lookup(&context.commands, command_query_offsets);
        let persistent_reset_offsets_by_name: FxHashMap<Name, Vec<PersistentReset>> =
            persistent_reset_offsets_by_name
                .into_iter()
                .map(|(name, offsets)| {
                    let resets = offsets
                        .into_iter()
                        .map(|offset| {
                            let command_index =
                                precomputed_command_index_for_offset(&command_offsets, offset);
                            let command_end_offset = command_index
                                .and_then(|index| context.commands.get(index))
                                .map(|command| command.span.end.offset)
                                .unwrap_or(offset);

                            PersistentReset {
                                offset,
                                command_index,
                                command_end_offset,
                            }
                        })
                        .collect();
                    (name, resets)
                })
                .collect();

        let mut effects = Vec::new();
        for reference in relevant_references {
            let Some(candidates) = candidate_bindings_by_name.get(&reference.name) else {
                continue;
            };

            let reset_offsets = persistent_reset_offsets_by_name
                .get(&reference.name)
                .map(Vec::as_slice)
                .unwrap_or(&[]);
            let event_command_index =
                precomputed_command_index_for_offset(&command_offsets, reference.span.start.offset);
            let resolved = self.resolved_binding(reference.id);
            let reference_function_scope = self.enclosing_function_scope(reference.scope);
            if let Some(candidate) = candidates.iter().rev().find(|candidate| {
                reference.span.start.offset > candidate.subshell_end
                    && !has_intervening_persistent_reset(
                        reset_offsets,
                        candidate.subshell_end,
                        reference.span.start.offset,
                        event_command_index,
                    )
                    && resolved_binding_allows_subshell_later_use(
                        resolved,
                        candidate,
                        reference.span.start.offset,
                        reference_function_scope,
                    )
            }) {
                effects.push(effect_for_candidate(
                    reference.name.clone(),
                    *candidate,
                    reference.span,
                    NonpersistentLaterUseKind::Reference,
                ));
            }
        }

        for synthetic_read in relevant_synthetic_reads {
            let Some(candidates) = candidate_bindings_by_name.get(synthetic_read.name()) else {
                continue;
            };

            let reset_offsets = persistent_reset_offsets_by_name
                .get(synthetic_read.name())
                .map(Vec::as_slice)
                .unwrap_or(&[]);
            let synthetic_command_index = precomputed_command_index_for_offset(
                &command_offsets,
                synthetic_read.span().start.offset,
            );
            let same_command_prefix_reset = synthetic_command_index
                .and_then(|index| context.commands.get(index))
                .is_some_and(|command| {
                    command
                        .prefix_reset_names
                        .iter()
                        .any(|name| name == synthetic_read.name())
                });
            let synthetic_command_end_offset = synthetic_command_index
                .and_then(|index| context.commands.get(index))
                .map(|command| command.span.end.offset)
                .unwrap_or(synthetic_read.span().start.offset);
            let synthetic_function_scope = self.enclosing_function_scope(synthetic_read.scope());
            if let Some(candidate) = candidates.iter().rev().find(|candidate| {
                synthetic_read.span().start.offset > candidate.subshell_end
                    && !same_command_prefix_reset
                    && candidate_allows_unresolved_later_use(candidate, synthetic_function_scope)
                    && !has_intervening_persistent_reset(
                        reset_offsets,
                        candidate.subshell_end,
                        synthetic_command_end_offset,
                        None,
                    )
            }) {
                effects.push(effect_for_candidate(
                    synthetic_read.name().clone(),
                    *candidate,
                    synthetic_read.span(),
                    NonpersistentLaterUseKind::SyntheticRead,
                ));
            }
        }

        for read in &context.extra_reads {
            let Some(candidates) = candidate_bindings_by_name.get(&read.name) else {
                continue;
            };

            let reset_offsets = persistent_reset_offsets_by_name
                .get(&read.name)
                .map(Vec::as_slice)
                .unwrap_or(&[]);
            let event_command_index =
                precomputed_command_index_for_offset(&command_offsets, read.span.start.offset);
            let read_function_scope = self.enclosing_function_scope(read.scope);
            if let Some(candidate) = candidates.iter().rev().find(|candidate| {
                read.span.start.offset > candidate.subshell_end
                    && candidate_allows_unresolved_later_use(candidate, read_function_scope)
                    && !has_intervening_persistent_reset(
                        reset_offsets,
                        candidate.subshell_end,
                        read.span.start.offset,
                        event_command_index,
                    )
            }) {
                effects.push(effect_for_candidate(
                    read.name.clone(),
                    *candidate,
                    read.span,
                    NonpersistentLaterUseKind::ExtraRead,
                ));
            }
        }

        for binding in self.bindings() {
            if !is_nonpersistent_later_use_binding(binding.kind, binding.attributes) {
                continue;
            }
            if ignored_names.contains(&binding.name) {
                continue;
            }

            let Some(candidates) = candidate_bindings_by_name.get(&binding.name) else {
                continue;
            };

            let reset_offsets = persistent_reset_offsets_by_name
                .get(&binding.name)
                .map(Vec::as_slice)
                .unwrap_or(&[]);
            let binding_function_scope = self.enclosing_function_scope(binding.scope);
            if let Some(candidate) = candidates.iter().rev().find(|candidate| {
                binding.span.start.offset > candidate.subshell_end
                    && candidate_allows_unresolved_later_use(candidate, binding_function_scope)
                    && !has_intervening_persistent_reset(
                        reset_offsets,
                        candidate.subshell_end,
                        binding.span.start.offset,
                        None,
                    )
            }) {
                effects.push(effect_for_candidate(
                    binding.name.clone(),
                    *candidate,
                    binding.span,
                    NonpersistentLaterUseKind::Binding,
                ));
            }
        }

        let mut seen = FxHashSet::default();
        effects.retain(|effect| {
            seen.insert((
                effect.assignment_binding,
                effect.later_use_span.start.offset,
                effect.later_use_span.end.offset,
                effect.name.clone(),
            ))
        });
        effects.sort_by_key(|effect| {
            (
                effect.later_use_span.start.offset,
                effect.later_use_span.end.offset,
                effect.assignment_span.start.offset,
                effect.assignment_span.end.offset,
            )
        });

        NonpersistentAssignmentAnalysis { effects }
    }
}

fn effect_for_candidate(
    name: Name,
    candidate: CandidateNonpersistentAssignment,
    later_use_span: Span,
    later_use_kind: NonpersistentLaterUseKind,
) -> NonpersistentAssignmentEffect {
    NonpersistentAssignmentEffect {
        name,
        assignment_binding: candidate.binding_id,
        assignment_span: candidate.assignment_span,
        later_use_span,
        later_use_kind,
    }
}

fn is_nonpersistent_assignment_candidate(kind: BindingKind, attributes: BindingAttributes) -> bool {
    match kind {
        BindingKind::Assignment
        | BindingKind::ParameterDefaultAssignment
        | BindingKind::AppendAssignment
        | BindingKind::ArrayAssignment
        | BindingKind::LoopVariable
        | BindingKind::ReadTarget
        | BindingKind::MapfileTarget
        | BindingKind::PrintfTarget
        | BindingKind::GetoptsTarget
        | BindingKind::ArithmeticAssignment => !attributes.contains(BindingAttributes::LOCAL),
        BindingKind::Declaration(_) => {
            attributes.contains(BindingAttributes::DECLARATION_INITIALIZED)
        }
        BindingKind::Imported => false,
        BindingKind::FunctionDefinition | BindingKind::Nameref => false,
    }
}

fn is_nonpersistent_later_use_binding(kind: BindingKind, attributes: BindingAttributes) -> bool {
    match kind {
        BindingKind::AppendAssignment => true,
        BindingKind::ArithmeticAssignment => true,
        BindingKind::Declaration(DeclarationBuiltin::Export) => {
            !attributes.contains(BindingAttributes::LOCAL)
        }
        BindingKind::Declaration(_) => false,
        BindingKind::Assignment
        | BindingKind::ArrayAssignment
        | BindingKind::LoopVariable
        | BindingKind::ReadTarget
        | BindingKind::MapfileTarget
        | BindingKind::PrintfTarget
        | BindingKind::GetoptsTarget
        | BindingKind::ParameterDefaultAssignment
        | BindingKind::FunctionDefinition
        | BindingKind::Imported
        | BindingKind::Nameref => false,
    }
}

fn is_persistent_subshell_reset_binding(kind: BindingKind, attributes: BindingAttributes) -> bool {
    match kind {
        BindingKind::Assignment
        | BindingKind::ParameterDefaultAssignment
        | BindingKind::AppendAssignment
        | BindingKind::ArrayAssignment
        | BindingKind::LoopVariable
        | BindingKind::ReadTarget
        | BindingKind::MapfileTarget
        | BindingKind::PrintfTarget
        | BindingKind::GetoptsTarget
        | BindingKind::ArithmeticAssignment => !attributes.contains(BindingAttributes::LOCAL),
        BindingKind::Declaration(_) => {
            attributes.contains(BindingAttributes::DECLARATION_INITIALIZED)
        }
        BindingKind::Imported => false,
        BindingKind::FunctionDefinition | BindingKind::Nameref => false,
    }
}

fn nonpersistent_scope_span_for_assignment(
    semantic: &SemanticModel,
    scope: ScopeId,
    scope_spans_by_id: &FxHashMap<ScopeId, Span>,
    suppress_bash_pipefail_pipeline_side_effects: bool,
) -> Option<NonpersistentScopeSpan> {
    semantic
        .ancestor_scopes(scope)
        .find(|scope_id| match semantic.scope_kind(*scope_id) {
            ScopeKind::Pipeline => !suppress_bash_pipefail_pipeline_side_effects,
            ScopeKind::Subshell | ScopeKind::CommandSubstitution => true,
            ScopeKind::Function(_) | ScopeKind::File => false,
        })
        .and_then(|scope_id| scope_spans_by_id.get(&scope_id).copied())
        .map(|span| NonpersistentScopeSpan { span })
}

fn resolved_binding_allows_subshell_later_use(
    resolved: Option<&Binding>,
    candidate: &CandidateNonpersistentAssignment,
    reference_offset: usize,
    reference_function_scope: Option<ScopeId>,
) -> bool {
    let Some(resolved) = resolved else {
        return candidate_allows_unresolved_later_use(candidate, reference_function_scope);
    };
    if resolved.id == candidate.binding_id {
        return false;
    }
    if resolved.span.start.offset > reference_offset {
        return true;
    }
    if resolved.span.start.offset < candidate.subshell_start {
        return true;
    }

    matches!(resolved.kind, BindingKind::Declaration(_))
        && !resolved
            .attributes
            .contains(BindingAttributes::DECLARATION_INITIALIZED)
}

fn candidate_allows_unresolved_later_use(
    candidate: &CandidateNonpersistentAssignment,
    later_function_scope: Option<ScopeId>,
) -> bool {
    !candidate.effective_local || later_function_scope == candidate.enclosing_function_scope
}

fn binding_effectively_targets_local(semantic: &SemanticModel, binding: &Binding) -> bool {
    if binding.attributes.contains(BindingAttributes::LOCAL) {
        return true;
    }

    let binding_function_scope = semantic.enclosing_function_scope(binding.scope);
    semantic
        .previous_visible_binding(&binding.name, binding.span, Some(binding.span))
        .is_some_and(|previous| {
            previous.attributes.contains(BindingAttributes::LOCAL)
                && semantic.enclosing_function_scope(previous.scope) == binding_function_scope
        })
}

fn has_intervening_persistent_reset(
    resets: &[PersistentReset],
    candidate_end: usize,
    event_offset: usize,
    event_command_index: Option<usize>,
) -> bool {
    resets.iter().any(|reset| {
        let effective_offset = if reset.offset > candidate_end {
            reset.offset
        } else {
            reset.command_end_offset
        };

        effective_offset > candidate_end
            && effective_offset < event_offset
            && event_command_index
                .is_none_or(|event_index| reset.command_index != Some(event_index))
    })
}

fn build_command_offset_lookup(
    commands: &[NonpersistentAssignmentCommandContext],
    mut offsets: Vec<usize>,
) -> CommandOffsetLookup {
    if offsets.is_empty() || commands.is_empty() {
        return CommandOffsetLookup::default();
    }

    offsets.sort_unstable();
    offsets.dedup();

    let mut command_order = (0..commands.len()).collect::<Vec<_>>();
    command_order.sort_unstable_by(|left, right| {
        let left_span = commands[*left].span;
        let right_span = commands[*right].span;
        left_span
            .start
            .offset
            .cmp(&right_span.start.offset)
            .then_with(|| right_span.end.offset.cmp(&left_span.end.offset))
            .then_with(|| right.cmp(left))
    });

    let mut entries = Vec::with_capacity(offsets.len());
    let mut active_commands = Vec::new();
    let mut next_command = 0;
    for offset in offsets {
        pop_finished_commands(&mut active_commands, offset);

        while let Some(index) = command_order.get(next_command).copied() {
            let span = commands[index].span;
            if span.start.offset > offset {
                break;
            }

            pop_finished_commands(&mut active_commands, span.start.offset);
            active_commands.push(OpenCommand {
                end_offset: span.end.offset,
                index,
            });
            next_command += 1;
        }

        pop_finished_commands(&mut active_commands, offset);
        if let Some(command) = active_commands.last() {
            entries.push(CommandOffsetLookupEntry {
                offset,
                index: command.index,
            });
        }
    }

    CommandOffsetLookup { entries }
}

fn precomputed_command_index_for_offset(
    command_offsets: &CommandOffsetLookup,
    offset: usize,
) -> Option<usize> {
    command_offsets
        .entries
        .binary_search_by_key(&offset, |entry| entry.offset)
        .ok()
        .map(|index| command_offsets.entries[index].index)
}

#[derive(Debug, Clone, Copy)]
struct OpenCommand {
    end_offset: usize,
    index: usize,
}

fn pop_finished_commands(active_commands: &mut Vec<OpenCommand>, offset: usize) {
    while active_commands
        .last()
        .is_some_and(|command| command.end_offset < offset)
    {
        active_commands.pop();
    }
}