Skip to content

Commit 349f253

Browse files
committed
Copy some comments from rustc
1 parent f73d0ee commit 349f253

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

crates/hir_ty/src/infer.rs

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,46 @@ impl Default for InternedStandardTypes {
141141
InternedStandardTypes { unknown: TyKind::Error.intern(&Interner) }
142142
}
143143
}
144-
144+
/// Represents coercing a value to a different type of value.
145+
///
146+
/// We transform values by following a number of `Adjust` steps in order.
147+
/// See the documentation on variants of `Adjust` for more details.
148+
///
149+
/// Here are some common scenarios:
150+
///
151+
/// 1. The simplest cases are where a pointer is not adjusted fat vs thin.
152+
/// Here the pointer will be dereferenced N times (where a dereference can
153+
/// happen to raw or borrowed pointers or any smart pointer which implements
154+
/// Deref, including Box<_>). The types of dereferences is given by
155+
/// `autoderefs`. It can then be auto-referenced zero or one times, indicated
156+
/// by `autoref`, to either a raw or borrowed pointer. In these cases unsize is
157+
/// `false`.
158+
///
159+
/// 2. A thin-to-fat coercion involves unsizing the underlying data. We start
160+
/// with a thin pointer, deref a number of times, unsize the underlying data,
161+
/// then autoref. The 'unsize' phase may change a fixed length array to a
162+
/// dynamically sized one, a concrete object to a trait object, or statically
163+
/// sized struct to a dynamically sized one. E.g., &[i32; 4] -> &[i32] is
164+
/// represented by:
165+
///
166+
/// ```
167+
/// Deref(None) -> [i32; 4],
168+
/// Borrow(AutoBorrow::Ref) -> &[i32; 4],
169+
/// Unsize -> &[i32],
170+
/// ```
171+
///
172+
/// Note that for a struct, the 'deep' unsizing of the struct is not recorded.
173+
/// E.g., `struct Foo<T> { x: T }` we can coerce &Foo<[i32; 4]> to &Foo<[i32]>
174+
/// The autoderef and -ref are the same as in the above example, but the type
175+
/// stored in `unsize` is `Foo<[i32]>`, we don't store any further detail about
176+
/// the underlying conversions from `[i32; 4]` to `[i32]`.
177+
///
178+
/// 3. Coercing a `Box<T>` to `Box<dyn Trait>` is an interesting special case. In
179+
/// that case, we have the pointer we need coming in, so there are no
180+
/// autoderefs, and no autoref. Instead we just do the `Unsize` transformation.
181+
/// At some point, of course, `Box` should move out of the compiler, in which
182+
/// case this is analogous to transforming a struct. E.g., Box<[i32; 4]> ->
183+
/// Box<[i32]> is an `Adjust::Unsize` with the target `Box<[i32]>`.
145184
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
146185
pub struct Adjustment {
147186
pub kind: Adjust,
@@ -152,34 +191,25 @@ pub struct Adjustment {
152191
pub enum Adjust {
153192
/// Go from ! to any type.
154193
NeverToAny,
155-
156194
/// Dereference once, producing a place.
157195
Deref(Option<OverloadedDeref>),
158-
159196
/// Take the address and produce either a `&` or `*` pointer.
160197
Borrow(AutoBorrow),
161-
162198
Pointer(PointerCast),
163199
}
164200

165-
// impl fmt::Display for Adjust {
166-
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167-
// match self {
168-
// Adjust::NeverToAny => write!(f, "NeverToAny"),
169-
// Adjust::Deref(_) => write!(f, "Deref"), // FIXME
170-
// Adjust::Borrow(AutoBorrow::Ref(mt)) => write!(f, "BorrowRef{:?}", mt),
171-
// Adjust::Borrow(AutoBorrow::RawPtr(mt)) => write!(f, "BorrowRawPtr{:?}", mt),
172-
// Adjust::Pointer(cast) => write!(f, "PtrCast{:?}", cast),
173-
// }
174-
// }
175-
// }
176-
201+
/// An overloaded autoderef step, representing a `Deref(Mut)::deref(_mut)`
202+
/// call, with the signature `&'a T -> &'a U` or `&'a mut T -> &'a mut U`.
203+
/// The target type is `U` in both cases, with the region and mutability
204+
/// being those shared by both the receiver and the returned reference.
177205
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
178206
pub struct OverloadedDeref(Mutability);
179207

180208
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
181209
pub enum AutoBorrow {
210+
/// Converts from T to &T.
182211
Ref(Mutability),
212+
/// Converts from T to *T.
183213
RawPtr(Mutability),
184214
}
185215

0 commit comments

Comments
 (0)