Identify mutations in rust file for svelte-like framework

I am looking to build a framework similar to svelte. The main idea I am looking to borrow from svelte is that when a variable is mutated, side-effects are triggered. Calls to these side-effects are inserted during a compilation step (hidden to the one writing the code). Svelte attempts to identify mutations in Javascript which I would imagine to be difficult, but Rust should be easier - just need to figure out how to get that information from the compiler.

Anyone know of examples which might lead me to the correct approach? I've had a look at rust-clippy. Anyone know of others?

To get this right you will most likely have to write a macro that deals in Rust syntax (probably with syn). I'm not going to say that you can't do it with variables, you probably could, but you will suffer for doing so.

Observable structs, much like XAML data templates, might be a more realistic route to consider. You could auto-implement an Observable trait (with derive macros) or some-such for structs, which could then somehow be used to reactively update a UI. Even with this simpler approach, you may struggle with strongly connected components in your object graphs - but you may be able to weasel out of that somehow.

Thanks for your thoughts!

My initial plan is to avoid macros by statically analyzing the code files. Basically, a custom compiler atop rustc. I think this would be more user friendly compared to custom data structures and/or macros.

I have looked at syn. However, I would need the ability to resolve type information and step-into/trace functions, which I don't think I would be able to accomplish with syn.

You could get most of the way there with just the function types. You would look for functions that take &mut. You would also have to map operators to there traits and handle assignment. But than you would have to look into some edge cases like internal mutability.

Is it possible to get type information about functions and operators in macros?

Definitely possible, but, like you mentioned, interior mutability would be tricky.

As far as I know, It is not possible to get type information from macros beyond their names (if specified).

The compiler has the information needed, so I was hoping to find some examples along that line. rust-clippy is the only relevant repo I have found thus far. The code looks rather involved but it might be something I look into.

My understanding is that macro expansion runs while building the AST, while type inference requires traversal of the AST. Therefore, in general, type information is not available during macro expansion.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.