Skip to content

Commit 99d30da

Browse files
Improve Instance docs
1 parent 4fb54ed commit 99d30da

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

src/librustc_middle/ty/instance.rs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ use rustc_macros::HashStable;
99

1010
use std::fmt;
1111

12+
/// A monomorphized `InstanceDef`.
13+
///
14+
/// Monomorphization happens on-the-fly and no monomorphized MIR is ever created. Instead, this type
15+
/// simply couples a potentially generic `InstanceDef` with some substs, and codegen and const eval
16+
/// will do all required substitution as they run.
1217
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
1318
#[derive(HashStable, Lift)]
1419
pub struct Instance<'tcx> {
@@ -18,10 +23,26 @@ pub struct Instance<'tcx> {
1823

1924
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable, HashStable)]
2025
pub enum InstanceDef<'tcx> {
26+
/// A user-defined callable item.
27+
///
28+
/// This includes:
29+
/// - `fn` items
30+
/// - closures
31+
/// - generators
2132
Item(DefId),
33+
34+
/// An intrinsic `fn` item (with `"rust-intrinsic"` or `"platform-intrinsic"` ABI).
35+
///
36+
/// Alongside `Virtual`, this is the only `InstanceDef` that does not have its own callable MIR.
37+
/// Instead, codegen and const eval "magically" evaluate calls to intrinsics purely in the
38+
/// caller.
2239
Intrinsic(DefId),
2340

24-
/// `<T as Trait>::method` where `method` receives unsizeable `self: Self`.
41+
/// `<T as Trait>::method` where `method` receives unsizeable `self: Self` (part of the
42+
/// `unsized_locals` feature).
43+
///
44+
/// The generated shim will take `Self` via `*mut Self` - conceptually this is `&owned Self` -
45+
/// and dereference the argument to call the original function.
2546
VtableShim(DefId),
2647

2748
/// `fn()` pointer where the function itself cannot be turned into a pointer.
@@ -37,27 +58,31 @@ pub enum InstanceDef<'tcx> {
3758
/// (the definition of the function itself).
3859
ReifyShim(DefId),
3960

40-
/// `<fn() as FnTrait>::call_*`
61+
/// `<fn() as FnTrait>::call_*` (generated `FnTrait` implementation for `fn()` pointers).
62+
///
4163
/// `DefId` is `FnTrait::call_*`.
4264
///
4365
/// NB: the (`fn` pointer) type must currently be monomorphic to avoid double substitution
4466
/// problems with the MIR shim bodies. `Instance::resolve` enforces this.
4567
// FIXME(#69925) support polymorphic MIR shim bodies properly instead.
4668
FnPtrShim(DefId, Ty<'tcx>),
4769

48-
/// `<dyn Trait as Trait>::fn`, "direct calls" of which are implicitly
49-
/// codegen'd as virtual calls.
70+
/// Dynamic dispatch to `<dyn Trait as Trait>::fn`.
5071
///
51-
/// NB: if this is reified to a `fn` pointer, a `ReifyShim` is used
52-
/// (see `ReifyShim` above for more details on that).
72+
/// This `InstanceDef` does not have callable MIR. Calls to `Virtual` instances must be
73+
/// codegen'd as virtual calls through the vtable.
74+
///
75+
/// If this is reified to a `fn` pointer, a `ReifyShim` is used (see `ReifyShim` above for more
76+
/// details on that).
5377
Virtual(DefId, usize),
5478

55-
/// `<[mut closure] as FnOnce>::call_once`
56-
ClosureOnceShim {
57-
call_once: DefId,
58-
},
79+
/// `<[FnMut closure] as FnOnce>::call_once`.
80+
///
81+
/// The `DefId` is the ID of the `call_once` method in `FnOnce`.
82+
ClosureOnceShim { call_once: DefId },
5983

6084
/// `core::ptr::drop_in_place::<T>`.
85+
///
6186
/// The `DefId` is for `core::ptr::drop_in_place`.
6287
/// The `Option<Ty<'tcx>>` is either `Some(T)`, or `None` for empty drop
6388
/// glue.
@@ -67,7 +92,12 @@ pub enum InstanceDef<'tcx> {
6792
// FIXME(#69925) support polymorphic MIR shim bodies properly instead.
6893
DropGlue(DefId, Option<Ty<'tcx>>),
6994

70-
///`<T as Clone>::clone` shim.
95+
/// Compiler-generated `<T as Clone>::clone` implementation.
96+
///
97+
/// For all types that automatically implement `Copy`, a trivial `Clone` impl is provided too.
98+
/// Additionally, arrays, tuples, and closures get a `Clone` shim even if they aren't `Copy`.
99+
///
100+
/// The `DefId` is for `Clone::clone`, the `Ty` is the type `T` with the builtin `Clone` impl.
71101
///
72102
/// NB: the type must currently be monomorphic to avoid double substitution
73103
/// problems with the MIR shim bodies. `Instance::resolve` enforces this.

src/librustc_trait_selection/traits/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ pub fn get_vtable_index_of_object_method<N>(
302302
) -> usize {
303303
// Count number of methods preceding the one we are selecting and
304304
// add them to the total offset.
305-
// Skip over associated types and constants.
305+
// Skip over associated types and constants, as those aren't stored in the vtable.
306306
let mut entries = object.vtable_base;
307307
for trait_item in tcx.associated_items(object.upcast_trait_ref.def_id()).in_definition_order() {
308308
if trait_item.def_id == method_def_id {

0 commit comments

Comments
 (0)