Post
Share your knowledge.
Why does Rust enum with Box create cycle warning even with phantom?
I'm working with Rust and I have the following code:
public enum Expr {
Apply(Box<Expr>, Box<Expr>),
}
It seems like I'm getting a cycle warning because Expr
contains Box<Expr>
. However, I'm using phantom types, and I thought that would make it safe to compile. Can someone explain why this cycle warning is happening despite using phantom types?
- Move CLI
- Move
Answers
1The cycle warning occurs because your Expr
enum definition creates a recursive type. In Rust, using Box<Expr>
inside the Expr
enum type causes a recursive relationship because Box<Expr>
directly references Expr
. This is why Rust gives a cycle warning.
Even though you mention phantom types, it seems that you're still providing a concrete type Expr
for the Box
instead of a phantom generic type that wouldn't require a real instance. Consider if you wanted to use a phantom type, you would use a generic parameter in the enum that doesn't need to exist at runtime, something like Box<PhantomData<T>>
if T
is a generic parameter not Expr
itself.
Do you know the answer?
Please log in and share it.
Move is an executable bytecode language used to implement custom transactions and smart contracts.