Skip to content

Commit e000179

Browse files
committed
Add documentation for the semantics of MIR rvalues
1 parent 8368590 commit e000179

File tree

2 files changed

+101
-23
lines changed

2 files changed

+101
-23
lines changed

compiler/rustc_middle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#![feature(unwrap_infallible)]
6060
#![feature(decl_macro)]
6161
#![feature(drain_filter)]
62+
#![feature(intra_doc_pointers)]
6263
#![recursion_limit = "512"]
6364
#![allow(rustc::potential_query_instability)]
6465

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 100 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,57 +2378,134 @@ impl<'tcx> Operand<'tcx> {
23782378
#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)]
23792379
/// The various kinds of rvalues that can appear in MIR.
23802380
///
2381-
/// Not all of these are allowed at every [`MirPhase`]. Check the documentation there to see which
2382-
/// ones you do not have to worry about. The MIR validator will generally enforce such restrictions,
2383-
/// causing an ICE if they are violated.
2381+
/// Not all of these are allowed at every [`MirPhase`] - when this is the case, it's stated below.
2382+
///
2383+
/// Computing any rvalue begins by evaluating the places and operands in the rvalue in the order in
2384+
/// which they appear. These are then used to produce a "value" - the same kind of value that an
2385+
/// [`Operand`] is.
23842386
pub enum Rvalue<'tcx> {
2385-
/// x (either a move or copy, depending on type of x)
2387+
/// Yields the operand unchanged
23862388
Use(Operand<'tcx>),
23872389

2388-
/// [x; 32]
2390+
/// Creates an array where each element is the value of the operand. This currently does not
2391+
/// drop the value even if the number of repetitions is zero, see [#74836].
2392+
///
2393+
/// Corresponds to source code like `[x; 32]`.
2394+
///
2395+
/// [#74836]: https://github.com/rust-lang/rust/issues/74836
23892396
Repeat(Operand<'tcx>, ty::Const<'tcx>),
23902397

2391-
/// &x or &mut x
2398+
/// Creates a reference of the indicated kind to the place.
2399+
///
2400+
/// There is not much to document here, because besides the obvious parts the semantics of this
2401+
/// are essentially entirely a part of the aliasing model. There are many UCG issues discussing
2402+
/// exactly what the behavior of this operation should be.
2403+
///
2404+
/// `Shallow` borrows are disallowed after drop lowering.
23922405
Ref(Region<'tcx>, BorrowKind, Place<'tcx>),
23932406

2394-
/// Accessing a thread local static. This is inherently a runtime operation, even if llvm
2395-
/// treats it as an access to a static. This `Rvalue` yields a reference to the thread local
2396-
/// static.
2407+
/// Returns a pointer/reference to the given thread local.
2408+
///
2409+
/// The yielded type is a `*mut T` if the static is mutable, otherwise if the static is extern a
2410+
/// `*const T`, and if neither of those apply a `&T`.
2411+
///
2412+
/// **Note:** This is a runtime operation that actually executes code and is in this sense more
2413+
/// like a function call. Also, DSEing these causes `fn main() {}` to SIGILL for some reason
2414+
/// that I never got a chance to look into.
2415+
///
2416+
/// **Needs clarification**: Are there weird additional semantics here related to the runtime
2417+
/// nature of this operation?
23972418
ThreadLocalRef(DefId),
23982419

2399-
/// Create a raw pointer to the given place
2400-
/// Can be generated by raw address of expressions (`&raw const x`),
2401-
/// or when casting a reference to a raw pointer.
2420+
/// Creates a pointer with the indicated mutability to the place.
2421+
///
2422+
/// This is generated by pointer casts like `&v as *const _` or raw address of expressions like
2423+
/// `&raw v` or `addr_of!(v)`.
2424+
///
2425+
/// Like with references, the semantics of this operation are heavily dependent on the aliasing
2426+
/// model.
24022427
AddressOf(Mutability, Place<'tcx>),
24032428

2404-
/// length of a `[X]` or `[X;n]` value
2429+
/// Yields the length of the place, as a `usize`.
2430+
///
2431+
/// If the type of the place is an array, this is the array length. This also works for slices
2432+
/// (`[T]`, not `&[T]`) through some mechanism that depends on how exactly places work (see
2433+
/// there for more details).
24052434
Len(Place<'tcx>),
24062435

2436+
/// Performs essentially all of the casts that can be performed via `as`.
2437+
///
2438+
/// This allows for casts from/to a variety of types.
2439+
///
2440+
/// **FIXME**: Document exactly which `CastKind`s allow which types of casts. Figure out why
2441+
/// `ArrayToPointer` and `MutToConstPointer` are special.
24072442
Cast(CastKind, Operand<'tcx>, Ty<'tcx>),
24082443

2444+
/// * `Offset` has the same semantics as [`offset`](pointer::offset), except that the second
2445+
/// paramter may be a `usize` as well.
2446+
/// * The comparison operations accept `bool`s, `char`s, signed or unsigned integers, floats,
2447+
/// raw pointers, or function pointers and return a `bool`.
2448+
/// * Left and right shift operations accept signed or unsigned integers not necessarily of the
2449+
/// same type and return a value of the same type as their LHS. For all other operations, the
2450+
/// types of the operands must match.
2451+
/// * The `Bit*` operations accept signed integers, unsigned integers, or bools and return a
2452+
/// value of that type.
2453+
/// * The remaining operations accept signed integers, unsigned integers, or floats of any
2454+
/// matching type and return a value of that type.
24092455
BinaryOp(BinOp, Box<(Operand<'tcx>, Operand<'tcx>)>),
2456+
2457+
/// Same as `BinaryOp`, but yields `(T, bool)` instead of `T`. In addition to performing the
2458+
/// same computation as the matching `BinaryOp`, checks if the infinite precison result would be
2459+
/// unequal to the actual result and sets the `bool` if this is the case. `BinOp::Offset` is not
2460+
/// allowed here.
2461+
///
2462+
/// **FIXME**: What about division/modulo? Are they allowed here at all? Are zero divisors still
2463+
/// UB? Also, which other combinations of types are disallowed?
24102464
CheckedBinaryOp(BinOp, Box<(Operand<'tcx>, Operand<'tcx>)>),
24112465

2466+
/// Yields the size or alignment of the type as a `usize`.
24122467
NullaryOp(NullOp, Ty<'tcx>),
2468+
2469+
/// Exactly like `BinaryOp`, but less operands.
2470+
///
2471+
/// Also does two's-complement arithmetic. Negation requires a signed integer or a float; binary
2472+
/// not requires a signed integer, unsigned integer, or bool. Both operation kinds return a
2473+
/// value with the same type as their operand.
24132474
UnaryOp(UnOp, Operand<'tcx>),
24142475

2415-
/// Read the discriminant of an ADT.
2476+
/// Computes the discriminant of the place, returning it as an integer of type
2477+
/// [`discriminant_ty`].
2478+
///
2479+
/// The validity requirements for the underlying value are undecided for this rvalue, see
2480+
/// [#91095]. Note too that the value of the discriminant is not the same thing as the
2481+
/// variant index; use [`discriminant_for_variant`] to convert.
2482+
///
2483+
/// For types defined in the source code as enums, this is well behaved. This is also well
2484+
/// formed for other types, but yields no particular value - there is no reason it couldn't be
2485+
/// defined to yield eg zero though.
24162486
///
2417-
/// Undefined (i.e., no effort is made to make it defined, but there’s no reason why it cannot
2418-
/// be defined to return, say, a 0) if ADT is not an enum.
2487+
/// [`discriminant_ty`]: crate::ty::Ty::discriminant_ty
2488+
/// [#91095]: https://github.com/rust-lang/rust/issues/91095
2489+
/// [`discriminant_for_variant`]: crate::ty::Ty::discriminant_for_variant
24192490
Discriminant(Place<'tcx>),
24202491

2421-
/// Creates an aggregate value, like a tuple or struct. This is
2422-
/// only needed because we want to distinguish `dest = Foo { x:
2423-
/// ..., y: ... }` from `dest.x = ...; dest.y = ...;` in the case
2424-
/// that `Foo` has a destructor. These rvalues can be optimized
2425-
/// away after type-checking and before lowering.
2492+
/// Creates an aggregate value, like a tuple or struct.
2493+
///
2494+
/// This is needed because dataflow analysis needs to distinguish
2495+
/// `dest = Foo { x: ..., y: ... }` from `dest.x = ...; dest.y = ...;` in the case that `Foo`
2496+
/// has a destructor.
2497+
///
2498+
/// Disallowed after deaggregation for all aggregate kinds except `Array` and `Generator`. After
2499+
/// generator lowering, `Generator` aggregate kinds are disallowed too.
24262500
Aggregate(Box<AggregateKind<'tcx>>, Vec<Operand<'tcx>>),
24272501

24282502
/// Transmutes a `*mut u8` into shallow-initialized `Box<T>`.
24292503
///
2430-
/// This is different a normal transmute because dataflow analysis will treat the box
2431-
/// as initialized but its content as uninitialized.
2504+
/// This is different a normal transmute because dataflow analysis will treat the box as
2505+
/// initialized but its content as uninitialized. Like other pointer casts, this in general
2506+
/// affects alias analysis.
2507+
///
2508+
/// Disallowed after drop elaboration.
24322509
ShallowInitBox(Operand<'tcx>, Ty<'tcx>),
24332510
}
24342511

0 commit comments

Comments
 (0)