Skip to content

Commit 9afa850

Browse files
author
Yuki Okushi
authored
Rollup merge of #105182 - aDotInTheVoid:rdj-no-foreign-traits, r=Enselic,GuillaumeGomez
Rustdoc-Json: Don't inline foreign traits It wasn't done correctly, and [we want to move towards only having local items in the index, and making foreign items easier to resolved](https://rust-lang.zulipchat.com/#narrow/stream/266220-rustdoc/topic/Rustdoc.20JSON.3A.20Include.20All.20Foreign.20Items.3F) Fixes #105025. This means #105015 is included to test this Fixes #105022 r? `@GuillaumeGomez`
2 parents 8f36866 + 79d897b commit 9afa850

File tree

8 files changed

+95
-61
lines changed

8 files changed

+95
-61
lines changed

src/librustdoc/json/mod.rs

+1-52
Original file line numberDiff line numberDiff line change
@@ -99,53 +99,6 @@ impl<'tcx> JsonRenderer<'tcx> {
9999
})
100100
.unwrap_or_default()
101101
}
102-
103-
fn get_trait_items(&mut self) -> Vec<(types::Id, types::Item)> {
104-
debug!("Adding foreign trait items");
105-
Rc::clone(&self.cache)
106-
.traits
107-
.iter()
108-
.filter_map(|(&id, trait_item)| {
109-
// only need to synthesize items for external traits
110-
if !id.is_local() {
111-
for item in &trait_item.items {
112-
trace!("Adding subitem to {id:?}: {:?}", item.item_id);
113-
self.item(item.clone()).unwrap();
114-
}
115-
let item_id = from_item_id(id.into(), self.tcx);
116-
Some((
117-
item_id.clone(),
118-
types::Item {
119-
id: item_id,
120-
crate_id: id.krate.as_u32(),
121-
name: self
122-
.cache
123-
.paths
124-
.get(&id)
125-
.unwrap_or_else(|| {
126-
self.cache
127-
.external_paths
128-
.get(&id)
129-
.expect("Trait should either be in local or external paths")
130-
})
131-
.0
132-
.last()
133-
.map(|s| s.to_string()),
134-
visibility: types::Visibility::Public,
135-
inner: types::ItemEnum::Trait(trait_item.clone().into_tcx(self.tcx)),
136-
span: None,
137-
docs: Default::default(),
138-
links: Default::default(),
139-
attrs: Default::default(),
140-
deprecation: Default::default(),
141-
},
142-
))
143-
} else {
144-
None
145-
}
146-
})
147-
.collect()
148-
}
149102
}
150103

151104
impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
@@ -276,11 +229,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
276229

277230
let e = ExternalCrate { crate_num: LOCAL_CRATE };
278231

279-
// FIXME(adotinthevoid): Remove this, as it's not consistent with not
280-
// inlining foreign items.
281-
let foreign_trait_items = self.get_trait_items();
282-
let mut index = (*self.index).clone().into_inner();
283-
index.extend(foreign_trait_items);
232+
let index = (*self.index).clone().into_inner();
284233

285234
debug!("Constructing Output");
286235
// This needs to be the default HashMap for compatibility with the public interface for
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub trait Trait {
2+
/// [`Enum::Variant`]
3+
fn method() {}
4+
}
5+
6+
pub enum Enum {
7+
Variant,
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/105025>
2+
// aux-build: enum_variant_in_trait_method.rs
3+
4+
extern crate enum_variant_in_trait_method;
5+
6+
pub struct Local;
7+
8+
/// local impl
9+
impl enum_variant_in_trait_method::Trait for Local {}
10+
11+
// @!has "$.index[*][?(@.name == 'Trait')]"
12+
// @!has "$.index[*][?(@.name == 'method')]"
13+
// @count "$.index[*][?(@.docs == 'local impl')].inner.items[*]" 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// The Docs
2+
pub trait HasDocs {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/105022>
2+
// aux-build: trait_with_docs.rs
3+
4+
extern crate trait_with_docs;
5+
6+
pub struct Local;
7+
8+
impl trait_with_docs::HasDocs for Local {}
9+
10+
// @!has "$.index[*][?(@.name == 'HasDocs')]"
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
#![no_std]
22
pub fn drop_default<T: core::default::Default>(_x: T) {}
33

4-
// FIXME(adotinthevoid): Theses shouldn't be here
5-
// @has "$.index[*][?(@.name=='Debug')]"
6-
7-
// Debug may have several items. All we depend on here the that `fmt` is first. See
8-
// https://github.com/rust-lang/rust/pull/104525#issuecomment-1331087852 for why we
9-
// can't use [*].
10-
11-
// @set Debug_fmt = "$.index[*][?(@.name=='Debug')].inner.items[0]"
12-
// @has "$.index[*][?(@.name=='fmt')].id" $Debug_fmt
4+
// @!has "$.index[*][?(@.name=='Debug')]"
5+
// @!has "$.index[*][?(@.name=='Default')]"

src/tools/jsondoclint/src/validator.rs

+9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ impl<'a> Validator<'a> {
6060

6161
fn check_item(&mut self, id: &'a Id) {
6262
if let Some(item) = &self.krate.index.get(id) {
63+
item.links.values().for_each(|id| self.add_any_id(id));
64+
6365
match &item.inner {
6466
ItemEnum::Import(x) => self.check_import(x),
6567
ItemEnum::Union(x) => self.check_union(x),
@@ -376,6 +378,10 @@ impl<'a> Validator<'a> {
376378
}
377379
}
378380

381+
fn add_any_id(&mut self, id: &'a Id) {
382+
self.add_id_checked(id, |_| true, "any kind of item");
383+
}
384+
379385
fn add_field_id(&mut self, id: &'a Id) {
380386
self.add_id_checked(id, Kind::is_struct_field, "StructField");
381387
}
@@ -446,3 +452,6 @@ fn set_remove<T: Hash + Eq + Clone>(set: &mut HashSet<T>) -> Option<T> {
446452
None
447453
}
448454
}
455+
456+
#[cfg(test)]
457+
mod tests;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::collections::HashMap;
2+
3+
use rustdoc_json_types::{Crate, Item, Visibility};
4+
5+
use super::*;
6+
7+
#[track_caller]
8+
fn check(krate: &Crate, errs: &[Error]) {
9+
let mut validator = Validator::new(krate);
10+
validator.check_crate();
11+
12+
assert_eq!(errs, &validator.errs[..]);
13+
}
14+
15+
fn id(s: &str) -> Id {
16+
Id(s.to_owned())
17+
}
18+
19+
#[test]
20+
fn errors_on_missing_links() {
21+
let k = Crate {
22+
root: id("0"),
23+
crate_version: None,
24+
includes_private: false,
25+
index: HashMap::from_iter([(
26+
id("0"),
27+
Item {
28+
name: Some("root".to_owned()),
29+
id: id(""),
30+
crate_id: 0,
31+
span: None,
32+
visibility: Visibility::Public,
33+
docs: None,
34+
links: HashMap::from_iter([("Not Found".to_owned(), id("1"))]),
35+
attrs: vec![],
36+
deprecation: None,
37+
inner: ItemEnum::Module(Module {
38+
is_crate: true,
39+
items: vec![],
40+
is_stripped: false,
41+
}),
42+
},
43+
)]),
44+
paths: HashMap::new(),
45+
external_crates: HashMap::new(),
46+
format_version: rustdoc_json_types::FORMAT_VERSION,
47+
};
48+
49+
check(&k, &[Error { kind: ErrorKind::NotFound, id: id("1") }]);
50+
}

0 commit comments

Comments
 (0)