Skip to content

Commit

Permalink
Add more const
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Dec 2, 2024
1 parent 517fdfa commit 3bf8499
Show file tree
Hide file tree
Showing 15 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion macro/src/dialect/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Parse for DialectInput {
}

Ok(Self {
name: name.ok_or(input.error("dialect name required"))?,
name: name.ok_or_else(|| input.error("dialect name required"))?,
table_gen,
td_file,
include_directories: includes,
Expand Down
8 changes: 4 additions & 4 deletions macro/src/dialect/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ impl<'a> Operation<'a> {
&self.name
}

pub fn can_infer_type(&self) -> bool {
pub const fn can_infer_type(&self) -> bool {
self.can_infer_type
}

pub fn dialect_name(&self) -> &str {
pub const fn dialect_name(&self) -> &str {
self.dialect_name
}

pub fn operation_name(&self) -> &str {
pub const fn operation_name(&self) -> &str {
self.operation_name
}

Expand Down Expand Up @@ -153,7 +153,7 @@ impl<'a> Operation<'a> {
&self.description
}

pub fn constructor_identifier(&self) -> &Ident {
pub const fn constructor_identifier(&self) -> &Ident {
&self.constructor_identifier
}

Expand Down
4 changes: 2 additions & 2 deletions macro/src/dialect/operation/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ impl<'a> Attribute<'a> {
})
}

pub fn set_identifier(&self) -> &Ident {
pub const fn set_identifier(&self) -> &Ident {
&self.set_identifier
}

pub fn remove_identifier(&self) -> &Ident {
pub const fn remove_identifier(&self) -> &Ident {
&self.remove_identifier
}

Expand Down
6 changes: 3 additions & 3 deletions macro/src/dialect/operation/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ impl<'a> OperationBuilder<'a> {
}
}

pub fn operation(&self) -> &Operation {
pub const fn operation(&self) -> &Operation {
self.operation
}

pub fn identifier(&self) -> &Ident {
pub const fn identifier(&self) -> &Ident {
&self.identifier
}

pub fn type_state(&self) -> &TypeState {
pub const fn type_state(&self) -> &TypeState {
&self.type_state
}

Expand Down
2 changes: 1 addition & 1 deletion macro/src/dialect/operation/builder/type_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct TypeState {
}

impl TypeState {
pub fn new(
pub const fn new(
results: Vec<String>,
operands: Vec<String>,
regions: Vec<String>,
Expand Down
2 changes: 1 addition & 1 deletion macro/src/dialect/operation/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'a> Region<'a> {
})
}

pub fn is_variadic(&self) -> bool {
pub const fn is_variadic(&self) -> bool {
self.variadic
}
}
Expand Down
2 changes: 1 addition & 1 deletion macro/src/dialect/operation/successor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'a> Successor<'a> {
})
}

pub fn is_variadic(&self) -> bool {
pub const fn is_variadic(&self) -> bool {
self.variadic
}
}
Expand Down
8 changes: 4 additions & 4 deletions macro/src/dialect/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ impl Type {
}
}

pub fn is_optional(&self) -> bool {
pub const fn is_optional(&self) -> bool {
self.optional
}

pub fn is_variadic(&self) -> bool {
pub const fn is_variadic(&self) -> bool {
self.variadic
}

// TODO Support variadic-of-variadic.
#[allow(unused)]
pub fn is_variadic_of_variadic(&self) -> bool {
pub const fn is_variadic_of_variadic(&self) -> bool {
self.variadic_of_variadic
}

pub fn is_unfixed(&self) -> bool {
pub const fn is_unfixed(&self) -> bool {
self.is_variadic() || self.is_optional()
}
}
2 changes: 1 addition & 1 deletion macro/src/dialect/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn sanitize_name(name: &str) -> Result<Ident, Error> {

// Try to parse the string as an ident, and prefix the identifier
// with "r#" if it is not a valid identifier.
Ok(syn::parse_str::<Ident>(&name).unwrap_or(format_ident!("r#{}", name)))
Ok(syn::parse_str::<Ident>(&name).unwrap_or_else(|_| format_ident!("r#{}", name)))
}

pub fn sanitize_documentation(string: &str) -> Result<String, Error> {
Expand Down
2 changes: 1 addition & 1 deletion melior/src/dialect/llvm/load_store_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl<'c> LoadStoreOptions<'c> {
}

/// Sets TBAA metadata.
pub fn tbaa(mut self, tbaa: ArrayAttribute<'c>) -> Self {
pub const fn tbaa(mut self, tbaa: ArrayAttribute<'c>) -> Self {
self.tbaa = Some(tbaa);
self
}
Expand Down
2 changes: 1 addition & 1 deletion melior/src/ir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl<'c> Block<'c> {
}

/// Converts a block into a raw object.
pub fn into_raw(self) -> MlirBlock {
pub const fn into_raw(self) -> MlirBlock {
let block = self.raw;

forget(self);
Expand Down
6 changes: 3 additions & 3 deletions melior/src/ir/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl<'c> Operation<'c> {
StringRef::new(name).to_raw(),
))
}
.ok_or(Error::AttributeNotFound(name.into()))
.ok_or_else(|| Error::AttributeNotFound(name.into()))
}

/// Checks if the operation has a attribute with the given name.
Expand All @@ -220,7 +220,7 @@ impl<'c> Operation<'c> {
pub fn remove_attribute(&mut self, name: &str) -> Result<(), Error> {
unsafe { mlirOperationRemoveAttributeByName(self.raw, StringRef::new(name).to_raw()) }
.then_some(())
.ok_or(Error::AttributeNotFound(name.into()))
.ok_or_else(|| Error::AttributeNotFound(name.into()))
}

/// Returns a reference to the next operation in the same block.
Expand Down Expand Up @@ -302,7 +302,7 @@ impl<'c> Operation<'c> {
}

/// Converts an operation into a raw object.
pub fn into_raw(self) -> MlirOperation {
pub const fn into_raw(self) -> MlirOperation {
let operation = self.raw;

forget(self);
Expand Down
3 changes: 3 additions & 0 deletions melior/src/ir/operation/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ impl<'c> OperationBuilder<'c> {
/// Adds regions in a [`Vec`](std::vec::Vec).
pub fn add_regions_vec(mut self, regions: Vec<Region<'c>>) -> Self {
unsafe {
// This may fire with -D clippy::nusery, however, it is

Check warning on line 78 in melior/src/ir/operation/builder.rs

View workflow job for this annotation

GitHub Actions / spell-check

Unknown word (nusery)
// guaranteed by the std that ManuallyDrop<T> has the same layout as T
#[allow(clippy::transmute_undefined_repr)]
mlirOperationStateAddOwnedRegions(
&mut self.raw,
regions.len() as isize,
Expand Down
2 changes: 1 addition & 1 deletion melior/src/ir/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<'c> Region<'c> {
}

/// Converts a region into a raw object.
pub fn into_raw(self) -> mlir_sys::MlirRegion {
pub const fn into_raw(self) -> mlir_sys::MlirRegion {
let region = self.raw;

forget(self);
Expand Down
4 changes: 2 additions & 2 deletions melior/src/pass/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl ExternalPass<'_> {
}

/// Converts an external pass to a raw object.
pub fn to_raw(self) -> MlirExternalPass {
pub const fn to_raw(self) -> MlirExternalPass {
self.raw
}

Expand Down Expand Up @@ -183,7 +183,7 @@ pub fn create_external<'c, T: RunExternalPass<'c>>(
StringRef::new(description).to_raw(),
StringRef::new(op_name).to_raw(),
dependent_dialects.len() as isize,
dependent_dialects.as_ptr() as _,
dependent_dialects.as_ptr().cast_mut() as _,
MlirExternalPassCallbacks {
construct: Some(transmute::<*const (), unsafe extern "C" fn(*mut c_void)>(
callback_construct::<T> as *const (),
Expand Down

0 comments on commit 3bf8499

Please sign in to comment.