| 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 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core 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 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 15 | pub(craterust-analyzer cargo daft-derive 0.1.5 crate/) struct ErrorStorerust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStore#<T> { |
| 16 | datarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStore#data.: RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<ErrorStoreDatarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#<T>>, |
| 17 | } |
| 18 | |
| 19 | impl<T> ErrorStorerust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStore#<T> { |
| 20 | /// Create a new `ErrorStore`. |
| 21 | pub(craterust-analyzer cargo daft-derive 0.1.5 crate/) fn newrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStore<T>`]new().() -> Selfrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStore<T>`] { |
| 22 | Selfrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStore<T>`] { datarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStore#data.: RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]new().(ErrorStoreDatarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#::defaultrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStoreData<T>`][Default]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(craterust-analyzer cargo daft-derive 0.1.5 crate/) fn into_innerrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStore<T>`]into_inner().(self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T> { |
| 30 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/take().(&mut self.datarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStore#data..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().().errorsrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#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(craterust-analyzer cargo daft-derive 0.1.5 crate/) fn sinkrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStore<T>`]sink().(&mut self) -> ErrorSinkrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#<'_, T> { |
| 37 | let new_id = self.datarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStore#data..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().().register_sinkrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStoreData<T>`]register_sink().(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 38 | ErrorSinkrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink# { datarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#data.: &self.datarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStore#data., idrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#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 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 55 | pub(craterust-analyzer cargo daft-derive 0.1.5 crate/) struct ErrorSinkrust-analyzer cargo daft-derive 0.1.5 internals/error_store/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 | datarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#data.: &'a RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<ErrorStoreDatarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#<T>>, |
| 65 | idrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#id.: usize, |
| 66 | } |
| 67 | |
| 68 | impl<'a, T> ErrorSinkrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#<'a, T> { |
| 69 | pub(craterust-analyzer cargo daft-derive 0.1.5 crate/) fn push_criticalrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorSink<'a, T>`]push_critical().(&self, error: T) { |
| 70 | // This is always okay because we only briefly borrow the RefCell at any |
| 71 | // time. |
| 72 | self.datarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#data..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().().push_criticalrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStoreData<T>`]push_critical().(self.idrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#id., error); |
| 73 | } |
| 74 | |
| 75 | pub(craterust-analyzer cargo daft-derive 0.1.5 crate/) fn push_warningrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorSink<'a, T>`]push_warning().(&self, error: T) { |
| 76 | // This is always okay because we only briefly borrow the RefCell at any |
| 77 | // time. |
| 78 | self.datarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#data..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().().push_warningrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStoreData<T>`]push_warning().(error); |
| 79 | } |
| 80 | |
| 81 | pub(craterust-analyzer cargo daft-derive 0.1.5 crate/) fn has_critical_errorsrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorSink<'a, T>`]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.datarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#data..borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow().().sinksrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#sinks.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().self.idrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#id.]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..has_critical_errorsrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSinkData#has_critical_errors. |
| 86 | } |
| 87 | |
| 88 | pub(craterust-analyzer cargo daft-derive 0.1.5 crate/) fn new_childrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorSink<'a, T>`]new_child().(&self) -> ErrorSinkrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#<'a, T> { |
| 89 | let mut errors = self.datarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#data..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().(); |
| 90 | let new_id = errors.register_sinkrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStoreData<T>`]register_sink().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(self.idrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#id.)); |
| 91 | Selfrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorSink<'a, T>`] { datarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#data.: self.datarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#data., idrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSink#id.: new_id } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 96 | struct ErrorStoreDatarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#<T> { |
| 97 | errorsrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#errors.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>, |
| 98 | sinksrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#sinks.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<ErrorSinkDatarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSinkData#>, |
| 99 | } |
| 100 | |
| 101 | impl<T> Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for ErrorStoreDatarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#<T> { |
| 102 | fn defaultrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStoreData<T>`][Default]default().() -> Selfrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStoreData<T>`][Default] { |
| 103 | Selfrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStoreData<T>`][Default] { errorsrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#errors.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(), sinksrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#sinks.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().() } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl<T> ErrorStoreDatarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#<T> { |
| 108 | /// Critical errors block progress |
| 109 | fn push_criticalrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStoreData<T>`]push_critical().(&mut self, id: usize, error: T) { |
| 110 | self.errorsrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#errors..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(error); |
| 111 | self.sinksrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#sinks.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().id]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()..has_critical_errorsrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSinkData#has_critical_errors. = true; |
| 112 | |
| 113 | // Propagate the fact that errors were encountered up the tree. |
| 114 | let mut curr = id; |
| 115 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(parent) = self.sinksrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#sinks.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().curr]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..parentrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSinkData#parent. { |
| 116 | self.sinksrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#sinks.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().parent]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()..has_critical_errorsrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSinkData#has_critical_errors. = true; |
| 117 | curr = parent; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /// Warning errors do not block progress |
| 122 | fn push_warningrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStoreData<T>`]push_warning().(&mut self, error: T) { |
| 123 | self.errorsrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#errors..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(error); |
| 124 | } |
| 125 | |
| 126 | fn register_sinkrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[`ErrorStoreData<T>`]register_sink().(&mut self, parent: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) -> usize { |
| 127 | // len is the next ID |
| 128 | let id = self.sinksrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#sinks..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 129 | self.sinksrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorStoreData#sinks..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(ErrorSinkDatarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSinkData#::newrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[ErrorSinkData]new().(parent)); |
| 130 | id |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 135 | struct ErrorSinkDatarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSinkData# { |
| 136 | // The parent ID in the map. |
| 137 | parentrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSinkData#parent.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>, |
| 138 | // Whether an error was pushed via this specific context or a descendant. |
| 139 | has_critical_errorsrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSinkData#has_critical_errors.: bool, |
| 140 | } |
| 141 | |
| 142 | impl ErrorSinkDatarust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSinkData# { |
| 143 | fn newrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[ErrorSinkData]new().(parent: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) -> Selfrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[ErrorSinkData] { |
| 144 | Selfrust-analyzer cargo daft-derive 0.1.5 internals/error_store/impl#[ErrorSinkData] { parentrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSinkData#parent., has_critical_errorsrust-analyzer cargo daft-derive 0.1.5 internals/error_store/ErrorSinkData#has_critical_errors.: false } |
| 145 | } |
| 146 | } |