Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c881f72

Browse files
committedJul 28, 2024·
fully document rustdoc-json-types
1 parent 1afc5fd commit c881f72

File tree

1 file changed

+533
-52
lines changed

1 file changed

+533
-52
lines changed
 

‎src/rustdoc-json-types/lib.rs

Lines changed: 533 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ use rustc_hash::FxHashMap;
77
use serde::{Deserialize, Serialize};
88
use std::path::PathBuf;
99

10-
/// rustdoc format-version.
10+
/// The version of JSON output that this crate represents.
11+
///
12+
/// This integer is incremented with every breaking change to the API,
13+
/// and is returned along with the JSON blob as [`Crate::format_version`].
14+
/// Consuming code should assert that this value matches the format version(s) that it supports.
1115
pub const FORMAT_VERSION: u32 = 32;
1216

13-
/// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information
17+
/// The root of the emitted JSON blob.
18+
///
19+
/// It contains all type/documentation information
1420
/// about the language items in the local crate, as well as info about external items to allow
1521
/// tools to find or link to them.
1622
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
@@ -33,13 +39,18 @@ pub struct Crate {
3339
pub format_version: u32,
3440
}
3541

42+
/// Metadata of a crate, either the same crate on which `rustdoc` was invoked, or its dependency.
3643
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
3744
pub struct ExternalCrate {
45+
/// The name of the crate.
3846
pub name: String,
47+
/// The root URL at which the crate's documentation lives.
3948
pub html_root_url: Option<String>,
4049
}
4150

42-
/// For external (not defined in the local crate) items, you don't get the same level of
51+
/// Information about an external (not defined in the local crate) [`Item`].
52+
///
53+
/// For external items, you don't get the same level of
4354
/// information. This struct should contain enough to generate a link/reference to the item in
4455
/// question, or can be used by a tool that takes the json output of multiple crates to find
4556
/// the actual item definition with all the relevant info.
@@ -60,6 +71,10 @@ pub struct ItemSummary {
6071
pub kind: ItemKind,
6172
}
6273

74+
/// Anything that can hold documentation - modules, structs, enums, functions, traits, etc.
75+
///
76+
/// The `Item` data type holds fields that can apply to any of these,
77+
/// and leaves kind-specific details (like function args or enum variants) to the `inner` field.
6378
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
6479
pub struct Item {
6580
/// The unique identifier of this item. Can be used to find this item in various mappings.
@@ -82,10 +97,13 @@ pub struct Item {
8297
pub links: FxHashMap<String, Id>,
8398
/// Stringified versions of the attributes on this item (e.g. `"#[inline]"`)
8499
pub attrs: Vec<String>,
100+
/// Information about the item’s deprecation, if present.
85101
pub deprecation: Option<Deprecation>,
102+
/// The type-specific fields describing this item.
86103
pub inner: ItemEnum,
87104
}
88105

106+
/// A range of source code.
89107
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
90108
pub struct Span {
91109
/// The path to the source file for this span relative to the path `rustdoc` was invoked with.
@@ -96,28 +114,39 @@ pub struct Span {
96114
pub end: (usize, usize),
97115
}
98116

117+
/// Information about the deprecation of an [`Item`].
99118
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
100119
pub struct Deprecation {
120+
/// Usually a version number when this [`Item`] first became deprecated.
101121
pub since: Option<String>,
122+
/// The reason for deprecation and/or what alternatives to use.
102123
pub note: Option<String>,
103124
}
104125

126+
/// Visibility of an [`Item`].
105127
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
106128
#[serde(rename_all = "snake_case")]
107129
pub enum Visibility {
130+
/// Explicitly public visibility set with `pub`.
108131
Public,
109132
/// For the most part items are private by default. The exceptions are associated items of
110133
/// public traits and variants of public enums.
111134
Default,
135+
/// Explicitly crate-wide visibility set with `pub(crate)`
112136
Crate,
113-
/// For `pub(in path)` visibility. `parent` is the module it's restricted to and `path` is how
114-
/// that module was referenced (like `"super::super"` or `"crate::foo::bar"`).
137+
/// For `pub(in path)` visibility.
115138
Restricted {
139+
/// ID of the module to which this visibility restricts items.
116140
parent: Id,
141+
/// The path with which [`parent`] was referenced
142+
/// (like `super::super` or `crate::foo::bar`).
143+
///
144+
/// [`parent`]: Visibility::Restricted::parent
117145
path: String,
118146
},
119147
}
120148

149+
/// Dynamic trait object type (`dyn Trait`).
121150
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
122151
pub struct DynTrait {
123152
/// All the traits implemented. One of them is the vtable, and the rest must be auto traits.
@@ -132,64 +161,133 @@ pub struct DynTrait {
132161
pub lifetime: Option<String>,
133162
}
134163

135-
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
136164
/// A trait and potential HRTBs
165+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
137166
pub struct PolyTrait {
167+
/// The path to the trait.
138168
#[serde(rename = "trait")]
139169
pub trait_: Path,
140170
/// Used for Higher-Rank Trait Bounds (HRTBs)
141171
/// ```text
142172
/// dyn for<'a> Fn() -> &'a i32"
143173
/// ^^^^^^^
144-
/// |
145-
/// this part
146174
/// ```
147175
pub generic_params: Vec<GenericParamDef>,
148176
}
149177

178+
/// A set of generic arguments provided to a path segment, e.g.
179+
///
180+
/// ```text
181+
/// std::option::Option::<u32>::None
182+
/// ^^^^^
183+
/// ```
150184
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
151185
#[serde(rename_all = "snake_case")]
152186
pub enum GenericArgs {
153-
/// <'a, 32, B: Copy, C = u32>
154-
AngleBracketed { args: Vec<GenericArg>, bindings: Vec<TypeBinding> },
155-
/// Fn(A, B) -> C
156-
Parenthesized { inputs: Vec<Type>, output: Option<Type> },
187+
/// `<'a, 32, B: Copy, C = u32>`
188+
AngleBracketed {
189+
/// The list of each argument on this type.
190+
/// ```text
191+
/// <'a, 32, B: Copy, C = u32>
192+
/// ^^^^^^
193+
/// ```
194+
args: Vec<GenericArg>,
195+
/// Associated type or constant bindings (e.g. `Item=i32` or `Item: Clone`) for this type.
196+
bindings: Vec<TypeBinding>,
197+
},
198+
/// `Fn(A, B) -> C`
199+
Parenthesized {
200+
/// The input types, enclosed in parentheses.
201+
inputs: Vec<Type>,
202+
/// The output type provided after the `->`, if present.
203+
output: Option<Type>,
204+
},
157205
}
158206

207+
/// One argument in a list of generic arguments to a path segment.
208+
///
209+
/// Part of [`GenericArgs`].
159210
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
160211
#[serde(rename_all = "snake_case")]
161212
pub enum GenericArg {
213+
/// A lifetime argument.
214+
/// ```text
215+
/// std::borrow::Cow<'static, str>
216+
/// ^^^^^^^
217+
/// ```
162218
Lifetime(String),
219+
/// A type argument.
220+
/// ```text
221+
/// std::borrow::Cow<'static, str>
222+
/// ^^^
223+
/// ```
163224
Type(Type),
225+
/// A constant as a generic argument.
226+
/// ```text
227+
/// core::array::IntoIter<u32, { 640 * 1024 }>
228+
/// ^^^^^^^^^^^^^^
229+
/// ```
164230
Const(Constant),
231+
/// A generic argument that's explicitly set to be inferred.
232+
/// ```text
233+
/// std::vec::Vec::<_>::new()
234+
/// ^
235+
/// ```
165236
Infer,
166237
}
167238

239+
/// A constant.
168240
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
169241
pub struct Constant {
242+
/// The stringified expression of this constant. Note that its mapping to the original
243+
/// source code is unstable and it's not guaranteed that it'll match the source code.
170244
pub expr: String,
245+
/// The value of the evaluated expression for this constant, which is only computed for numeric
246+
/// types.
171247
pub value: Option<String>,
248+
/// Whether this constant is a bool, numeric, string, or char literal.
172249
pub is_literal: bool,
173250
}
174251

252+
/// Describes a bound applied to an associated type/constant.
253+
///
254+
/// Example:
255+
/// ```text
256+
/// IntoIterator<Item = u32, IntoIter: Clone>
257+
/// ^^^^^^^^^^ ^^^^^^^^^^^^^^^
258+
/// ```
175259
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
176260
pub struct TypeBinding {
261+
/// The name of the associated type/constant.
177262
pub name: String,
263+
/// Arguments provided to the associated type/constant.
178264
pub args: GenericArgs,
265+
/// The kind of bound applied to the associated type/constant.
179266
pub binding: TypeBindingKind,
180267
}
181268

269+
/// The way in which an associate type/constant is bound.
182270
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
183271
#[serde(rename_all = "snake_case")]
184272
pub enum TypeBindingKind {
273+
/// The required value/type is specified exactly. e.g.
274+
/// ```text
275+
/// Iterator<Item = u32, IntoIter: DoubleEndedIterator>
276+
/// ^^^^^^^^^^
277+
/// ```
185278
Equality(Term),
279+
/// The type is required to satisfy a set of bounds.
280+
/// ```text
281+
/// Iterator<Item = u32, IntoIter: DoubleEndedIterator>
282+
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
283+
/// ```
186284
Constraint(Vec<GenericBound>),
187285
}
188286

189287
/// An opaque identifier for an item.
190288
///
191-
/// It can be used to lookup in [Crate::index] or [Crate::paths] to resolve it
192-
/// to an [Item].
289+
/// It can be used to lookup in [`Crate::index`] or [`Crate::paths`] to resolve it
290+
/// to an [`Item`].
193291
///
194292
/// Id's are only valid within a single JSON blob. They cannot be used to
195293
/// resolve references between the JSON output's for different crates.
@@ -201,115 +299,230 @@ pub enum TypeBindingKind {
201299
// FIXME(aDotInTheVoid): Consider making this non-public in rustdoc-types.
202300
pub struct Id(pub String);
203301

302+
/// The fundamental kind of an item. Unlike [`ItemEnum`], this does not carry any aditional info.
303+
///
304+
/// Part of [`ItemSummary`].
204305
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
205306
#[serde(rename_all = "snake_case")]
206307
pub enum ItemKind {
308+
/// A module declaration, e.g. `mod foo;` or `mod foo {}`
207309
Module,
310+
/// A crate imported via the `extern crate` syntax.
208311
ExternCrate,
312+
/// An import of 1 or more items into scope, using the `use` keyword.
209313
Import,
314+
/// A `struct` declaration.
210315
Struct,
316+
/// A field of a struct.
211317
StructField,
318+
/// A `union` declaration.
212319
Union,
320+
/// An `enum` declaration.
213321
Enum,
322+
/// A variant of a enum.
214323
Variant,
324+
/// A function declaration, e.g. `fn f() {}`
215325
Function,
326+
/// A type alias declaration, e.g. `type Pig = std::borrow::Cow<'static, str>;`
216327
TypeAlias,
217328
OpaqueTy,
329+
/// The declaration of a constant, e.g. `const GREETING: &str = "Hi :3";`
218330
Constant,
331+
/// A `trait` declaration.
219332
Trait,
333+
/// A trait alias declaration, e.g. `trait Int = Add + Sub + Mul + Div;`
334+
///
335+
/// See [the tracking issue](https://github.com/rust-lang/rust/issues/41517)
220336
TraitAlias,
337+
/// An `impl` block.
221338
Impl,
339+
/// A `static` declaration.
222340
Static,
341+
/// `type`s from an `extern` block.
342+
///
343+
/// See [the tracking issue](https://github.com/rust-lang/rust/issues/43467)
223344
ForeignType,
345+
/// A macro declaration.
346+
///
347+
/// Corresponds to either `ItemEnum::Macro(_)`
348+
/// or `ItemEnum::ProcMacro(ProcMacro { kind: MacroKind::Bang })`
224349
Macro,
350+
/// A procedural macro attribute.
351+
///
352+
/// Corresponds to `ItemEnum::ProcMacro(ProcMacro { kind: MacroKind::Attr })`
225353
ProcAttribute,
354+
/// A procedural macro usable in the `#[derive()]` attribute.
355+
///
356+
/// Corresponds to `ItemEnum::ProcMacro(ProcMacro { kind: MacroKind::Derive })`
226357
ProcDerive,
358+
/// An associated constant of a trait or a type.
227359
AssocConst,
360+
/// An associated type of a trait or a type.
228361
AssocType,
362+
/// A primitive type, e.g. `u32`.
363+
///
364+
/// [`Item`]s of this kind only come from the core library.
229365
Primitive,
366+
/// A keyword declaration.
367+
///
368+
/// [`Item`]s of this kind only come from the come library and exist solely
369+
/// to carry documentation for the respective keywords.
230370
Keyword,
231371
}
232372

373+
/// Specific fields of an item.
374+
///
375+
/// Part of [`Item`].
233376
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
234377
#[serde(rename_all = "snake_case")]
235378
pub enum ItemEnum {
379+
/// A module declaration, e.g. `mod foo;` or `mod foo {}`
236380
Module(Module),
381+
/// A crate imported via the `extern crate` syntax.
237382
ExternCrate {
383+
/// The name of the imported crate.
238384
name: String,
385+
/// If the crate is renamed, this is its name in the crate.
239386
rename: Option<String>,
240387
},
388+
/// An import of 1 or more items into scope, using the `use` keyword.
241389
Import(Import),
242390

391+
/// A `union` declaration.
243392
Union(Union),
393+
/// A `struct` declaration.
244394
Struct(Struct),
395+
/// A field of a struct.
245396
StructField(Type),
397+
/// An `enum` declaration.
246398
Enum(Enum),
399+
/// A variant of a enum.
247400
Variant(Variant),
248401

402+
/// A function declaration (including methods and other associated functions)
249403
Function(Function),
250404

405+
/// A `trait` declaration.
251406
Trait(Trait),
407+
/// A trait alias declaration, e.g. `trait Int = Add + Sub + Mul + Div;`
408+
///
409+
/// See [the tracking issue](https://github.com/rust-lang/rust/issues/41517)
252410
TraitAlias(TraitAlias),
411+
/// An `impl` block.
253412
Impl(Impl),
254413

414+
/// A type alias declaration, e.g. `type Pig = std::borrow::Cow<'static, str>;`
255415
TypeAlias(TypeAlias),
256416
OpaqueTy(OpaqueTy),
417+
/// The declaration of a constant, e.g. `const GREETING: &str = "Hi :3";`
257418
Constant {
419+
/// The type of the constant.
258420
#[serde(rename = "type")]
259421
type_: Type,
422+
/// The declared constant itself.
260423
#[serde(rename = "const")]
261424
const_: Constant,
262425
},
263426

427+
/// A declaration of a `static`.
264428
Static(Static),
265429

266-
/// `type`s from an extern block
430+
/// `type`s from an `extern` block.
431+
///
432+
/// See [the tracking issue](https://github.com/rust-lang/rust/issues/43467)
267433
ForeignType,
268434

269-
/// Declarative macro_rules! macro
435+
/// A macro_rules! declarative macro. Contains a single string with the source
436+
/// representation of the macro with the patterns stripped.
270437
Macro(String),
438+
/// A procedural macro.
271439
ProcMacro(ProcMacro),
272440

441+
/// A primitive type, e.g. `u32`.
442+
///
443+
/// [`Item`]s of this kind only come from the core library.
273444
Primitive(Primitive),
274445

446+
/// An associated constant of a trait or a type.
275447
AssocConst {
448+
/// The type of the constant.
276449
#[serde(rename = "type")]
277450
type_: Type,
278-
/// e.g. `const X: usize = 5;`
451+
/// The stringified expression for the default value, if provided, e.g.
452+
/// ```rust
453+
/// const X: usize = 640 * 1024;
454+
/// // ^^^^^^^^^^
455+
/// ```
279456
default: Option<String>,
280457
},
458+
/// An associated type of a trait or a type.
281459
AssocType {
460+
/// The generic parameters and where clauses on ahis associated type.
282461
generics: Generics,
462+
/// The bounds for this associated type. e.g.
463+
/// ```rust
464+
/// trait IntoIterator {
465+
/// type Item;
466+
/// type IntoIter: Iterator<Item = Self::Item>;
467+
/// // ^^^^^^^^^^^^^^^^^^^^^^^^^^^
468+
/// }
469+
/// ```
283470
bounds: Vec<GenericBound>,
284-
/// e.g. `type X = usize;`
471+
/// The default for this type, if provided, e.g.
472+
/// ```rust
473+
/// type X = usize;
474+
/// // ^^^^^
475+
/// ```
285476
default: Option<Type>,
286477
},
287478
}
288479

480+
/// A module declaration, e.g. `mod foo;` or `mod foo {}`.
289481
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
290482
pub struct Module {
483+
/// Whether this is the root item of a crate.
484+
///
485+
/// This item doesn't correspond to any construction in the source code and is generated by the
486+
/// compiler.
291487
pub is_crate: bool,
488+
/// [`Item`]s declared inside this module.
292489
pub items: Vec<Id>,
293490
/// If `true`, this module is not part of the public API, but it contains
294491
/// items that are re-exported as public API.
295492
pub is_stripped: bool,
296493
}
297494

495+
/// A `union`.
298496
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
299497
pub struct Union {
498+
/// The generic parameters and where clauses on this union.
300499
pub generics: Generics,
500+
/// Whether any fields have been removed from the result, due to being private or hidden.
301501
pub fields_stripped: bool,
502+
/// The list of fields in the union.
503+
///
504+
/// All of the corresponding [`Item`]s are of kind [`ItemEnum::StructField`].
302505
pub fields: Vec<Id>,
506+
/// All impls (both of traits and inherent) for this union.
507+
///
508+
/// All of the corresponding [`Item`]s are of kind [`ItemEnum::Impl`].
303509
pub impls: Vec<Id>,
304510
}
305511

512+
/// A `struct`.
306513
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
307514
pub struct Struct {
515+
/// The kind of the struct (e.g. unit, tuple-like or struct-like) and the data specific to it,
516+
/// i.e. fields.
308517
pub kind: StructKind,
518+
/// The generic parameters and where clauses on this struct.
309519
pub generics: Generics,
520+
/// All impls (both of traits and inherent) for this struct.
521+
/// All of the corresponding [`Item`]s are of kind [`ItemEnum::Impl`].
310522
pub impls: Vec<Id>,
311523
}
312524

525+
/// The kind of a [`Struct`] and the data specific to it, i.e. fields.
313526
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
314527
#[serde(rename_all = "snake_case")]
315528
pub enum StructKind {
@@ -321,31 +534,47 @@ pub enum StructKind {
321534
Unit,
322535
/// A struct with unnamed fields.
323536
///
537+
/// All [`Id`]'s will point to [`ItemEnum::StructField`].
538+
/// Unlike most of JSON, private and `#[doc(hidden)]` fields will be given as `None`
539+
/// instead of being omitted, because order matters.
540+
///
324541
/// ```rust
325542
/// pub struct TupleStruct(i32);
326543
/// pub struct EmptyTupleStruct();
327544
/// ```
328-
///
329-
/// All [`Id`]'s will point to [`ItemEnum::StructField`]. Private and
330-
/// `#[doc(hidden)]` fields will be given as `None`
331545
Tuple(Vec<Option<Id>>),
332546
/// A struct with named fields.
333547
///
334548
/// ```rust
335549
/// pub struct PlainStruct { x: i32 }
336550
/// pub struct EmptyPlainStruct {}
337551
/// ```
338-
Plain { fields: Vec<Id>, fields_stripped: bool },
552+
Plain {
553+
/// The list of fields in the struct.
554+
///
555+
/// All of the corresponding [`Item`]s are of kind [`ItemEnum::StructField`].
556+
fields: Vec<Id>,
557+
/// Whether any fields have been removed from the result, due to being private or hidden.
558+
fields_stripped: bool,
559+
},
339560
}
340561

562+
/// An `enum`.
341563
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
342564
pub struct Enum {
565+
/// Information about the type parameters and `where` clauses of the enum.
343566
pub generics: Generics,
567+
/// Whether any variants have been removed from the result, due to being private or hidden.
344568
pub variants_stripped: bool,
569+
/// The list of variants in the enum.
570+
///
571+
/// All of the corresponding [`Item`]s are of kind [`ItemEnum::Variant`]
345572
pub variants: Vec<Id>,
573+
/// `impl`s for the enum.
346574
pub impls: Vec<Id>,
347575
}
348576

577+
/// A variant of an enum.
349578
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
350579
pub struct Variant {
351580
/// Whether the variant is plain, a tuple-like, or struct-like. Contains the fields.
@@ -354,6 +583,7 @@ pub struct Variant {
354583
pub discriminant: Option<Discriminant>,
355584
}
356585

586+
/// The kind of an [`Enum`] [`Variant`] and the data specific to it, i.e. fields.
357587
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
358588
#[serde(rename_all = "snake_case")]
359589
pub enum VariantKind {
@@ -368,7 +598,8 @@ pub enum VariantKind {
368598
Plain,
369599
/// A variant with unnamed fields.
370600
///
371-
/// Unlike most of json, `#[doc(hidden)]` fields will be given as `None`
601+
/// All [`Id`]'s will point to [`ItemEnum::StructField`].
602+
/// Unlike most of JSON, `#[doc(hidden)]` fields will be given as `None`
372603
/// instead of being omitted, because order matters.
373604
///
374605
/// ```rust
@@ -386,9 +617,16 @@ pub enum VariantKind {
386617
/// EmptyStructVariant {},
387618
/// }
388619
/// ```
389-
Struct { fields: Vec<Id>, fields_stripped: bool },
620+
Struct {
621+
/// The list of variants in the enum.
622+
/// All of the corresponding [`Item`]s are of kind [`ItemEnum::Variant`].
623+
fields: Vec<Id>,
624+
/// Whether any variants have been removed from the result, due to being private or hidden.
625+
fields_stripped: bool,
626+
},
390627
}
391628

629+
/// The value that distinguishes a variant in an [`Enum`] from other variants.
392630
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
393631
pub struct Discriminant {
394632
/// The expression that produced the discriminant.
@@ -406,62 +644,123 @@ pub struct Discriminant {
406644
pub value: String,
407645
}
408646

647+
/// A set of fundamental properties of a function.
409648
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
410649
pub struct Header {
650+
/// Is this function marked as `const`?
411651
#[serde(rename = "const")]
412652
pub const_: bool,
653+
/// Is this function unsafe?
413654
#[serde(rename = "unsafe")]
414655
pub unsafe_: bool,
656+
/// Is this function async?
415657
#[serde(rename = "async")]
416658
pub async_: bool,
659+
/// The ABI used by the function.
417660
pub abi: Abi,
418661
}
419662

663+
/// The ABI (Application Binary Interface) used by a function.
664+
///
665+
/// If a variant has an `unwind` field, this means the ABI that it represents can be specified in 2
666+
/// ways: `extern "_"` and `extern "_-unwind"`, and a value of `true` for that field signifies the
667+
/// latter variant.
668+
///
669+
/// See the [Rustonomicon section](https://doc.rust-lang.org/nightly/nomicon/ffi.html#ffi-and-unwinding)
670+
/// on unwinding for more info.
420671
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
421672
pub enum Abi {
422673
// We only have a concrete listing here for stable ABI's because their are so many
423674
// See rustc_ast_passes::feature_gate::PostExpansionVisitor::check_abi for the list
675+
/// The default ABI, but that can also be written explicitly with `extern "Rust"`.
424676
Rust,
677+
/// Can be specified as `extern "C"` or, as a shorthand, just `extern`.
425678
C { unwind: bool },
679+
/// Can be specified as `extern "cdecl"`.
426680
Cdecl { unwind: bool },
681+
/// Can be specified as `extern "stdcall"`.
427682
Stdcall { unwind: bool },
683+
/// Can be specified as `extern "fastcall"`.
428684
Fastcall { unwind: bool },
685+
/// Can be specified as `extern "aapcs"`.
429686
Aapcs { unwind: bool },
687+
/// Can be specified as `extern "win64"`.
430688
Win64 { unwind: bool },
689+
/// Can be specifed as `extern "sysv64"`.
431690
SysV64 { unwind: bool },
691+
/// Can be specified as `extern "system"`.
432692
System { unwind: bool },
693+
/// Any other ABI, including unstable ones.
433694
Other(String),
434695
}
435696

436-
/// Represents a function (including methods and other associated functions)
697+
/// A function declaration (including methods and other associated functions).
437698
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
438699
pub struct Function {
700+
/// Information about the function signature, or declaration.
439701
pub decl: FnDecl,
702+
/// Information about the function’s type parameters and `where` clauses.
440703
pub generics: Generics,
704+
/// Information about core properties of the function, e.g. whether it's `const`, its ABI, etc.
441705
pub header: Header,
706+
/// Whether the function has a body, i.e. an implementation.
442707
pub has_body: bool,
443708
}
444709

710+
/// Generic parameters accepted by an item and `where` clauses imposed on it and the parameters.
445711
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
446712
pub struct Generics {
713+
/// A list of generic parameter definitions (e.g. `<T: Clone + Hash, U: Copy>`).
447714
pub params: Vec<GenericParamDef>,
715+
/// A list of where predicates (e.g. `where T: Iterator, T::Item: Copy`).
448716
pub where_predicates: Vec<WherePredicate>,
449717
}
450718

719+
/// One generic parameter accepted by an item.
451720
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
452721
pub struct GenericParamDef {
722+
/// Name of the parameter.
723+
/// ```rust
724+
/// fn f<'resource, Resource>(x: &'resource Resource) {}
725+
/// // ^^^^^^^^ ^^^^^^^^
726+
/// ```
453727
pub name: String,
728+
/// The kind of the parameter and data specific to a particular parameter kind, e.g. type
729+
/// bounds.
454730
pub kind: GenericParamDefKind,
455731
}
456732

733+
/// The kind of a [`GenericParamDef`].
457734
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
458735
#[serde(rename_all = "snake_case")]
459736
pub enum GenericParamDefKind {
737+
/// Denotes a lifetime parameter.
460738
Lifetime {
739+
/// Lifetimes that this lifetime parameter is required to outlive.
740+
///
741+
/// ```rust
742+
/// fn f<'a, 'b, 'resource: 'a + 'b>(a: &'a str, b: &'b str, res: &'resource str) {}
743+
/// // ^^^^^^^
744+
/// ```
461745
outlives: Vec<String>,
462746
},
747+
748+
/// Denotes a type parameter.
463749
Type {
750+
/// Bounds applied directly to the type. Note that the bounds from `where` clauses
751+
/// that constrain this parameter won't appear here.
752+
///
753+
/// ```rust
754+
/// fn default2<T: Default>() -> [T; 2] where T: Clone { todo!() }
755+
/// // ^^^^^^^
756+
/// ```
464757
bounds: Vec<GenericBound>,
758+
/// The default type for this parameter, if provided, e.g.
759+
///
760+
/// ```rust
761+
/// trait PartialEq<Rhs = Self> {}
762+
/// // ^^^^
763+
/// ```
465764
default: Option<Type>,
466765
/// This is normally `false`, which means that this generic parameter is
467766
/// declared in the Rust source text.
@@ -488,43 +787,75 @@ pub enum GenericParamDefKind {
488787
/// the Rust source text.
489788
synthetic: bool,
490789
},
790+
791+
/// Denotes a constant parameter.
491792
Const {
793+
/// The type of the constant as declared.
492794
#[serde(rename = "type")]
493795
type_: Type,
796+
/// The stringified expression for the default value, if provided. It's not guaranteed that
797+
/// it'll match the actual source code for the default value.
494798
default: Option<String>,
495799
},
496800
}
497801

802+
/// One `where` clause.
803+
/// ```rust
804+
/// fn default<T>() -> T where T: Default { T::default() }
805+
/// // ^^^^^^^^^^
806+
/// ```
498807
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
499808
#[serde(rename_all = "snake_case")]
500809
pub enum WherePredicate {
810+
/// A type is expected to comply with a set of bounds
501811
BoundPredicate {
812+
/// The type that's being constrained.
813+
///
814+
/// ```rust
815+
/// fn f<T>(x: T) where for<'a> &'a T: Iterator {}
816+
/// // ^
817+
/// ```
502818
#[serde(rename = "type")]
503819
type_: Type,
820+
/// The set of bounds that constrain the type.
821+
///
822+
/// ```rust
823+
/// fn f<T>(x: T) where for<'a> &'a T: Iterator {}
824+
/// // ^^^^^^^^
825+
/// ```
504826
bounds: Vec<GenericBound>,
505827
/// Used for Higher-Rank Trait Bounds (HRTBs)
506-
/// ```text
507-
/// where for<'a> &'a T: Iterator,"
508-
/// ^^^^^^^
509-
/// |
510-
/// this part
828+
/// ```rust
829+
/// fn f<T>(x: T) where for<'a> &'a T: Iterator {}
830+
/// // ^^^^^^^
511831
/// ```
512832
generic_params: Vec<GenericParamDef>,
513833
},
834+
835+
/// A lifetime is expected to outlive other lifetimes.
514836
LifetimePredicate {
837+
/// The name of the lifetime.
515838
lifetime: String,
839+
/// The lifetimes that must be encompassed by the lifetime.
516840
outlives: Vec<String>,
517841
},
842+
843+
/// A type must exactly equal another type.
518844
EqPredicate {
845+
/// The left side of the equation.
519846
lhs: Type,
847+
/// The right side of the equation.
520848
rhs: Term,
521849
},
522850
}
523851

852+
/// Either a trait bound or a lifetime bound.
524853
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
525854
#[serde(rename_all = "snake_case")]
526855
pub enum GenericBound {
856+
/// A trait bound.
527857
TraitBound {
858+
/// The full path to the trait.
528859
#[serde(rename = "trait")]
529860
trait_: Path,
530861
/// Used for Higher-Rank Trait Bounds (HRTBs)
@@ -535,157 +866,277 @@ pub enum GenericBound {
535866
/// this part
536867
/// ```
537868
generic_params: Vec<GenericParamDef>,
869+
/// The context for which a trait is supposed to be used, e.g. `const
538870
modifier: TraitBoundModifier,
539871
},
872+
/// A lifetime bound, e.g.
873+
/// ```rust
874+
/// fn f<'a, T>(x: &'a str, y: &T) where T: 'a {}
875+
/// // ^^^
876+
/// ```
540877
Outlives(String),
541878
/// `use<'a, T>` precise-capturing bound syntax
542879
Use(Vec<String>),
543880
}
544881

882+
/// A set of modifiers applied to a trait.
545883
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
546884
#[serde(rename_all = "snake_case")]
547885
pub enum TraitBoundModifier {
886+
/// Marks the absence of a modifier.
548887
None,
888+
/// Indicates that the trait bound relaxes a trait bound applied to a parameter by default,
889+
/// e.g. `T: Sized?`, the `Sized` trait is required for all generic type parameters by default
890+
/// unless specified otherwise with this modifier.
549891
Maybe,
892+
/// Indicates that the trait bound must be applicable in both a run-time and a compile-time
893+
/// context.
550894
MaybeConst,
551895
}
552896

897+
/// Either a type or a constant, usually stored as the right-hand side of an equation in places like
898+
/// [`TypeBinding`]
553899
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
554900
#[serde(rename_all = "snake_case")]
555901
pub enum Term {
902+
/// A type.
903+
///
904+
/// ```rust
905+
/// fn f(x: impl IntoIterator<Item = u32>) {}
906+
/// // ^^^
907+
/// ```
556908
Type(Type),
909+
/// A constant.
910+
///
911+
/// ```ignore (incomplete feature in the snippet)
912+
/// trait Foo {
913+
/// const BAR: usize;
914+
/// }
915+
///
916+
/// fn f(x: impl Foo<BAR = 42>) {}
917+
/// // ^^
918+
/// ```
557919
Constant(Constant),
558920
}
559921

922+
/// A type.
560923
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
561924
#[serde(rename_all = "snake_case")]
562925
pub enum Type {
563-
/// Structs, enums, and unions
926+
/// Structs, enums, unions and type aliases, e.g. `std::option::Option<u32>`
564927
ResolvedPath(Path),
928+
/// Dynamic trait object type (`dyn Trait`).
565929
DynTrait(DynTrait),
566-
/// Parameterized types
930+
/// Parameterized types. The contained string is the name of the parameter.
567931
Generic(String),
568-
/// Built in numeric (i*, u*, f*) types, bool, and char
932+
/// Built-in numeric types (e.g. `u32`, `f32`), `bool`, `char`.
569933
Primitive(String),
570-
/// `extern "ABI" fn`
934+
/// A function pointer type, e.g. `fn(u32) -> u32`, `extern "C" fn() -> *const u8`
571935
FunctionPointer(Box<FunctionPointer>),
572-
/// `(String, u32, Box<usize>)`
936+
/// A tuple type, e.g. `(String, u32, Box<usize>)`
573937
Tuple(Vec<Type>),
574-
/// `[u32]`
938+
/// An unsized slice type, e.g. `[u32]`.
575939
Slice(Box<Type>),
576-
/// [u32; 15]
940+
/// An array type, e.g. `[u32; 15]`
577941
Array {
942+
/// The type of the contained element.
578943
#[serde(rename = "type")]
579944
type_: Box<Type>,
945+
/// The stringified expression that is the length of the array.
946+
///
947+
/// Keep in mind that it's not guaranteed to match the actual source code of the expression.
580948
len: String,
581949
},
582-
/// `u32 is 1..`
950+
/// A pattern type, e.g. `u32 is 1..`
951+
///
952+
/// See [the tracking issue](https://github.com/rust-lang/rust/issues/123646)
583953
Pat {
954+
/// The base type, e.g. the `u32` in `u32 is 1..`
584955
#[serde(rename = "type")]
585956
type_: Box<Type>,
586957
#[doc(hidden)]
587958
__pat_unstable_do_not_use: String,
588959
},
589-
/// `impl TraitA + TraitB + ...`
960+
/// An opaque type that satisfies a set of bounds, `impl TraitA + TraitB + ...`
590961
ImplTrait(Vec<GenericBound>),
591-
/// `_`
962+
/// A type that's left to be inferred, `_`
592963
Infer,
593-
/// `*mut u32`, `*u8`, etc.
964+
/// A raw pointer type, e.g. `*mut u32`, `*const u8`, etc.
594965
RawPointer {
966+
/// This is `true` for `*mut _` and `false` for `*const _`.
595967
mutable: bool,
968+
/// The type of the pointee.
596969
#[serde(rename = "type")]
597970
type_: Box<Type>,
598971
},
599972
/// `&'a mut String`, `&str`, etc.
600973
BorrowedRef {
974+
/// The name of the lifetime of the reference, if provided.
601975
lifetime: Option<String>,
976+
/// This is `true` for `&mut i32` and `false` for `&i32`
602977
mutable: bool,
978+
/// The type of the pointee, e.g. the `i32` in `&'a mut i32`
603979
#[serde(rename = "type")]
604980
type_: Box<Type>,
605981
},
606982
/// Associated types like `<Type as Trait>::Name` and `T::Item` where
607983
/// `T: Iterator` or inherent associated types like `Struct::Name`.
608984
QualifiedPath {
985+
/// The name of the associated type in the parent type.
986+
///
987+
/// ```ignore (incomplete expresssion)
988+
/// <core::array::IntoIter<u32, 42> as Iterator>::Item
989+
/// // ^^^^
990+
/// ```
609991
name: String,
992+
/// The generic arguments provided to the associated type.
993+
///
994+
/// ```ignore (incomplete expression)
995+
/// <core::slice::IterMut<'static, u32> as BetterIterator>::Item<'static>
996+
/// // ^^^^^^^^^
997+
/// ```
610998
args: Box<GenericArgs>,
999+
/// The type with which this type is associated.
1000+
///
1001+
/// ```ignore (incomplete expression)
1002+
/// <core::array::IntoIter<u32, 42> as Iterator>::Item
1003+
/// // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1004+
/// ```
6111005
self_type: Box<Type>,
6121006
/// `None` iff this is an *inherent* associated type.
6131007
#[serde(rename = "trait")]
6141008
trait_: Option<Path>,
6151009
},
6161010
}
6171011

1012+
/// A type that has a simple path to it. This is the kind of type of structs, unions, enums, etc.
6181013
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
6191014
pub struct Path {
1015+
/// The name of the type as declared, e.g. in
1016+
///
1017+
/// ```rust
1018+
/// mod foo {
1019+
/// struct Bar;
1020+
/// }
1021+
/// ```
1022+
///
1023+
/// for `foo::Bar`, this field will be `Bar`.
6201024
pub name: String,
1025+
/// The ID of the type.
6211026
pub id: Id,
622-
/// Generic arguments to the type
623-
/// ```test
1027+
/// Generic arguments to the type.
1028+
///
1029+
/// ```ignore (incomplete expression)
6241030
/// std::borrow::Cow<'static, str>
625-
/// ^^^^^^^^^^^^^^
626-
/// |
627-
/// this part
1031+
/// // ^^^^^^^^^^^^^^
6281032
/// ```
6291033
pub args: Option<Box<GenericArgs>>,
6301034
}
6311035

1036+
/// A type that is a function pointer.
6321037
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
6331038
pub struct FunctionPointer {
1039+
/// The signature of the function.
6341040
pub decl: FnDecl,
6351041
/// Used for Higher-Rank Trait Bounds (HRTBs)
636-
/// ```text
637-
/// for<'c> fn(val: &'c i32) -> i32
638-
/// ^^^^^^^
639-
/// |
640-
/// this part
1042+
///
1043+
/// ```ignore (incomplete expression)
1044+
/// for<'c> fn(val: &'c i32) -> i32
1045+
/// // ^^^^^^^
6411046
/// ```
6421047
pub generic_params: Vec<GenericParamDef>,
1048+
/// The core properties of the function, such as the ABI it conforms to, whether it's unsafe, etc.
6431049
pub header: Header,
6441050
}
6451051

1052+
/// The signature of a function.
6461053
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
6471054
pub struct FnDecl {
6481055
/// List of argument names and their type.
6491056
///
6501057
/// Note that not all names will be valid identifiers, as some of
6511058
/// them may be patterns.
6521059
pub inputs: Vec<(String, Type)>,
1060+
/// The output type, if specified.
6531061
pub output: Option<Type>,
1062+
/// Whether the function accepts an arbitrary amount of trailing arguments the C way.
1063+
///
1064+
/// ```ignore (incomplete code)
1065+
/// fn printf(fmt: &str, ...);
1066+
/// ```
6541067
pub c_variadic: bool,
6551068
}
6561069

1070+
/// A `trait` declaration.
6571071
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
6581072
pub struct Trait {
1073+
/// Whether the trait is marked `auto` and is thus implemented automatically
1074+
/// for all aplicable types.
6591075
pub is_auto: bool,
1076+
/// Whether the trait is marked as `unsafe`.
6601077
pub is_unsafe: bool,
1078+
/// Whether the trait is [object safe](https://doc.rust-lang.org/reference/items/traits.html#object-safety).
6611079
pub is_object_safe: bool,
1080+
/// Associated [`Item`]s that can/must be implemented by the `impl` blocks.
6621081
pub items: Vec<Id>,
1082+
/// Information about the type parameters and `where` clauses of the trait.
6631083
pub generics: Generics,
1084+
/// Constraints that must be met by the implementor of the trait.
6641085
pub bounds: Vec<GenericBound>,
1086+
/// The implementations of the trait.
6651087
pub implementations: Vec<Id>,
6661088
}
6671089

1090+
/// A trait alias declaration, e.g. `trait Int = Add + Sub + Mul + Div;`
1091+
///
1092+
/// See [the tracking issue](https://github.com/rust-lang/rust/issues/41517)
6681093
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
6691094
pub struct TraitAlias {
1095+
/// Information about the type parameters and `where` clauses of the alias.
6701096
pub generics: Generics,
1097+
/// The bounds that are associated with the alias.
6711098
pub params: Vec<GenericBound>,
6721099
}
6731100

1101+
/// An `impl` block.
6741102
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
6751103
pub struct Impl {
1104+
/// Whether this impl is for an unsafe trait.
6761105
pub is_unsafe: bool,
1106+
/// Information about the impl’s type parameters and `where` clauses.
6771107
pub generics: Generics,
1108+
/// The list of the names of all the trait methods that weren't mentioned in this impl but
1109+
/// were provided by the trait itself.
1110+
///
1111+
/// For example, for this impl of the [`PartialEq`] trait:
1112+
/// ```rust
1113+
/// struct Foo;
1114+
///
1115+
/// impl PartialEq for Foo {
1116+
/// fn eq(&self, other: &Self) -> bool { todo!() }
1117+
/// }
1118+
/// ```
1119+
/// This field will be `["ne"]`, as it has a default implementation defined for it.
6781120
pub provided_trait_methods: Vec<String>,
1121+
/// The trait being implemented or `None` if the impl is inherent, which means
1122+
/// `impl Struct {}` as opposed to `impl Trait for Struct {}`.
6791123
#[serde(rename = "trait")]
6801124
pub trait_: Option<Path>,
1125+
/// The type that the impl block is for.
6811126
#[serde(rename = "for")]
6821127
pub for_: Type,
1128+
/// The list of associated items contained in this impl block.
6831129
pub items: Vec<Id>,
1130+
/// Whether this is a negative impl (e.g. `!Sized` or `!Send`).
6841131
pub negative: bool,
1132+
/// Whether this is an impl that’s implied by the compiler
1133+
/// (for autotraits, e.g. `Send` or `Sync`).
6851134
pub synthetic: bool,
1135+
// FIXME: document this
6861136
pub blanket_impl: Option<Type>,
6871137
}
6881138

1139+
/// A `use` statement.
6891140
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
6901141
#[serde(rename_all = "snake_case")]
6911142
pub struct Import {
@@ -699,16 +1150,34 @@ pub struct Import {
6991150
/// pub use i32 as my_i32;
7001151
/// ```
7011152
pub id: Option<Id>,
702-
/// Whether this import uses a glob: `use source::*;`
1153+
/// Whether this statement is a wildcard `use`, e.g. `use source::*;`
7031154
pub glob: bool,
7041155
}
7051156

1157+
/// A procedural macro.
7061158
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
7071159
pub struct ProcMacro {
1160+
/// How this macro is supposed to be called: `foo!()`, `#[foo]` or `#[derive(foo)]`
7081161
pub kind: MacroKind,
1162+
/// Helper attributes defined by a macro to be used inside it.
1163+
///
1164+
/// Defined only for attribute & derive macros.
1165+
///
1166+
/// E.g. the [`Default`] derive macro defines a `#[default]` helper attribute so that one can
1167+
/// do:
1168+
///
1169+
/// ```rust
1170+
/// #[derive(Default)]
1171+
/// enum Option<T> {
1172+
/// #[default]
1173+
/// None,
1174+
/// Some(T),
1175+
/// }
1176+
/// ```
7091177
pub helpers: Vec<String>,
7101178
}
7111179

1180+
/// The way a [`ProcMacro`] is declared to be used.
7121181
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
7131182
#[serde(rename_all = "snake_case")]
7141183
pub enum MacroKind {
@@ -720,10 +1189,13 @@ pub enum MacroKind {
7201189
Derive,
7211190
}
7221191

1192+
/// A type alias declaration, e.g. `type Pig = std::borrow::Cow<'static, str>;`
7231193
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
7241194
pub struct TypeAlias {
1195+
/// The type referred to by this alias.
7251196
#[serde(rename = "type")]
7261197
pub type_: Type,
1198+
/// Information about the type parameters and `where` clauses of the alias.
7271199
pub generics: Generics,
7281200
}
7291201

@@ -733,17 +1205,26 @@ pub struct OpaqueTy {
7331205
pub generics: Generics,
7341206
}
7351207

1208+
/// A `static` declaration.
7361209
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
7371210
pub struct Static {
1211+
/// The type of the static.
7381212
#[serde(rename = "type")]
7391213
pub type_: Type,
1214+
/// This is `true` for mutable statics, declared as `static mut X: T = f();`
7401215
pub mutable: bool,
1216+
/// The stringified expression for the initial value.
1217+
///
1218+
/// It's not guaranteed that it'll match the actual source code for the initial value.
7411219
pub expr: String,
7421220
}
7431221

1222+
/// A primitive type declaration. Declarations of this kind can only come from the core library.
7441223
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
7451224
pub struct Primitive {
1225+
/// The name of the type.
7461226
pub name: String,
1227+
/// The implementations, inherent and of traits, on the primitive type.
7471228
pub impls: Vec<Id>,
7481229
}
7491230

0 commit comments

Comments
 (0)
Please sign in to comment.