From 04cc830afd456ac42a4417052e6dd44e91f99426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Mon, 9 Mar 2020 18:22:25 -0700 Subject: [PATCH] Simplify a few patterns in the move prover Closes: #2870 Approved by: wqfish --- .../move-lang/src/to_bytecode/translate.rs | 6 ++-- language/move-prover/spec-lang/src/env.rs | 36 +++++++------------ language/move-prover/spec-lang/src/lib.rs | 9 +++-- .../move-prover/spec-lang/src/translate.rs | 12 ++----- types/src/ledger_info.rs | 9 +++-- 5 files changed, 24 insertions(+), 48 deletions(-) diff --git a/language/move-lang/src/to_bytecode/translate.rs b/language/move-lang/src/to_bytecode/translate.rs index 6b78bb70d1cf..f729e9b2c2ad 100644 --- a/language/move-lang/src/to_bytecode/translate.rs +++ b/language/move-lang/src/to_bytecode/translate.rs @@ -199,10 +199,8 @@ fn module( }; let deps: Vec<&F::CompiledModule> = vec![]; let (compiled_module, source_map) = - match ir_to_bytecode::compiler::compile_module(addr, ir_module, deps) { - Ok(res) => res, - Err(e) => return Err(vec![(ident.loc(), format!("IR ERROR: {}", e))]), - }; + ir_to_bytecode::compiler::compile_module(addr, ir_module, deps) + .map_err(|e| vec![(ident.loc(), format!("IR ERROR: {}", e))])?; Ok((mname, compiled_module, source_map)) } diff --git a/language/move-prover/spec-lang/src/env.rs b/language/move-prover/spec-lang/src/env.rs index 733c0177a5b9..5ee5813b2c1a 100644 --- a/language/move-prover/spec-lang/src/env.rs +++ b/language/move-prover/spec-lang/src/env.rs @@ -721,14 +721,10 @@ impl<'env> ModuleEnv<'env> { /// Gets a FunctionEnv in this module by name. pub fn find_function(&self, name: Symbol) -> Option> { let id = FunId(name); - if let Some(data) = self.data.function_data.get(&id) { - Some(FunctionEnv { - module_env: self.clone(), - data, - }) - } else { - None - } + self.data.function_data.get(&id).map(|data| FunctionEnv { + module_env: self.clone(), + data, + }) } /// Gets a FunctionEnv by id. @@ -782,14 +778,10 @@ impl<'env> ModuleEnv<'env> { /// Gets a StructEnv in this module by name. pub fn find_struct(&self, name: Symbol) -> Option> { let id = StructId(name); - if let Some(data) = self.data.struct_data.get(&id) { - Some(StructEnv { - module_env: self.clone(), - data, - }) - } else { - None - } + self.data.struct_data.get(&id).map(|data| StructEnv { + module_env: self.clone(), + data, + }) } /// Gets the struct id from a definition index which must be valid for this environment. @@ -1100,14 +1092,10 @@ impl<'env> StructEnv<'env> { /// Find a field by its name. pub fn find_field(&'env self, name: Symbol) -> Option> { let id = FieldId(name); - if let Some(data) = self.data.field_data.get(&id) { - Some(FieldEnv { - struct_env: self.clone(), - data, - }) - } else { - None - } + self.data.field_data.get(&id).map(|data| FieldEnv { + struct_env: self.clone(), + data, + }) } /// Returns the type parameters associated with this struct. diff --git a/language/move-prover/spec-lang/src/lib.rs b/language/move-prover/spec-lang/src/lib.rs index 19bb88ccd428..3ebe3f3c316d 100644 --- a/language/move-prover/spec-lang/src/lib.rs +++ b/language/move-prover/spec-lang/src/lib.rs @@ -32,11 +32,10 @@ pub fn run_spec_lang_compiler( deps: Vec, address_opt: Option<&str>, ) -> anyhow::Result { - let address_opt = if let Some(s) = address_opt { - Some(Address::parse_str(s).map_err(|s| anyhow!(s))?) - } else { - None - }; + let address_opt = address_opt + .map(Address::parse_str) + .transpose() + .map_err(|s| anyhow!(s))?; let mut env = GlobalEnv::new(); // First pass: compile move code. diff --git a/language/move-prover/spec-lang/src/translate.rs b/language/move-prover/spec-lang/src/translate.rs index c8fc355dd191..88b07914c24e 100644 --- a/language/move-prover/spec-lang/src/translate.rs +++ b/language/move-prover/spec-lang/src/translate.rs @@ -1938,11 +1938,7 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo expected_type: &Type, ) -> Exp { // Translate generic arguments, if any. - let generics = if let Some(ts) = generics { - Some(self.translate_single_types(&ts)) - } else { - None - }; + let generics = generics.as_ref().map(|ts| self.translate_single_types(&ts)); // Translate arguments. let (arg_types, args) = self.translate_exp_list(args); // Lookup candidates. @@ -2117,11 +2113,7 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo ) -> Exp { let struct_name = self.parent.module_access_to_qualified(maccess); let struct_name_loc = self.to_loc(&maccess.loc); - let generics = if let Some(ts) = generics { - Some(self.translate_single_types(&ts)) - } else { - None - }; + let generics = generics.as_ref().map(|ts| self.translate_single_types(&ts)); if let Some(entry) = self.parent.parent.struct_table.get(&struct_name) { let entry = entry.clone(); let (instantiation, diag) = self.make_instantiation(entry.type_params.len(), generics); diff --git a/types/src/ledger_info.rs b/types/src/ledger_info.rs index b7fd0daed600..ead58081e0b4 100644 --- a/types/src/ledger_info.rs +++ b/types/src/ledger_info.rs @@ -137,11 +137,10 @@ impl TryFrom for LedgerInfo { let round = proto.round; let timestamp_usecs = proto.timestamp_usecs; - let next_validator_set = if let Some(validator_set_proto) = proto.next_validator_set { - Some(ValidatorSet::try_from(validator_set_proto)?) - } else { - None - }; + let next_validator_set = proto + .next_validator_set + .map(ValidatorSet::try_from) + .transpose()?; Ok(LedgerInfo::new( BlockInfo::new( epoch,