Comments (2)
So the issue here is that while shared references are covariant, mutable references are invariant. So if we want our subtyping relationship to be correct, then we cannot implement DerefMut
like we currently do. I.e if A
is a subtype of B
, then we cannot implement DerefMut
on A
with B
as the target. I dont think this will necessarily cause UB at the moment, but im not sure. However if we keep this then we cannot rely on Gd<T>
being a pointer to a valid T
and we will need to runtime check this anywhere we need this to be true.
So as i see it there are a couple of options here:
- Keep this as is
- Remove
DerefMut
impls on subtypes - Stop using
Deref
/DerefMut
entirely for subtyping
1 will require us to now reexamine our usage of Gd
and see if we every rely on Gd<T>
actually being a pointer to a T
. And possibly add runtime checks where needed.
2 will require rewriting some functions (such as notification), and possibly worse ergonomics than currently. But it might not be too bad overall, hard to say without trying it.
3 will require a big rewrite, and it's a bit unclear what the best method of supporting subtyping then is. it's likely that we'd need more code duplication than currently. how much is unclear to me at the moment. traits with default methods and blanket impls seem feasible to me, but im not fully convinced of it.
from gdext.
I went for approach 1) in the above list: we keep the behavior as-is, but do runtime checks + panics to prevent UB.
See PR #546 for details.
Rationale: I was the one to originally bring up this scenario on Discord, and in more than a year of gdext usage, I haven't seen a single user reporting this problem. It's unlikely to happen in practice, and for the handful of cases where it may happen, runtime checks are good enough. We can disable them in Release mode.
Why not enforce this via type system, by only providing Deref
? Because it comes at quite a cost: we can no longer use const-correctness for Godot types and would need to use &self
receivers everywhere. While const-correctness is currently a hint rather than critical for safety and Godot's API is not 100% perfect in this regard, the distinction of &self
and &mut self
in Godot methods has been very useful overall. It makes code more explicit and allows IDEs to highlight modifications more easily.
With our declared focus on pragmatism, I believe retaining DerefMut
is the better choice. Technically this issue is not solved, as it's at least theoretically possible to have Gd<T>
with a mismatching runtime type, so I'll close this as wontfix. In practice, such cases are rare to occur accidentally, and easy to detect through the debug checks. Situations where this can still cause UB should be considered bugs and fixed.
from gdext.
Related Issues (20)
- Trait bound 'Player: GodotDefault' is not satisfied error during Hello World example HOT 3
- `#[func]` function arguments with invalid types produce weird errors HOT 2
- Deep copies taking into account Rust state (unlike `Node::duplicate()`) HOT 8
- Array<Option<Gd<_>>> Crashes on Null Pointer in Godot's Editor HOT 2
- Inheritance and polymorphism proposals
- Virtual function dispatch for Resources not working HOT 4
- Add mocks for some builtin types for use when Godot isn't running HOT 5
- Missing classes in `engine` module relating to navigation HOT 1
- Recent commit causes "GDExtension initialization function 'gdext_rust_init' returned an error" HOT 1
- Vector3 signed_angle_to is incorrect
- Simple VideoStreamPlayback causes thread panic HOT 4
- It is possible to have a `Base` pointing to a dead object HOT 2
- Issues with string & sse4 support HOT 3
- Problems related to multithreaded access imposed by Godot
- It is tricky to figure out how to properly use `WithBaseField` generically
- commit() failing for SurfaceTool in simple case HOT 6
- godot endlessly prompting to restart HOT 5
- Foreign Classes / Interfaces HOT 1
- Converting Typed Arrays to Variant Arrays Panics HOT 12
- Can't export variable with suffix HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gdext.