summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Bridle <ben@derelict.engineering>2025-03-08 13:52:45 +1300
committerBen Bridle <ben@derelict.engineering>2025-03-08 14:22:53 +1300
commit2daf600d4713f6feacef795fac923f5826925efb (patch)
tree5caef62e4086cccbff04c64793fedc9473a56927
parent869d9b4b9c6f5d095084c851af33d8590bd3edee (diff)
downloadassembler-2daf600d4713f6feacef795fac923f5826925efb.zip
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.
-rw-r--r--src/resolver.rs11
-rw-r--r--src/source_unit.rs11
2 files changed, 20 insertions, 2 deletions
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<SourceUnit>) {
- self.library_source_units.append(&mut source_units);
+ pub fn add_library_source_units(&mut self, source_units: Vec<SourceUnit>) {
+ 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,