Skip to content

Commit baefd93

Browse files
authored
[NFC] Add some comments about flow in SubtypingDiscoverer and Unsubtyping (#6359)
I audited all of SubtypingDiscoverer for flow/non-flow constraints and added some comments to clarify things for our future selves if we ever need to generalize it.
1 parent 1ef95ae commit baefd93

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/ir/subtype-exprs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ struct SubtypingDiscoverer : public OverriddenVisitor<SubType> {
161161
// sources, call_indirect target types may be supertypes of their source
162162
// table types. In this case, the cast will always succeed, but only if we
163163
// keep the types related.
164+
// TODO: No value flows here, so we could use |noteNonFlowSubtype|, but
165+
// this is a trivial situation that is not worth optimizing.
164166
self()->noteSubtype(tableType, curr->heapType);
165167
} else if (HeapType::isSubType(curr->heapType, tableType)) {
166168
self()->noteCast(tableType, curr->heapType);
@@ -256,6 +258,8 @@ struct SubtypingDiscoverer : public OverriddenVisitor<SubType> {
256258
void visitTupleExtract(TupleExtract* curr) {}
257259
void visitRefI31(RefI31* curr) {}
258260
void visitI31Get(I31Get* curr) {
261+
// This could be |noteNonFlowSubtype| but as there are no subtypes of i31
262+
// it does not matter.
259263
self()->noteSubtype(curr->i31, Type(HeapType::i31, Nullable));
260264
}
261265
void visitCallRef(CallRef* curr) {
@@ -271,6 +275,9 @@ struct SubtypingDiscoverer : public OverriddenVisitor<SubType> {
271275
// generalize, should they generalize the target more or the parameters
272276
// more? etc.), so we do the simple thing here for now of requiring the
273277
// target type not generalize.
278+
//
279+
// Note that this could be |noteNonFlowSubtype| but since we are comparing
280+
// a type to itself here, that does not matter.
274281
self()->noteSubtype(curr->target, curr->target->type);
275282

276283
if (curr->target->type.isSignature()) {

src/passes/Unsubtyping.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,17 @@ struct Unsubtyping
201201
// basic type then we can simply ignore this: we only remove subtyping
202202
// between user types, so subtyping wrt basic types is unchanged, and so
203203
// this constraint will never be a problem.
204+
//
205+
// This is sort of a hack because in general to be precise we should not
206+
// just consider basic types here - in general, we should note for each
207+
// constraint whether it is a flow-based one or not, and only take the
208+
// flow-based ones into account when looking at the impact of casts.
209+
// However, in practice this is enough as the only non-trivial case of
210+
// |noteNonFlowSubtype| is for RefEq, which uses a basic type (eqref). Other
211+
// cases of non-flow subtyping end up trivial, e.g., the target of a
212+
// CallRef is compared to itself (and we ignore constraints of A :> A).
213+
// However, if we change how |noteNonFlowSubtype| is used in
214+
// SubtypingDiscoverer then we may need to generalize this.
204215
if (super.isRef() && super.getHeapType().isBasic()) {
205216
return;
206217
}

0 commit comments

Comments
 (0)