Stream: t-compiler/help

Topic: how vtable is constructed and organized?


Jason Hu (Aug 04 2020 at 21:31, on Zulip):

Hi,

I am recently thinking to look into trait objects and it is unavoidable to consider vtables. how in general a vtable is organized? how a vtable is constructed?

Jonas Schievink (Aug 04 2020 at 21:34, on Zulip):

Copying an old comment I wrote up:

The vtable_methods query yields all method entries for a specific vtable (that is, the list of all dyn-compatible methods of all transitive supertraits). On every unsizing cast to a trait object these methods are enqueued for code generation (and the drop glue is also added).

Codegen then assembles the vtable from drop glue, size, alignment, and the method pointers. This defines the vtable's layout in memory.

bjorn3 (Aug 04 2020 at 21:35, on Zulip):

https://github.com/rust-lang/rust/blob/master/src/librustc_codegen_ssa/meth.rs

drop_in_place, size, align, function pointers for all methods (for non object-safe methods a null pointer gets written)

Jason Hu (Aug 04 2020 at 21:37, on Zulip):

that is brilliant! thx for the info.

Last update: Sep 28 2020 at 16:00UTC