| 1 |
use super::lifetimes::{Lifetime, Lifetimes, MaybeStatic}; |
| 2 |
use super::{ |
| 3 |
Borrow, LinkedLifetimes, MaybeOwn, Mutability, OutStructId, ReturnableStructPath, StructId, |
| 4 |
StructPath, TypeContext, TypeId, |
| 5 |
}; |
| 6 |
use core::fmt::Debug; |
| 7 |
|
| 8 |
/// Abstraction over where a type can appear in a function signature. |
| 9 |
/// |
| 10 |
/// # "Output only" and "everywhere" types |
| 11 |
/// |
| 12 |
/// While Rust is able to give up ownership of values, languages that Diplomat |
| 13 |
/// supports (C++, Javascript, etc.) generally cannot. For example, we can |
| 14 |
/// construct a `Box<MyOpaque>` in a Rust function and _return_ it to the other |
| 15 |
/// language as a pointer. However, we cannot _accept_ `Box<MyOpaque>` as an input |
| 16 |
/// because there's nothing stopping other languages from using that value again. |
| 17 |
/// Therefore, we classify boxed opaques as "output only" types, since they can |
| 18 |
/// only be returned from Rust but not taken as inputs. |
| 19 |
/// |
| 20 |
/// Furthermore, Diplomat also supports "bag o' stuff" structs where all fields get |
| 21 |
/// translated at the boundary. If one contains an "output only" type as a field, |
| 22 |
/// then the whole struct must also be "output only". In particular, this means |
| 23 |
/// that if a boxed opaque is nested in a bunch of "bag o' stuff" structs, than |
| 24 |
/// all of those structs must also be "output only". |
| 25 |
/// |
| 26 |
/// Currently, there are only two classes of structs: those that are "output only", |
| 27 |
/// and those that are not. These are represented by the types [`OutputOnly`] |
| 28 |
/// and [`Everywhere`] marker types respectively, which are the _only_ two types |
| 29 |
/// that implement [`TyPosition`]. |
| 30 |
/// |
| 31 |
/// # How does abstraction help? |
| 32 |
/// |
| 33 |
/// The HIR was designed around the idea of making invalid states unrepresentable. |
| 34 |
/// Since "output only" types can contain values that "everywhere" types cannot, |
| 35 |
/// it doesn't make sense for them to be represented in the same type, even if |
| 36 |
/// they're mostly the same. One of these differences is that opaques (which are |
| 37 |
/// always behind a pointer) can only be represented as a borrow in "everywhere" |
| 38 |
/// types, but can additionally be represented as owned in "output only" types. |
| 39 |
/// If we were to use the same type for both, then backends working with "everywhere" |
| 40 |
/// types would constantly have unreachable statements for owned opaque cases. |
| 41 |
/// |
| 42 |
/// That being said, "output only" and "everywhere" types are still mostly the |
| 43 |
/// same, so this trait allows us to describe the differences. For example, the |
| 44 |
/// HIR uses a singular [`Type`](super::Type) type for representing both |
| 45 |
/// "output only" types and "everywhere" types, since it takes advantage of this |
| 46 |
/// traits associated types to "fill in" the different parts: |
| 47 |
/// ```ignore |
| 48 |
/// pub enum Type<P: TyPosition = Everywhere> { |
| 49 |
/// Primitive(PrimitiveType), |
| 50 |
/// Opaque(OpaquePath<Optional, P::OpaqueOwnership>), |
| 51 |
/// Struct(P::StructPath), |
| 52 |
/// Enum(EnumPath), |
| 53 |
/// Slice(Slice), |
| 54 |
/// } |
| 55 |
/// ``` |
| 56 |
/// |
| 57 |
/// When `P` takes on [`Everywhere`], this signature becomes: |
| 58 |
/// ```ignore |
| 59 |
/// pub enum Type { |
| 60 |
/// Primitive(PrimitiveType), |
| 61 |
/// Opaque(OpaquePath<Optional, Borrow>), |
| 62 |
/// Struct(StructPath), |
| 63 |
/// Enum(EnumPath), |
| 64 |
/// Slice(Slice), |
| 65 |
/// } |
| 66 |
/// ``` |
| 67 |
/// |
| 68 |
/// This allows us to represent any kind of type that can appear "everywhere" |
| 69 |
/// i.e. in inputs or outputs. Notice how the second generic in the `Opaque` |
| 70 |
/// variant becomes [`Borrow`]. This describes the semantics of the pointer that |
| 71 |
/// the opaque lives behind, and shows that for "everywhere" types, opaques |
| 72 |
/// can _only_ be represented as living behind a borrow. |
| 73 |
/// |
| 74 |
/// Contrast this to when `P` takes on [`OutputOnly`]: |
| 75 |
/// ```ignore |
| 76 |
/// pub enum Type { |
| 77 |
/// Primitive(PrimitiveType), |
| 78 |
/// Opaque(OpaquePath<Optional, MaybeOwn>), |
| 79 |
/// Struct(OutStructPath), |
| 80 |
/// Enum(EnumPath), |
| 81 |
/// Slice(Slice), |
| 82 |
/// } |
| 83 |
/// ``` |
| 84 |
/// Here, the second generic of the `Opaque` variant becomes [`MaybeOwn`], meaning |
| 85 |
/// that "output only" types can contain opaques that are either borrowed _or_ owned. |
| 86 |
/// |
| 87 |
/// Therefore, this trait allows be extremely precise about making invalid states |
| 88 |
/// unrepresentable, while also reducing duplicated code. |
| 89 |
/// |
| 90 |
pub trait TyPosition: Debug + Copy { |
| 91 |
const IS_OUT_ONLY: bool; |
| 92 |
|
| 93 |
/// Type representing how we can point to opaques, which must always be behind a pointer. |
| 94 |
/// |
| 95 |
/// The types represented by [`OutputOnly`] are capable of either owning or |
| 96 |
/// borrowing opaques, and so the associated type for that impl is [`MaybeOwn`]. |
| 97 |
/// |
| 98 |
/// On the other hand, types represented by [`Everywhere`] can only contain |
| 99 |
/// borrowes, so the associated type for that impl is [`Borrow`]. |
| 100 |
type OpaqueOwnership: Debug + OpaqueOwner; |
| 101 |
|
| 102 |
type StructId: Debug; |
| 103 |
|
| 104 |
type StructPath: Debug + StructPathLike; |
| 105 |
} |
| 106 |
|
| 107 |
/// One of two types implementing [`TyPosition`], representing types that can be |
| 108 |
/// used as both input and output to functions. |
| 109 |
/// |
| 110 |
/// The complement of this type is [`OutputOnly`]. |
| 111 |
#[derive(Debug, Copy, Clone)] |
| 112 |
#[non_exhaustive] |
| 113 |
pub struct Everywhere; |
| 114 |
|
| 115 |
/// One of two types implementing [`TyPosition`], representing types that can |
| 116 |
/// only be used as return types in functions. |
| 117 |
/// |
| 118 |
/// The complement of this type is [`Everywhere`]. |
| 119 |
#[derive(Debug, Copy, Clone)] |
| 120 |
#[non_exhaustive] |
| 121 |
pub struct OutputOnly; |
| 122 |
|
| 123 |
impl TyPosition for Everywhere { |
| 124 |
const IS_OUT_ONLY: bool = false; |
| 125 |
type OpaqueOwnership = Borrow; |
| 126 |
type StructId = StructId; |
| 127 |
type StructPath = StructPath; |
| 128 |
} |
| 129 |
|
| 130 |
impl TyPosition for OutputOnly { |
| 131 |
const IS_OUT_ONLY: bool = true; |
| 132 |
type OpaqueOwnership = MaybeOwn; |
| 133 |
type StructId = OutStructId; |
| 134 |
type StructPath = ReturnableStructPath; |
| 135 |
} |
| 136 |
|
| 137 |
pub trait StructPathLike { |
| 138 |
fn lifetimes(&self) -> &Lifetimes; |
| 139 |
fn id(&self) -> TypeId; |
| 140 |
|
| 141 |
/// Get a map of lifetimes used on this path to lifetimes as named in the def site. See [`LinkedLifetimes`] |
| 142 |
/// for more information. |
| 143 |
fn link_lifetimes<'def, 'tcx>( |
| 144 |
&'def self, |
| 145 |
tcx: &'tcx TypeContext, |
| 146 |
) -> LinkedLifetimes<'def, 'tcx>; |
| 147 |
} |
| 148 |
|
| 149 |
impl StructPathLike for StructPath { |
| 150 |
fn lifetimes(&self) -> &Lifetimes { |
| 151 |
&self.lifetimes |
| 152 |
} |
| 153 |
fn id(&self) -> TypeId { |
| 154 |
self.tcx_id.into() |
| 155 |
} |
| 156 |
|
| 157 |
fn link_lifetimes<'def, 'tcx>( |
| 158 |
&'def self, |
| 159 |
tcx: &'tcx TypeContext, |
| 160 |
) -> LinkedLifetimes<'def, 'tcx> { |
| 161 |
let struc = self.resolve(tcx); |
| 162 |
let env = &struc.lifetimes; |
| 163 |
LinkedLifetimes::new(env, None, &self.lifetimes) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
impl StructPathLike for ReturnableStructPath { |
| 168 |
fn lifetimes(&self) -> &Lifetimes { |
| 169 |
self.lifetimes() |
| 170 |
} |
| 171 |
fn id(&self) -> TypeId { |
| 172 |
match self { |
| 173 |
ReturnableStructPath::Struct(p) => p.tcx_id.into(), |
| 174 |
ReturnableStructPath::OutStruct(p) => p.tcx_id.into(), |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
fn link_lifetimes<'def, 'tcx>( |
| 179 |
&'def self, |
| 180 |
tcx: &'tcx TypeContext, |
| 181 |
) -> LinkedLifetimes<'def, 'tcx> { |
| 182 |
match self { |
| 183 |
Self::Struct(p) => p.link_lifetimes(tcx), |
| 184 |
Self::OutStruct(p) => p.link_lifetimes(tcx), |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
/// Abstraction over how a type can hold a pointer to an opaque. |
| 189 |
/// |
| 190 |
/// This trait is designed as a helper abstraction for the `OpaqueOwnership` |
| 191 |
/// associated type in the [`TyPosition`] trait. As such, only has two implementing |
| 192 |
/// types: [`MaybeOwn`] and [`Borrow`] for the [`OutputOnly`] and [`Everywhere`] |
| 193 |
/// implementations of [`TyPosition`] respectively. |
| 194 |
pub trait OpaqueOwner { |
| 195 |
/// Return the mutability of this owner |
| 196 |
fn mutability(&self) -> Option<Mutability>; |
| 197 |
|
| 198 |
fn is_owned(&self) -> bool; |
| 199 |
|
| 200 |
/// Return the lifetime of the borrow, if any. |
| 201 |
fn lifetime(&self) -> Option<MaybeStatic<Lifetime>>; |
| 202 |
} |
| 203 |
|
| 204 |
impl OpaqueOwner for MaybeOwn { |
| 205 |
fn mutability(&self) -> Option<Mutability> { |
| 206 |
match self { |
| 207 |
MaybeOwn::Own => None, |
| 208 |
MaybeOwn::Borrow(b) => b.mutability(), |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
fn is_owned(&self) -> bool { |
| 213 |
match self { |
| 214 |
MaybeOwn::Own => true, |
| 215 |
MaybeOwn::Borrow(_) => false, |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
fn lifetime(&self) -> Option<MaybeStatic<Lifetime>> { |
| 220 |
match self { |
| 221 |
MaybeOwn::Own => None, |
| 222 |
MaybeOwn::Borrow(b) => b.lifetime(), |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
impl OpaqueOwner for Borrow { |
| 228 |
fn mutability(&self) -> Option<Mutability> { |
| 229 |
Some(self.mutability) |
| 230 |
} |
| 231 |
|
| 232 |
fn is_owned(&self) -> bool { |
| 233 |
false |
| 234 |
} |
| 235 |
|
| 236 |
fn lifetime(&self) -> Option<MaybeStatic<Lifetime>> { |
| 237 |
Some(self.lifetime) |
| 238 |
} |
| 239 |
} |