Skip to content

Commit

Permalink
move type_has_function into Type as has_function, as suggested by Vin…
Browse files Browse the repository at this point in the history
…eeth.
  • Loading branch information
brmataptos committed Sep 30, 2024
1 parent 1782285 commit a8dba3c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,13 @@ use std::{collections::BTreeSet, iter::Iterator, ops::Deref, vec::Vec};

type QualifiedFunId = QualifiedId<FunId>;

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<Type>) -> Vec<Type> {
func_returns
fn identify_function_types_with_functions_in_args(func_types: Vec<Type>) -> Vec<Type> {
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)

Check warning on line 25 in third_party/move/move-compiler-v2/src/env_pipeline/function_checker.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-compiler-v2/src/env_pipeline/function_checker.rs#L25

Added line #L25 was not covered by tests
} else {
None
Expand All @@ -44,14 +36,14 @@ fn identify_function_types_with_functions_in_args(func_returns: Vec<Type>) -> 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) = &param.1 {
let rest_unboxed = rest.deref();
if type_has_function(rest_unboxed) {
if rest_unboxed.has_function() {
Some((*param, rest_unboxed))
} else {
None
Expand Down
20 changes: 15 additions & 5 deletions third_party/move/move-model/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReferenceKind> {
if let Type::Reference(kind, _) = self {
Expand Down

0 comments on commit a8dba3c

Please sign in to comment.