Skip to content

Commit cbbe555

Browse files
authored
Implement TryClone for WasmSubType; use it in the TypeRegistry (#12527)
* Implement `TryClone` for `WasmSubType`; use it in the `TypeRegistry` * fix clippy
1 parent b96811d commit cbbe555

3 files changed

Lines changed: 66 additions & 18 deletions

File tree

crates/cranelift/src/func_environ.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3962,22 +3962,16 @@ impl FuncEnvironment<'_> {
39623962

39633963
pub fn continuation_arguments(&self, index: TypeIndex) -> &[WasmValType] {
39643964
let idx = self.module.types[index].unwrap_module_type_index();
3965-
self.types[self.types[idx]
3966-
.unwrap_cont()
3967-
.clone()
3968-
.unwrap_module_type_index()]
3969-
.unwrap_func()
3970-
.params()
3965+
self.types[self.types[idx].unwrap_cont().unwrap_module_type_index()]
3966+
.unwrap_func()
3967+
.params()
39713968
}
39723969

39733970
pub fn continuation_returns(&self, index: TypeIndex) -> &[WasmValType] {
39743971
let idx = self.module.types[index].unwrap_module_type_index();
3975-
self.types[self.types[idx]
3976-
.unwrap_cont()
3977-
.clone()
3978-
.unwrap_module_type_index()]
3979-
.unwrap_func()
3980-
.returns()
3972+
self.types[self.types[idx].unwrap_cont().unwrap_module_type_index()]
3973+
.unwrap_func()
3974+
.returns()
39813975
}
39823976

39833977
pub fn tag_params(&self, tag_index: TagIndex) -> &[WasmValType] {

crates/environ/src/types.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ impl WasmFuncType {
846846
}
847847

848848
/// WebAssembly continuation type -- equivalent of `wasmparser`'s ContType.
849-
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
849+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
850850
pub struct WasmContType(EngineOrModuleTypeIndex);
851851

852852
impl fmt::Display for WasmContType {
@@ -926,6 +926,15 @@ pub struct WasmExnType {
926926
pub fields: Box<[WasmFieldType]>,
927927
}
928928

929+
impl TryClone for WasmExnType {
930+
fn try_clone(&self) -> Result<Self, OutOfMemory> {
931+
Ok(Self {
932+
func_ty: self.func_ty,
933+
fields: self.fields.try_clone()?,
934+
})
935+
}
936+
}
937+
929938
impl fmt::Display for WasmExnType {
930939
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
931940
write!(f, "(exn ({})", self.func_ty)?;
@@ -1018,7 +1027,7 @@ impl WasmStorageType {
10181027
}
10191028

10201029
/// The type of a struct field or array element.
1021-
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
1030+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
10221031
pub struct WasmFieldType {
10231032
/// The field's element type.
10241033
pub element_type: WasmStorageType,
@@ -1027,6 +1036,12 @@ pub struct WasmFieldType {
10271036
pub mutable: bool,
10281037
}
10291038

1039+
impl TryClone for WasmFieldType {
1040+
fn try_clone(&self) -> Result<Self, OutOfMemory> {
1041+
Ok(*self)
1042+
}
1043+
}
1044+
10301045
impl fmt::Display for WasmFieldType {
10311046
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10321047
if self.mutable {
@@ -1054,7 +1069,7 @@ impl TypeTrace for WasmFieldType {
10541069
}
10551070

10561071
/// A concrete array type.
1057-
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
1072+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
10581073
pub struct WasmArrayType(pub WasmFieldType);
10591074

10601075
impl fmt::Display for WasmArrayType {
@@ -1086,6 +1101,14 @@ pub struct WasmStructType {
10861101
pub fields: Box<[WasmFieldType]>,
10871102
}
10881103

1104+
impl TryClone for WasmStructType {
1105+
fn try_clone(&self) -> Result<Self, OutOfMemory> {
1106+
Ok(Self {
1107+
fields: self.fields.try_clone()?,
1108+
})
1109+
}
1110+
}
1111+
10891112
impl fmt::Display for WasmStructType {
10901113
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10911114
write!(f, "(struct")?;
@@ -1128,6 +1151,15 @@ pub struct WasmCompositeType {
11281151
pub shared: bool,
11291152
}
11301153

1154+
impl TryClone for WasmCompositeType {
1155+
fn try_clone(&self) -> Result<Self, OutOfMemory> {
1156+
Ok(Self {
1157+
inner: self.inner.try_clone()?,
1158+
shared: self.shared,
1159+
})
1160+
}
1161+
}
1162+
11311163
impl fmt::Display for WasmCompositeType {
11321164
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11331165
if self.shared {
@@ -1152,6 +1184,18 @@ pub enum WasmCompositeInnerType {
11521184
Exn(WasmExnType),
11531185
}
11541186

1187+
impl TryClone for WasmCompositeInnerType {
1188+
fn try_clone(&self) -> Result<Self, OutOfMemory> {
1189+
Ok(match self {
1190+
Self::Array(ty) => Self::Array(*ty),
1191+
Self::Func(ty) => Self::Func(ty.try_clone()?),
1192+
Self::Struct(ty) => Self::Struct(ty.try_clone()?),
1193+
Self::Cont(ty) => Self::Cont(*ty),
1194+
Self::Exn(ty) => Self::Exn(ty.try_clone()?),
1195+
})
1196+
}
1197+
}
1198+
11551199
impl fmt::Display for WasmCompositeInnerType {
11561200
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11571201
match self {
@@ -1299,6 +1343,16 @@ pub struct WasmSubType {
12991343
pub composite_type: WasmCompositeType,
13001344
}
13011345

1346+
impl TryClone for WasmSubType {
1347+
fn try_clone(&self) -> Result<Self, OutOfMemory> {
1348+
Ok(Self {
1349+
is_final: self.is_final,
1350+
supertype: self.supertype,
1351+
composite_type: self.composite_type.try_clone()?,
1352+
})
1353+
}
1354+
}
1355+
13021356
impl fmt::Display for WasmSubType {
13031357
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13041358
if self.is_final && self.supertype.is_none() {

crates/wasmtime/src/runtime/type_registry.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,15 +761,15 @@ impl TypeRegistryInner {
761761
.zip(iter_entity_range(range.clone()))
762762
.map(|(mut ty, module_index)| {
763763
non_canon_types
764-
.push((module_index, ty.clone()))
764+
.push((module_index, ty.try_clone()?))
765765
.expect("reserved capacity");
766766
ty.canonicalize_for_hash_consing(range.clone(), &mut |idx| {
767767
debug_assert!(idx < range.clone().start);
768768
map[idx]
769769
});
770-
ty
770+
Ok(ty)
771771
})
772-
.try_collect()?,
772+
.try_collect::<Box<[_]>, OutOfMemory>()?,
773773
};
774774

775775
// Any references in the hash-consing key to types outside of this rec

0 commit comments

Comments
 (0)