lambdust 0.1.1

A Scheme dialect with gradual typing and effect systems
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
715
716
717
718
719
720
721
722
//! GC-aware diagnostic system for Lambdust.
//!
//! This module provides error reporting and diagnostic preservation that integrates
//! with the parallel garbage collector while maintaining complete diagnostic information,
//! stack traces, and source location data across GC cycles.

use crate::utils::{GcIntegration, GcIntegrationConfig};
use crate::utils::gc::{ObjectId, gc_alloc, GcObject, GenerationId};
use crate::diagnostics::{Error, Span, Result};
use crate::eval::{StackTrace, StackFrame, FrameType};
use std::sync::{Arc, RwLock, Mutex, atomic::{AtomicBool, AtomicU64, AtomicU32, Ordering}};
use std::collections::HashMap;
use std::time::Instant;
use std::fmt;

/// GC-aware diagnostic manager that preserves error information across garbage collection.
#[derive(Debug)]
pub struct GcDiagnosticManager {
    /// GC integration manager
    gc_integration: Arc<GcIntegration>,
    /// Registry of preserved diagnostic information
    diagnostic_registry: RwLock<HashMap<DiagnosticId, DiagnosticEntry>>,
    /// Error context preservation system
    context_manager: Arc<ErrorContextManager>,
    /// Configuration
    config: GcDiagnosticConfig,
    /// Next diagnostic ID
    next_diagnostic_id: AtomicU64,
}

/// Configuration for GC-aware diagnostics.
#[derive(Debug, Clone)]
pub struct GcDiagnosticConfig {
    /// Whether to enable GC tracking for diagnostic information
    pub track_diagnostics: bool,
    /// Whether to preserve error contexts across GC cycles
    pub preserve_contexts: bool,
    /// Whether to preserve stack traces in errors
    pub preserve_stack_traces: bool,
    /// Maximum number of preserved diagnostics
    pub max_preserved_diagnostics: usize,
    /// Whether to compress old diagnostic information
    pub compress_old_diagnostics: bool,
}

/// Unique identifier for diagnostic entries.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DiagnosticId(u64);

/// Entry in the diagnostic registry for GC tracking.
#[derive(Debug)]
pub struct DiagnosticEntry {
    /// Unique identifier
    pub id: DiagnosticId,
    /// The preserved error information
    pub error_info: PreservedError,
    /// GC object ID if tracked
    pub gc_object_id: Option<ObjectId>,
    /// Creation timestamp
    pub created_at: Instant,
    /// Whether this diagnostic is still active
    pub active: AtomicBool,
}

/// Error information preserved across GC cycles.
#[derive(Debug, Clone)]
pub struct PreservedError {
    /// Error message
    pub message: String,
    /// Error kind/type
    pub kind: ErrorKind,
    /// Source location if available
    pub span: Option<Span>,
    /// Stack trace at error time
    pub stack_trace: Option<StackTrace>,
    /// Additional context information
    pub context: ErrorContext,
    /// Cause chain (nested errors)
    pub cause_chain: Vec<PreservedError>,
    /// Error metadata
    pub metadata: HashMap<String, String>,
}

/// Classification of error types for GC handling.
#[derive(Debug, Clone, PartialEq)]
pub enum ErrorKind {
    /// Runtime evaluation error
    RuntimeError,
    /// Syntax or parsing error
    SyntaxError,
    /// Type-related error
    TypeError,
    /// I/O or system error
    IoError,
    /// FFI-related error
    FfiError,
    /// Macro expansion error
    MacroError,
    /// Continuation-related error
    ContinuationError,
    /// Memory or GC-related error
    MemoryError,
    /// Custom error type
    Custom(String),
}

/// Context information for error diagnosis.
#[derive(Debug, Clone, Default)]
pub struct ErrorContext {
    /// Current expression being evaluated
    pub current_expr: Option<String>,
    /// Variable bindings at error time
    pub bindings: HashMap<String, String>,
    /// Procedure call chain
    pub call_chain: Vec<String>,
    /// Module context
    pub module_context: Option<String>,
    /// Additional contextual notes
    pub notes: Vec<String>,
}

/// Manager for error context preservation.
#[derive(Debug)]
pub struct ErrorContextManager {
    /// Preserved contexts indexed by diagnostic ID
    preserved_contexts: RwLock<HashMap<DiagnosticId, PreservedErrorContext>>,
    /// Context compression settings
    config: ContextConfig,
}

/// Configuration for error context management.
#[derive(Debug, Clone)]
pub struct ContextConfig {
    /// Maximum number of preserved contexts
    pub max_preserved_contexts: usize,
    /// Whether to compress context data
    pub compress_contexts: bool,
    /// Maximum size of individual context entries
    pub max_context_size: usize,
}

/// Preserved error context with GC-safe storage.
#[derive(Debug, Clone)]
pub struct PreservedErrorContext {
    /// The error context
    pub context: ErrorContext,
    /// Preservation timestamp
    pub preserved_at: Instant,
    /// Whether this context is compressed
    pub compressed: bool,
    /// Original size before compression
    pub original_size: usize,
}

/// GC wrapper for diagnostic information.
#[derive(Debug)]
pub struct DiagnosticGcWrapper {
    /// The preserved error information
    preserved_error: PreservedError,
    /// GC metadata
    generation: AtomicU32,
    marked: AtomicBool,
}

/// A GC-aware error that maintains diagnostic information across collection cycles.
#[derive(Debug)]
pub struct GcAwareError {
    /// The underlying error
    inner_error: Error,
    /// Diagnostic ID for GC tracking
    diagnostic_id: Option<DiagnosticId>,
    /// Preserved information
    preserved_info: Option<PreservedError>,
}

impl GcDiagnosticManager {
    /// Creates a new GC-aware diagnostic manager.
    pub fn new(
        gc_integration: Arc<GcIntegration>,
        config: GcDiagnosticConfig,
    ) -> Self {
        let context_manager = Arc::new(ErrorContextManager::new(
            ContextConfig::default()
        ));

        Self {
            gc_integration,
            diagnostic_registry: RwLock::new(HashMap::new()),
            context_manager,
            config,
            next_diagnostic_id: AtomicU64::new(1),
        }
    }

    /// Creates a diagnostic manager with default configuration.
    pub fn with_default_config(gc_integration: Arc<GcIntegration>) -> Self {
        Self::new(gc_integration, GcDiagnosticConfig::default())
    }

    /// Creates a GC-aware error with preserved diagnostic information.
    pub fn create_gc_aware_error(
        &self,
        error: Error,
        stack_trace: Option<StackTrace>,
        context: Option<ErrorContext>,
    ) -> GcAwareError {
        if !self.config.track_diagnostics {
            return GcAwareError {
                inner_error: error,
                diagnostic_id: None,
                preserved_info: None,
            };
        }

        let diagnostic_id = DiagnosticId(
            self.next_diagnostic_id.fetch_add(1, Ordering::SeqCst)
        );

        // Create preserved error information
        let preserved_error = self.create_preserved_error(
            &error,
            stack_trace,
            context.unwrap_or_default(),
        );

        // Create GC wrapper if tracking is enabled
        let gc_object_id = if self.config.track_diagnostics {
            let wrapper = DiagnosticGcWrapper::new(preserved_error.clone());
            let gc_ptr = gc_alloc(wrapper);
            let object_id = gc_ptr.id();
            
            // Register with GC integration (as a macro root for now)
            self.gc_integration.register_macro_root(object_id);
            
            Some(object_id)
        } else {
            None
        };

        // Create registry entry
        let entry = DiagnosticEntry {
            id: diagnostic_id,
            error_info: preserved_error.clone(),
            gc_object_id,
            created_at: Instant::now(),
            active: AtomicBool::new(true),
        };

        // Register the diagnostic
        if let Ok(mut registry) = self.diagnostic_registry.write() {
            registry.insert(diagnostic_id, entry);
            
            // Clean up old entries if we exceed the limit
            if registry.len() > self.config.max_preserved_diagnostics {
                self.cleanup_old_diagnostics(&mut registry);
            }
        }

        GcAwareError {
            inner_error: error,
            diagnostic_id: Some(diagnostic_id),
            preserved_info: Some(preserved_error),
        }
    }

    /// Creates preserved error information from an Error.
    fn create_preserved_error(
        &self,
        error: &Error,
        stack_trace: Option<StackTrace>,
        context: ErrorContext,
    ) -> PreservedError {
        let kind = self.classify_error(error);
        let (message, span) = self.extract_error_details(error);
        
        PreservedError {
            message,
            kind,
            span,
            stack_trace,
            context,
            cause_chain: Vec::new(), // Could be extended to handle error chains
            metadata: HashMap::new(),
        }
    }

    /// Extracts message and span from an Error enum.
    fn extract_error_details(&self, error: &Error) -> (String, Option<Span>) {
        match error {
            Error::LexError { message, span } => (message.clone(), Some(*span)),
            Error::ParseError { message, span } => (message.clone(), Some(*span)),
            Error::TypeError { message, span } => (message.clone(), Some(*span)),
            Error::MacroError { message, span } => (message.clone(), Some(*span)),
            Error::RuntimeError { message, span } => (message.clone(), *span),
            Error::FfiError { message } => (message.clone(), None),
            Error::IoError { message } => (message.clone(), None),
            Error::InternalError { message } => (message.clone(), None),
            Error::Exception { exception, span } => (exception.to_string(), *span),
        }
    }

    /// Classifies an error for GC handling.
    fn classify_error(&self, error: &Error) -> ErrorKind {
        match error {
            Error::LexError { .. } => ErrorKind::SyntaxError,
            Error::ParseError { .. } => ErrorKind::SyntaxError,
            Error::TypeError { .. } => ErrorKind::TypeError,
            Error::MacroError { .. } => ErrorKind::MacroError,
            Error::RuntimeError { .. } => ErrorKind::RuntimeError,
            Error::FfiError { .. } => ErrorKind::FfiError,
            Error::IoError { .. } => ErrorKind::IoError,
            Error::InternalError { .. } => ErrorKind::RuntimeError,
            Error::Exception { .. } => ErrorKind::RuntimeError,
        }
    }

    /// Retrieves preserved diagnostic information.
    pub fn get_preserved_diagnostic(&self, diagnostic_id: DiagnosticId) -> Option<PreservedError> {
        if let Ok(registry) = self.diagnostic_registry.read() {
            registry.get(&diagnostic_id).map(|entry| entry.error_info.clone())
        } else {
            None
        }
    }

    /// Cleans up old diagnostic entries to stay within limits.
    fn cleanup_old_diagnostics(&self, registry: &mut HashMap<DiagnosticId, DiagnosticEntry>) {
        if registry.len() <= self.config.max_preserved_diagnostics {
            return;
        }

        // Sort by creation time and remove oldest entries
        let mut entries: Vec<_> = registry.iter()
            .map(|(id, entry)| (*id, entry.created_at))
            .collect();
        entries.sort_by_key(|(_, time)| *time);
        
        let to_remove = registry.len() - self.config.max_preserved_diagnostics;
        let ids_to_remove: Vec<_> = entries.iter()
            .take(to_remove)
            .map(|(id, _)| *id)
            .collect();
        
        for id in ids_to_remove {
            if let Some(entry) = registry.remove(&id) {
                // Unregister from GC if it was tracked
                if let Some(gc_object_id) = entry.gc_object_id {
                    self.gc_integration.unregister_continuation_root(gc_object_id);
                }
            }
        }
    }

    /// Gets statistics about diagnostic preservation.
    pub fn get_diagnostic_statistics(&self) -> DiagnosticStatistics {
        let active_count = if let Ok(registry) = self.diagnostic_registry.read() {
            registry.values()
                .filter(|entry| entry.active.load(Ordering::SeqCst))
                .count()
        } else {
            0
        };

        let total_count = if let Ok(registry) = self.diagnostic_registry.read() {
            registry.len()
        } else {
            0
        };

        let preserved_contexts = self.context_manager.preserved_context_count();

        DiagnosticStatistics {
            active_diagnostics: active_count,
            total_diagnostics: total_count,
            preserved_contexts,
            gc_roots_tracked: self.count_gc_tracked_diagnostics(),
        }
    }

    /// Counts the number of GC-tracked diagnostics.
    fn count_gc_tracked_diagnostics(&self) -> usize {
        if let Ok(registry) = self.diagnostic_registry.read() {
            registry.values()
                .filter(|entry| entry.gc_object_id.is_some())
                .count()
        } else {
            0
        }
    }

    /// Gets the configuration.
    pub fn config(&self) -> &GcDiagnosticConfig {
        &self.config
    }
}

impl ErrorContextManager {
    /// Creates a new error context manager.
    pub fn new(config: ContextConfig) -> Self {
        Self {
            preserved_contexts: RwLock::new(HashMap::new()),
            config,
        }
    }

    /// Preserves an error context.
    pub fn preserve_context(&self, diagnostic_id: DiagnosticId, context: ErrorContext) -> Result<()> {
        let context_size = self.estimate_context_size(&context);
        
        let preserved_context = PreservedErrorContext {
            context: if context_size > self.config.max_context_size && self.config.compress_contexts {
                self.compress_context(context)
            } else {
                context
            },
            preserved_at: Instant::now(),
            compressed: context_size > self.config.max_context_size,
            original_size: context_size,
        };

        if let Ok(mut contexts) = self.preserved_contexts.write() {
            contexts.insert(diagnostic_id, preserved_context);
            
            // Clean up old contexts if we exceed the limit
            if contexts.len() > self.config.max_preserved_contexts {
                self.cleanup_old_contexts(&mut contexts);
            }
        }

        Ok(())
    }

    /// Gets a preserved error context.
    pub fn get_preserved_context(&self, diagnostic_id: DiagnosticId) -> Option<PreservedErrorContext> {
        if let Ok(contexts) = self.preserved_contexts.read() {
            contexts.get(&diagnostic_id).cloned()
        } else {
            None
        }
    }

    /// Gets the count of preserved contexts.
    pub fn preserved_context_count(&self) -> usize {
        if let Ok(contexts) = self.preserved_contexts.read() {
            contexts.len()
        } else {
            0
        }
    }

    /// Estimates the memory size of an error context.
    fn estimate_context_size(&self, context: &ErrorContext) -> usize {
        let mut size = 0;
        
        if let Some(ref expr) = context.current_expr {
            size += expr.len();
        }
        
        size += context.bindings.iter()
            .map(|(k, v)| k.len() + v.len())
            .sum::<usize>();
            
        size += context.call_chain.iter()
            .map(|s| s.len())
            .sum::<usize>();
            
        if let Some(ref module) = context.module_context {
            size += module.len();
        }
        
        size += context.notes.iter()
            .map(|s| s.len())
            .sum::<usize>();
            
        size
    }

    /// Compresses an error context to reduce memory usage.
    fn compress_context(&self, mut context: ErrorContext) -> ErrorContext {
        // Simple compression - truncate large strings and limit collections
        if let Some(ref mut expr) = context.current_expr {
            if expr.len() > 200 {
                expr.truncate(197);
                expr.push_str("...");
            }
        }

        // Limit number of bindings
        if context.bindings.len() > 20 {
            let keys: Vec<_> = context.bindings.keys().take(20).cloned().collect();
            context.bindings.retain(|k, _| keys.contains(k));
        }

        // Limit call chain
        if context.call_chain.len() > 10 {
            context.call_chain.truncate(10);
        }

        // Limit notes
        if context.notes.len() > 5 {
            context.notes.truncate(5);
        }

        context
    }

    /// Cleans up old contexts to stay within limits.
    fn cleanup_old_contexts(&self, contexts: &mut HashMap<DiagnosticId, PreservedErrorContext>) {
        if contexts.len() <= self.config.max_preserved_contexts {
            return;
        }

        let mut entries: Vec<_> = contexts.iter()
            .map(|(id, context)| (*id, context.preserved_at))
            .collect();
        entries.sort_by_key(|(_, time)| *time);
        
        let to_remove = contexts.len() - self.config.max_preserved_contexts;
        let ids_to_remove: Vec<_> = entries.iter()
            .take(to_remove)
            .map(|(id, _)| *id)
            .collect();
        
        for id in ids_to_remove {
            contexts.remove(&id);
        }
    }
}

impl DiagnosticGcWrapper {
    /// Creates a new GC wrapper for diagnostic information.
    pub fn new(error: PreservedError) -> Self {
        Self {
            preserved_error: error,
            generation: AtomicU32::new(0),
            marked: AtomicBool::new(false),
        }
    }

    /// Gets a reference to the preserved error.
    pub fn error(&self) -> &PreservedError {
        &self.preserved_error
    }
}

impl GcObject for DiagnosticGcWrapper {
    fn generation(&self) -> GenerationId {
        self.generation.load(Ordering::Relaxed)
    }

    fn set_generation(&mut self, generation: GenerationId) {
        self.generation.store(generation, Ordering::Relaxed);
    }

    fn references(&self) -> Vec<crate::utils::gc::GcPtr> {
        // No internal references for now
        Vec::new()
    }

    fn mark(&self) {
        self.marked.store(true, Ordering::Relaxed);
    }

    fn is_marked(&self) -> bool {
        self.marked.load(Ordering::Relaxed)
    }

    fn clear_mark(&self) {
        self.marked.store(false, Ordering::Relaxed);
    }

    fn size_hint(&self) -> usize {
        // Estimate based on preserved error content
        let message_size = self.preserved_error.message.len();
        let context_size = self.estimate_context_size(&self.preserved_error.context);
        let base_size = std::mem::size_of::<PreservedError>();
        base_size + message_size + context_size + 128 // Extra overhead
    }
}

impl DiagnosticGcWrapper {
    /// Estimates the size of an error context.
    fn estimate_context_size(&self, context: &ErrorContext) -> usize {
        let mut size = 0;
        
        if let Some(ref expr) = context.current_expr {
            size += expr.len();
        }
        
        size += context.bindings.iter()
            .map(|(k, v)| k.len() + v.len())
            .sum::<usize>();
            
        size += context.call_chain.iter()
            .map(|s| s.len())
            .sum::<usize>();
            
        if let Some(ref module) = context.module_context {
            size += module.len();
        }
        
        size += context.notes.iter()
            .map(|s| s.len())
            .sum::<usize>();
            
        size
    }
}

impl GcAwareError {
    /// Gets the diagnostic ID if this error is GC-tracked.
    pub fn diagnostic_id(&self) -> Option<DiagnosticId> {
        self.diagnostic_id
    }

    /// Gets the preserved error information if available.
    pub fn preserved_info(&self) -> Option<&PreservedError> {
        self.preserved_info.as_ref()
    }

    /// Gets the underlying error.
    pub fn inner_error(&self) -> &Error {
        &self.inner_error
    }
}

impl fmt::Display for GcAwareError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.inner_error)
    }
}


impl Default for GcDiagnosticConfig {
    fn default() -> Self {
        Self {
            track_diagnostics: true,
            preserve_contexts: true,
            preserve_stack_traces: true,
            max_preserved_diagnostics: 100,
            compress_old_diagnostics: true,
        }
    }
}

impl Default for ContextConfig {
    fn default() -> Self {
        Self {
            max_preserved_contexts: 200,
            compress_contexts: true,
            max_context_size: 4096, // 4KB max per context
        }
    }
}

/// Statistics about diagnostic preservation.
#[derive(Debug, Clone)]
pub struct DiagnosticStatistics {
    /// Number of active diagnostics
    pub active_diagnostics: usize,
    /// Total number of preserved diagnostics
    pub total_diagnostics: usize,
    /// Number of preserved error contexts
    pub preserved_contexts: usize,
    /// Number of diagnostics tracked by GC
    pub gc_roots_tracked: usize,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::utils::GcIntegration;
    use crate::diagnostics::Error;

    #[test]
    fn test_diagnostic_manager_creation() {
        let gc_integration = Arc::new(GcIntegration::with_default_config());
        let manager = GcDiagnosticManager::with_default_config(gc_integration);
        
        let stats = manager.get_diagnostic_statistics();
        assert_eq!(stats.active_diagnostics, 0);
        assert_eq!(stats.total_diagnostics, 0);
    }

    #[test]
    fn test_gc_aware_error_creation() {
        let gc_integration = Arc::new(GcIntegration::with_default_config());
        let manager = GcDiagnosticManager::with_default_config(gc_integration);
        
        let error = Error::runtime_error("test error".to_string(), None);
        let context = ErrorContext::default();
        
        let gc_error = manager.create_gc_aware_error(error, None, Some(context));
        
        assert!(gc_error.diagnostic_id().is_some());
        assert!(gc_error.preserved_info().is_some());
        
        let stats = manager.get_diagnostic_statistics();
        assert_eq!(stats.active_diagnostics, 1);
        assert_eq!(stats.total_diagnostics, 1);
    }

    #[test]
    fn test_error_context_preservation() {
        let context_manager = ErrorContextManager::new(ContextConfig::default());
        let diagnostic_id = DiagnosticId(1);
        
        let mut context = ErrorContext::default();
        context.current_expr = Some("(+ 1 2)".to_string());
        context.notes.push("Test note".to_string());
        
        let result = context_manager.preserve_context(diagnostic_id, context);
        assert!(result.is_ok());
        
        let preserved = context_manager.get_preserved_context(diagnostic_id);
        assert!(preserved.is_some());
        
        let preserved = preserved.unwrap();
        assert_eq!(preserved.context.current_expr, Some("(+ 1 2)".to_string()));
        assert_eq!(preserved.context.notes.len(), 1);
    }
}