Skip to content

Commit 6a69328

Browse files
committed
Added some documentation to 'Ty'
1 parent 0c57959 commit 6a69328

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,9 +1867,9 @@ impl<'tcx> Region<'tcx> {
18671867

18681868
/// Constructors for `Ty`
18691869
impl<'tcx> Ty<'tcx> {
1870-
// Avoid this in favour of more specific `new_*` methods, where possible.
18711870
#[allow(rustc::usage_of_ty_tykind)]
18721871
#[inline]
1872+
/// Turns a [`TyKind`] into a [`Ty`]. Use more specific `new_*` methods(such as `new_int`), where possible.
18731873
pub fn new(tcx: TyCtxt<'tcx>, st: TyKind<'tcx>) -> Ty<'tcx> {
18741874
tcx.mk_ty_from_kind(st)
18751875
}
@@ -1928,12 +1928,12 @@ impl<'tcx> Ty<'tcx> {
19281928
.copied()
19291929
.unwrap_or_else(|| Ty::new_infer(tcx, ty::FreshFloatTy(n)))
19301930
}
1931-
1931+
/// Creates a new [`Ty`] representing a parameter, with index `index` and name `name`.
19321932
#[inline]
19331933
pub fn new_param(tcx: TyCtxt<'tcx>, index: u32, name: Symbol) -> Ty<'tcx> {
19341934
tcx.mk_ty_from_kind(Param(ParamTy { index, name }))
19351935
}
1936-
1936+
/// Creates an new [`Ty`] representing a bound type variable, with debruijn index `index` and bound type `bound_ty`.
19371937
#[inline]
19381938
pub fn new_bound(
19391939
tcx: TyCtxt<'tcx>,
@@ -2257,22 +2257,22 @@ impl<'tcx> Ty<'tcx> {
22572257
_ => false,
22582258
}
22592259
}
2260-
2260+
/// Checks if this type is of [`TyKind::Never`].
22612261
#[inline]
22622262
pub fn is_never(self) -> bool {
22632263
matches!(self.kind(), Never)
22642264
}
2265-
2265+
/// Checks if this type is primitive
22662266
#[inline]
22672267
pub fn is_primitive(self) -> bool {
22682268
self.kind().is_primitive()
22692269
}
2270-
2270+
/// Checks if this type is an Algebraic Data Type(a struct, union, or enum).
22712271
#[inline]
22722272
pub fn is_adt(self) -> bool {
22732273
matches!(self.kind(), Adt(..))
22742274
}
2275-
2275+
/// Checks if a type is a reference.
22762276
#[inline]
22772277
pub fn is_ref(self) -> bool {
22782278
matches!(self.kind(), Ref(..))
@@ -2295,36 +2295,35 @@ impl<'tcx> Ty<'tcx> {
22952295
pub fn is_ty_or_numeric_infer(self) -> bool {
22962296
matches!(self.kind(), Infer(_))
22972297
}
2298-
2298+
/// Checks if this type is [`core::marker::PhantomData`].
22992299
#[inline]
23002300
pub fn is_phantom_data(self) -> bool {
23012301
if let Adt(def, _) = self.kind() { def.is_phantom_data() } else { false }
23022302
}
2303-
2303+
/// Checks if this type is a boolean.
23042304
#[inline]
23052305
pub fn is_bool(self) -> bool {
23062306
*self.kind() == Bool
23072307
}
2308-
23092308
/// Returns `true` if this type is a `str`.
23102309
#[inline]
23112310
pub fn is_str(self) -> bool {
23122311
*self.kind() == Str
23132312
}
2314-
2313+
/// Checks if a type is a parameter type, with index `index`.
23152314
#[inline]
23162315
pub fn is_param(self, index: u32) -> bool {
23172316
match self.kind() {
23182317
ty::Param(ref data) => data.index == index,
23192318
_ => false,
23202319
}
23212320
}
2322-
2321+
/// Checks if this type is a silice type.
23232322
#[inline]
23242323
pub fn is_slice(self) -> bool {
23252324
matches!(self.kind(), Slice(_))
23262325
}
2327-
2326+
/// Checks if this type is a slice or a reference/pointer to a slice.
23282327
#[inline]
23292328
pub fn is_array_slice(self) -> bool {
23302329
match self.kind() {
@@ -2333,20 +2332,21 @@ impl<'tcx> Ty<'tcx> {
23332332
_ => false,
23342333
}
23352334
}
2336-
2335+
/// Checks if this type is an array.
23372336
#[inline]
23382337
pub fn is_array(self) -> bool {
23392338
matches!(self.kind(), Array(..))
23402339
}
2341-
2340+
23422341
#[inline]
23432342
pub fn is_simd(self) -> bool {
23442343
match self.kind() {
23452344
Adt(def, _) => def.repr().simd(),
23462345
_ => false,
23472346
}
23482347
}
2349-
2348+
/// If the type is a slice, array, or a string slice, it will returns its element type.
2349+
/// If this type is not one of those, this function will panic.
23502350
pub fn sequence_element_type(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
23512351
match self.kind() {
23522352
Array(ty, _) | Slice(ty) => *ty,
@@ -2380,7 +2380,7 @@ impl<'tcx> Ty<'tcx> {
23802380
_ => bug!("`simd_size_and_type` called on invalid type"),
23812381
}
23822382
}
2383-
2383+
/// Checks if this type is a mutable reference or a mutable pointer.
23842384
#[inline]
23852385
pub fn is_mutable_ptr(self) -> bool {
23862386
matches!(
@@ -2398,7 +2398,7 @@ impl<'tcx> Ty<'tcx> {
23982398
_ => None,
23992399
}
24002400
}
2401-
2401+
/// Checks if this type is a raw pointer.
24022402
#[inline]
24032403
pub fn is_unsafe_ptr(self) -> bool {
24042404
matches!(self.kind(), RawPtr(_))
@@ -2409,15 +2409,15 @@ impl<'tcx> Ty<'tcx> {
24092409
pub fn is_any_ptr(self) -> bool {
24102410
self.is_ref() || self.is_unsafe_ptr() || self.is_fn_ptr()
24112411
}
2412-
2412+
/// Checks if this type is an [`alloc::boxed::Box`].
24132413
#[inline]
24142414
pub fn is_box(self) -> bool {
24152415
match self.kind() {
24162416
Adt(def, _) => def.is_box(),
24172417
_ => false,
24182418
}
24192419
}
2420-
2420+
/// Returns the type contained within a `Box<T>`.
24212421
/// Panics if called on any type other than `Box<T>`.
24222422
pub fn boxed_ty(self) -> Ty<'tcx> {
24232423
match self.kind() {
@@ -2449,27 +2449,27 @@ impl<'tcx> Ty<'tcx> {
24492449
pub fn is_floating_point(self) -> bool {
24502450
matches!(self.kind(), Float(_) | Infer(FloatVar(_)))
24512451
}
2452-
2452+
/// Checks if this type is an unsized trait object(`dyn Trait`).
24532453
#[inline]
24542454
pub fn is_trait(self) -> bool {
24552455
matches!(self.kind(), Dynamic(_, _, ty::Dyn))
24562456
}
2457-
2457+
/// Checks if this type is an sized trait object(`dyn* Trait`).
24582458
#[inline]
24592459
pub fn is_dyn_star(self) -> bool {
24602460
matches!(self.kind(), Dynamic(_, _, ty::DynStar))
24612461
}
2462-
2462+
/// Checks if this type is an enum.
24632463
#[inline]
24642464
pub fn is_enum(self) -> bool {
24652465
matches!(self.kind(), Adt(adt_def, _) if adt_def.is_enum())
24662466
}
2467-
2467+
/// Checks if this type is an union.
24682468
#[inline]
24692469
pub fn is_union(self) -> bool {
24702470
matches!(self.kind(), Adt(adt_def, _) if adt_def.is_union())
24712471
}
2472-
2472+
/// Checks if this type is a closure.
24732473
#[inline]
24742474
pub fn is_closure(self) -> bool {
24752475
matches!(self.kind(), Closure(..))
@@ -2479,7 +2479,7 @@ impl<'tcx> Ty<'tcx> {
24792479
pub fn is_coroutine(self) -> bool {
24802480
matches!(self.kind(), Coroutine(..))
24812481
}
2482-
2482+
/// Checks if this type is an intgeral type(signed, unsigned on infered).
24832483
#[inline]
24842484
pub fn is_integral(self) -> bool {
24852485
matches!(self.kind(), Infer(IntVar(_)) | Int(_) | Uint(_))
@@ -2494,22 +2494,22 @@ impl<'tcx> Ty<'tcx> {
24942494
pub fn is_fresh(self) -> bool {
24952495
matches!(self.kind(), Infer(FreshTy(_) | FreshIntTy(_) | FreshFloatTy(_)))
24962496
}
2497-
2497+
/// Checks if this type is a `char`.
24982498
#[inline]
24992499
pub fn is_char(self) -> bool {
25002500
matches!(self.kind(), Char)
25012501
}
2502-
2502+
/// Checks if this type is numeric(integral or floating point).
25032503
#[inline]
25042504
pub fn is_numeric(self) -> bool {
25052505
self.is_integral() || self.is_floating_point()
25062506
}
2507-
2507+
/// Checks if this type is a signed intiger.
25082508
#[inline]
25092509
pub fn is_signed(self) -> bool {
25102510
matches!(self.kind(), Int(_))
25112511
}
2512-
2512+
/// Checks if this type is `usize` or `isize`.
25132513
#[inline]
25142514
pub fn is_ptr_sized_integral(self) -> bool {
25152515
matches!(self.kind(), Int(ty::IntTy::Isize) | Uint(ty::UintTy::Usize))
@@ -2582,7 +2582,7 @@ impl<'tcx> Ty<'tcx> {
25822582
_ => None,
25832583
}
25842584
}
2585-
2585+
/// If this type is a function definition or a function pointer, this will retunrn its signature. Otherwise, this function will panic.
25862586
pub fn fn_sig(self, tcx: TyCtxt<'tcx>) -> PolyFnSig<'tcx> {
25872587
match self.kind() {
25882588
FnDef(def_id, args) => tcx.fn_sig(*def_id).instantiate(tcx, args),
@@ -2597,12 +2597,12 @@ impl<'tcx> Ty<'tcx> {
25972597
_ => bug!("Ty::fn_sig() called on non-fn type: {:?}", self),
25982598
}
25992599
}
2600-
2600+
/// Checks if this type is a function definition or a function pointer.
26012601
#[inline]
26022602
pub fn is_fn(self) -> bool {
26032603
matches!(self.kind(), FnDef(..) | FnPtr(_))
26042604
}
2605-
2605+
/// Checks if this type is a function pointer.
26062606
#[inline]
26072607
pub fn is_fn_ptr(self) -> bool {
26082608
matches!(self.kind(), FnPtr(_))
@@ -2612,7 +2612,7 @@ impl<'tcx> Ty<'tcx> {
26122612
pub fn is_impl_trait(self) -> bool {
26132613
matches!(self.kind(), Alias(ty::Opaque, ..))
26142614
}
2615-
2615+
/// Returns the `AdtDef` defining this type, if it is an ADT(struct,union or enum). If this type is not an ADT, returns `None`.
26162616
#[inline]
26172617
pub fn ty_adt_def(self) -> Option<AdtDef<'tcx>> {
26182618
match self.kind() {

compiler/rustc_middle/src/ty/util.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for OpaqueTypeExpander<'tcx> {
10071007

10081008
impl<'tcx> Ty<'tcx> {
10091009
/// Returns the `Size` for primitive types (bool, uint, int, char, float).
1010+
/// If the type is not primitive, will panic with message "non primitive type".
10101011
pub fn primitive_size(self, tcx: TyCtxt<'tcx>) -> Size {
10111012
match *self.kind() {
10121013
ty::Bool => Size::from_bytes(1),
@@ -1018,7 +1019,8 @@ impl<'tcx> Ty<'tcx> {
10181019
_ => bug!("non primitive type"),
10191020
}
10201021
}
1021-
1022+
/// Returns the size and sign of an inieger type.
1023+
/// If the type is not an intieger, will panic with message "non integer discriminant".
10221024
pub fn int_size_and_signed(self, tcx: TyCtxt<'tcx>) -> (Size, bool) {
10231025
match *self.kind() {
10241026
ty::Int(ity) => (Integer::from_int_ty(&tcx, ity).size(), true),

0 commit comments

Comments
 (0)