Skip to content

Commit

Permalink
[cleanup] return Result instead of panic-ing in TypeLayoutBuilder
Browse files Browse the repository at this point in the history
No need to panic here, just makes life harder for a caller that made a mistake.

Closes: diem#50
  • Loading branch information
sblackshear authored and bors-diem committed Feb 16, 2022
1 parent 54e3c30 commit 595b6d0
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions language/tools/move-bytecode-utils/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::module_cache::GetModule;
use anyhow::{bail, Result};
use anyhow::{anyhow, bail, Result};
use move_binary_format::{
access::ModuleAccess,
file_format::{SignatureToken, StructDefinition, StructFieldInformation, StructHandleIndex},
Expand Down Expand Up @@ -363,11 +363,9 @@ impl StructLayoutBuilder {
layout_type: LayoutType,
) -> Result<MoveStructLayout> {
let s_handle = m.struct_handle_at(s.struct_handle);
assert_eq!(
s_handle.type_parameters.len(),
type_arguments.len(),
"Wrong number of type arguments for struct",
);
if s_handle.type_parameters.len() != type_arguments.len() {
bail!("Wrong number of type arguments for struct")
}
match &s.field_information {
StructFieldInformation::Native => {
bail!("Can't extract fields for native struct")
Expand Down Expand Up @@ -426,19 +424,20 @@ impl StructLayoutBuilder {
resolver: &impl GetModule,
layout_type: LayoutType,
) -> Result<MoveStructLayout> {
let module = resolver
.get_module_by_id(declaring_module)
.unwrap_or_else(|_| panic!("Error while resolving module {}", declaring_module))
.unwrap();
let module = match resolver.get_module_by_id(declaring_module) {
Err(_) | Ok(None) => bail!("Could not find module"),
Ok(Some(m)) => m,
};
let def = module
.borrow()
.find_struct_def_by_name(name)
.unwrap_or_else(|| {
panic!(
.ok_or_else(|| {
anyhow!(
"Could not find struct named {} in module {}",
name, declaring_module
name,
declaring_module
)
});
})?;
Self::build_from_definition(module.borrow(), def, type_arguments, resolver, layout_type)
}

Expand Down

0 comments on commit 595b6d0

Please sign in to comment.