Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/types/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ impl<'brand> DisconnectConstructible<'brand, Option<&Arrow<'brand>>> for Arrow<'

impl<'brand> JetConstructible<'brand> for Arrow<'brand> {
fn jet(inference_context: &Context<'brand>, jet: &dyn Jet) -> Self {
inference_context.check_jet(jet);

Arrow {
source: jet.source_ty().to_type(inference_context),
target: jet.target_ty().to_type(inference_context),
Expand Down
26 changes: 25 additions & 1 deletion src/types/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
//! the other.
//!

use std::any::TypeId;
use std::fmt;
use std::marker::PhantomData;
use std::sync::{Arc, Mutex, MutexGuard};

use ghost_cell::GhostToken;

use crate::dag::{Dag, DagLike};
use crate::jet::Jet;

use super::{
Bound, CompleteBound, Error, Final, Incomplete, Type, TypeInner, UbElement, WithGhostToken,
Expand Down Expand Up @@ -48,6 +50,8 @@ pub struct Context<'brand> {

struct ContextInner<'brand> {
slab: Vec<Bound<'brand>>,
/// Concrete jet type registered in this context, if any.
jet_type: Option<TypeId>,
}

impl fmt::Debug for Context<'_> {
Expand Down Expand Up @@ -81,7 +85,10 @@ impl<'brand> Context<'brand> {
Context {
inner: Arc::new(Mutex::new(WithGhostToken {
token,
inner: ContextInner { slab: vec![] },
inner: ContextInner {
slab: vec![],
jet_type: None,
},
})),
}
}
Expand Down Expand Up @@ -147,6 +154,23 @@ impl<'brand> Context<'brand> {
}
}

/// Asserts that all jets in this context have the same concrete type.
///
/// Records the jet's type on first call, panics on subsequent calls with
/// a different concrete type.
pub fn check_jet(&self, jet: &dyn Jet) {
let new_id = jet.as_any().type_id();
let mut lock = self.lock();

if let Some(existing_id) = lock.inner.jet_type {
assert!(existing_id == new_id, "mixed jet types in context");

return;
}

lock.inner.jet_type = Some(new_id);
}

/// Accesses a bound.
pub(super) fn get(&self, bound: &BoundRef<'brand>) -> Bound<'brand> {
let lock = self.lock();
Expand Down
22 changes: 22 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ impl fmt::Display for Type<'_> {
mod tests {
use super::*;

use crate::jet::Core;
use crate::node::JetConstructible;
use crate::node::{ConstructNode, CoreConstructible};

#[test]
Expand Down Expand Up @@ -446,4 +448,24 @@ mod tests {
let _ = format!("{:?}", case.arrow().source);
});
}

#[test]
fn check_jet_same_type_ok() {
Context::with_context(|ctx| {
let _ = Arc::<ConstructNode>::jet(&ctx, &Core::Add32);
let _ = Arc::<ConstructNode>::jet(&ctx, &Core::Subtract32);
});
}

#[cfg(feature = "elements")]
#[test]
#[should_panic(expected = "mixed jet types in context")]
fn check_jet_different_types_panics() {
use crate::jet::Elements;

Context::with_context(|ctx| {
let _ = Arc::<ConstructNode>::jet(&ctx, &Core::Add32);
let _ = Arc::<ConstructNode>::jet(&ctx, &Elements::Add32);
});
}
}
Loading