Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

randomPoison
Copy link
Collaborator

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.

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.
Copy link
Collaborator

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.

Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants