@@ -10,22 +10,24 @@ use rustdoc_json_types::{
1010
1111use crate :: { item_kind:: Kind , Error , ErrorKind } ;
1212
13+ /// The Validator walks over the JSON tree, and ensures it is well formed.
14+ /// It is made of several parts.
15+ ///
16+ /// - `check_*`: These take a type from [`rustdoc_json_types`], and check that
17+ /// it is well formed. This involves calling `check_*` functions on
18+ /// fields of that item, and `add_*` functions on [`Id`]s.
19+ /// - `add_*`: These add an [`Id`] to the worklist, after validating it to check if
20+ /// the `Id` is a kind expected in this suituation.
1321#[ derive( Debug ) ]
1422pub struct Validator < ' a > {
1523 pub ( crate ) errs : Vec < Error > ,
1624 krate : & ' a Crate ,
25+ /// Worklist of Ids to check.
26+ todo : HashSet < & ' a Id > ,
27+ /// Ids that have already been visited, so don't need to be checked again.
1728 seen_ids : HashSet < & ' a Id > ,
29+ /// Ids that have already been reported missing.
1830 missing_ids : HashSet < & ' a Id > ,
19- todo : HashSet < & ' a Id > ,
20- }
21-
22- fn set_remove < T : Hash + Eq + Clone > ( set : & mut HashSet < T > ) -> Option < T > {
23- if let Some ( id) = set. iter ( ) . next ( ) {
24- let id = id. clone ( ) ;
25- set. take ( & id)
26- } else {
27- None
28- }
2931}
3032
3133impl < ' a > Validator < ' a > {
@@ -82,6 +84,8 @@ impl<'a> Validator<'a> {
8284 }
8385 }
8486 }
87+ } else {
88+ assert ! ( self . krate. paths. contains_key( id) ) ;
8589 }
8690 }
8791
@@ -336,17 +340,12 @@ impl<'a> Validator<'a> {
336340 fp. generic_params . iter ( ) . for_each ( |gpd| self . check_generic_param_def ( gpd) ) ;
337341 }
338342
339- // Aux functions
340- fn add_id ( & mut self , id : & ' a Id ) {
341- if !self . seen_ids . contains ( id) {
342- self . todo . insert ( id) ;
343- }
344- }
345-
346343 fn add_id_checked ( & mut self , id : & ' a Id , valid : fn ( Kind ) -> bool , expected : & str ) {
347344 if let Some ( kind) = self . kind_of ( id) {
348345 if valid ( kind) {
349- self . add_id ( id) ;
346+ if !self . seen_ids . contains ( id) {
347+ self . todo . insert ( id) ;
348+ }
350349 } else {
351350 self . fail_expecting ( id, expected) ;
352351 }
@@ -402,3 +401,12 @@ impl<'a> Validator<'a> {
402401 }
403402 }
404403}
404+
405+ fn set_remove < T : Hash + Eq + Clone > ( set : & mut HashSet < T > ) -> Option < T > {
406+ if let Some ( id) = set. iter ( ) . next ( ) {
407+ let id = id. clone ( ) ;
408+ set. take ( & id)
409+ } else {
410+ None
411+ }
412+ }
0 commit comments