summaryrefslogtreecommitdiff
path: root/src/errors/merge_error.rs
blob: a694b7110efb039ee324d036d07eaebcd49bbfef (plain) (blame)
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
use crate::*;

use ansi::*;
use log::error;


pub struct MergeError<'a> {
    pub resolver: &'a Resolver,
    /// A list of source units involved in a cycle.
    pub cyclic_unit_ids: Vec<usize>,
}

impl MergeError<'_> {
    pub fn report(&self) {
        error!("A cyclic dependency was found between the following libraries:");
        for id in &self.cyclic_unit_ids {
            if let Some(unit) = self.resolver.source_units.get(*id) {
                let path = &unit.source_unit.path();
                match unit.source_unit.name() {
                    Some(name) =>
                        eprintln!("{name}{NORMAL}{DIM} ({path}){NORMAL}"),
                    None =>
                        eprintln!("{path}"),
                };
                // Print each parent involved in the dependency cycle.
                for parent_id in &unit.parent_ids {
                    if !self.cyclic_unit_ids.contains(parent_id) { continue; }
                    if let Some(parent_unit) = self.resolver.source_units.get(*parent_id) {
                        let parent_path = &parent_unit.source_unit.path();
                        match parent_unit.source_unit.name() {
                            Some(parent_name) =>
                                eprintln!("  => {parent_name} {DIM}({parent_path}){NORMAL}"),
                            None =>
                                eprintln!("  => {parent_path}"),
                        };
                    }
                }
            }
        }
    }
}