Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/ir/subtype-exprs.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,19 @@ struct SubtypingDiscoverer : public OverriddenVisitor<SubType> {
self()->noteSubtype(curr->replacement, type);
}
void visitRefAs(RefAs* curr) {
if (curr->op == RefAsNonNull) {
self()->noteCast(curr->value, curr);
switch (curr->op) {
case RefAsNonNull:
self()->noteCast(curr->value, curr);
return;
case AnyConvertExtern:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this require extern?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In principle, yes, but extern has no defined subtypes, so in practice (for Unsubtyping, at least) it doesn't matter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I guess it doesn't make a difference...

return;
case ExternConvertAny:
if (curr->type != Type::unreachable) {
auto any =
HeapTypes::any.getBasic(curr->type.getHeapType().getShared());
self()->noteSubtype(curr->value, Type(any, Nullable));
}
return;
}
}
void visitStringNew(StringNew* curr) {}
Expand Down
82 changes: 82 additions & 0 deletions test/lit/passes/unsubtyping.wast
Original file line number Diff line number Diff line change
Expand Up @@ -1920,3 +1920,85 @@
)
)
)

;; extern.convert_any requires its input to remain a subtype of any.
(module
;; CHECK: (rec
;; CHECK-NEXT: (type $A (sub (struct)))
(type $A (sub (struct)))
;; CHECK: (type $B (sub $A (struct)))
(type $B (sub $A (struct)))

;; CHECK: (type $2 (func (result externref)))

;; CHECK: (type $3 (func (param anyref)))

;; CHECK: (func $test (type $2) (result externref)
;; CHECK-NEXT: (extern.convert_any
;; CHECK-NEXT: (struct.new_default $B)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $test (result externref)
(extern.convert_any
(struct.new $B)
)
)

;; CHECK: (func $cast (type $3) (param $0 anyref)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.cast (ref null $A)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $cast (param anyref)
(drop
;; Since $B flows into any, it must remain a subtype of $A to continue
;; passing this cast.
(ref.cast (ref null $A)
(local.get 0)
)
)
)
)

;; Same as above with shared types.
(module
;; CHECK: (rec
;; CHECK-NEXT: (type $A (sub (shared (struct))))
(type $A (sub (shared (struct))))
;; CHECK: (type $B (sub $A (shared (struct))))
(type $B (sub $A (shared (struct))))

;; CHECK: (type $2 (func (result (ref null (shared extern)))))

;; CHECK: (type $3 (func (param (ref null (shared any)))))

;; CHECK: (func $test (type $2) (result (ref null (shared extern)))
;; CHECK-NEXT: (extern.convert_any
;; CHECK-NEXT: (struct.new_default $B)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $test (result (ref null (shared extern)))
(extern.convert_any
(struct.new $B)
)
)

;; CHECK: (func $cast (type $3) (param $0 (ref null (shared any)))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.cast (ref null $A)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $cast (param (ref null (shared any)))
(drop
;; Since $B flows into any, it must remain a subtype of $A to continue
;; passing this cast.
(ref.cast (ref null $A)
(local.get 0)
)
)
)
)
Loading