@@ -3,11 +3,11 @@ use std::collections::BTreeMap;
3
3
use std:: io;
4
4
use std:: path:: PathBuf ;
5
5
use std:: rc:: Rc ;
6
- use std:: sync:: mpsc:: { channel, Receiver } ;
6
+ use std:: sync:: mpsc:: channel;
7
7
use std:: sync:: Arc ;
8
8
9
9
use rustc_data_structures:: fx:: FxHashMap ;
10
- use rustc_hir:: def_id:: { DefId , LOCAL_CRATE } ;
10
+ use rustc_hir:: def_id:: LOCAL_CRATE ;
11
11
use rustc_middle:: ty:: TyCtxt ;
12
12
use rustc_session:: Session ;
13
13
use rustc_span:: edition:: Edition ;
@@ -31,7 +31,7 @@ use crate::formats::item_type::ItemType;
31
31
use crate :: formats:: FormatRenderer ;
32
32
use crate :: html:: escape:: Escape ;
33
33
use crate :: html:: format:: Buffer ;
34
- use crate :: html:: markdown:: { self , plain_text_summary, ErrorCodes , IdMap } ;
34
+ use crate :: html:: markdown:: { self , plain_text_summary, ErrorCodes } ;
35
35
use crate :: html:: { layout, sources} ;
36
36
37
37
/// Major driving force in all rustdoc rendering. This contains information
@@ -53,20 +53,16 @@ crate struct Context<'tcx> {
53
53
/// real location of an item. This is used to allow external links to
54
54
/// publicly reused items to redirect to the right location.
55
55
crate render_redirect_pages : bool ,
56
- /// `None` by default, depends on the `generate-redirect-map` option flag. If this field is set
57
- /// to `Some(...)`, it'll store redirections and then generate a JSON file at the top level of
58
- /// the crate.
59
- crate redirections : Option < Rc < RefCell < FxHashMap < String , String > > > > ,
60
- /// The map used to ensure all generated 'id=' attributes are unique.
61
- pub ( super ) id_map : Rc < RefCell < IdMap > > ,
62
- /// Tracks section IDs for `Deref` targets so they match in both the main
63
- /// body and the sidebar.
64
- pub ( super ) deref_id_map : Rc < RefCell < FxHashMap < DefId , String > > > ,
65
56
crate shared : Arc < SharedContext < ' tcx > > ,
66
- all : Rc < RefCell < AllTypes > > ,
67
- /// Storage for the errors produced while generating documentation so they
68
- /// can be printed together at the end.
69
- crate errors : Rc < Receiver < String > > ,
57
+ /// The [`Cache`] used during rendering.
58
+ ///
59
+ /// Ideally the cache would be in [`SharedContext`], but it's mutated
60
+ /// between when the `SharedContext` is created and when `Context`
61
+ /// is created, so more refactoring would be needed.
62
+ ///
63
+ /// It's immutable once in `Context`, so it's not as bad that it's not in
64
+ /// `SharedContext`.
65
+ // FIXME: move `cache` to `SharedContext`
70
66
crate cache : Rc < Cache > ,
71
67
}
72
68
@@ -91,7 +87,7 @@ impl<'tcx> Context<'tcx> {
91
87
}
92
88
93
89
pub ( super ) fn derive_id ( & self , id : String ) -> String {
94
- let mut map = self . id_map . borrow_mut ( ) ;
90
+ let mut map = self . shared . id_map . borrow_mut ( ) ;
95
91
map. derive ( id)
96
92
}
97
93
@@ -149,8 +145,8 @@ impl<'tcx> Context<'tcx> {
149
145
} ;
150
146
151
147
{
152
- self . id_map . borrow_mut ( ) . reset ( ) ;
153
- self . id_map . borrow_mut ( ) . populate ( & INITIAL_IDS ) ;
148
+ self . shared . id_map . borrow_mut ( ) . reset ( ) ;
149
+ self . shared . id_map . borrow_mut ( ) . populate ( & INITIAL_IDS ) ;
154
150
}
155
151
156
152
if !self . render_redirect_pages {
@@ -169,7 +165,7 @@ impl<'tcx> Context<'tcx> {
169
165
path. push ( '/' ) ;
170
166
}
171
167
path. push_str ( & item_path ( ty, names. last ( ) . unwrap ( ) ) ) ;
172
- match self . redirections {
168
+ match self . shared . redirections {
173
169
Some ( ref redirections) => {
174
170
let mut current_path = String :: new ( ) ;
175
171
for name in & self . current {
@@ -383,6 +379,11 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
383
379
edition,
384
380
codes : ErrorCodes :: from ( unstable_features. is_nightly_build ( ) ) ,
385
381
playground,
382
+ id_map : RefCell :: new ( id_map) ,
383
+ deref_id_map : RefCell :: new ( FxHashMap :: default ( ) ) ,
384
+ all : RefCell :: new ( AllTypes :: new ( ) ) ,
385
+ errors : receiver,
386
+ redirections : if generate_redirect_map { Some ( Default :: default ( ) ) } else { None } ,
386
387
} ;
387
388
388
389
// Add the default themes to the `Vec` of stylepaths
@@ -409,13 +410,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
409
410
current : Vec :: new ( ) ,
410
411
dst,
411
412
render_redirect_pages : false ,
412
- id_map : Rc :: new ( RefCell :: new ( id_map) ) ,
413
- deref_id_map : Rc :: new ( RefCell :: new ( FxHashMap :: default ( ) ) ) ,
414
413
shared : Arc :: new ( scx) ,
415
- all : Rc :: new ( RefCell :: new ( AllTypes :: new ( ) ) ) ,
416
- errors : Rc :: new ( receiver) ,
417
414
cache : Rc :: new ( cache) ,
418
- redirections : if generate_redirect_map { Some ( Default :: default ( ) ) } else { None } ,
419
415
} ;
420
416
421
417
CURRENT_DEPTH . with ( |s| s. set ( 0 ) ) ;
@@ -464,7 +460,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
464
460
} else {
465
461
String :: new ( )
466
462
} ;
467
- let all = self . all . replace ( AllTypes :: new ( ) ) ;
463
+ let all = self . shared . all . replace ( AllTypes :: new ( ) ) ;
468
464
let v = layout:: render (
469
465
& self . shared . layout ,
470
466
& page,
@@ -494,7 +490,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
494
490
& style_files,
495
491
) ;
496
492
self . shared . fs . write ( & settings_file, v. as_bytes ( ) ) ?;
497
- if let Some ( redirections) = self . redirections . take ( ) {
493
+ if let Some ( ref redirections) = self . shared . redirections {
498
494
if !redirections. borrow ( ) . is_empty ( ) {
499
495
let redirect_map_path =
500
496
self . dst . join ( & * krate. name . as_str ( ) ) . join ( "redirect-map.json" ) ;
@@ -506,7 +502,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
506
502
507
503
// Flush pending errors.
508
504
Arc :: get_mut ( & mut self . shared ) . unwrap ( ) . fs . close ( ) ;
509
- let nb_errors = self . errors . iter ( ) . map ( |err| diag. struct_err ( & err) . emit ( ) ) . count ( ) ;
505
+ let nb_errors = self . shared . errors . iter ( ) . map ( |err| diag. struct_err ( & err) . emit ( ) ) . count ( ) ;
510
506
if nb_errors > 0 {
511
507
Err ( Error :: new ( io:: Error :: new ( io:: ErrorKind :: Other , "I/O error" ) , "" ) )
512
508
} else {
@@ -585,13 +581,13 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
585
581
self . shared . fs . write ( & joint_dst, buf. as_bytes ( ) ) ?;
586
582
587
583
if !self . render_redirect_pages {
588
- self . all . borrow_mut ( ) . append ( full_path ( self , & item) , & item_type) ;
584
+ self . shared . all . borrow_mut ( ) . append ( full_path ( self , & item) , & item_type) ;
589
585
}
590
586
// If the item is a macro, redirect from the old macro URL (with !)
591
587
// to the new one (without).
592
588
if item_type == ItemType :: Macro {
593
589
let redir_name = format ! ( "{}.{}!.html" , item_type, name) ;
594
- if let Some ( ref redirections) = self . redirections {
590
+ if let Some ( ref redirections) = self . shared . redirections {
595
591
let crate_name = & self . shared . layout . krate ;
596
592
redirections. borrow_mut ( ) . insert (
597
593
format ! ( "{}/{}" , crate_name, redir_name) ,
0 commit comments