Repair next_id and reject duplicate ids on load (#1050)

Loading a design raises next_id above the largest feature, component and occurrence id, so a missing or stale value can't hand a new item an id already in use, and duplicate ids are rejected as a damaged design. Id allocation uses checked_add, so an exhausted id space is an error instead of a wrap or a panic.

Part of #1016 (points 1, 2 and 4). nekomario28's #1061 reached the same fix independently; thanks to both.

Co-authored-by: nekomario28 <nekomario28@users.noreply.github.com>
This commit is contained in:
Lasse Lianandnekomario28 authored and GitHub committed 2026-10-14 09:55:53 -04:00
1 parent dde6d48dd0
commit 1ac8bf4aa8
3 files changed
+79 -3

No files matched your search

+1 -1
View File
@@ -507,7 +507,7 @@ impl Document {
return Err(DocError::Invalid("too many occurrences".into()));
}
let id = self.next_id.max(1);
self.next_id = id + 1;
self.next_id = id.checked_add(1).ok_or_else(|| DocError::Invalid("too many items in the design".into()))?;
let cname = self.components.iter().find(|c| c.id == component).map(|c| c.name.clone()).unwrap_or_default();
let k = self.occurrences.iter().filter(|o| o.component == component).count() + 1;
self.occurrences.push(crate::Occurrence { id, component, parent, name: format!("{cname}:{k}"), transform, grounded: false });