|
14 | 14 | //! the other. |
15 | 15 | //! |
16 | 16 |
|
| 17 | +use std::any::TypeId; |
17 | 18 | use std::fmt; |
18 | 19 | use std::marker::PhantomData; |
19 | 20 | use std::sync::{Arc, Mutex, MutexGuard}; |
20 | 21 |
|
21 | 22 | use ghost_cell::GhostToken; |
22 | 23 |
|
23 | 24 | use crate::dag::{Dag, DagLike}; |
| 25 | +use crate::jet::Jet; |
24 | 26 |
|
25 | 27 | use super::{ |
26 | 28 | Bound, CompleteBound, Error, Final, Incomplete, Type, TypeInner, UbElement, WithGhostToken, |
@@ -48,6 +50,8 @@ pub struct Context<'brand> { |
48 | 50 |
|
49 | 51 | struct ContextInner<'brand> { |
50 | 52 | slab: Vec<Bound<'brand>>, |
| 53 | + /// Concrete jet type registered in this context, if any. |
| 54 | + jet_type: Option<TypeId>, |
51 | 55 | } |
52 | 56 |
|
53 | 57 | impl fmt::Debug for Context<'_> { |
@@ -81,7 +85,10 @@ impl<'brand> Context<'brand> { |
81 | 85 | Context { |
82 | 86 | inner: Arc::new(Mutex::new(WithGhostToken { |
83 | 87 | token, |
84 | | - inner: ContextInner { slab: vec![] }, |
| 88 | + inner: ContextInner { |
| 89 | + slab: vec![], |
| 90 | + jet_type: None, |
| 91 | + }, |
85 | 92 | })), |
86 | 93 | } |
87 | 94 | } |
@@ -147,6 +154,23 @@ impl<'brand> Context<'brand> { |
147 | 154 | } |
148 | 155 | } |
149 | 156 |
|
| 157 | + /// Asserts that all jets in this context have the same concrete type. |
| 158 | + /// |
| 159 | + /// Records the jet's type on first call, panics on subsequent calls with |
| 160 | + /// a different concrete type. |
| 161 | + pub fn check_jet(&self, jet: &dyn Jet) { |
| 162 | + let new_id = jet.as_any().type_id(); |
| 163 | + let mut lock = self.lock(); |
| 164 | + |
| 165 | + if let Some(existing_id) = lock.inner.jet_type { |
| 166 | + assert!(existing_id == new_id, "mixed jet types in context"); |
| 167 | + |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + lock.inner.jet_type = Some(new_id); |
| 172 | + } |
| 173 | + |
150 | 174 | /// Accesses a bound. |
151 | 175 | pub(super) fn get(&self, bound: &BoundRef<'brand>) -> Bound<'brand> { |
152 | 176 | let lock = self.lock(); |
|
0 commit comments