From 595b6d02702215061d1e0bd615560b12a1c24e65 Mon Sep 17 00:00:00 2001 From: Sam Blackshear Date: Fri, 11 Feb 2022 17:08:47 -0800 Subject: [PATCH] [cleanup] return Result instead of panic-ing in TypeLayoutBuilder No need to panic here, just makes life harder for a caller that made a mistake. Closes: #50 --- .../tools/move-bytecode-utils/src/layout.rs | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/language/tools/move-bytecode-utils/src/layout.rs b/language/tools/move-bytecode-utils/src/layout.rs index f41eac61fb..6adbd52340 100644 --- a/language/tools/move-bytecode-utils/src/layout.rs +++ b/language/tools/move-bytecode-utils/src/layout.rs @@ -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}, @@ -363,11 +363,9 @@ impl StructLayoutBuilder { layout_type: LayoutType, ) -> Result { 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") @@ -426,19 +424,20 @@ impl StructLayoutBuilder { resolver: &impl GetModule, layout_type: LayoutType, ) -> Result { - 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) }