Add several assertions to help static analysis and fix some GIR annotations
See the commit messages for details.
Spotted while scrolling through the output from https://gitlab.gnome.org/GNOME/glib/-/jobs/4604298, although this MR doesn’t approach fixing all of the warnings in that log.