Program slicing for Rust

I've been working on a tool that y'all might find interesting: a program slicer. Program slicing is the task of determining which pieces of code can influence the value of a given variable. For example, this screenshot shows the slice of input on line 14:

image

You can try out a demo here: https://willcrichton.net/demos/slicer/

Right now, the tool isn't easy to install because it requires modifications to rustc, but I will look to get those changes merged in the future. Instead, I'm just interested in: would you find a tool like this useful? What kinds of problems would you use it for?

12 Likes

I suppose as part of an IDE, this could be useful when reading a lengthy section of code, trying to better understand what it’s doing. It seems somewhat similar to the concept of the IDE highlighting all occurrences of a variable when you select it, or the idea of rust-analyzer underlining (or otherwise emphasizing) all mutable variables as well as all &mut self method calls. “Somewhat similar” in the sense that these two tools can also help you a lot in figuring out what can influence the value of a given variable.

Maybe there’d be value in somehow indicating how many steps of indirection there are towards influencing the variable in question? Of course only if your analysis even allows for any straightforward concept of “number of steps of indirection” in the first place.

6 Likes

Yes, I think there's a good amount of overlap with rust-analyzer. One big difference between my slicer and rust-analyzer is that the slicer understands references by hooking into the borrow checker. For example, if you have the program:

let mut x = 1;
let y = &mut x;
*y += 1;
println!("{}", x);

Then a slice on x includes y, because the tool knows that y points to x. To your point, you could imagine a simpler action which is "highlight all occurrences, including references".

I 100% agree with the concept of visualizing causality -- why does something get included in the slice? I haven't decided the best UI yet, but it's clearly valuable information. I like steps of indirection as a first approach.

Ah interesting. I was once asked to implement a simple program slicer in an interview, but I never figured out what the thing I wrote was called.

1 Like

Yes, that would be useful.

If I select a word in Sublime Text, it highlights all other occurrences of the same word. I use it often to see where a variable is used, which is the most basic version of this.

1 Like

I could see this being useful in interprocedural conflict situations.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.