|
| 1 | +use std::{collections::HashSet, fmt::Write as _}; |
| 2 | + |
| 3 | +use common::{InputDb, diagnostics::CompleteDiagnostic, file::IngotFileKind}; |
| 4 | +use driver::{DriverDataBase, db::DiagnosticsCollection}; |
| 5 | +use url::Url; |
| 6 | + |
| 7 | +pub(crate) struct DependencyIssues<'db> { |
| 8 | + issues: Vec<DependencyIssue<'db>>, |
| 9 | +} |
| 10 | + |
| 11 | +enum DependencyIssue<'db> { |
| 12 | + MissingSourceFiles(Url), |
| 13 | + Diagnostics { |
| 14 | + url: Url, |
| 15 | + hir: DiagnosticsCollection<'db>, |
| 16 | + mir: Vec<CompleteDiagnostic>, |
| 17 | + }, |
| 18 | +} |
| 19 | + |
| 20 | +impl DependencyIssue<'_> { |
| 21 | + fn format(&self, db: &DriverDataBase, out: &mut String) { |
| 22 | + let url = match self { |
| 23 | + Self::MissingSourceFiles(url) | Self::Diagnostics { url, .. } => url, |
| 24 | + }; |
| 25 | + append_dependency_header(db, url, out); |
| 26 | + match self { |
| 27 | + DependencyIssue::MissingSourceFiles(url) => { |
| 28 | + let _ = writeln!(out, "Error: Could not find source files for ingot {url}"); |
| 29 | + } |
| 30 | + DependencyIssue::Diagnostics { hir, mir, .. } => { |
| 31 | + if !hir.is_empty() { |
| 32 | + out.push_str(&hir.format_diags(db)); |
| 33 | + } |
| 34 | + if !mir.is_empty() { |
| 35 | + out.push_str(&db.format_complete_diagnostics(mir)); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + if !out.ends_with('\n') { |
| 40 | + out.push('\n'); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl<'db> DependencyIssues<'db> { |
| 46 | + pub(crate) fn collect( |
| 47 | + db: &'db DriverDataBase, |
| 48 | + ingot_url: &Url, |
| 49 | + seen: &mut HashSet<Url>, |
| 50 | + ) -> Self { |
| 51 | + let mut issues = Vec::new(); |
| 52 | + for dependency_url in db.dependency_graph().dependency_urls(db, ingot_url) { |
| 53 | + if !seen.insert(dependency_url.clone()) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + let Some(ingot) = db.workspace().containing_ingot(db, dependency_url.clone()) else { |
| 57 | + continue; |
| 58 | + }; |
| 59 | + if !ingot_has_source_files(db, ingot) { |
| 60 | + issues.push(DependencyIssue::MissingSourceFiles(dependency_url)); |
| 61 | + continue; |
| 62 | + } |
| 63 | + let hir = db.run_on_ingot(ingot); |
| 64 | + let mir = if hir.has_errors(db) { |
| 65 | + Vec::new() |
| 66 | + } else { |
| 67 | + db.mir_diagnostics_for_ingot(ingot) |
| 68 | + }; |
| 69 | + if !hir.is_empty() || !mir.is_empty() { |
| 70 | + issues.push(DependencyIssue::Diagnostics { |
| 71 | + url: dependency_url, |
| 72 | + hir, |
| 73 | + mir, |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | + Self { issues } |
| 78 | + } |
| 79 | + |
| 80 | + pub(crate) fn is_empty(&self) -> bool { |
| 81 | + self.issues.is_empty() |
| 82 | + } |
| 83 | + |
| 84 | + pub(crate) fn message(&self) -> &'static str { |
| 85 | + if self.issues.len() == 1 { |
| 86 | + "Errors in dependency" |
| 87 | + } else { |
| 88 | + "Errors in dependencies" |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + pub(crate) fn format(&self, db: &DriverDataBase) -> String { |
| 93 | + let mut out = String::new(); |
| 94 | + let _ = writeln!(out, "Error: {}", self.message()); |
| 95 | + for issue in &self.issues { |
| 96 | + issue.format(db, &mut out); |
| 97 | + out.push('\n'); |
| 98 | + } |
| 99 | + out |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +fn ingot_has_source_files(db: &DriverDataBase, ingot: hir::Ingot<'_>) -> bool { |
| 104 | + ingot |
| 105 | + .files(db) |
| 106 | + .iter() |
| 107 | + .any(|(_, file)| matches!(file.kind(db), Some(IngotFileKind::Source))) |
| 108 | +} |
| 109 | + |
| 110 | +fn append_dependency_header(db: &DriverDataBase, dependency_url: &Url, out: &mut String) { |
| 111 | + let dependency = if let Some(ingot) = |
| 112 | + db.workspace().containing_ingot(db, dependency_url.clone()) |
| 113 | + && let Some(config) = ingot.config(db) |
| 114 | + { |
| 115 | + let name = config.metadata.name.as_deref().unwrap_or("unknown"); |
| 116 | + if let Some(version) = &config.metadata.version { |
| 117 | + format!("Dependency: {name} (version: {version})") |
| 118 | + } else { |
| 119 | + format!("Dependency: {name}") |
| 120 | + } |
| 121 | + } else { |
| 122 | + "Dependency: <unknown>".to_string() |
| 123 | + }; |
| 124 | + let _ = writeln!(out, "\n{dependency}\nURL: {dependency_url}\n"); |
| 125 | +} |
0 commit comments