Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: d540f23ceec055152d4018de8fa04a6dda54228e (plain)
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
// Read from xmlo and immediately lower to ASG IR
//
//  Copyright (C) 2014-2020 Ryan Specialty Group, LLC.
//
//  This file is part of TAME.
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

use super::reader::{XmloError, XmloEvent, XmloResult};
use crate::ir::asg::{
    Asg, AsgError, IdentKind, IdentKindError, IdentObjectState, IndexType,
    ObjectRef, Source,
};
use crate::sym::Symbol;
use std::collections::HashSet;
use std::convert::TryInto;
use std::error::Error;
use std::fmt::Display;
use std::hash::BuildHasher;

pub type Result<'i, S, Ix> =
    std::result::Result<AsgBuilderState<'i, S, Ix>, AsgBuilderError<Ix>>;

/// Builder state between imports.
///
/// [`AsgBuilder::import_xmlo`] is designed to return its state after having
///   been invoked,
///     which contains in part a set of relative package import paths that
///     were discovered during processing (`found`).
/// This state can then be fed back to `import_xmlo` when recursively
///   importing those packages.
///
/// The values [`name`](AsgBuilderState::name) and
///   [`relroot`](AsgBuilderState::relroot) are set only once when the first
///   package is processed.
/// A package is considered to be the first if `name` is [`None`].
///
/// [`roots`](AsgBuilderState::roots) is added to as certain identifiers are
///   discovered that should be used as starting points for a topological
///   sort.
/// This is used by the linker to only include dependencies that are
///   actually used by a particular program.
#[derive(Debug, Default)]
pub struct AsgBuilderState<'i, S, Ix>
where
    S: BuildHasher,
    Ix: IndexType,
{
    /// Discovered roots.
    ///
    /// Roots represent starting points for a topological sort of the
    ///   graph.
    /// They are meaningful to the linker.
    pub roots: Vec<ObjectRef<Ix>>,

    /// Relative paths to imported packages that have been discovered.
    ///
    /// The caller will use these to perform recursive loads.
    /// This is contained within an [`Option`] so that the caller can `take`
    ///   ownership over its contents.
    ///
    /// See [`AsgBuilder::import_xmlo`] for behavior when this value is
    ///   [`None`].
    pub found: Option<HashSet<&'i str, S>>,

    /// Program name once discovered.
    ///
    /// This will be set by the first package encountered.
    pub name: Option<&'i Symbol<'i>>,

    /// Relative path to project root once discovered.
    ///
    /// This will be set by the first package encountered.
    pub relroot: Option<String>,
}

impl<'i, S, Ix> AsgBuilderState<'i, S, Ix>
where
    S: BuildHasher,
    Ix: IndexType,
{
    /// Whether this is the first discovered package.
    ///
    /// This is true if [`name`](AsgBuilderState::name) is [`None`].
    fn is_first(&self) -> bool {
        self.name.is_none()
    }
}

/// Populate [`Asg`] with data derived from `xmlo` files.
///
/// This accepts anything capable of iterating over [`XmloResult`].
///
/// For more information on what data are processed,
///   see [`AsgBuilderState`].
pub trait AsgBuilder<'i, O, S, Ix>
where
    O: IdentObjectState<'i, O>,
    S: BuildHasher,
    Ix: IndexType,
{
    /// Import [`XmloResult`]s into an [`Asg`].
    ///
    /// This is an IR lowering operation.
    /// The [`XmloResult`] produces data gleaned from
    ///   [`legacyir`](crate::ir::legacyir),
    ///     and this process lowers it into the IR [`asg`](crate::ir::asg).
    ///
    /// Each call to this method augments the provided [`AsgBuilderState`];
    ///   see its documentation for more information.
    /// Its initial value can be provided as [`Default::default`].
    fn import_xmlo(
        &mut self,
        xmlo: impl Iterator<Item = XmloResult<XmloEvent<'i>>>,
        state: AsgBuilderState<'i, S, Ix>,
    ) -> Result<'i, S, Ix>;
}

impl<'i, O, S, Ix, G> AsgBuilder<'i, O, S, Ix> for G
where
    O: IdentObjectState<'i, O>,
    S: BuildHasher + Default,
    Ix: IndexType,
    G: Asg<'i, O, Ix>,
{
    fn import_xmlo(
        &mut self,
        mut xmlo: impl Iterator<Item = XmloResult<XmloEvent<'i>>>,
        mut state: AsgBuilderState<'i, S, Ix>,
    ) -> Result<'i, S, Ix> {
        let mut elig = None;
        let first = state.is_first();
        let found = state.found.get_or_insert(Default::default());

        while let Some(ev) = xmlo.next() {
            match ev? {
                XmloEvent::Package(attrs) => {
                    if first {
                        state.name = attrs.name;
                        state.relroot = attrs.relroot;
                    }

                    elig = attrs.elig;
                }

                XmloEvent::SymDeps(sym, deps) => {
                    // Maps should not pull in symbols since we may end up
                    // mapping to params that are never actually used.
                    // TODO: Can these just be removed from the xmlo files
                    //   rather than adding exceptions?
                    // TODO: No string comparison.
                    if !sym.starts_with(":map:") {
                        for dep_sym in deps {
                            self.add_dep_lookup(sym, dep_sym);
                        }
                    }
                }

                XmloEvent::SymDecl(sym, attrs) => {
                    if let Some(sym_src) = attrs.src {
                        found.insert(sym_src);
                    } else {
                        let extern_ = attrs.extern_;
                        let kindval = (&attrs).try_into()?;

                        let mut src: Source = attrs.into();

                        // Existing convention is to omit @src of local package
                        // (in this case, the program being linked)
                        if first {
                            src.pkg_name = None;
                        }

                        let link_root = matches!(
                            kindval,
                            IdentKind::Meta
                                | IdentKind::Map
                                | IdentKind::RetMap
                        );

                        if extern_ {
                            self.declare_extern(sym, kindval, src)?;
                        } else {
                            let node = self.declare(sym, kindval, src)?;

                            if link_root {
                                state.roots.push(node);
                            }
                        }
                    }
                }

                XmloEvent::Fragment(sym, text) => {
                    let frag = self.lookup(sym).ok_or(
                        AsgBuilderError::MissingFragmentIdent(sym.to_string()),
                    )?;

                    self.set_fragment(frag, text)?;
                }

                // We don't need to read any further than the end of the
                // header (symtable, sym-deps, fragments).  Note that this
                // may change in the future, in which case this
                // responsibility can be delegated to the linker (to produce
                // an `Iterator` that stops at EOH).
                XmloEvent::Eoh => break,
            }
        }

        if let Some(elig_sym) = elig {
            state
                .roots
                .push(self.lookup(elig_sym).ok_or(
                    AsgBuilderError::BadEligRef(elig_sym.to_string()),
                )?);
        }

        Ok(state)
    }
}

/// Error populating graph with [`XmloResult`]-derived data.
#[derive(Debug, PartialEq)]
pub enum AsgBuilderError<Ix: IndexType> {
    /// Error with the source `xmlo` file.
    XmloError(XmloError),

    /// Error parsing into [`IdentKind`].
    IdentKindError(IdentKindError),

    /// [`Asg`] operation error.
    AsgError(AsgError<Ix>),

    /// Fragment encountered for an unknown identifier.
    MissingFragmentIdent(String),

    /// Eligibility classification references unknown identifier.
    ///
    /// This is generated by the compiler and so should never happen.
    /// (That's not to say that it won't, but it shouldn't.)
    BadEligRef(String),
}

impl<Ix: IndexType> Display for AsgBuilderError<Ix> {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::XmloError(e) => e.fmt(fmt),
            Self::IdentKindError(e) => e.fmt(fmt),
            Self::AsgError(e) => e.fmt(fmt),

            Self::MissingFragmentIdent(name) => write!(
                fmt,
                "encountered fragment for unknown identifier `{}`",
                name,
            ),

            Self::BadEligRef(name) => write!(
                fmt,
                "internal error: package elig references nonexistant symbol `{}`",
                name,
            ),
        }
    }
}

impl<Ix: IndexType> From<XmloError> for AsgBuilderError<Ix> {
    fn from(src: XmloError) -> Self {
        Self::XmloError(src)
    }
}

impl<Ix: IndexType> From<IdentKindError> for AsgBuilderError<Ix> {
    fn from(src: IdentKindError) -> Self {
        Self::IdentKindError(src)
    }
}

impl<Ix: IndexType> From<AsgError<Ix>> for AsgBuilderError<Ix> {
    fn from(src: AsgError<Ix>) -> Self {
        Self::AsgError(src)
    }
}

impl<Ix: IndexType> Error for AsgBuilderError<Ix> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::XmloError(e) => Some(e),
            Self::IdentKindError(e) => Some(e),
            Self::AsgError(e) => Some(e),
            _ => None,
        }
    }
}

// These tests are coupled with BaseAsg, which is not ideal.
#[cfg(test)]
mod test {
    use super::*;
    use crate::ir::asg::{DefaultAsg, FragmentText, IdentObject};
    use crate::ir::legacyir::{PackageAttrs, SymAttrs, SymType};
    use crate::sym::SymbolIndex;
    use std::collections::hash_map::RandomState;

    type SutIx = u8;
    type Sut<'i> = DefaultAsg<'i, IdentObject<'i>, SutIx>;
    type SutState<'i> = AsgBuilderState<'i, RandomState, SutIx>;

    #[test]
    fn gets_data_from_package_event() {
        let mut sut = Sut::new();

        let name = symbol_dummy!(1, "name");
        let relroot = "some/path".to_string();

        let evs = vec![Ok(XmloEvent::Package(PackageAttrs {
            name: Some(&name),
            relroot: Some(relroot.clone()),
            ..Default::default()
        }))];

        let state = sut
            .import_xmlo(evs.into_iter(), SutState::default())
            .expect("parsing of proper PackageAttrs must succeed");

        assert_eq!(Some(&name), state.name);
        assert_eq!(Some(relroot), state.relroot);
    }

    #[test]
    fn xmlo_error_returned() {
        let mut sut = Sut::new();

        let evs = vec![Err(XmloError::UnexpectedRoot)];
        let result = sut.import_xmlo(evs.into_iter(), SutState::default());

        assert_eq!(
            AsgBuilderError::XmloError(XmloError::UnexpectedRoot),
            result.expect_err("expected error to be proxied"),
        );
    }

    #[test]
    fn adds_elig_as_root() {
        let mut sut = Sut::new();
        let elig_sym = symbol_dummy!(1, "elig");

        // The symbol must be on the graph, or it'll fail.
        let elig_node = sut
            .declare(&elig_sym, IdentKind::Meta, Default::default())
            .unwrap();

        let evs = vec![Ok(XmloEvent::Package(PackageAttrs {
            elig: Some(&elig_sym),
            ..Default::default()
        }))];

        let state = sut
            .import_xmlo(evs.into_iter(), SutState::default())
            .unwrap();

        assert!(state.roots.contains(&elig_node));
    }

    #[test]
    fn adds_sym_deps() {
        let mut sut = Sut::new();

        let sym_from = symbol_dummy!(1, "from");
        let sym_to1 = symbol_dummy!(2, "to1");
        let sym_to2 = symbol_dummy!(3, "to2");

        let evs =
            vec![Ok(XmloEvent::SymDeps(&sym_from, vec![&sym_to1, &sym_to2]))];

        let _ = sut
            .import_xmlo(evs.into_iter(), SutState::default())
            .expect("unexpected failure");

        let node_from = sut.lookup(&sym_from).expect("from node not added");
        let node_to1 = sut.lookup(&sym_to1).expect("to1 node not added");
        let node_to2 = sut.lookup(&sym_to2).expect("to2 node not added");

        assert!(sut.has_dep(node_from, node_to1));
        assert!(sut.has_dep(node_from, node_to2));
    }

    #[test]
    fn ignores_map_sym_deps() {
        let mut sut = Sut::new();

        let sym_from = symbol_dummy!(1, ":map:sym");
        let sym_to = symbol_dummy!(2, "to");

        let evs = vec![Ok(XmloEvent::SymDeps(&sym_from, vec![&sym_to]))];

        let _ = sut
            .import_xmlo(evs.into_iter(), SutState::default())
            .expect("unexpected failure");

        assert!(sut.lookup(&sym_from).is_none());
    }

    #[test]
    fn sym_decl_with_src_not_added_and_populates_found() {
        let mut sut = Sut::new();

        let sym = symbol_dummy!(1, "sym");
        let src_a = symbol_dummy!(2, "src_a");
        let src_b = symbol_dummy!(3, "src_b");

        let evs = vec![
            Ok(XmloEvent::SymDecl(
                &sym,
                SymAttrs {
                    src: Some(&src_a),
                    ..Default::default()
                },
            )),
            Ok(XmloEvent::SymDecl(
                &sym,
                SymAttrs {
                    src: Some(&src_b),
                    ..Default::default()
                },
            )),
        ];

        let mut founds = sut
            .import_xmlo(evs.into_iter(), SutState::default())
            .expect("unexpected failure")
            .found
            .unwrap()
            .iter()
            .map(|s| *s)
            .collect::<Vec<_>>();

        // Just to remove nondeterminism in case the iteration order happens
        // to change (we're using RandomState).
        founds.sort();

        assert_eq!(vec![&src_a as &str, &src_b as &str], founds);

        // Symbols with `src` set are external and should not be added to
        // the graph.
        assert!(sut.lookup(&sym).is_none());
    }

    #[test]
    fn sym_decl_added_to_graph() {
        let mut sut = Sut::new();

        let sym_extern = symbol_dummy!(1, "sym_extern");
        let sym_non_extern = symbol_dummy!(2, "sym_non_extern");
        let sym_map = symbol_dummy!(3, "sym_map");
        let sym_retmap = symbol_dummy!(4, "sym_retmap");
        let pkg_name = symbol_dummy!(5, "pkg name");

        let evs = vec![
            // Note that externs should not be recognized as roots even if
            // their type would be.
            Ok(XmloEvent::SymDecl(
                &sym_extern,
                SymAttrs {
                    pkg_name: Some(&pkg_name),
                    extern_: true,
                    ty: Some(SymType::Meta),
                    ..Default::default()
                },
            )),
            // These three will be roots
            Ok(XmloEvent::SymDecl(
                &sym_non_extern,
                SymAttrs {
                    pkg_name: Some(&pkg_name),
                    ty: Some(SymType::Meta),
                    ..Default::default()
                },
            )),
            Ok(XmloEvent::SymDecl(
                &sym_map,
                SymAttrs {
                    pkg_name: Some(&pkg_name),
                    ty: Some(SymType::Map),
                    ..Default::default()
                },
            )),
            Ok(XmloEvent::SymDecl(
                &sym_retmap,
                SymAttrs {
                    pkg_name: Some(&pkg_name),
                    ty: Some(SymType::RetMap),
                    ..Default::default()
                },
            )),
        ];

        let state = sut
            .import_xmlo(evs.into_iter(), SutState::default())
            .unwrap();

        // Both above symbols were local (no `src`).
        assert!(state.found.unwrap().len() == 0);

        assert_eq!(
            vec![
                sut.lookup(&sym_non_extern).unwrap(),
                sut.lookup(&sym_map).unwrap(),
                sut.lookup(&sym_retmap).unwrap(),
            ],
            state.roots
        );

        // Note that each of these will have their package names cleared
        // since this is considered to be the first package encountered.

        assert_eq!(
            &IdentObject::Extern(
                &sym_extern,
                IdentKind::Meta,
                Source {
                    pkg_name: None,
                    ..Default::default()
                },
            ),
            sut.get(sut.lookup(&sym_extern).unwrap()).unwrap(),
        );

        assert_eq!(
            &IdentObject::Ident(
                &sym_non_extern,
                IdentKind::Meta,
                Source {
                    pkg_name: None,
                    ..Default::default()
                },
            ),
            sut.get(sut.lookup(&sym_non_extern).unwrap()).unwrap(),
        );

        assert_eq!(
            &IdentObject::Ident(
                &sym_map,
                IdentKind::Map,
                Source {
                    pkg_name: None,
                    ..Default::default()
                },
            ),
            sut.get(sut.lookup(&sym_map).unwrap()).unwrap(),
        );

        assert_eq!(
            &IdentObject::Ident(
                &sym_retmap,
                IdentKind::RetMap,
                Source {
                    pkg_name: None,
                    ..Default::default()
                },
            ),
            sut.get(sut.lookup(&sym_retmap).unwrap()).unwrap(),
        );
    }

    // See above test, where pkg_name was cleared.
    #[test]
    fn sym_decl_pkg_name_retained_if_not_first() {
        let mut sut = Sut::new();

        let sym = symbol_dummy!(1, "sym");
        let pkg_name = symbol_dummy!(2, "pkg name");

        // This is all that's needed to not consider this to be the first
        // package, so that pkg_name is retained below.
        let state = AsgBuilderState::<'_, RandomState, SutIx> {
            name: Some(&pkg_name),
            ..Default::default()
        };

        let evs = vec![Ok(XmloEvent::SymDecl(
            &sym,
            SymAttrs {
                pkg_name: Some(&pkg_name),
                ty: Some(SymType::Meta),
                ..Default::default()
            },
        ))];

        let _ = sut.import_xmlo(evs.into_iter(), state).unwrap();

        assert_eq!(
            // `pkg_name` retained
            &IdentObject::Ident(
                &sym,
                IdentKind::Meta,
                Source {
                    pkg_name: Some(&pkg_name),
                    ..Default::default()
                },
            ),
            sut.get(sut.lookup(&sym).unwrap()).unwrap(),
        );
    }

    #[test]
    fn ident_kind_conversion_error_propagates() {
        let mut sut = Sut::new();

        let sym = symbol_dummy!(1, "sym");
        let bad_attrs = SymAttrs::default();

        let evs = vec![Ok(XmloEvent::SymDecl(&sym, bad_attrs))];

        let result = sut
            .import_xmlo(evs.into_iter(), SutState::default())
            .expect_err("expected IdentKind conversion error");

        assert!(matches!(result, AsgBuilderError::IdentKindError(_)));
    }

    #[test]
    fn declare_extern_error_propagates() {
        let mut sut = Sut::new();

        let sym = symbol_dummy!(1, "sym");

        let evs = vec![
            Ok(XmloEvent::SymDecl(
                &sym,
                SymAttrs {
                    extern_: true,
                    ty: Some(SymType::Meta),
                    ..Default::default()
                },
            )),
            // Incompatible
            Ok(XmloEvent::SymDecl(
                &sym,
                SymAttrs {
                    extern_: true,
                    ty: Some(SymType::Map),
                    ..Default::default()
                },
            )),
        ];

        let result = sut
            .import_xmlo(evs.into_iter(), SutState::default())
            .expect_err("expected extern error");

        assert!(matches!(result, AsgBuilderError::AsgError(_)));
    }

    #[test]
    fn declare_error_propagates() {
        let mut sut = Sut::new();

        let sym = symbol_dummy!(1, "sym");

        let evs = vec![
            Ok(XmloEvent::SymDecl(
                &sym,
                SymAttrs {
                    ty: Some(SymType::Meta),
                    ..Default::default()
                },
            )),
            // Redeclare
            Ok(XmloEvent::SymDecl(
                &sym,
                SymAttrs {
                    ty: Some(SymType::Meta),
                    ..Default::default()
                },
            )),
        ];

        let result = sut
            .import_xmlo(evs.into_iter(), SutState::default())
            .expect_err("expected declare error");

        assert!(matches!(result, AsgBuilderError::AsgError(_)));
    }

    #[test]
    fn sets_fragment() {
        let mut sut = Sut::new();

        let sym = symbol_dummy!(1, "sym");
        let frag = FragmentText::from("foo");

        let evs = vec![
            Ok(XmloEvent::SymDecl(
                &sym,
                SymAttrs {
                    ty: Some(SymType::Meta),
                    ..Default::default()
                },
            )),
            Ok(XmloEvent::Fragment(&sym, frag.clone())),
        ];

        let _ = sut
            .import_xmlo(evs.into_iter(), SutState::default())
            .unwrap();

        let node = sut
            .lookup(&sym)
            .expect("ident/fragment was not added to graph");

        assert_eq!(
            Some(&IdentObject::IdentFragment(
                &sym,
                IdentKind::Meta,
                Default::default(),
                frag
            )),
            sut.get(node),
        );
    }

    #[test]
    fn error_missing_ident_for_fragment() {
        let mut sut = Sut::new();

        let sym = symbol_dummy!(1, "sym");

        // Note: missing `SymDecl`.
        let evs = vec![Ok(XmloEvent::Fragment(&sym, "foo".into()))];

        let result = sut
            .import_xmlo(evs.into_iter(), SutState::default())
            .expect_err("expected error for fragment without ident");

        assert_eq!(
            AsgBuilderError::MissingFragmentIdent(sym.to_string()),
            result,
        );
    }

    #[test]
    fn fragment_error_propagates() {
        let mut sut = Sut::new();

        let sym = symbol_dummy!(1, "sym");
        let frag = FragmentText::from("foo");

        let evs = vec![
            Ok(XmloEvent::SymDecl(
                &sym,
                SymAttrs {
                    // Invalid fragment destination
                    extern_: true,
                    ty: Some(SymType::Meta),
                    ..Default::default()
                },
            )),
            Ok(XmloEvent::Fragment(&sym, frag.clone())),
        ];

        let result = sut
            .import_xmlo(evs.into_iter(), SutState::default())
            .expect_err("expected fragment set failure");

        assert!(matches!(
            result,
            AsgBuilderError::AsgError(AsgError::ObjectTransition(_))
        ));

        let node = sut
            .lookup(&sym)
            .expect("ident/fragment was not added to graph");

        // The identifier should not have been modified on failure.
        assert!(matches!(
            sut.get(node).unwrap(),
            IdentObject::Extern(_, _, _)
        ));
    }

    #[test]
    fn stops_at_eoh() {
        let mut sut = Sut::new();

        let pkg_name = symbol_dummy!(1, "pkg name");

        let evs = vec![
            // Stop here.
            Ok(XmloEvent::Eoh),
            // Shouldn't make it to this one.
            Ok(XmloEvent::Package(PackageAttrs {
                name: Some(&pkg_name),
                ..Default::default()
            })),
        ];

        let state = sut
            .import_xmlo(evs.into_iter(), SutState::default())
            .unwrap();

        // Should still be true because we didn't get to the `PackageAttrs`
        // event.
        assert!(state.is_first());
    }
}