-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Replace one of the examples of the aliasing rule #2715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
src/borrowing/examples.md
Outdated
changes the result. | ||
|
||
- The equivalent code in C exhibits undefined behavior, which may result in | ||
mis-compilation and unexpected behavior, even if it doesn't cause a crash. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the basis for this assertion? I was under the impression that C assumed everything aliased and therefore could not re-order operations very aggressively.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pointers would need to be tagged with restrict
to allow this reordering in C.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've gone back and reworked the example to better demonstrate UB in the C version. The C version now uses the restrict
keyword and demonstrates a change in behavior when optimizations are enabled. I have also included links to Godbolt so that it's easy to demonstrate this for students. Let me know if this seems like a better option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it makes sense to say that the Rust function would be miscompiled if not for the aliasing rule. If Rust semantics were different, it would change how Rust is optimized--in every language optimizations must be semantics-preserving.
We could say that the Rust rules enable more optimizations than C compilers can perform (in general, e.g. without restrict
), which is true, but in my opinion not really the strongest reason behind why Rust does things this way.
The existing example is too similar to the first one, since it also is demonstrating a dangling reference to a heap allocation after modifying the
Vec
. This new example demonstrates a different angle to the aliasing rule: That it prevents incorrect optimizations. This gives us a chance to bring up undefined behavior, since that can cause problems in much more subtle ways.I'm not sure if this example is quite correct, though. I'm still trying to figure out why exactly this might get mis-compiled in C, but that subtlety may not matter for the purpose of showing the point of the aliasing rule.