Skip to content

Commit 5443fd1

Browse files
committed
Mark stripped items
removing them creates dangling references
1 parent 3cdb0bf commit 5443fd1

File tree

2 files changed

+18
-32
lines changed

2 files changed

+18
-32
lines changed

src/librustdoc/json/conversions.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ impl From<clean::Item> for Item {
2929
id: def_id.into(),
3030
crate_id: def_id.krate.as_u32(),
3131
name,
32-
stripped: inner == clean::StrippedItem(_),
32+
stripped: match inner {
33+
clean::StrippedItem(_) => true,
34+
_ => false,
35+
},
3336
source: source.into(),
3437
visibility: visibility.into(),
3538
docs: attrs.collapsed_doc_value().unwrap_or_default(),
@@ -179,27 +182,18 @@ impl From<clean::ItemEnum> for ItemEnum {
179182
bounds: g.into_iter().map(Into::into).collect(),
180183
default: t.map(Into::into),
181184
},
182-
StrippedItem(inner) => ItemEnum::StrippedItem(Box::new((*inner).into())),
185+
StrippedItem(inner) => (*inner).into(),
183186
_ => panic!("{:?} is not supported for JSON output", item),
184187
}
185188
}
186189
}
187190

188-
fn remove_stripped(items: &[clean::Item]) -> Vec<Id> {
189-
items
190-
.into_iter()
191-
.filter_map(|i| {
192-
if let clean::StrippedItem(_) = i.inner {
193-
return None;
194-
}
195-
Some(i.def_id.into())
196-
})
197-
.collect()
198-
}
199-
200191
impl From<clean::Module> for Module {
201192
fn from(module: clean::Module) -> Self {
202-
Module { is_crate: module.is_crate, items: remove_stripped(&module.items) }
193+
Module {
194+
is_crate: module.is_crate,
195+
items: module.items.into_iter().map(|i| i.def_id.into()).collect(),
196+
}
203197
}
204198
}
205199

@@ -210,7 +204,7 @@ impl From<clean::Struct> for Struct {
210204
struct_type: struct_type.into(),
211205
generics: generics.into(),
212206
fields_stripped,
213-
fields: remove_stripped(&fields),
207+
fields: fields.into_iter().map(|i| i.def_id.into()).collect(),
214208
impls: Vec::new(), // Added in JsonRenderer::item
215209
}
216210
}
@@ -223,7 +217,7 @@ impl From<clean::Union> for Struct {
223217
struct_type: struct_type.into(),
224218
generics: generics.into(),
225219
fields_stripped,
226-
fields: remove_stripped(&fields),
220+
fields: fields.into_iter().map(|i| i.def_id.into()).collect(),
227221
impls: Vec::new(), // Added in JsonRenderer::item
228222
}
229223
}
@@ -410,7 +404,7 @@ impl From<clean::Trait> for Trait {
410404
Trait {
411405
is_auto: auto,
412406
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
413-
items: remove_stripped(&items),
407+
items: items.into_iter().map(|i| i.def_id.into()).collect(),
414408
generics: generics.into(),
415409
bounds: bounds.into_iter().map(Into::into).collect(),
416410
implementors: Vec::new(), // Added in JsonRenderer::item
@@ -437,7 +431,7 @@ impl From<clean::Impl> for Impl {
437431
provided_trait_methods: provided_trait_methods.into_iter().collect(),
438432
trait_: trait_.map(Into::into),
439433
for_: for_.into(),
440-
items: remove_stripped(&items),
434+
items: items.into_iter().map(|i| i.def_id.into()).collect(),
441435
negative: polarity == Some(clean::ImplPolarity::Negative),
442436
synthetic,
443437
blanket_impl: blanket_impl.map(Into::into),
@@ -476,7 +470,7 @@ impl From<clean::Enum> for Enum {
476470
Enum {
477471
generics: generics.into(),
478472
variants_stripped,
479-
variants: remove_stripped(&variants.into_iter().collect::<Vec<_>>()),
473+
variants: variants.into_iter().map(|i| i.def_id.into()).collect(),
480474
impls: Vec::new(), // Added in JsonRenderer::item
481475
}
482476
}
@@ -489,7 +483,7 @@ impl From<clean::VariantStruct> for Struct {
489483
struct_type: struct_type.into(),
490484
generics: Default::default(),
491485
fields_stripped,
492-
fields: remove_stripped(&fields),
486+
fields: fields.into_iter().map(|i| i.def_id.into()).collect(),
493487
impls: Vec::new(),
494488
}
495489
}
@@ -501,7 +495,7 @@ impl From<clean::Variant> for Variant {
501495
match variant.kind {
502496
CLike => Variant::Plain,
503497
Tuple(t) => Variant::Tuple(t.into_iter().map(Into::into).collect()),
504-
Struct(s) => Variant::Struct(remove_stripped(&s.fields)),
498+
Struct(s) => Variant::Struct(s.fields.into_iter().map(|i| i.def_id.into()).collect()),
505499
}
506500
}
507501
}

src/librustdoc/json/mod.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ impl JsonRenderer {
9999
.0
100100
.last()
101101
.map(Clone::clone),
102+
stripped: false,
102103
visibility: types::Visibility::Public,
103104
kind: types::ItemKind::Trait,
104105
inner: types::ItemEnum::TraitItem(trait_item.clone().into()),
@@ -139,17 +140,8 @@ impl FormatRenderer for JsonRenderer {
139140
/// the hashmap because certain items (traits and types) need to have their mappings for trait
140141
/// implementations filled out before they're inserted.
141142
fn item(&mut self, item: clean::Item, cache: &Cache) -> Result<(), Error> {
142-
if let clean::StrippedItem(_) = item.inner {
143-
return Ok(());
144-
}
145-
146143
// Flatten items that recursively store other items
147-
item.inner.inner_items().for_each(|i| {
148-
if let clean::StrippedItem(_) = i.inner {
149-
} else {
150-
self.item(i.clone(), cache).unwrap()
151-
}
152-
});
144+
item.inner.inner_items().for_each(|i| self.item(i.clone(), cache).unwrap());
153145

154146
let id = item.def_id;
155147
let mut new_item: types::Item = item.into();

0 commit comments

Comments
 (0)