-
Notifications
You must be signed in to change notification settings - Fork 219
/
matches.rs
277 lines (252 loc) · 9.41 KB
/
matches.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//! Implementation of matching (structural subtyping) for core Wasm types.
//!
//! We only ever do structural matching for one link at a time in a subtype
//! chain. That is, we never recurse on new `SubType`s. This is because
//! subtyping relations are required to be declared, so the earlier links in the
//! chain were already checked when we processed those declarations.
//!
//! Note that while we don't recursively *match* on each sub- and supertype field
//! when checking whether a struct type matches another struct type, we do check
//! that either `field_type_a == field_type b` or that it was previously
//! declared that `field_type a <: field_type b`. The latter case means that we
//! previously checked that they matched when we saw the declaration, and we
//! don't need to match again; we just look at the declarations from now on.
use crate::{
types::{CoreTypeId, RecGroupId, TypeList},
ArrayType, CompositeType, FieldType, FuncType, RefType, StorageType, StructType, SubType,
ValType,
};
/// Wasm type matching.
pub trait Matches {
/// Does `a` structurally match `b`?
///
/// Both `a` and `b` must be canonicalized already.
///
/// This is expected to recursively break down and inspect the *parts* of
/// `Self` but should always bottom out in subtype checks, rather than
/// looping back to new match calls on a *whole* new `Self`. This is what
/// maintains the "single-link-in-the-chain" property mentioned in the
/// module comment above.
fn matches(types: &TypeList, a: Self, b: Self) -> bool;
}
/// A `T` with its containing `RecGroupId`.
///
/// The `RecGroupId` can be used to resolve canonicalized type references that
/// are indices into the local rec group.
#[derive(Debug, Copy, Clone)]
pub(crate) struct WithRecGroup<T> {
inner: T,
rec_group_id: RecGroupId,
}
impl<T> WithRecGroup<T> {
#[inline]
fn rec_group(x: Self) -> RecGroupId {
x.rec_group_id
}
}
impl<T> std::ops::Deref for WithRecGroup<T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
&self.inner
}
}
impl<T> std::ops::DerefMut for WithRecGroup<T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl WithRecGroup<CoreTypeId> {
/// Construct a new `WithRecGroup<CoreTypeId>` by looking up the
/// `CoreTypeId`'s rec group id in the `TypeList`.
pub(crate) fn new(types: &TypeList, id: CoreTypeId) -> Self {
let rec_group_id = types.rec_group_id_of(id);
WithRecGroup {
inner: id,
rec_group_id,
}
}
}
impl<T> WithRecGroup<T> {
/// Project into a field of the inner value, while maintaining the
/// `RecGroupId` context.
pub(crate) fn map<U>(x: Self, f: impl FnOnce(T) -> U) -> WithRecGroup<U> {
WithRecGroup {
inner: f(x.inner),
rec_group_id: x.rec_group_id,
}
}
}
impl<'a> Matches for WithRecGroup<&'a SubType> {
fn matches(types: &TypeList, a: Self, b: Self) -> bool {
// NB: matching does not check finality and supertypes. That is checked
// once when we define types, not repeatedly every time we check
// matches.
Matches::matches(
types,
WithRecGroup::map(a, |a| &a.composite_type),
WithRecGroup::map(b, |b| &b.composite_type),
)
}
}
impl<'a> Matches for WithRecGroup<&'a CompositeType> {
fn matches(types: &TypeList, a: Self, b: Self) -> bool {
match (&*a, &*b) {
(CompositeType::Func(fa), CompositeType::Func(fb)) => Matches::matches(
types,
WithRecGroup::map(a, |_| fa),
WithRecGroup::map(b, |_| fb),
),
(CompositeType::Func(_), _) => false,
(CompositeType::Array(aa), CompositeType::Array(ab)) => Matches::matches(
types,
WithRecGroup::map(a, |_| *aa),
WithRecGroup::map(b, |_| *ab),
),
(CompositeType::Array(_), _) => false,
(CompositeType::Struct(sa), CompositeType::Struct(sb)) => Matches::matches(
types,
WithRecGroup::map(a, |_| sa),
WithRecGroup::map(b, |_| sb),
),
(CompositeType::Struct(_), _) => false,
}
}
}
impl<'a> Matches for WithRecGroup<&'a FuncType> {
fn matches(types: &TypeList, a: Self, b: Self) -> bool {
if a.params().len() != b.params().len() || a.results().len() != b.results().len() {
return false;
}
// A quick recap of covariance, contravariance, and how it applies to
// subtyping function types, because I (and almost everyone else, it
// seems) always need a reminder:
//
// *Covariance* is when `a <: b` and `a' <: b'`. That is, the subtyping
// checks on the nested things (`a'` and `b'`) goes the same way as the
// outer things (`a` and `b`).
//
// *Contravariance* is when `a <: b` and `b' <: a'`. That is, the
// subtyping on the nested things is reversed compared to the outer
// things.
//
// Now, when we are checking subtyping for function types, we have the
// following variance:
//
// * Parameters are contravariant: `(a -> c) <: (b -> c)` when `b <: a`.
//
// For example, we can substitute a `Cat -> Cat` function with a
// `Animal -> Cat` function because `Cat <: Animal` and so all
// arguments that could be given to the function are still valid.
//
// We can't do the opposite and replace an `Animal -> Cat` function
// with a `Cat -> Cat` function. What would the `Cat -> Cat` function
// do when given a `Dog`? It is unsound.
//
// * Results are covariant: `(a -> b) <: (a -> c)` when `b <: c`.
//
// For example, we can substitute a `Cat -> Animal` function with a
// `Cat -> Cat` function because callers expect to be returned an
// `Animal` and all `Cat`s are `Animal`s. (Also: all `Cat`s are
// `Beautiful`!)
//
// We cannot do the opposite and substitute a `Cat -> Cat` function
// with a `Cat -> Animal` function, since callers expect a `Cat` but
// the new function could return a `Pig`.
//
// As always, Wikipedia is also helpful:
// https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)
let params_match = a.params().iter().zip(b.params()).all(|(pa, pb)| {
// Parameters are contravariant.
Matches::matches(
types,
WithRecGroup::map(b, |_| *pb),
WithRecGroup::map(a, |_| *pa),
)
});
if !params_match {
return false;
}
a.results().iter().zip(b.results()).all(|(ra, rb)| {
// Results are covariant.
Matches::matches(
types,
WithRecGroup::map(a, |_| *ra),
WithRecGroup::map(b, |_| *rb),
)
})
}
}
impl Matches for WithRecGroup<ArrayType> {
fn matches(types: &TypeList, a: Self, b: Self) -> bool {
Matches::matches(
types,
WithRecGroup::map(a, |a| a.0),
WithRecGroup::map(b, |b| b.0),
)
}
}
impl<'a> Matches for WithRecGroup<&'a StructType> {
fn matches(types: &TypeList, a: Self, b: Self) -> bool {
// Note: Struct types support width and depth subtyping.
a.fields.len() >= b.fields.len()
&& a.fields.iter().zip(b.fields.iter()).all(|(fa, fb)| {
Matches::matches(
types,
WithRecGroup::map(a, |_| *fa),
WithRecGroup::map(b, |_| *fb),
)
})
}
}
impl Matches for WithRecGroup<FieldType> {
fn matches(types: &TypeList, a: Self, b: Self) -> bool {
(b.mutable || !a.mutable)
&& Matches::matches(
types,
WithRecGroup::map(a, |a| a.element_type),
WithRecGroup::map(b, |b| b.element_type),
)
}
}
impl Matches for WithRecGroup<StorageType> {
fn matches(types: &TypeList, a: Self, b: Self) -> bool {
use StorageType as ST;
match (*a, *b) {
(ST::I8, ST::I8) | (ST::I16, ST::I16) => true,
(ST::I8 | ST::I16, _) => false,
(ST::Val(va), ST::Val(vb)) => Matches::matches(
types,
WithRecGroup::map(a, |_| va),
WithRecGroup::map(b, |_| vb),
),
(ST::Val(_), _) => false,
}
}
}
impl Matches for WithRecGroup<ValType> {
fn matches(types: &TypeList, a: Self, b: Self) -> bool {
match (*a, *b) {
(ValType::Ref(ra), ValType::Ref(rb)) => Matches::matches(
types,
WithRecGroup::map(a, |_| ra),
WithRecGroup::map(b, |_| rb),
),
(ValType::Ref(_), _) => false,
(ValType::I32 | ValType::I64 | ValType::F32 | ValType::F64 | ValType::V128, _) => {
*a == *b
}
}
}
}
impl Matches for WithRecGroup<RefType> {
fn matches(types: &TypeList, a: Self, b: Self) -> bool {
types.reftype_is_subtype_impl(
*a,
Some(WithRecGroup::rec_group(a)),
*b,
Some(WithRecGroup::rec_group(b)),
)
}
}