Skip to content

Commit 358ed21

Browse files
authored
Tidy up helpers for pushing lowered core types onto param/result lists (#2175)
1 parent f4cb5c0 commit 358ed21

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

crates/wasmparser/src/validator/component_types.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ impl LoweredTypes {
6767
}
6868
}
6969

70-
fn push(&mut self, ty: ValType) -> bool {
70+
#[track_caller]
71+
fn assert_push(&mut self, ty: ValType) {
72+
assert!(self.try_push(ty));
73+
}
74+
75+
#[must_use = "value is not actually pushed when maxed"]
76+
fn try_push(&mut self, ty: ValType) -> bool {
7177
if self.maxed() {
7278
return false;
7379
}
@@ -132,12 +138,12 @@ fn push_primitive_wasm_types(ty: &PrimitiveValType, lowered_types: &mut LoweredT
132138
| PrimitiveValType::S32
133139
| PrimitiveValType::U32
134140
| PrimitiveValType::Char
135-
| PrimitiveValType::ErrorContext => lowered_types.push(ValType::I32),
136-
PrimitiveValType::S64 | PrimitiveValType::U64 => lowered_types.push(ValType::I64),
137-
PrimitiveValType::F32 => lowered_types.push(ValType::F32),
138-
PrimitiveValType::F64 => lowered_types.push(ValType::F64),
141+
| PrimitiveValType::ErrorContext => lowered_types.try_push(ValType::I32),
142+
PrimitiveValType::S64 | PrimitiveValType::U64 => lowered_types.try_push(ValType::I64),
143+
PrimitiveValType::F32 => lowered_types.try_push(ValType::F32),
144+
PrimitiveValType::F64 => lowered_types.try_push(ValType::F64),
139145
PrimitiveValType::String => {
140-
lowered_types.push(ValType::I32) && lowered_types.push(ValType::I32)
146+
lowered_types.try_push(ValType::I32) && lowered_types.try_push(ValType::I32)
141147
}
142148
}
143149
}
@@ -926,9 +932,9 @@ impl ComponentFuncType {
926932

927933
if abi == Abi::Lower && options.concurrency.is_async() {
928934
for _ in 0..2 {
929-
sig.params.push(ValType::I32);
935+
sig.params.assert_push(ValType::I32);
930936
}
931-
sig.results.push(ValType::I32);
937+
sig.results.assert_push(ValType::I32);
932938
options.require_memory(offset)?;
933939
if self.result.is_some_and(|ty| ty.contains_ptr(types)) {
934940
options.require_realloc(offset)?;
@@ -957,7 +963,7 @@ impl ComponentFuncType {
957963
// Function will have a single pointer parameter to pass the arguments
958964
// via linear memory
959965
sig.params.clear();
960-
assert!(sig.params.push(ValType::I32));
966+
assert!(sig.params.try_push(ValType::I32));
961967
options.require_memory(offset)?;
962968

963969
// We need realloc as well when lifting a function
@@ -991,17 +997,17 @@ impl ComponentFuncType {
991997
match abi {
992998
Abi::Lower => {
993999
sig.params.max = MAX_LOWERED_TYPES;
994-
assert!(sig.params.push(ValType::I32));
1000+
assert!(sig.params.try_push(ValType::I32));
9951001
}
9961002
Abi::Lift => {
997-
assert!(sig.results.push(ValType::I32));
1003+
assert!(sig.results.try_push(ValType::I32));
9981004
}
9991005
}
10001006
}
10011007
}
10021008
}
10031009
(Abi::Lift, Concurrency::Async { callback: Some(_) }) => {
1004-
sig.results.push(ValType::I32);
1010+
sig.results.assert_push(ValType::I32);
10051011
}
10061012
(Abi::Lift, Concurrency::Async { callback: None }) => {}
10071013
}
@@ -1148,7 +1154,9 @@ impl ComponentDefinedType {
11481154
types,
11491155
lowered_types,
11501156
),
1151-
Self::List(_) => lowered_types.push(ValType::I32) && lowered_types.push(ValType::I32),
1157+
Self::List(_) => {
1158+
lowered_types.try_push(ValType::I32) && lowered_types.try_push(ValType::I32)
1159+
}
11521160
Self::FixedSizeList(ty, length) => {
11531161
(0..*length).all(|_n| ty.push_wasm_types(types, lowered_types))
11541162
}
@@ -1157,10 +1165,10 @@ impl ComponentDefinedType {
11571165
.iter()
11581166
.all(|ty| ty.push_wasm_types(types, lowered_types)),
11591167
Self::Flags(names) => {
1160-
(0..(names.len() + 31) / 32).all(|_| lowered_types.push(ValType::I32))
1168+
(0..(names.len() + 31) / 32).all(|_| lowered_types.try_push(ValType::I32))
11611169
}
11621170
Self::Enum(_) | Self::Own(_) | Self::Borrow(_) | Self::Future(_) | Self::Stream(_) => {
1163-
lowered_types.push(ValType::I32)
1171+
lowered_types.try_push(ValType::I32)
11641172
}
11651173
Self::Option(ty) => {
11661174
Self::push_variant_wasm_types([ty].into_iter(), types, lowered_types)
@@ -1177,7 +1185,7 @@ impl ComponentDefinedType {
11771185
lowered_types: &mut LoweredTypes,
11781186
) -> bool {
11791187
// Push the discriminant
1180-
if !lowered_types.push(ValType::I32) {
1188+
if !lowered_types.try_push(ValType::I32) {
11811189
return false;
11821190
}
11831191

@@ -1194,7 +1202,7 @@ impl ComponentDefinedType {
11941202
match lowered_types.get_mut(start + i) {
11951203
Some(prev) => *prev = Self::join_types(*prev, ty),
11961204
None => {
1197-
if !lowered_types.push(ty) {
1205+
if !lowered_types.try_push(ty) {
11981206
return false;
11991207
}
12001208
}

0 commit comments

Comments
 (0)