Battling with Miri when using raw pointers

I spent the morning battling Miri, or perhaps more accurately battling my raw pointer code which involves a stack of raw pointers.

I think I am very gradually getting the hang of it, but it is still a slightly erratic process for me. Currently my tests are running ok, but that may not last.

Possibly more examples of what you cannot do and the solutions would help. Today I tried lots of things, what eventually worked was making a function take a raw pointer instead of a reference. I had no idea it would work in advance, and I still only have a rather vague intuition of how and why it worked.

I don't know of many tutorials on the subject, am I missing anything?

Edit: I probably need to read this again and more carefully:

1 Like

The hardest part of working with stacked borrows is the interaction between references and raw pointers— If you do everything with references, then the compiler will make sure that your code is OK. On the flip side, if you do everything with raw pointers, the compiler will make very few assumptions about your code and your intuition from something like C will probably be correct.

So, the general advice that I've heard is to minimize the number of conversions between references and pointers (in either direction). If you're writing something that needs to use raw pointers, try to keep all of the conversions between pointers and references at the boundary interfaces of your code. Convert all of your references to pointers up front, work more-or-less exclusively in pointer land, and then (if necessary) convert the final result back into a reference when you return it to external code.

That won't guarantee that you get everything right, of course, but it seems to be the most reliable way to make sure you uphold the various aliasing/pointer-invalidation requirements.

10 Likes

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.