runic-diagnostics 0.1.0

An MLIR-inspired compiler framework
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
mod line_column;
mod source_id;
mod span;

use alloc::{boxed::Box, string::String, sync::Arc, vec::Vec};
use core::fmt;

pub use self::{
    line_column::*,
    source_id::{SourceId, SourceKey, SourceNamespace, SourceRevision},
    span::{SourceSpan, Span, Spanned, TextRange, TextRangeError},
};

/// A borrowed source resolved from a provider.
#[derive(Clone, Copy, Debug)]
pub struct Source<'a> {
    pub id: SourceId,
    pub display_name: &'a str,
    pub byte_len: u32,
    pub text: Option<&'a str>,
    pub revision: Option<SourceRevision>,
}

/// A resolved source that retains its complete provenance key.
#[derive(Clone, Copy, Debug)]
pub struct ResolvedSource<'a> {
    pub key: SourceKey,
    pub source: Source<'a>,
}

/// Resolves one source universe by namespaced source ID.
pub trait SourceProvider {
    fn get(&self, id: SourceId) -> Option<Source<'_>>;

    fn line_column(&self, id: SourceId, offset: u32) -> Option<LineColumn>;

    /// Returns the most recently registered source with the given display name, if supported.
    ///
    /// Display names are not required to be unique. Providers which do not maintain a reverse
    /// index may leave this operation unsupported.
    fn find_by_name(&self, _display_name: &str) -> Option<SourceId> {
        None
    }

    /// Returns the byte offset of `position`, if the provider supports reverse lookup.
    fn offset_of(&self, _id: SourceId, _position: LineColumn) -> Option<u32> {
        None
    }
}

/// A cloneable, type-erased source provider suitable for retaining with owned diagnostics.
#[derive(Clone)]
pub struct SharedSourceProvider {
    inner: Arc<dyn SourceProvider + Send + Sync + 'static>,
}

impl SharedSourceProvider {
    pub fn new<P>(provider: Arc<P>) -> Self
    where
        P: SourceProvider + Send + Sync + 'static,
    {
        Self { inner: provider }
    }

    pub fn from_arc(provider: Arc<dyn SourceProvider + Send + Sync + 'static>) -> Self {
        Self { inner: provider }
    }

    pub fn as_provider(&self) -> &(dyn SourceProvider + Send + Sync + 'static) {
        self.inner.as_ref()
    }
}

impl<P> From<Arc<P>> for SharedSourceProvider
where
    P: SourceProvider + Send + Sync + 'static,
{
    fn from(provider: Arc<P>) -> Self {
        Self::new(provider)
    }
}

impl From<Arc<dyn SourceProvider + Send + Sync + 'static>> for SharedSourceProvider {
    fn from(provider: Arc<dyn SourceProvider + Send + Sync + 'static>) -> Self {
        Self::from_arc(provider)
    }
}

impl fmt::Debug for SharedSourceProvider {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("SharedSourceProvider(..)")
    }
}

impl SourceProvider for SharedSourceProvider {
    fn get(&self, id: SourceId) -> Option<Source<'_>> {
        self.inner.get(id)
    }

    fn line_column(&self, id: SourceId, offset: u32) -> Option<LineColumn> {
        self.inner.line_column(id, offset)
    }

    fn find_by_name(&self, display_name: &str) -> Option<SourceId> {
        self.inner.find_by_name(display_name)
    }

    fn offset_of(&self, id: SourceId, position: LineColumn) -> Option<u32> {
        self.inner.offset_of(id, position)
    }
}

impl<P> SourceProvider for &P
where
    P: SourceProvider + ?Sized,
{
    fn get(&self, id: SourceId) -> Option<Source<'_>> {
        (**self).get(id)
    }

    fn line_column(&self, id: SourceId, offset: u32) -> Option<LineColumn> {
        (**self).line_column(id, offset)
    }

    fn find_by_name(&self, display_name: &str) -> Option<SourceId> {
        (**self).find_by_name(display_name)
    }

    fn offset_of(&self, id: SourceId, position: LineColumn) -> Option<u32> {
        (**self).offset_of(id, position)
    }
}

impl<P> SourceProvider for &mut P
where
    P: SourceProvider + ?Sized,
{
    fn get(&self, id: SourceId) -> Option<Source<'_>> {
        (**self).get(id)
    }

    fn line_column(&self, id: SourceId, offset: u32) -> Option<LineColumn> {
        (**self).line_column(id, offset)
    }

    fn find_by_name(&self, display_name: &str) -> Option<SourceId> {
        (**self).find_by_name(display_name)
    }

    fn offset_of(&self, id: SourceId, position: LineColumn) -> Option<u32> {
        (**self).offset_of(id, position)
    }
}

impl<P> SourceProvider for Box<P>
where
    P: SourceProvider + ?Sized,
{
    fn get(&self, id: SourceId) -> Option<Source<'_>> {
        (**self).get(id)
    }

    fn line_column(&self, id: SourceId, offset: u32) -> Option<LineColumn> {
        (**self).line_column(id, offset)
    }

    fn find_by_name(&self, display_name: &str) -> Option<SourceId> {
        (**self).find_by_name(display_name)
    }

    fn offset_of(&self, id: SourceId, position: LineColumn) -> Option<u32> {
        (**self).offset_of(id, position)
    }
}

impl<P> SourceProvider for Arc<P>
where
    P: SourceProvider + ?Sized,
{
    fn get(&self, id: SourceId) -> Option<Source<'_>> {
        (**self).get(id)
    }

    fn line_column(&self, id: SourceId, offset: u32) -> Option<LineColumn> {
        (**self).line_column(id, offset)
    }

    fn find_by_name(&self, display_name: &str) -> Option<SourceId> {
        (**self).find_by_name(display_name)
    }

    fn offset_of(&self, id: SourceId, position: LineColumn) -> Option<u32> {
        (**self).offset_of(id, position)
    }
}

#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct EmptySourceProvider;

pub(crate) static EMPTY_SOURCE_PROVIDER: EmptySourceProvider = EmptySourceProvider;

impl SourceProvider for EmptySourceProvider {
    fn get(&self, _id: SourceId) -> Option<Source<'_>> {
        None
    }

    fn line_column(&self, _id: SourceId, _offset: u32) -> Option<LineColumn> {
        None
    }
}

/// Resolves session and diagnostic-attached source universes without fallback.
#[derive(Clone, Copy)]
pub struct LayeredSourceProvider<'a> {
    session: &'a dyn SourceProvider,
    attached: Option<&'a dyn SourceProvider>,
}

impl<'a> LayeredSourceProvider<'a> {
    pub fn new(session: &'a dyn SourceProvider, attached: Option<&'a dyn SourceProvider>) -> Self {
        Self { session, attached }
    }

    pub fn session_only(session: &'a dyn SourceProvider) -> Self {
        Self::new(session, None)
    }

    pub fn resolve(&self, key: SourceKey) -> Option<ResolvedSource<'a>> {
        self.resolve_checked(key).ok()
    }

    pub(crate) fn resolve_checked(
        &self,
        key: SourceKey,
    ) -> Result<ResolvedSource<'a>, SourceResolveError> {
        let (provider, id) = self.provider(key).ok_or(SourceResolveError::Missing)?;
        let source = provider.get(id).ok_or(SourceResolveError::Missing)?;
        if source.id != id {
            return Err(SourceResolveError::IdMismatch {
                returned: source.id,
            });
        }
        if let Some(text) = source.text {
            let text_len =
                u32::try_from(text.len()).map_err(|_| SourceResolveError::ByteLengthMismatch {
                    declared: source.byte_len,
                    actual: text.len(),
                })?;
            if source.byte_len != text_len {
                return Err(SourceResolveError::ByteLengthMismatch {
                    declared: source.byte_len,
                    actual: text.len(),
                });
            }
        }
        Ok(ResolvedSource { key, source })
    }

    pub fn line_column(&self, key: SourceKey, offset: u32) -> Option<LineColumn> {
        let (provider, id) = self.provider(key)?;
        let resolved = self.resolve(key)?;
        if offset > resolved.source.byte_len {
            return None;
        }
        let location = provider.line_column(id, offset)?;
        if let Some(text) = resolved.source.text
            && line_column_from_text(text, offset) != Some(location)
        {
            return None;
        }
        Some(location)
    }

    fn provider(&self, key: SourceKey) -> Option<(&'a dyn SourceProvider, SourceId)> {
        match key {
            SourceKey::Session(id) => Some((self.session, id)),
            SourceKey::Attached(id) => self.attached.map(|provider| (provider, id)),
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SourceResolveError {
    Missing,
    IdMismatch { returned: SourceId },
    ByteLengthMismatch { declared: u32, actual: usize },
}

#[derive(Debug)]
struct SourceRecord {
    id: SourceId,
    display_name: String,
    text: String,
    revision: Option<SourceRevision>,
    line_starts: Box<[u32]>,
}

/// Built-in owned source provider for `no_std + alloc` applications.
#[derive(Clone, Debug)]
pub struct SourceMap {
    namespace: SourceNamespace,
    next_local: Option<u32>,
    sources: Vec<Arc<SourceRecord>>,
}

impl SourceMap {
    pub const fn new(namespace: SourceNamespace) -> Self {
        assert!(!namespace.is_unknown(), "source maps require a known namespace");
        Self {
            namespace,
            next_local: Some(0),
            sources: Vec::new(),
        }
    }

    pub const fn namespace(&self) -> SourceNamespace {
        self.namespace
    }

    pub fn len(&self) -> usize {
        self.sources.len()
    }

    pub fn is_empty(&self) -> bool {
        self.sources.is_empty()
    }

    /// Returns the most recently inserted source with the given display name.
    pub fn find_by_name(&self, display_name: &str) -> Option<SourceId> {
        self.sources
            .iter()
            .rev()
            .find(|source| source.display_name == display_name)
            .map(|source| source.id)
    }

    pub fn validate_source_len(bytes: usize) -> Result<u32, SourceMapError> {
        u32::try_from(bytes).map_err(|_| SourceMapError::SourceTooLarge { bytes })
    }

    pub fn insert(
        &mut self,
        display_name: impl Into<String>,
        text: impl Into<String>,
        revision: Option<SourceRevision>,
    ) -> Result<SourceId, SourceMapError> {
        let text = text.into();
        let byte_len = Self::validate_source_len(text.len())?;
        let local = self.next_local.ok_or(SourceMapError::SourceIdExhausted)?;
        let id = SourceId::new(self.namespace, local);
        let line_starts = line_starts(&text, byte_len);
        let next_local = local.checked_add(1);

        self.sources.push(Arc::new(SourceRecord {
            id,
            display_name: display_name.into(),
            text,
            revision,
            line_starts,
        }));
        self.next_local = next_local;
        Ok(id)
    }

    /// Returns the byte offset of `position` in source `id`, the inverse of `line_column`.
    ///
    /// A column one past the last character of a line addresses the line end.
    pub fn offset_of(&self, id: SourceId, position: LineColumn) -> Option<u32> {
        let record = self.record(id)?;
        let line_start = *record.line_starts.get(position.line_index().to_usize())?;
        let line_start = usize::try_from(line_start).ok()?;
        let rest = record.text.get(line_start..)?;
        let line = rest.split_once('\n').map_or(rest, |(line, _)| line);
        let column = position.column_index().to_usize();
        let in_line = match line.char_indices().nth(column) {
            Some((index, _)) => index,
            None if line.chars().count() == column => line.len(),
            None => return None,
        };
        u32::try_from(line_start + in_line).ok()
    }

    /// Reads `path` and registers it under its path as display name.
    ///
    /// A path that is already registered returns the existing id without re-reading the file.
    #[cfg(feature = "std")]
    pub fn load_file(&mut self, path: impl AsRef<std::path::Path>) -> std::io::Result<SourceId> {
        let path = path.as_ref();
        let display_name = path.to_string_lossy().into_owned();
        if let Some(id) = self.find_by_name(&display_name) {
            return Ok(id);
        }
        let text = std::fs::read_to_string(path)?;
        self.insert(display_name, text, None).map_err(std::io::Error::other)
    }

    fn record(&self, id: SourceId) -> Option<&SourceRecord> {
        if id.namespace() != self.namespace {
            return None;
        }
        let index = usize::try_from(id.local()).ok()?;
        let record = self.sources.get(index)?.as_ref();
        (record.id == id).then_some(record)
    }

    #[cfg(test)]
    pub(crate) fn set_next_local_for_test(&mut self, next: Option<u32>) {
        self.next_local = next;
    }
}

impl SourceProvider for SourceMap {
    fn get(&self, id: SourceId) -> Option<Source<'_>> {
        let record = self.record(id)?;
        Some(Source {
            id: record.id,
            display_name: &record.display_name,
            byte_len: u32::try_from(record.text.len()).ok()?,
            text: Some(&record.text),
            revision: record.revision,
        })
    }

    fn line_column(&self, id: SourceId, offset: u32) -> Option<LineColumn> {
        let record = self.record(id)?;
        let offset = usize::try_from(offset).ok()?;
        if offset > record.text.len() || !record.text.is_char_boundary(offset) {
            return None;
        }
        let offset_u32 = u32::try_from(offset).ok()?;
        let line_index = record
            .line_starts
            .partition_point(|line_start| *line_start <= offset_u32)
            .checked_sub(1)?;
        let line_start = usize::try_from(record.line_starts[line_index]).ok()?;
        let line = u32::try_from(line_index).ok()?.checked_add(1)?;
        let column = u32::try_from(record.text[line_start..offset].chars().count())
            .ok()?
            .checked_add(1)?;
        LineColumn::new(line, column)
    }

    fn find_by_name(&self, display_name: &str) -> Option<SourceId> {
        SourceMap::find_by_name(self, display_name)
    }

    fn offset_of(&self, id: SourceId, position: LineColumn) -> Option<u32> {
        SourceMap::offset_of(self, id, position)
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SourceMapError {
    SourceTooLarge { bytes: usize },
    SourceIdExhausted,
}

impl fmt::Display for SourceMapError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::SourceTooLarge { bytes } => {
                write!(formatter, "source length {bytes} exceeds u32::MAX")
            }
            Self::SourceIdExhausted => formatter.write_str("source ID space is exhausted"),
        }
    }
}

impl core::error::Error for SourceMapError {}

fn line_starts(text: &str, byte_len: u32) -> Box<[u32]> {
    let mut starts = Vec::new();
    starts.push(0);
    for (index, byte) in text.bytes().enumerate() {
        if byte == b'\n' {
            let next = index + 1;
            if next <= usize::try_from(byte_len).unwrap_or(usize::MAX) {
                starts.push(u32::try_from(next).expect("validated source length fits in u32"));
            }
        }
    }
    starts.into_boxed_slice()
}

fn line_column_from_text(text: &str, offset: u32) -> Option<LineColumn> {
    let offset = usize::try_from(offset).ok()?;
    if offset > text.len() || !text.is_char_boundary(offset) {
        return None;
    }
    let prefix = &text[..offset];
    let line = u32::try_from(prefix.bytes().filter(|byte| *byte == b'\n').count())
        .ok()?
        .checked_add(1)?;
    let line_start = prefix.rfind('\n').map_or(0, |index| index + 1);
    let column = u32::try_from(text[line_start..offset].chars().count()).ok()?.checked_add(1)?;
    LineColumn::new(line, column)
}

#[cfg(test)]
mod tests {
    use alloc::{format, string::ToString};

    use super::*;

    #[test]
    fn offset_of_inverts_line_column() {
        let namespace = SourceNamespace::new(core::num::NonZeroU32::new(7).unwrap());
        let mut map = SourceMap::new(namespace);
        let id = map.insert("a.runic", "ab\ncd\u{e9}\n", None).unwrap();
        // Every char boundary, including each line end and the end of input.
        for offset in [0u32, 1, 2, 3, 4, 5, 7, 8] {
            let position = map.line_column(id, offset).unwrap();
            assert_eq!(map.offset_of(id, position), Some(offset), "offset {offset}");
        }
        assert_eq!(map.offset_of(id, LineColumn::new(9, 1).unwrap()), None);
        assert_eq!(map.offset_of(id, LineColumn::new(1, 9).unwrap()), None);
    }

    #[test]
    fn offset_of_is_forwarded_by_provider_wrappers() {
        let namespace = SourceNamespace::new(core::num::NonZeroU32::new(9).unwrap());
        let mut map = SourceMap::new(namespace);
        let id = map.insert("a.runic", "ab\ncd\n", None).unwrap();
        let position = LineColumn::new(2, 1).unwrap();
        let expected = Some(3);
        assert_eq!(map.offset_of(id, position), expected);

        let dynamic: &dyn SourceProvider = &map;
        assert_eq!(dynamic.offset_of(id, position), expected, "&dyn SourceProvider");
        assert_eq!(SourceProvider::offset_of(&&map, id, position), expected, "&P");
        assert_eq!(SourceProvider::offset_of(&&mut map, id, position), expected, "&mut P");

        let arc = Arc::new(map);
        assert_eq!(SourceProvider::offset_of(&arc, id, position), expected, "Arc<P>");
        let dynamic: &dyn SourceProvider = &arc;
        assert_eq!(dynamic.offset_of(id, position), expected, "&dyn over Arc<P>");
        let shared = SharedSourceProvider::new(arc.clone());
        assert_eq!(shared.offset_of(id, position), expected, "SharedSourceProvider");
        let boxed: Box<dyn SourceProvider> = Box::new(arc);
        assert_eq!(SourceProvider::offset_of(&boxed, id, position), expected, "Box<P>");
    }

    #[cfg(feature = "std")]
    #[test]
    fn load_file_registers_once_per_path() {
        let dir = std::env::temp_dir().join(format!("runic-diag-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("m.runic");
        std::fs::write(&path, "hello\n").unwrap();
        let namespace = SourceNamespace::new(core::num::NonZeroU32::new(8).unwrap());
        let mut map = SourceMap::new(namespace);
        let first = map.load_file(&path).unwrap();
        let second = map.load_file(&path).unwrap();
        assert_eq!(first, second);
        assert_eq!(map.get(first).unwrap().text, Some("hello\n"));
        assert!(map.load_file(dir.join("missing.runic")).is_err());
        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn ranges_and_spans_preserve_checked_provenance() {
        assert!(core::mem::size_of::<SourceSpan>() < 32);
        assert_eq!(TextRange::new(4, 3), Err(TextRangeError::Reversed { start: 4, end: 3 }));
        let empty = TextRange::new(u32::MAX, u32::MAX).unwrap();
        assert!(empty.is_empty());

        if usize::BITS > u32::BITS {
            let too_large = usize::try_from(u32::MAX).unwrap() + 1;
            assert_eq!(
                TextRange::try_from_usize(too_large, too_large),
                Err(TextRangeError::OffsetTooLarge { offset: too_large })
            );
        }

        let id = SourceId::new(SourceNamespace::new_unchecked(7), 9);
        let session = SourceSpan::session(id, empty).with_revision(SourceRevision(11));
        let attached = SourceSpan::attached(id, empty);
        assert_eq!(session.source(), SourceKey::Session(id));
        assert_eq!(session.revision(), Some(SourceRevision(11)));
        assert_eq!(session.range(), empty);
        assert_eq!(attached.source(), SourceKey::Attached(id));
        assert_ne!(session.source(), attached.source());
    }

    #[test]
    fn source_map_ids_are_namespaced_monotonic_and_failed_insertions_do_not_mutate() {
        let mut first = SourceMap::new(SourceNamespace::new_unchecked(1));
        let mut second = SourceMap::new(SourceNamespace::new_unchecked(2));
        let first_id = first.insert("a", "text", None).unwrap();
        let next_id = first.insert("b", "", Some(SourceRevision(2))).unwrap();
        let second_id = second.insert("a", "text", None).unwrap();
        assert_eq!(first_id.local(), 0);
        assert_eq!(next_id.local(), 1);
        assert_ne!(first_id, second_id);
        assert_eq!(first.len(), 2);

        first.set_next_local_for_test(None);
        assert_eq!(first.insert("never", "inserted", None), Err(SourceMapError::SourceIdExhausted));
        assert_eq!(first.len(), 2);

        if usize::BITS > u32::BITS {
            let too_large = usize::try_from(u32::MAX).unwrap() + 1;
            assert_eq!(
                SourceMap::validate_source_len(too_large),
                Err(SourceMapError::SourceTooLarge { bytes: too_large })
            );
        }
    }

    #[test]
    fn source_map_clone_is_an_immutable_snapshot() {
        let namespace = SourceNamespace::new_unchecked(7);
        let mut sources = SourceMap::new(namespace);
        let first = sources.insert("module.runic", "begin\nend", None).unwrap();
        let snapshot = sources.clone();

        let latest = sources.insert("module.runic", "begin\nnop\nend", None).unwrap();

        assert_eq!(snapshot.len(), 1);
        assert_eq!(snapshot.find_by_name("module.runic"), Some(first));
        assert_eq!(sources.find_by_name("module.runic"), Some(latest));
        assert!(snapshot.get(latest).is_none());
        assert_eq!(snapshot.get(first).unwrap().text, Some("begin\nend"));
    }

    #[test]
    #[should_panic(expected = "source maps require a known namespace")]
    fn source_map_rejects_the_reserved_unknown_namespace() {
        let _ = SourceMap::new(SourceNamespace::UNKNOWN);
    }

    #[test]
    fn shared_source_provider_delegates_and_hides_implementation_details() {
        let mut sources = SourceMap::new(SourceNamespace::new_unchecked(8));
        let id = sources.insert("module.runic", "begin\nend", None).unwrap();
        let shared = SharedSourceProvider::from(Arc::new(sources));

        assert_eq!(shared.get(id).unwrap().display_name, "module.runic");
        assert_eq!(shared.line_column(id, 6), LineColumn::new(2, 1));
        assert_eq!(shared.find_by_name("module.runic"), Some(id));
        assert_eq!(format!("{shared:?}"), "SharedSourceProvider(..)");

        let erased: Arc<dyn SourceProvider + Send + Sync> = Arc::new(shared);
        let erased = SharedSourceProvider::from(erased);
        assert_eq!(erased.find_by_name("module.runic"), Some(id));
    }

    #[test]
    fn line_columns_are_one_based_scalar_coordinates_with_lf_line_breaks() {
        let mut sources = SourceMap::new(SourceNamespace::new_unchecked(1));
        let id = sources.insert("unicode", "a😀e\u{301}\r\nz\n", None).unwrap();
        let cases = [
            (0, (1, 1)),
            (1, (1, 2)),
            (5, (1, 3)),
            (6, (1, 4)),
            (8, (1, 5)),
            (9, (1, 6)),
            (10, (2, 1)),
            (11, (2, 2)),
            (12, (3, 1)),
        ];
        for (offset, (line, column)) in cases {
            let line = LineNumber::new(line).unwrap();
            let column = ColumnNumber::new(column).unwrap();
            let location = sources.line_column(id, offset).unwrap();
            assert_eq!((location.line(), location.column()), (line, column));
        }
        assert_eq!(sources.line_column(id, 2), None);
        assert_eq!(sources.line_column(id, 13), None);
        assert_eq!(LineColumn::new(0, 1), None);
        assert_eq!(LineColumn::new(1, 0), None);
    }

    #[test]
    fn layered_resolution_never_collapses_provenance_or_accepts_bad_providers() {
        let namespace = SourceNamespace::new_unchecked(5);
        let mut session = SourceMap::new(namespace);
        let mut attached = SourceMap::new(namespace);
        let session_id = session.insert("same", "session", None).unwrap();
        let attached_id = attached.insert("same", "attached", None).unwrap();
        assert_eq!(session_id, attached_id);

        let layered = LayeredSourceProvider::new(&session, Some(&attached));
        assert_eq!(
            layered.resolve(SourceKey::Session(session_id)).unwrap().source.text,
            Some("session")
        );
        assert_eq!(
            layered.resolve(SourceKey::Attached(attached_id)).unwrap().source.text,
            Some("attached")
        );
        assert!(
            LayeredSourceProvider::session_only(&session)
                .resolve(SourceKey::Attached(attached_id))
                .is_none()
        );

        struct BadProvider {
            requested: SourceId,
            returned: SourceId,
            bad_len: bool,
            bad_location: bool,
        }

        impl SourceProvider for BadProvider {
            fn get(&self, id: SourceId) -> Option<Source<'_>> {
                (id == self.requested).then_some(Source {
                    id: self.returned,
                    display_name: "bad",
                    byte_len: if self.bad_len { 99 } else { 1 },
                    text: Some("x"),
                    revision: None,
                })
            }

            fn line_column(&self, _id: SourceId, _offset: u32) -> Option<LineColumn> {
                LineColumn::new(1, u32::from(self.bad_location) + 1)
            }
        }

        let other = SourceId::new(namespace, 99);
        let wrong_id = BadProvider {
            requested: session_id,
            returned: other,
            bad_len: false,
            bad_location: false,
        };
        assert!(
            LayeredSourceProvider::session_only(&wrong_id)
                .resolve(SourceKey::Session(session_id))
                .is_none()
        );
        let wrong_len = BadProvider {
            requested: session_id,
            returned: session_id,
            bad_len: true,
            bad_location: false,
        };
        assert!(
            LayeredSourceProvider::session_only(&wrong_len)
                .resolve(SourceKey::Session(session_id))
                .is_none()
        );
        let wrong_location = BadProvider {
            requested: session_id,
            returned: session_id,
            bad_len: false,
            bad_location: true,
        };
        let wrong_location = LayeredSourceProvider::session_only(&wrong_location);
        assert_eq!(wrong_location.line_column(SourceKey::Session(session_id), 0), None);
        assert_eq!(wrong_location.line_column(SourceKey::Session(session_id), 2), None);
    }

    #[test]
    fn source_map_is_transport_safe() {
        fn require<T: Send + Sync>() {}
        require::<SourceMap>();

        let empty_name = "".to_string();
        let mut sources = SourceMap::new(SourceNamespace::new_unchecked(1));
        let id = sources.insert(empty_name, "", None).unwrap();
        let source = sources.get(id).unwrap();
        assert_eq!(source.display_name, "");
        assert_eq!(source.byte_len, 0);
        assert_eq!(sources.line_column(id, 0), LineColumn::new(1, 1));
    }
}