Refactor type checks when popping refs#1730
Merged
fitzgen merged 1 commit intobytecodealliance:mainfrom Aug 21, 2024
Merged
Conversation
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
commented
Aug 20, 2024
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) | ||
| } |
Member
Author
There was a problem hiding this comment.
@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.
fitzgen
approved these changes
Aug 21, 2024
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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit refactors users of
pop_refduring validation to be able to pass in an expected type. This removes the need for a few manualis_subtypechecks and helps centralize type-checking in one location.