gtk-rs / gtk-rs-core

Rust bindings for GNOME libraries
https://gtk-rs.org/gtk-rs-core
MIT License
280 stars 112 forks source link

[BUG] Impl traits don't have enough trait bounds #1515

Open felinira opened 1 week ago

felinira commented 1 week ago

Bug description

XyzImpl traits don't actually check whether they are implemented on classes that derive / implement the specific class. The Impl traits themselves only provide implementations, so usually they would be fine to implement wherever. However, almost all bindings also offer to call parent implementations, or other ffi methods as part of some Ext traits.

In practise this means that you can call unrelated methods on any object, thus being able to cause UB.

Examples:

There are probably more ways to make this lead to more serious and harder to detect behaviour.

Solution

Adding trait bounds like this should probably resolve all these issues:

pub trait WidgetImpl: ObjectSubclass where <Self as ObjectSubclass>::Type: IsA<Widget>

sdroege commented 1 week ago

As discussed on Matrix yesterday, adding such trait bounds will require them to be added recursively in more places. E.g. ButtonImpl will need both IsA<Widget> and IsA<Button>.

Also it means that the glib::wrapper! usage must list all super-classes and can't just skip one. That part will probably break a bit of code.

This seems like the right thing to do though.