Skip to content

Commit

Permalink
wasmparser: Rename Inherits trait to Matches
Browse files Browse the repository at this point in the history
This aligns with the nomenclature used in the spec.

No functional changes, just mechanical renaming.
  • Loading branch information
fitzgen committed Sep 20, 2023
1 parent 0e945a5 commit 0e1d66c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
70 changes: 35 additions & 35 deletions crates/wasmparser/src/readers/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ const _: () = {
assert!(std::mem::size_of::<ValType>() == 4);
};

pub(crate) trait Inherits {
fn inherits<'a, F>(&self, other: &Self, type_at: &F) -> bool
pub(crate) trait Matches {
fn matches<'a, F>(&self, other: &Self, type_at: &F) -> bool
where
F: Fn(u32) -> &'a SubType;
}
Expand Down Expand Up @@ -100,13 +100,13 @@ impl ValType {
}
}

impl Inherits for ValType {
fn inherits<'a, F>(&self, other: &Self, type_at: &F) -> bool
impl Matches for ValType {
fn matches<'a, F>(&self, other: &Self, type_at: &F) -> bool
where
F: Fn(u32) -> &'a SubType,
{
match (self, other) {
(Self::Ref(r1), Self::Ref(r2)) => r1.inherits(r2, type_at),
(Self::Ref(r1), Self::Ref(r2)) => r1.matches(r2, type_at),
(a, b) => a == b,
}
}
Expand Down Expand Up @@ -545,14 +545,14 @@ impl RefType {
}
}

impl Inherits for RefType {
fn inherits<'a, F>(&self, other: &Self, type_at: &F) -> bool
impl Matches for RefType {
fn matches<'a, F>(&self, other: &Self, type_at: &F) -> bool
where
F: Fn(u32) -> &'a SubType,
{
self == other
|| ((other.is_nullable() || !self.is_nullable())
&& self.heap_type().inherits(&other.heap_type(), type_at))
&& self.heap_type().matches(&other.heap_type(), type_at))
}
}

Expand Down Expand Up @@ -641,8 +641,8 @@ pub enum HeapType {
I31,
}

impl Inherits for HeapType {
fn inherits<'a, F>(&self, other: &Self, type_at: &F) -> bool
impl Matches for HeapType {
fn matches<'a, F>(&self, other: &Self, type_at: &F) -> bool
where
F: Fn(u32) -> &'a SubType,
{
Expand Down Expand Up @@ -677,7 +677,7 @@ impl Inherits for HeapType {

(HT::Indexed(a), HT::Indexed(b)) => type_at(*a)
.structural_type
.inherits(&type_at(*b).structural_type, type_at),
.matches(&type_at(*b).structural_type, type_at),

(HT::None, HT::Indexed(b)) => matches!(
type_at(*b).structural_type,
Expand Down Expand Up @@ -790,15 +790,15 @@ impl RecGroup {
}
}

impl Inherits for SubType {
fn inherits<'a, F>(&self, other: &Self, type_at: &F) -> bool
impl Matches for SubType {
fn matches<'a, F>(&self, other: &Self, type_at: &F) -> bool
where
F: Fn(u32) -> &'a SubType,
{
!other.is_final
&& self
.structural_type
.inherits(&other.structural_type, type_at)
.matches(&other.structural_type, type_at)
}
}

Expand Down Expand Up @@ -831,15 +831,15 @@ pub struct StructType {
pub fields: Box<[FieldType]>,
}

impl Inherits for StructuralType {
fn inherits<'a, F>(&self, other: &Self, type_at: &F) -> bool
impl Matches for StructuralType {
fn matches<'a, F>(&self, other: &Self, type_at: &F) -> bool
where
F: Fn(u32) -> &'a SubType,
{
match (self, other) {
(StructuralType::Func(a), StructuralType::Func(b)) => a.inherits(b, type_at),
(StructuralType::Array(a), StructuralType::Array(b)) => a.inherits(b, type_at),
(StructuralType::Struct(a), StructuralType::Struct(b)) => a.inherits(b, type_at),
(StructuralType::Func(a), StructuralType::Func(b)) => a.matches(b, type_at),
(StructuralType::Array(a), StructuralType::Array(b)) => a.matches(b, type_at),
(StructuralType::Struct(a), StructuralType::Struct(b)) => a.matches(b, type_at),
_ => false,
}
}
Expand Down Expand Up @@ -916,8 +916,8 @@ impl FuncType {
}
}

impl Inherits for FuncType {
fn inherits<'a, F>(&self, other: &Self, type_at: &F) -> bool
impl Matches for FuncType {
fn matches<'a, F>(&self, other: &Self, type_at: &F) -> bool
where
F: Fn(u32) -> &'a SubType,
{
Expand All @@ -929,47 +929,47 @@ impl Inherits for FuncType {
.params()
.iter()
.zip(other.params())
.all(|(a, b)| b.inherits(a, type_at))
.all(|(a, b)| b.matches(a, type_at))
&& self
.results()
.iter()
.zip(other.results())
.all(|(a, b)| a.inherits(b, type_at))
.all(|(a, b)| a.matches(b, type_at))
}
}

impl Inherits for ArrayType {
fn inherits<'a, F>(&self, other: &Self, type_at: &F) -> bool
impl Matches for ArrayType {
fn matches<'a, F>(&self, other: &Self, type_at: &F) -> bool
where
F: Fn(u32) -> &'a SubType,
{
self.0.inherits(&other.0, type_at)
self.0.matches(&other.0, type_at)
}
}

impl Inherits for FieldType {
fn inherits<'a, F>(&self, other: &Self, type_at: &F) -> bool
impl Matches for FieldType {
fn matches<'a, F>(&self, other: &Self, type_at: &F) -> bool
where
F: Fn(u32) -> &'a SubType,
{
(other.mutable || !self.mutable) && self.element_type.inherits(&other.element_type, type_at)
(other.mutable || !self.mutable) && self.element_type.matches(&other.element_type, type_at)
}
}

impl Inherits for StorageType {
fn inherits<'a, F>(&self, other: &Self, type_at: &F) -> bool
impl Matches for StorageType {
fn matches<'a, F>(&self, other: &Self, type_at: &F) -> bool
where
F: Fn(u32) -> &'a SubType,
{
match (self, other) {
(Self::Val(a), Self::Val(b)) => a.inherits(b, type_at),
(Self::Val(a), Self::Val(b)) => a.matches(b, type_at),
(a @ (Self::I8 | Self::I16 | Self::Val(_)), b) => a == b,
}
}
}

impl Inherits for StructType {
fn inherits<'a, F>(&self, other: &Self, type_at: &F) -> bool
impl Matches for StructType {
fn matches<'a, F>(&self, other: &Self, type_at: &F) -> bool
where
F: Fn(u32) -> &'a SubType,
{
Expand All @@ -979,7 +979,7 @@ impl Inherits for StructType {
.fields
.iter()
.zip(other.fields.iter())
.all(|(a, b)| a.inherits(b, type_at))
.all(|(a, b)| a.matches(b, type_at))
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/wasmparser/src/validator/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{collections::HashSet, sync::Arc};
use indexmap::IndexMap;

use crate::limits::*;
use crate::readers::Inherits;
use crate::readers::Matches;
use crate::validator::core::arc::MaybeOwned;
use crate::{
BinaryReaderError, ConstExpr, Data, DataKind, Element, ElementKind, ExternalKind, FuncType,
Expand Down Expand Up @@ -568,7 +568,7 @@ impl Module {
}
match self.type_at(types, supertype_index, offset)? {
Type::Sub(st) => {
if !&ty.inherits(st, &|idx| self.subtype_at(types, idx, offset).unwrap()) {
if !&ty.matches(st, &|idx| self.subtype_at(types, idx, offset).unwrap()) {
bail!(offset, "subtype must match supertype");
}
}
Expand Down Expand Up @@ -957,7 +957,7 @@ impl Module {
/// E.g. a non-nullable reference can be assigned to a nullable reference, but not vice versa.
/// Or an indexed func ref is assignable to a generic func ref, but not vice versa.
pub(crate) fn matches(&self, ty1: ValType, ty2: ValType, types: &TypeList) -> bool {
ty1.inherits(&ty2, &|idx| self.subtype_at(types, idx, 0).unwrap())
ty1.matches(&ty2, &|idx| self.subtype_at(types, idx, 0).unwrap())
}

fn check_tag_type(
Expand Down

0 comments on commit 0e1d66c

Please sign in to comment.