From 29291357f11ba6f81961a0cb3e1035d7493b1c3e Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 14 May 2024 12:50:38 +0100 Subject: [PATCH] Add Type::row_var_bound --- hugr/src/types.rs | 8 ++++++++ hugr/src/types/type_param.rs | 16 ++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/hugr/src/types.rs b/hugr/src/types.rs index e763bad12..78a407e1c 100644 --- a/hugr/src/types.rs +++ b/hugr/src/types.rs @@ -351,6 +351,14 @@ impl Type { TypeBound::Copyable.contains(self.least_upper_bound()) } + /// If this Type is a row variable, return its bound, otherwise None + pub fn row_var_bound(&self) -> Option { + match self.0 { + TypeEnum::RowVariable(_, b) => Some(b), + _ => None, + } + } + /// Checks that this [Type] represents a single Type, not a row variable, /// that all variables used within are in the provided list of bound variables, /// and that for each [CustomType], the corresponding diff --git a/hugr/src/types/type_param.rs b/hugr/src/types/type_param.rs index 92daa58ab..9cfba9852 100644 --- a/hugr/src/types/type_param.rs +++ b/hugr/src/types/type_param.rs @@ -12,7 +12,6 @@ use crate::extension::ExtensionRegistry; use crate::extension::ExtensionSet; use crate::extension::SignatureError; -use super::TypeEnum; use super::{check_typevar_decl, CustomType, Substitution, Type, TypeBound}; /// The upper non-inclusive bound of a [`TypeParam::BoundedNat`] @@ -366,7 +365,7 @@ fn check_type_arg_rv( ) if param.contains(cached_decl) => Ok(()), (TypeArg::Type { ty }, TypeParam::Type { b: bound }) if bound.contains(ty.least_upper_bound()) - && (allow_rowvars || !matches!(ty.0, TypeEnum::RowVariable(_, _))) => + && (allow_rowvars || ty.row_var_bound().is_none()) => { Ok(()) } @@ -378,12 +377,13 @@ fn check_type_arg_rv( } // Also allow a single "Type" to be used for a List *only* if the Type is a row variable // (i.e., it's not really a Type, it's multiple Types) - ( - TypeArg::Type { - ty: Type(TypeEnum::RowVariable(_, b), _), - }, - TypeParam::List { param }, - ) if param.contains(&(*b).into()) => Ok(()), + (TypeArg::Type { ty }, TypeParam::List { param }) + if ty + .row_var_bound() + .is_some_and(|b| param.contains(&b.into())) => + { + Ok(()) + } (TypeArg::Sequence { elems: items }, TypeParam::Tuple { params: types }) => { if items.len() != types.len() {