feat: rec attrset

This commit is contained in:
2025-08-08 12:12:23 +08:00
parent 67cdcfea33
commit a9cfddbf5c
13 changed files with 147 additions and 180 deletions

View File

@@ -72,16 +72,6 @@ impl Ir {
}
}
unsafe fn unwrap_hir_unchecked(self) -> Hir {
if cfg!(debug_assertions) {
self.unwrap_hir()
} else if let Self::Hir(hir) = self {
hir
} else {
unsafe { core::hint::unreachable_unchecked() }
}
}
unsafe fn unwrap_lir_ref_unchecked(&self) -> &Lir {
#[cfg(debug_assertions)]
if let Self::Lir(lir) = self {
@@ -214,8 +204,8 @@ impl Context {
));
}
let root = root.tree().expr().unwrap().downgrade(&mut self)?;
self.resolve(&root)?;
Ok(EvalContext::eval(&mut self, &root)?.to_public(&mut HashSet::new()))
self.resolve(root)?;
Ok(EvalContext::eval(&mut self, root)?.to_public(&mut HashSet::new()))
}
}
@@ -223,7 +213,7 @@ impl DowngradeContext for Context {
fn new_expr(&mut self, expr: Hir) -> ExprId {
let id = unsafe { ExprId::from(self.irs.len()) };
self.irs.push(Ir::Hir(expr).into());
self.nodes.push(self.graph.add_node(unsafe { id.clone() }));
self.nodes.push(self.graph.add_node(id));
self.resolved.push(false);
self.compiled.push(OnceCell::new());
id
@@ -234,9 +224,9 @@ impl DowngradeContext for Context {
f(&self.irs[idx].borrow().unwrap_hir_ref_unchecked(), self)
}
}
fn with_expr_mut<T>(&mut self, id: &ExprId, f: impl FnOnce(&mut Hir, &mut Self) -> T) -> T {
fn with_expr_mut<T>(&mut self, id: ExprId, f: impl FnOnce(&mut Hir, &mut Self) -> T) -> T {
unsafe {
let idx = id.clone().raw();
let idx = id.raw();
let self_mut = &mut *(self as *mut Self);
f(
&mut self
@@ -253,11 +243,12 @@ impl DowngradeContext for Context {
impl ResolveContext for Context {
fn lookup(&self, name: &str) -> LookupResult {
let mut arg_idx = 0;
let mut has_with = false;
for scope in self.scopes.iter().rev() {
match scope {
Scope::Let(scope) => {
if let Some(expr) = scope.get(name) {
return LookupResult::Expr(unsafe { expr.clone() });
if let Some(&expr) = scope.get(name) {
return LookupResult::Expr(expr);
}
}
Scope::Arg(ident) => {
@@ -266,15 +257,19 @@ impl ResolveContext for Context {
}
arg_idx += 1;
}
Scope::With => return LookupResult::Unknown,
Scope::With => has_with = true,
}
}
LookupResult::NotFound
if has_with {
LookupResult::Unknown
} else {
LookupResult::NotFound
}
}
fn new_dep(&mut self, expr: &ExprId, dep: ExprId) {
fn new_dep(&mut self, expr: ExprId, dep: ExprId) {
unsafe {
let expr = expr.clone().raw();
let expr = expr.raw();
let dep = dep.raw();
let expr = *self.nodes.get_unchecked(expr);
let dep = *self.nodes.get_unchecked(dep);
@@ -282,9 +277,9 @@ impl ResolveContext for Context {
}
}
fn resolve(&mut self, expr: &ExprId) -> Result<()> {
fn resolve(&mut self, expr: ExprId) -> Result<()> {
unsafe {
let idx = expr.clone().raw();
let idx = expr.raw();
let self_mut = &mut *(self as *mut Self);
replace_with_and_return(
&mut *self.irs.get_unchecked(idx).borrow_mut(),
@@ -294,7 +289,9 @@ impl ResolveContext for Context {
}))
},
|ir| {
let hir = ir.unwrap_hir_unchecked();
let Ir::Hir(hir) = ir else {
return (Ok(()), ir);
};
match hir.resolve(self_mut) {
Ok(lir) => (Ok(()), Ir::Lir(lir)),
Err(err) => (
@@ -310,8 +307,8 @@ impl ResolveContext for Context {
Ok(())
}
fn new_func(&mut self, body: &ExprId, param: Param) {
self.funcs.insert(unsafe { body.clone() }, param);
fn new_func(&mut self, body: ExprId, param: Param) {
self.funcs.insert(body, param);
}
fn with_let_env<'a, T>(
@@ -321,7 +318,7 @@ impl ResolveContext for Context {
) -> T {
let mut scope = HashMap::new();
for (name, expr) in bindings {
scope.insert(name.clone(), unsafe { expr.clone() });
scope.insert(name.clone(), *expr);
}
self.scopes.push(Scope::Let(scope));
let res = f(self);
@@ -347,8 +344,8 @@ impl ResolveContext for Context {
}
impl EvalContext for Context {
fn eval(&mut self, expr: &ExprId) -> Result<nixjit_eval::Value> {
let idx = unsafe { expr.clone().raw() };
fn eval(&mut self, expr: ExprId) -> Result<nixjit_eval::Value> {
let idx = unsafe { expr.raw() };
let lir = unsafe {
&*(self
.irs