Skip to content

Refactor type checks when popping refs#1730

Merged
fitzgen merged 1 commit intobytecodealliance:mainfrom
alexcrichton:remove-is-subtype-checks
Aug 21, 2024
Merged

Refactor type checks when popping refs#1730
fitzgen merged 1 commit intobytecodealliance:mainfrom
alexcrichton:remove-is-subtype-checks

Conversation

@alexcrichton
Copy link
Copy Markdown
Member

This commit refactors users of pop_ref during validation to be able to pass in an expected type. This removes the need for a few manual is_subtype checks and helps centralize type-checking in one location.

This commit refactors users of `pop_ref` during validation to be able to
pass in an expected type. This removes the need for a few manual
`is_subtype` checks and helps centralize type-checking in one location.
@alexcrichton alexcrichton requested a review from fitzgen August 20, 2024 21:31
Comment on lines 1133 to 1135
/// instructions. Returns the given `heap_type` as a `ValType`.
fn check_downcast(
&mut self,
nullable: bool,
mut heap_type: HeapType,
inst_name: &str,
) -> Result<ValType> {
fn check_downcast(&mut self, nullable: bool, mut heap_type: HeapType) -> Result<RefType> {
self.resources
.check_heap_type(&mut heap_type, self.offset)?;

let sub_ty = RefType::new(nullable, heap_type)
.map(ValType::from)
.ok_or_else(|| {
BinaryReaderError::new("implementation limit: type index too large", self.offset)
})?;

let sup_ty = self.pop_ref()?.unwrap_or_else(|| {
sub_ty
.as_reference_type()
.expect("we created this as a reference just above")
});
let sup_ty = RefType::new(true, self.resources.top_type(&sup_ty.heap_type()))
let sub_ty = RefType::new(nullable, heap_type).ok_or_else(|| {
BinaryReaderError::new("implementation limit: type index too large", self.offset)
})?;
let sup_ty = RefType::new(true, self.resources.top_type(&heap_type))
.expect("can't panic with non-concrete heap types");

if !self.resources.is_subtype(sub_ty, sup_ty.into()) {
bail!(
self.offset,
"{inst_name}'s heap type must be a sub type of the type on the stack: \
{sub_ty} is not a sub type of {sup_ty}"
);
}

self.pop_ref(Some(sup_ty))?;
Ok(sub_ty)
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@fitzgen asking for your review on this PR, and in particular for the changes in this function. I don't believe I'm preserving the precise shape of what was here previously but I think I'm maintaining the spec still, but wanted to confirm.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, this looks good to me.

Comment on lines 1133 to 1135
/// instructions. Returns the given `heap_type` as a `ValType`.
fn check_downcast(
&mut self,
nullable: bool,
mut heap_type: HeapType,
inst_name: &str,
) -> Result<ValType> {
fn check_downcast(&mut self, nullable: bool, mut heap_type: HeapType) -> Result<RefType> {
self.resources
.check_heap_type(&mut heap_type, self.offset)?;

let sub_ty = RefType::new(nullable, heap_type)
.map(ValType::from)
.ok_or_else(|| {
BinaryReaderError::new("implementation limit: type index too large", self.offset)
})?;

let sup_ty = self.pop_ref()?.unwrap_or_else(|| {
sub_ty
.as_reference_type()
.expect("we created this as a reference just above")
});
let sup_ty = RefType::new(true, self.resources.top_type(&sup_ty.heap_type()))
let sub_ty = RefType::new(nullable, heap_type).ok_or_else(|| {
BinaryReaderError::new("implementation limit: type index too large", self.offset)
})?;
let sup_ty = RefType::new(true, self.resources.top_type(&heap_type))
.expect("can't panic with non-concrete heap types");

if !self.resources.is_subtype(sub_ty, sup_ty.into()) {
bail!(
self.offset,
"{inst_name}'s heap type must be a sub type of the type on the stack: \
{sub_ty} is not a sub type of {sup_ty}"
);
}

self.pop_ref(Some(sup_ty))?;
Ok(sub_ty)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, this looks good to me.

@fitzgen fitzgen added this pull request to the merge queue Aug 21, 2024
Merged via the queue into bytecodealliance:main with commit 2371dca Aug 21, 2024
@alexcrichton alexcrichton deleted the remove-is-subtype-checks branch August 21, 2024 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants