Skip to content

Commit

Permalink
refactor: Remove "trait TypeParametrised" (#1019)
Browse files Browse the repository at this point in the history
The docs say it was intended for sharing code between OpDef and TypeDef,
but it was actually impl'd only by TypeDef (following earlier cleanups I
think). So, simplify by removing it...(nothing here was ever `pub`)
  • Loading branch information
acl-cqc committed May 10, 2024
1 parent 3d8f6f6 commit 69635f0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 30 deletions.
19 changes: 1 addition & 18 deletions hugr/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use crate::hugr::IdentList;
use crate::ops::constant::{ValueName, ValueNameRef};
use crate::ops::custom::{ExtensionOp, OpaqueOp};
use crate::ops::{self, OpName, OpNameRef};
use crate::types::type_param::{check_type_args, TypeArgError};
use crate::types::type_param::{TypeArg, TypeParam};
use crate::types::type_param::{TypeArg, TypeArgError, TypeParam};
use crate::types::{check_typevar_decl, CustomType, Substitution, TypeBound, TypeName};
use crate::types::{FunctionType, TypeNameRef};

Expand Down Expand Up @@ -233,22 +232,6 @@ impl CustomConcrete for CustomType {
}
}

/// Type-parametrised functionality shared between [`TypeDef`] and [`OpDef`].
trait TypeParametrised {
/// The concrete object built by binding type arguments to parameters
type Concrete: CustomConcrete;
/// The extension-unique name.
fn name(&self) -> &<Self::Concrete as CustomConcrete>::Identifier;
/// Type parameters.
fn params(&self) -> &[TypeParam];
/// The parent extension.
fn extension(&self) -> &ExtensionId;
/// Check provided type arguments are valid against parameters.
fn check_args_impl(&self, args: &[TypeArg]) -> Result<(), SignatureError> {
check_type_args(args, self.params()).map_err(SignatureError::TypeArgMismatch)
}
}

/// A constant value provided by a extension.
/// Must be an instance of a type available to the extension.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion hugr/src/extension/declarative/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};
use smol_str::SmolStr;

use crate::extension::prelude::PRELUDE_ID;
use crate::extension::{CustomValidator, ExtensionSet, SignatureFunc, TypeDef, TypeParametrised};
use crate::extension::{CustomValidator, ExtensionSet, SignatureFunc, TypeDef};
use crate::types::type_param::TypeParam;
use crate::types::{CustomType, FunctionType, PolyFuncType, Type, TypeRow};
use crate::Extension;
Expand Down
2 changes: 1 addition & 1 deletion hugr/src/extension/declarative/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! [specification]: https://github.com/CQCL/hugr/blob/main/specification/hugr.md#declarative-format
//! [`ExtensionSetDeclaration`]: super::ExtensionSetDeclaration

use crate::extension::{TypeDef, TypeDefBound, TypeParametrised};
use crate::extension::{TypeDef, TypeDefBound};
use crate::types::type_param::TypeParam;
use crate::types::{CustomType, TypeBound, TypeName};
use crate::Extension;
Expand Down
18 changes: 8 additions & 10 deletions hugr/src/extension/type_def.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::hash_map::Entry;

use super::{CustomConcrete, ExtensionBuildError};
use super::{Extension, ExtensionId, SignatureError, TypeParametrised};
use super::{Extension, ExtensionId, SignatureError};

use crate::types::{least_upper_bound, CustomType, TypeName};

use crate::types::type_param::TypeArg;
use crate::types::type_param::{check_type_args, TypeArg};

use crate::types::type_param::TypeParam;

Expand Down Expand Up @@ -49,7 +49,7 @@ pub struct TypeDef {
impl TypeDef {
/// Check provided type arguments are valid against parameters.
pub fn check_args(&self, args: &[TypeArg]) -> Result<(), SignatureError> {
self.check_args_impl(args)
check_type_args(args, &self.params).map_err(SignatureError::TypeArgMismatch)
}

/// Check [`CustomType`] is a valid instantiation of this definition.
Expand All @@ -72,7 +72,7 @@ impl TypeDef {
));
}

self.check_args_impl(custom.type_args())?;
check_type_args(custom.type_args(), &self.params)?;

let calc_bound = self.bound(custom.args());
if calc_bound == custom.bound() {
Expand All @@ -93,7 +93,7 @@ impl TypeDef {
/// valid instances of the type parameters.
pub fn instantiate(&self, args: impl Into<Vec<TypeArg>>) -> Result<CustomType, SignatureError> {
let args = args.into();
self.check_args_impl(&args)?;
check_type_args(&args, &self.params)?;
let bound = self.bound(&args);
Ok(CustomType::new(
self.name().clone(),
Expand Down Expand Up @@ -122,12 +122,10 @@ impl TypeDef {
}
}
}
}

impl TypeParametrised for TypeDef {
type Concrete = CustomType;

fn params(&self) -> &[TypeParam] {
/// The static parameters to the TypeDef; a [TypeArg] appropriate for each
/// must be provided to produce an actual type.
pub fn params(&self) -> &[TypeParam] {
&self.params
}

Expand Down

0 comments on commit 69635f0

Please sign in to comment.