Skip to content
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

Change vtable code generation to use pointers instead of references for this #2163

Closed
Tazdevil971 opened this issue Feb 16, 2022 · 1 comment

Comments

@Tazdevil971
Copy link

The current vtable code generation uses references instead of pointers for this.

I think this should be changed as C++ mutable references are semantically different from rust's ones. And with the current implementation it's impossible to avoid UB in certain edge cases.

Multithreaded C++ code calling rust always results in the creation of a mutable reference unless the function is marked const, if multiple calls happen at once we would have more than one mutable reference around. Mutable aliasing here is unavoidable.

The solution would be to use pointers instead of references, as they have basically the same semantics as C++ mutable references and don't create such problems.

Also, pointers offer greater flexibility when it comes to casting between types, which happens a lot around rust implemented C++ interfaces.

The only disadvantage is loosing the guarantee that this must always be not null. A possible fix would be to use NonNull<T> for this, but since there is no const equivalent of NonNull<T> I don't think this is a viable solution.

@emilio
Copy link
Contributor

emilio commented Feb 18, 2022

I agree pointers seem safer here.

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

No branches or pull requests

2 participants