Skip to content

Commit

Permalink
Simplify a few patterns in the move prover
Browse files Browse the repository at this point in the history
Closes: diem#2870
Approved by: wqfish
  • Loading branch information
huitseeker authored and bors-libra committed Mar 11, 2020
1 parent 76d43b0 commit 04cc830
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 48 deletions.
6 changes: 2 additions & 4 deletions language/move-lang/src/to_bytecode/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
36 changes: 12 additions & 24 deletions language/move-prover/spec-lang/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,14 +721,10 @@ impl<'env> ModuleEnv<'env> {
/// Gets a FunctionEnv in this module by name.
pub fn find_function(&self, name: Symbol) -> Option<FunctionEnv<'env>> {
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.
Expand Down Expand Up @@ -782,14 +778,10 @@ impl<'env> ModuleEnv<'env> {
/// Gets a StructEnv in this module by name.
pub fn find_struct(&self, name: Symbol) -> Option<StructEnv<'_>> {
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.
Expand Down Expand Up @@ -1100,14 +1092,10 @@ impl<'env> StructEnv<'env> {
/// Find a field by its name.
pub fn find_field(&'env self, name: Symbol) -> Option<FieldEnv<'env>> {
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.
Expand Down
9 changes: 4 additions & 5 deletions language/move-prover/spec-lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ pub fn run_spec_lang_compiler(
deps: Vec<String>,
address_opt: Option<&str>,
) -> anyhow::Result<GlobalEnv> {
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.
Expand Down
12 changes: 2 additions & 10 deletions language/move-prover/spec-lang/src/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions types/src/ledger_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,10 @@ impl TryFrom<crate::proto::types::LedgerInfo> 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,
Expand Down

0 comments on commit 04cc830

Please sign in to comment.