Code cleanup

This commit is contained in:
Mia
2026-03-06 15:28:30 +01:00
parent bd0b619127
commit 92c02c6ca2
9 changed files with 63 additions and 68 deletions
+10 -12
View File
@@ -6,7 +6,7 @@ use derive_more::{Debug, Display};
use fxhash::FxBuildHasher;
use leaf_allocators::SyncAllocator;
use scc::HashMap;
use std::{borrow::Cow, fmt::Display, hash::Hash, sync::OnceLock};
use std::{borrow::Cow, hash::Hash, sync::OnceLock};
#[derive(
derive_more::Debug, Display, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash,
@@ -69,7 +69,7 @@ impl<'l> Context<'l> {
}
pub fn get_or_create_assembly(&'l self, ident: AssemblyIdentifier) -> &'l Assembly<'l> {
&*self
&self
.assemblies
.entry_sync(ident.clone())
.or_insert_with(|| Assembly::new(self, ident))
@@ -77,7 +77,7 @@ impl<'l> Context<'l> {
pub fn intern_str(&'l self, str: &str) -> &'l str {
if let Some(value) = self.strings.get_sync(str) {
return (*value).into();
return *value;
}
let str = self.alloc.alloc_str(str);
let _ = self.strings.insert_sync(str, str);
@@ -109,12 +109,11 @@ impl Hash for Assembly<'_> {
impl<'l> Assembly<'l> {
pub fn new(ctx: &'l Context<'l>, ident: AssemblyIdentifier) -> &'l Self {
let assembly = ctx.alloc.alloc(Self {
(ctx.alloc.alloc(Self {
ctx,
ident,
functions: boxcar::vec![],
});
assembly
})) as _
}
pub fn ctx(&self) -> Ctx<'l> {
@@ -144,11 +143,10 @@ impl<'l> Assembly<'l> {
&'l self,
filter: impl Fn(&'l Function<'l>) -> bool,
) -> Option<&'l Function<'l>> {
for (_, func) in self.functions.iter() {
if filter(func) {
return Some(func);
}
}
None
self.functions
.iter()
.map(|(_, func)| func)
.find(|&func| filter(func))
.map(|v| v as _)
}
}
+2 -7
View File
@@ -161,12 +161,7 @@ pub enum InstructionVariant<'l> {
impl InstructionVariant<'_> {
pub fn is_block_termination(&self) -> bool {
match self {
Self::Return(_) => true,
Self::Jump(_) => true,
Self::Branch { .. } => true,
_ => false,
}
matches!(self, Self::Return(_) | Self::Jump(_) | Self::Branch { .. })
}
}
@@ -197,7 +192,7 @@ impl std::fmt::Debug for InstructionVariant<'_> {
write!(
f,
"call {}(",
func.name.get().unwrap_or(&"<anonymous function>".into())
func.name.get().unwrap_or(&"<anonymous function>")
)?;
let mut separator = "";
for arg in args {
+1 -1
View File
@@ -83,7 +83,7 @@ pub struct FunctionBody<'l> {
pub blocks: Vec<&'l Block<'l>>,
}
impl<'l> FmtDebug for FunctionBody<'_> {
impl FmtDebug for FunctionBody<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
struct BlockDbg<'l>(&'l Block<'l>);
+2 -1
View File
@@ -61,6 +61,7 @@ pub enum Type<'l> {
}
impl<'l> Type<'l> {
#[allow(clippy::missing_transmute_annotations)]
pub fn ctx(&self) -> &'l Context<'l> {
match self.non_default_ctx() {
Some(ctx) => ctx,
@@ -69,7 +70,7 @@ impl<'l> Type<'l> {
static ALLOCATOR: OnceLock<SyncArenaAllocator> = OnceLock::new();
let ctx: &'static Context = DEFAULT.get_or_init(|| {
let allocator: &'static SyncArenaAllocator =
ALLOCATOR.get_or_init(|| SyncArenaAllocator::default());
ALLOCATOR.get_or_init(SyncArenaAllocator::default);
Context::new(allocator)
});
std::mem::transmute(ctx)
+4 -4
View File
@@ -50,12 +50,12 @@ impl Hash for Float {
#[display("{}", _0)]
pub struct Const<T>(T);
impl<'l, T: 'l> Into<AnyConst<'l>> for &'l Const<T>
impl<'l, T: 'l> From<&'l Const<T>> for AnyConst<'l>
where
for<'a> &'a T: Into<AnyConst<'a>>,
{
fn into(self) -> AnyConst<'l> {
(&self.0).into()
fn from(val: &'l Const<T>) -> Self {
(&val.0).into()
}
}
@@ -134,7 +134,7 @@ impl<'l> AnyConst<'l> {
pub fn flags(&self) -> super::ValueFlags {
match self {
AnyConst::Function(function) => ValueFlags::Function,
AnyConst::Function(_) => ValueFlags::Function,
AnyConst::Type(_) => ValueFlags::Type,
_ => ValueFlags::Const,
}