Skip to content

Commit 0131c49

Browse files
committed
jsondoclint: Check that Path types exitst in Crate::paths.
1 parent 9a1f01c commit 0131c49

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

src/tools/jsondoclint/src/validator.rs

+6
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ impl<'a> Validator<'a> {
303303
PathKind::Trait => self.add_trait_or_alias_id(&x.id),
304304
PathKind::Type => self.add_type_id(&x.id),
305305
}
306+
307+
// FIXME: More robust support for checking things in $.index also exist in $.paths
308+
if !self.krate.paths.contains_key(&x.id) {
309+
self.fail(&x.id, ErrorKind::Custom(format!("No entry in '$.paths' for {x:?}")));
310+
}
311+
306312
if let Some(args) = &x.args {
307313
self.check_generic_args(&**args);
308314
}

src/tools/jsondoclint/src/validator/tests.rs

+96-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_hash::FxHashMap;
2-
use rustdoc_json_types::{FORMAT_VERSION, Item, ItemKind, Visibility};
2+
use rustdoc_json_types::{Abi, FORMAT_VERSION, FunctionHeader, Item, ItemKind, Visibility};
33

44
use super::*;
55
use crate::json_find::SelectorPart;
@@ -102,6 +102,101 @@ fn errors_on_local_in_paths_and_not_index() {
102102
}]);
103103
}
104104

105+
#[test]
106+
fn errors_on_missing_path() {
107+
// crate-name=foo
108+
// ```
109+
// pub struct Bar;
110+
// pub fn mk_bar() -> Bar { ... }
111+
// ```
112+
113+
let generics = Generics { params: vec![], where_predicates: vec![] };
114+
115+
let krate = Crate {
116+
root: Id(0),
117+
crate_version: None,
118+
includes_private: false,
119+
index: FxHashMap::from_iter([
120+
(Id(0), Item {
121+
id: Id(0),
122+
crate_id: 0,
123+
name: Some("foo".to_owned()),
124+
span: None,
125+
visibility: Visibility::Public,
126+
docs: None,
127+
links: FxHashMap::default(),
128+
attrs: Vec::new(),
129+
deprecation: None,
130+
inner: ItemEnum::Module(Module {
131+
is_crate: true,
132+
items: vec![Id(1), Id(2)],
133+
is_stripped: false,
134+
}),
135+
}),
136+
(Id(1), Item {
137+
id: Id(0),
138+
crate_id: 0,
139+
name: Some("Bar".to_owned()),
140+
span: None,
141+
visibility: Visibility::Public,
142+
docs: None,
143+
links: FxHashMap::default(),
144+
attrs: Vec::new(),
145+
deprecation: None,
146+
inner: ItemEnum::Struct(Struct {
147+
kind: StructKind::Unit,
148+
generics: generics.clone(),
149+
impls: vec![],
150+
}),
151+
}),
152+
(Id(2), Item {
153+
id: Id(0),
154+
crate_id: 0,
155+
name: Some("mk_bar".to_owned()),
156+
span: None,
157+
visibility: Visibility::Public,
158+
docs: None,
159+
links: FxHashMap::default(),
160+
attrs: Vec::new(),
161+
deprecation: None,
162+
inner: ItemEnum::Function(Function {
163+
sig: FunctionSignature {
164+
inputs: vec![],
165+
output: Some(Type::ResolvedPath(Path {
166+
name: "Bar".to_owned(),
167+
id: Id(1),
168+
args: None,
169+
})),
170+
is_c_variadic: false,
171+
},
172+
generics,
173+
header: FunctionHeader {
174+
is_const: false,
175+
is_unsafe: false,
176+
is_async: false,
177+
abi: Abi::Rust,
178+
},
179+
has_body: true,
180+
}),
181+
}),
182+
]),
183+
paths: FxHashMap::from_iter([(Id(0), ItemSummary {
184+
crate_id: 0,
185+
path: vec!["foo".to_owned()],
186+
kind: ItemKind::Module,
187+
})]),
188+
external_crates: FxHashMap::default(),
189+
format_version: rustdoc_json_types::FORMAT_VERSION,
190+
};
191+
192+
check(&krate, &[Error {
193+
kind: ErrorKind::Custom(
194+
r#"No entry in '$.paths' for Path { name: "Bar", id: Id(1), args: None }"#.to_owned(),
195+
),
196+
id: Id(1),
197+
}]);
198+
}
199+
105200
#[test]
106201
#[should_panic = "LOCAL_CRATE_ID is wrong"]
107202
fn checks_local_crate_id_is_correct() {

0 commit comments

Comments
 (0)