Skip to content

Commit 27eee69

Browse files
committed
rustdoc_json: represent generic args consistently.
They show up in three places: once as `Option<Box<GenericArgs>>`, once as `Box<GenericArgs>`, and once as `GenericArgs`. The first option is best. It is more compact because generic args are often missing. This commit changes the latter two to the former. Example output, before and after, for the `AssocItemConstraint` change: ``` {"name":"Offset","args":{"angle_bracketed":{"args":[],"constraints":[]}},"binding":{...}} {"name":"Offset","args":null,"binding":{...}} ``` Example output, before and after, for the `Type::QualifiedPath` change: ``` {"qualified_path":{"name":"Offset","args":{"angle_bracketed":{"args":[],"constraints":[]}}, ...}} {"qualified_path":{"name":"Offset","args":null, ...}} ``` This reduces JSON output size, but not by much (e.g. 0.5%), because `AssocItemConstraint` and `Type::QualifiedPath` are uncommon.
1 parent effe11b commit 27eee69

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

src/librustdoc/json/conversions.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,13 @@ pub(crate) fn from_deprecation(deprecation: attrs::Deprecation) -> Deprecation {
170170
Deprecation { since, note: note.map(|s| s.to_string()) }
171171
}
172172

173-
impl FromClean<clean::GenericArgs> for GenericArgs {
173+
impl FromClean<clean::GenericArgs> for Option<Box<GenericArgs>> {
174174
fn from_clean(args: &clean::GenericArgs, renderer: &JsonRenderer<'_>) -> Self {
175175
use clean::GenericArgs::*;
176-
match args {
176+
if args.is_empty() {
177+
return None;
178+
}
179+
Some(Box::new(match args {
177180
AngleBracketed { args, constraints } => GenericArgs::AngleBracketed {
178181
args: args.into_json(renderer),
179182
constraints: constraints.into_json(renderer),
@@ -183,7 +186,7 @@ impl FromClean<clean::GenericArgs> for GenericArgs {
183186
output: output.as_ref().map(|a| a.as_ref().into_json(renderer)),
184187
},
185188
ReturnTypeNotation => GenericArgs::ReturnTypeNotation,
186-
}
189+
}))
187190
}
188191
}
189192

@@ -589,11 +592,7 @@ impl FromClean<clean::Path> for Path {
589592
// used in the final segment, e.g. `std::vec::Vec<u32>`. So
590593
// check that the non-final segments have no generic args.
591594
assert!(rest_segs.iter().all(|seg| seg.args.is_empty()));
592-
if final_seg.args.is_empty() {
593-
None
594-
} else {
595-
Some(Box::new(final_seg.args.into_json(renderer)))
596-
}
595+
final_seg.args.into_json(renderer)
597596
} else {
598597
None // no generics on any segments because there are no segments
599598
}
@@ -608,7 +607,7 @@ impl FromClean<clean::QPathData> for Type {
608607

609608
Self::QualifiedPath {
610609
name: assoc.name.to_string(),
611-
args: Box::new(assoc.args.into_json(renderer)),
610+
args: assoc.args.into_json(renderer),
612611
self_type: Box::new(self_type.into_json(renderer)),
613612
trait_: trait_.as_ref().map(|trait_| trait_.into_json(renderer)),
614613
}

src/rustdoc-json-types/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub type FxHashMap<K, V> = HashMap<K, V>; // re-export for use in src/librustdoc
3030
/// This integer is incremented with every breaking change to the API,
3131
/// and is returned along with the JSON blob as [`Crate::format_version`].
3232
/// Consuming code should assert that this value matches the format version(s) that it supports.
33-
pub const FORMAT_VERSION: u32 = 46;
33+
pub const FORMAT_VERSION: u32 = 47;
3434

3535
/// The root of the emitted JSON blob.
3636
///
@@ -354,7 +354,7 @@ pub struct AssocItemConstraint {
354354
/// The name of the associated type/constant.
355355
pub name: String,
356356
/// Arguments provided to the associated type/constant.
357-
pub args: GenericArgs,
357+
pub args: Option<Box<GenericArgs>>,
358358
/// The kind of bound applied to the associated type/constant.
359359
pub binding: AssocItemConstraintKind,
360360
}
@@ -1110,7 +1110,7 @@ pub enum Type {
11101110
/// <core::slice::IterMut<'static, u32> as BetterIterator>::Item<'static>
11111111
/// // ^^^^^^^^^
11121112
/// ```
1113-
args: Box<GenericArgs>,
1113+
args: Option<Box<GenericArgs>>,
11141114
/// The type with which this type is associated.
11151115
///
11161116
/// ```ignore (incomplete expression)

src/rustdoc-json-types/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn test_union_info_roundtrip() {
4545
#[cfg(target_pointer_width = "64")]
4646
fn test_type_sizes() {
4747
// tidy-alphabetical-start
48-
assert_eq!(size_of::<AssocItemConstraint>(), 208);
48+
assert_eq!(size_of::<AssocItemConstraint>(), 112);
4949
assert_eq!(size_of::<Crate>(), 184);
5050
assert_eq!(size_of::<ExternalCrate>(), 48);
5151
assert_eq!(size_of::<FunctionPointer>(), 168);

src/tools/jsondoclint/src/validator.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl<'a> Validator<'a> {
271271
Type::RawPointer { is_mutable: _, type_ } => self.check_type(&**type_),
272272
Type::BorrowedRef { lifetime: _, is_mutable: _, type_ } => self.check_type(&**type_),
273273
Type::QualifiedPath { name: _, args, self_type, trait_ } => {
274-
self.check_generic_args(&**args);
274+
self.check_opt_generic_args(&args);
275275
self.check_type(&**self_type);
276276
if let Some(trait_) = trait_ {
277277
self.check_path(trait_, PathKind::Trait);
@@ -309,13 +309,12 @@ impl<'a> Validator<'a> {
309309
self.fail(&x.id, ErrorKind::Custom(format!("No entry in '$.paths' for {x:?}")));
310310
}
311311

312-
if let Some(args) = &x.args {
313-
self.check_generic_args(&**args);
314-
}
312+
self.check_opt_generic_args(&x.args);
315313
}
316314

317-
fn check_generic_args(&mut self, x: &'a GenericArgs) {
318-
match x {
315+
fn check_opt_generic_args(&mut self, x: &'a Option<Box<GenericArgs>>) {
316+
let Some(x) = x else { return };
317+
match &**x {
319318
GenericArgs::AngleBracketed { args, constraints } => {
320319
args.iter().for_each(|arg| self.check_generic_arg(arg));
321320
constraints.iter().for_each(|bind| self.check_assoc_item_constraint(bind));
@@ -355,7 +354,7 @@ impl<'a> Validator<'a> {
355354
}
356355

357356
fn check_assoc_item_constraint(&mut self, bind: &'a AssocItemConstraint) {
358-
self.check_generic_args(&bind.args);
357+
self.check_opt_generic_args(&bind.args);
359358
match &bind.binding {
360359
AssocItemConstraintKind::Equality(term) => self.check_term(term),
361360
AssocItemConstraintKind::Constraint(bounds) => {

0 commit comments

Comments
 (0)