| 1 | // Copyright 2025 Oxide Computer Company |
| 2 | |
| 3 | //! Handle lists of errors that occur while generating the proc macro. |
| 4 | //! |
| 5 | //! See the documentation of [`ErrorStore`] for more information. |
| 6 | |
| 7 | use std::cell::RefCell; |
| 8 | |
| 9 | /// Top-level struct that holds all errors encountered during the invocation of |
| 10 | /// a proc macro. |
| 11 | /// |
| 12 | /// This allows for collecting errors from multiple sources, and for tracking |
| 13 | /// errors in a hierarchical fashion. |
| 14 | #[derive(Debug)] |
| 15 | pub(crate) struct ErrorStore<T> { |
| 16 | data: RefCell<ErrorStoreData<T>>, |
| 17 | } |
| 18 | |
| 19 | impl<T> ErrorStore<T> { |
| 20 | /// Create a new `ErrorStore`. |
| 21 | pub(crate) fn new() -> Self { |
| 22 | Self { data: RefCell::new(ErrorStoreData::default()) } |
| 23 | } |
| 24 | |
| 25 | /// Obtain the list of errors collected by this store. |
| 26 | /// |
| 27 | /// This consumes the store, and implies that there are no [`ErrorSink`] |
| 28 | /// instances that are still alive. |
| 29 | pub(crate) fn into_inner(self) -> Vec<T> { |
| 30 | std::mem::take(&mut self.data.borrow_mut().errors) |
| 31 | } |
| 32 | |
| 33 | /// Create a new sink for collecting errors. |
| 34 | /// |
| 35 | /// This is a top-level sink, i.e. it has no parent. |
| 36 | pub(crate) fn sink(&mut self) -> ErrorSink<'_, T> { |
| 37 | let new_id = self.data.borrow_mut().register_sink(None); |
| 38 | ErrorSink { data: &self.data, id: new_id } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// A collector for errors. |
| 43 | /// |
| 44 | /// An `ErrorSink` is a context into which errors can be pushed. It can have |
| 45 | /// child `ErrorSink` instances, and the [`ErrorStore`] from which it is |
| 46 | /// ultimately derived tracks whether any errors were pushed to a given |
| 47 | /// `ErrorSink` or its descendants. |
| 48 | /// |
| 49 | /// The lifetime parameter `'a` is the lifetime of the `ErrorStore` that the |
| 50 | /// `ErrorSink` is ultimately derived from. The parameter ensures that |
| 51 | /// `ErrorSink` instances don't outlive the [`ErrorStore`] -- this means that at |
| 52 | /// the time an [`ErrorStore`] is consumed, there aren't any outstanding |
| 53 | /// `ErrorSink` instances. |
| 54 | #[derive(Debug)] |
| 55 | pub(crate) struct ErrorSink<'a, T> { |
| 56 | // It's a bit weird to use both a lifetime parameter and a RefCell, but it |
| 57 | // makes sense here. With `Rc<RefCell<T>>`, there's no way to statically |
| 58 | // guarantee that the error collection process is done. The lifetime |
| 59 | // parameter statically guarantees that. |
| 60 | // |
| 61 | // Do we need interior mutability? Because of our nested structure, the only |
| 62 | // other alternatives are some kind of `&mut &mut &mut ... T`, or dynamic |
| 63 | // dispatch. Both seem worse than just doing this. |
| 64 | data: &'a RefCell<ErrorStoreData<T>>, |
| 65 | id: usize, |
| 66 | } |
| 67 | |
| 68 | impl<'a, T> ErrorSink<'a, T> { |
| 69 | pub(crate) fn push_critical(&self, error: T) { |
| 70 | // This is always okay because we only briefly borrow the RefCell at any |
| 71 | // time. |
| 72 | self.data.borrow_mut().push_critical(self.id, error); |
| 73 | } |
| 74 | |
| 75 | pub(crate) fn push_warning(&self, error: T) { |
| 76 | // This is always okay because we only briefly borrow the RefCell at any |
| 77 | // time. |
| 78 | self.data.borrow_mut().push_warning(error); |
| 79 | } |
| 80 | |
| 81 | pub(crate) fn has_critical_errors(&self) -> bool { |
| 82 | // ErrorStore::push_critical_error propagates `has_critical_errors` up the tree while |
| 83 | // writing errors, so we can just check the current ID while reading |
| 84 | // this information. |
| 85 | self.data.borrow().sinks[self.id].has_critical_errors |
| 86 | } |
| 87 | |
| 88 | pub(crate) fn new_child(&self) -> ErrorSink<'a, T> { |
| 89 | let mut errors = self.data.borrow_mut(); |
| 90 | let new_id = errors.register_sink(Some(self.id)); |
| 91 | Self { data: self.data, id: new_id } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | #[derive(Debug)] |
| 96 | struct ErrorStoreData<T> { |
| 97 | errors: Vec<T>, |
| 98 | sinks: Vec<ErrorSinkData>, |
| 99 | } |
| 100 | |
| 101 | impl<T> Default for ErrorStoreData<T> { |
| 102 | fn default() -> Self { |
| 103 | Self { errors: Vec::new(), sinks: Vec::new() } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl<T> ErrorStoreData<T> { |
| 108 | /// Critical errors block progress |
| 109 | fn push_critical(&mut self, id: usize, error: T) { |
| 110 | self.errors.push(error); |
| 111 | self.sinks[id].has_critical_errors = true; |
| 112 | |
| 113 | // Propagate the fact that errors were encountered up the tree. |
| 114 | let mut curr = id; |
| 115 | while let Some(parent) = self.sinks[curr].parent { |
| 116 | self.sinks[parent].has_critical_errors = true; |
| 117 | curr = parent; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /// Warning errors do not block progress |
| 122 | fn push_warning(&mut self, error: T) { |
| 123 | self.errors.push(error); |
| 124 | } |
| 125 | |
| 126 | fn register_sink(&mut self, parent: Option<usize>) -> usize { |
| 127 | // len is the next ID |
| 128 | let id = self.sinks.len(); |
| 129 | self.sinks.push(ErrorSinkData::new(parent)); |
| 130 | id |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | #[derive(Debug)] |
| 135 | struct ErrorSinkData { |
| 136 | // The parent ID in the map. |
| 137 | parent: Option<usize>, |
| 138 | // Whether an error was pushed via this specific context or a descendant. |
| 139 | has_critical_errors: bool, |
| 140 | } |
| 141 | |
| 142 | impl ErrorSinkData { |
| 143 | fn new(parent: Option<usize>) -> Self { |
| 144 | Self { parent, has_critical_errors: false } |
| 145 | } |
| 146 | } |