From 2daf600d4713f6feacef795fac923f5826925efb Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Sat, 8 Mar 2025 13:52:45 +1300 Subject: Ignore source units that have already been included There is the possibility that the same source file could be discovered twice, if the same folder is included multiple times as a library folder or if symbolic links are used that link to already-included files. These duplicate source files are now ignored, to prevent erroneous 'duplicate definition' errors from being generated. --- src/resolver.rs | 11 +++++++++-- src/source_unit.rs | 11 +++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/resolver.rs b/src/resolver.rs index 8d3a16d..d02d80f 100644 --- a/src/resolver.rs +++ b/src/resolver.rs @@ -114,8 +114,15 @@ impl Resolver { } /// Add a set of source units that might contain definitions for unresolved symbols. - pub fn add_library_source_units(&mut self, mut source_units: Vec) { - self.library_source_units.append(&mut source_units); + pub fn add_library_source_units(&mut self, source_units: Vec) { + for source_unit in source_units { + // Discard source units that have already been included. + let in_included = self.source_units.iter().any(|s| s.source_unit == source_unit); + let in_library = self.library_source_units.iter().any(|s| *s == source_unit); + if !in_included && !in_library { + self.library_source_units.push(source_unit); + } + } } /// Attempt to resolve unresolved references with library source units. diff --git a/src/source_unit.rs b/src/source_unit.rs index 9c51ccb..28cc854 100644 --- a/src/source_unit.rs +++ b/src/source_unit.rs @@ -118,6 +118,17 @@ impl SourceUnit { } } +impl PartialEq for SourceUnit { + fn eq(&self, other: &SourceUnit) -> bool { + if let Ok(this_path) = self.main.path.canonicalize() { + if let Ok(other_path) = other.main.path.canonicalize() { + return this_path == other_path; + } + } + return false; + } +} + pub struct SourceFile { pub path: PathBuf, -- cgit v1.2.3-70-g09d2