I have the following two traits:
pub trait ASTVisitor<T> {
fn visit_module(&mut self, tlu: &Module) -> T;
fn visit_function(&mut self, func: &Function) -> T;
// more functions that are similar ...
}
pub trait ASTNode {
fn visit<T>(&self, visitor: &mut impl ASTVisitor<T>) -> T;
}
The ASTVisitor
trait is implemented by any struct that wants to traverse the AST. The ASTNode
trait is implemented by any node in the AST that can be visited.
The following is an example implementation of ASTNode
for Module
:
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Module { /* ... */}
impl ASTNode for Module {
fn visit<T>(&self, visitor: &mut impl ASTVisitor<T>) -> T {
visitor.visit_module(self)
}
}
Now, to my current challenge: I want to build an ASTVisitor
, that keeps a reference to an ASTNode
:
#[derive(Debug, Clone)]
pub struct Sema {
current_func: Option<&Func>, // <-- !! Error: Expected named lifetime parameter !!
}
impl ASTVisitor<()> for Sema {
fn visit_module(&mut self, tlu: &Module) {
todo!()
}
/* ... */
}
The reference to Func
needs a named lifetime. So far, so good. Everything makes sense. However, I couldn't figure out how to fix this. I've tried the following:
Playground Link to my attempted solution
Rust complains that *self
was previously mutable borrowed, which is an odd error.