Skip to content

Commit

Permalink
make downcast() run-time type checking disregard type parameters
Browse files Browse the repository at this point in the history
This is sad, but seems to be necessary to make Miri accept the code.
  • Loading branch information
dwrensha committed Sep 23, 2024
1 parent 9e1386d commit ee107e9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 22 deletions.
28 changes: 14 additions & 14 deletions capnp/src/dynamic_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ impl<'a> Reader<'a> {
/// the current way the `Introspect` and `OwnedStruct` traits are set up does not
/// seem to allow this.
pub fn downcast_struct<T: crate::traits::OwnedStruct>(self) -> T::Reader<'a> {
let sr: dynamic_struct::Reader = self.downcast();
let TypeVariant::Struct(rs) = T::introspect().which() else {
panic!("not a struct");
};
if sr.schema.raw != rs {
panic!("tried to downcast to wrong struct type");
}
sr.reader.into()
let sb: dynamic_struct::Reader = self.downcast();
assert!(
Into::<crate::introspect::Type>::into(crate::introspect::TypeVariant::Struct(
sb.schema.raw
))
.may_downcast_to(T::introspect())
);
sb.reader.into()
}
}

Expand Down Expand Up @@ -243,12 +243,12 @@ impl<'a> Builder<'a> {
/// seem to allow this.
pub fn downcast_struct<T: crate::traits::OwnedStruct>(self) -> T::Builder<'a> {
let sb: dynamic_struct::Builder = self.downcast();
let TypeVariant::Struct(rs) = T::introspect().which() else {
panic!("not a struct");
};
if sb.schema.raw != rs {
panic!("tried to downcast to wrong struct type");
}
assert!(
Into::<crate::introspect::Type>::into(crate::introspect::TypeVariant::Struct(
sb.schema.raw
))
.may_downcast_to(T::introspect())
);
sb.builder.into()
}
}
Expand Down
4 changes: 2 additions & 2 deletions capnp/src/enum_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl<'a, T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect>
{
fn downcast_reader(v: crate::dynamic_value::Reader<'a>) -> Self {
let dl: crate::dynamic_list::Reader = v.downcast();
assert_eq!(dl.element_type(), T::introspect());
assert!(dl.element_type().may_downcast_to(T::introspect()));
Reader {
reader: dl.reader,
marker: PhantomData,
Expand All @@ -284,7 +284,7 @@ impl<'a, T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect>
{
fn downcast_builder(v: crate::dynamic_value::Builder<'a>) -> Self {
let dl: crate::dynamic_list::Builder = v.downcast();
assert_eq!(dl.element_type(), T::introspect());
assert!(dl.element_type().may_downcast_to(T::introspect()));
Builder {
builder: dl.builder,
marker: PhantomData,
Expand Down
36 changes: 36 additions & 0 deletions capnp/src/introspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,42 @@ impl Type {
)
}
}

/// Returns true is a dynamic value of type `self` is
/// allowed to be downcast to type `other`.
pub(crate) fn may_downcast_to(&self, other: Self) -> bool {
match (self.which(), other.which()) {
(TypeVariant::Void, TypeVariant::Void) => true,
(TypeVariant::UInt8, TypeVariant::UInt8) => true,
(TypeVariant::UInt16, TypeVariant::UInt16) => true,
(TypeVariant::UInt32, TypeVariant::UInt32) => true,
(TypeVariant::UInt64, TypeVariant::UInt64) => true,
(TypeVariant::Int8, TypeVariant::Int8) => true,
(TypeVariant::Int16, TypeVariant::Int16) => true,
(TypeVariant::Int32, TypeVariant::Int32) => true,
(TypeVariant::Int64, TypeVariant::Int64) => true,
(TypeVariant::Float32, TypeVariant::Float32) => true,
(TypeVariant::Float64, TypeVariant::Float64) => true,
(TypeVariant::Text, TypeVariant::Text) => true,
(TypeVariant::Data, TypeVariant::Data) => true,
(TypeVariant::Enum(es1), TypeVariant::Enum(es2)) => es1 == es2,
(TypeVariant::Struct(rbs1), TypeVariant::Struct(rbs2)) => {
// Ignore any type parameters. The original intent was that
// we would additionally check that the `field_types` fields
// were equal function pointers here. However, according to
// Miri's behavior at least, that check returns `false`
// more than we would like it to. So we settle for being
// a bit more accepting.
core::ptr::eq(rbs1.generic, rbs2.generic)
}
(TypeVariant::List(element1), TypeVariant::List(element2)) => {
element1.may_downcast_to(element2)
}
(TypeVariant::AnyPointer, TypeVariant::AnyPointer) => true,
(TypeVariant::Capability, TypeVariant::Capability) => true,
_ => false,
}
}
}

#[derive(Copy, Clone, PartialEq, Eq)]
Expand Down
4 changes: 2 additions & 2 deletions capnp/src/list_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl<'a, T: crate::traits::Owned> From<Reader<'a, T>> for crate::dynamic_value::
impl<'a, T: crate::traits::Owned> crate::dynamic_value::DowncastReader<'a> for Reader<'a, T> {
fn downcast_reader(v: crate::dynamic_value::Reader<'a>) -> Self {
let dl: crate::dynamic_list::Reader = v.downcast();
assert_eq!(dl.element_type(), T::introspect());
assert!(dl.element_type().may_downcast_to(T::introspect()));
Reader {
reader: dl.reader,
marker: core::marker::PhantomData,
Expand All @@ -307,7 +307,7 @@ impl<'a, T: crate::traits::Owned> From<Builder<'a, T>> for crate::dynamic_value:
impl<'a, T: crate::traits::Owned> crate::dynamic_value::DowncastBuilder<'a> for Builder<'a, T> {
fn downcast_builder(v: crate::dynamic_value::Builder<'a>) -> Self {
let dl: crate::dynamic_list::Builder = v.downcast();
assert_eq!(dl.element_type(), T::introspect());
assert!(dl.element_type().may_downcast_to(T::introspect()));
Builder {
builder: dl.builder,
marker: core::marker::PhantomData,
Expand Down
4 changes: 2 additions & 2 deletions capnp/src/primitive_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl<'a, T: PrimitiveElement + crate::introspect::Introspect>
{
fn downcast_reader(v: crate::dynamic_value::Reader<'a>) -> Self {
let dl: crate::dynamic_list::Reader = v.downcast();
assert_eq!(dl.element_type(), T::introspect());
assert!(dl.element_type().may_downcast_to(T::introspect()));
Reader {
reader: dl.reader,
marker: marker::PhantomData,
Expand All @@ -373,7 +373,7 @@ impl<'a, T: PrimitiveElement + crate::introspect::Introspect>
{
fn downcast_builder(v: crate::dynamic_value::Builder<'a>) -> Self {
let dl: crate::dynamic_list::Builder = v.downcast();
assert_eq!(dl.element_type(), T::introspect());
assert!(dl.element_type().may_downcast_to(T::introspect()));
Builder {
builder: dl.builder,
marker: marker::PhantomData,
Expand Down
4 changes: 2 additions & 2 deletions capnp/src/struct_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ impl<'a, T: crate::traits::OwnedStruct> From<Reader<'a, T>> for crate::dynamic_v
impl<'a, T: crate::traits::OwnedStruct> crate::dynamic_value::DowncastReader<'a> for Reader<'a, T> {
fn downcast_reader(v: crate::dynamic_value::Reader<'a>) -> Self {
let dl: crate::dynamic_list::Reader = v.downcast();
assert_eq!(dl.element_type(), T::introspect());
assert!(dl.element_type().may_downcast_to(T::introspect()));
Reader {
reader: dl.reader,
marker: PhantomData,
Expand All @@ -316,7 +316,7 @@ impl<'a, T: crate::traits::OwnedStruct> crate::dynamic_value::DowncastBuilder<'a
{
fn downcast_builder(v: crate::dynamic_value::Builder<'a>) -> Self {
let dl: crate::dynamic_list::Builder = v.downcast();
assert_eq!(dl.element_type(), T::introspect());
assert!(dl.element_type().may_downcast_to(T::introspect()));
Builder {
builder: dl.builder,
marker: PhantomData,
Expand Down

0 comments on commit ee107e9

Please sign in to comment.