Skip to content

Commit

Permalink
Small typeuse validation cleanup (#309)
Browse files Browse the repository at this point in the history
Clearer pattern matching on the typeuse resolution path.
  • Loading branch information
jblebrun authored Jan 26, 2024
1 parent 7d8c9f3 commit c7fefcb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
40 changes: 21 additions & 19 deletions wrausmt-format/src/text/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,25 +529,27 @@ fn get_func_params(typeuse: &TypeUse<Resolved>, types: &[TypeField]) -> Vec<FPar
fn validate_inline_typeuse(typeuse: &TypeUse<Unresolved>, ic: &ResolutionContext) -> Result<()> {
// We only need to check something if the incoming inline typeuse defined a
// function and an index explicitly.
let (new_typeidx, new_functiontype) = match (typeuse.index(), typeuse.function_type()) {
(Some(ti), Some(ft)) if !ti.name().as_bytes().is_empty() => (ti, ft),
_ => return Ok(()),
};

// If a type doesn't exist, then one is (hopefully) being created. That
// gets checked elsewhere.
let existing_functiontype = match ic.typeindex(new_typeidx.name()) {
Some(ei) => &ic.types[ei as usize].functiontype,
_ => return Ok(()),
};

// If no params/results were in the inline def, the existing type doesn't
// need to be void, since in that case, the (type $i) is just reference
// type i as anything.
if !new_functiontype.matches_existing(existing_functiontype) {
Err(ResolveError::DuplicateTypeIndex(new_typeidx.name().clone()))
} else {
Ok(())
match typeuse {
TypeUse::NamedInline {
functiontype,
index,
} if !index.name().as_bytes().is_empty() => {
// If a type doesn't exist, then one is (hopefully) being created. That
// gets checked elsewhere.
let existing_functiontype = match ic.typeindex(index.name()) {
Some(ei) => &ic.types[ei as usize].functiontype,
_ => return Ok(()),
};

// If no params/results were in the inline def, the existing type doesn't
// need to be void, since in that case, the (type $i) is just reference
// type i as anything.
functiontype
.matches_existing(existing_functiontype)
.true_or(ResolveError::DuplicateTypeIndex(index.name().clone()))?;
Ok(())
}
_ => Ok(()),
}
}

Expand Down
8 changes: 0 additions & 8 deletions wrausmt-runtime/src/syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,6 @@ impl<R: ResolvedState> TypeUse<R> {
params: vec![],
})
}

pub fn function_type(&self) -> Option<&FunctionType> {
match self {
TypeUse::ByIndex(_) => None,
TypeUse::NamedInline { functiontype, .. } => Some(functiontype),
TypeUse::AnonymousInline(ft) => Some(ft),
}
}
}

impl<R: ResolvedState> std::fmt::Debug for TypeUse<R> {
Expand Down

0 comments on commit c7fefcb

Please sign in to comment.