From e8a62166e9067c328b2cb4b9b981c93fa3e233b7 Mon Sep 17 00:00:00 2001 From: "Brian R. Murphy" <132495859+brmataptos@users.noreply.github.com> Date: Mon, 30 Sep 2024 11:21:26 -0700 Subject: [PATCH] move type_has_function into Type as has_function, as suggested by Vineeth. --- .../src/env_pipeline/function_checker.rs | 20 ++++++------------- .../env_pipeline/recursive_struct_checker.rs | 11 +++++----- third_party/move/move-model/src/ty.rs | 20 ++++++++++++++----- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/third_party/move/move-compiler-v2/src/env_pipeline/function_checker.rs b/third_party/move/move-compiler-v2/src/env_pipeline/function_checker.rs index 8085c408fb6c1f..d7dbcc3cfce2b3 100644 --- a/third_party/move/move-compiler-v2/src/env_pipeline/function_checker.rs +++ b/third_party/move/move-compiler-v2/src/env_pipeline/function_checker.rs @@ -15,21 +15,13 @@ use std::{collections::BTreeSet, iter::Iterator, ops::Deref, vec::Vec}; type QualifiedFunId = QualifiedId; -fn type_has_function(ty: &Type) -> bool { - match ty { - Type::Tuple(tys) => tys.iter().any(|ty| ty.is_function()), - Type::Fun(..) => true, - _ => false, - } -} - // Takes a list of function types, returns those which have a function type in their argument type -fn identify_function_types_with_functions_in_args(func_returns: Vec) -> Vec { - func_returns +fn identify_function_types_with_functions_in_args(func_types: Vec) -> Vec { + func_types .into_iter() .filter_map(|ty| { if let Type::Fun(argt, _) = &ty { - if type_has_function(argt.deref()) { + if argt.deref().has_function() { Some(ty) } else { None @@ -44,14 +36,14 @@ fn identify_function_types_with_functions_in_args(func_returns: Vec) -> Ve // Takes a list of function-typed parameters, along with argument and result type // Returns a list of any parameters whose result type has a function value, along with that result type. fn identify_function_typed_params_with_functions_in_rets( - func_params: Vec<&Parameter>, + func_types: Vec<&Parameter>, ) -> Vec<(&Parameter, &Type)> { - func_params + func_types .iter() .filter_map(|param| { if let Type::Fun(_argt, rest) = ¶m.1 { let rest_unboxed = rest.deref(); - if type_has_function(rest_unboxed) { + if rest_unboxed.has_function() { Some((*param, rest_unboxed)) } else { None diff --git a/third_party/move/move-compiler-v2/src/env_pipeline/recursive_struct_checker.rs b/third_party/move/move-compiler-v2/src/env_pipeline/recursive_struct_checker.rs index 987a182f663d5e..9f4d51838ff560 100644 --- a/third_party/move/move-compiler-v2/src/env_pipeline/recursive_struct_checker.rs +++ b/third_party/move/move-compiler-v2/src/env_pipeline/recursive_struct_checker.rs @@ -117,11 +117,12 @@ impl<'a> RecursiveStructChecker<'a> { struct_name, struct_name, ); - self.mod_env.env.error_with_labels( - &struct_env.get_loc(), - "cyclic data", - vec![(field_env.get_loc().clone(), note)], - ); + self.mod_env + .env + .error_with_labels(&struct_env.get_loc(), "cyclic data", vec![( + field_env.get_loc().clone(), + note, + )]); } /// Report cyclic dependency of structs diff --git a/third_party/move/move-model/src/ty.rs b/third_party/move/move-model/src/ty.rs index 9fc0002f44ece0..158c26dd3ea9c4 100644 --- a/third_party/move/move-model/src/ty.rs +++ b/third_party/move/move-model/src/ty.rs @@ -819,16 +819,26 @@ impl Type { matches!(self, Type::Primitive(_)) } - /// Determines whether this is a reference. - pub fn is_reference(&self) -> bool { - matches!(self, Type::Reference(_, _)) - } - /// Determines whether this is a function. pub fn is_function(&self) -> bool { matches!(self, Type::Fun(..)) } + /// Determines whether this is a function or a tuple with a function; + /// this is useful to test a function parameter/return type for function values. + pub fn has_function(&self) -> bool { + match self { + Type::Tuple(tys) => tys.iter().any(|ty| ty.is_function()), + Type::Fun(..) => true, + _ => false, + } + } + + /// Determines whether this is a reference. + pub fn is_reference(&self) -> bool { + matches!(self, Type::Reference(_, _)) + } + /// If this is a reference, return the kind of the reference, otherwise None. pub fn ref_kind(&self) -> Option { if let Type::Reference(kind, _) = self {