]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/core.rs
Auto merge of #68194 - jonas-schievink:fix-thumb-ci, r=alexcrichton
[rust.git] / src / librustdoc / core.rs
1 use rustc::middle::cstore::CrateStore;
2 use rustc::middle::privacy::AccessLevels;
3 use rustc::session::config::ErrorOutputType;
4 use rustc::session::DiagnosticOutput;
5 use rustc::session::{self, config};
6 use rustc::ty::{Ty, TyCtxt};
7 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8 use rustc_driver::abort_on_err;
9 use rustc_feature::UnstableFeatures;
10 use rustc_hir::def::Namespace::TypeNS;
11 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
12 use rustc_hir::HirId;
13 use rustc_interface::interface;
14 use rustc_lint;
15 use rustc_resolve as resolve;
16 use rustc_session::lint;
17
18 use rustc_errors::emitter::{Emitter, EmitterWriter};
19 use rustc_errors::json::JsonEmitter;
20 use rustc_span::source_map;
21 use rustc_span::symbol::sym;
22 use rustc_span::DUMMY_SP;
23 use syntax::ast::CRATE_NODE_ID;
24 use syntax::attr;
25
26 use rustc_data_structures::sync::{self, Lrc};
27 use std::cell::RefCell;
28 use std::mem;
29 use std::rc::Rc;
30
31 use crate::clean;
32 use crate::clean::{AttributesExt, MAX_DEF_ID};
33 use crate::config::{Options as RustdocOptions, RenderOptions};
34 use crate::html::render::RenderInfo;
35
36 use crate::passes::{self, Condition::*, ConditionalPass};
37
38 pub use rustc::session::config::{CodegenOptions, DebuggingOptions, Input, Options};
39 pub use rustc::session::search_paths::SearchPath;
40
41 pub type ExternalPaths = FxHashMap<DefId, (Vec<String>, clean::TypeKind)>;
42
43 pub struct DocContext<'tcx> {
44     pub tcx: TyCtxt<'tcx>,
45     pub resolver: Rc<RefCell<interface::BoxedResolver>>,
46     /// Later on moved into `html::render::CACHE_KEY`
47     pub renderinfo: RefCell<RenderInfo>,
48     /// Later on moved through `clean::Crate` into `html::render::CACHE_KEY`
49     pub external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
50     /// Used while populating `external_traits` to ensure we don't process the same trait twice at
51     /// the same time.
52     pub active_extern_traits: RefCell<FxHashSet<DefId>>,
53     // The current set of type and lifetime substitutions,
54     // for expanding type aliases at the HIR level:
55     /// Table `DefId` of type parameter -> substituted type
56     pub ty_substs: RefCell<FxHashMap<DefId, clean::Type>>,
57     /// Table `DefId` of lifetime parameter -> substituted lifetime
58     pub lt_substs: RefCell<FxHashMap<DefId, clean::Lifetime>>,
59     /// Table `DefId` of const parameter -> substituted const
60     pub ct_substs: RefCell<FxHashMap<DefId, clean::Constant>>,
61     /// Table synthetic type parameter for `impl Trait` in argument position -> bounds
62     pub impl_trait_bounds: RefCell<FxHashMap<ImplTraitParam, Vec<clean::GenericBound>>>,
63     pub fake_def_ids: RefCell<FxHashMap<CrateNum, DefId>>,
64     pub all_fake_def_ids: RefCell<FxHashSet<DefId>>,
65     /// Auto-trait or blanket impls processed so far, as `(self_ty, trait_def_id)`.
66     // FIXME(eddyb) make this a `ty::TraitRef<'tcx>` set.
67     pub generated_synthetics: RefCell<FxHashSet<(Ty<'tcx>, DefId)>>,
68     pub auto_traits: Vec<DefId>,
69 }
70
71 impl<'tcx> DocContext<'tcx> {
72     pub fn sess(&self) -> &session::Session {
73         &self.tcx.sess
74     }
75
76     pub fn enter_resolver<F, R>(&self, f: F) -> R
77     where
78         F: FnOnce(&mut resolve::Resolver<'_>) -> R,
79     {
80         self.resolver.borrow_mut().access(f)
81     }
82
83     /// Call the closure with the given parameters set as
84     /// the substitutions for a type alias' RHS.
85     pub fn enter_alias<F, R>(
86         &self,
87         ty_substs: FxHashMap<DefId, clean::Type>,
88         lt_substs: FxHashMap<DefId, clean::Lifetime>,
89         ct_substs: FxHashMap<DefId, clean::Constant>,
90         f: F,
91     ) -> R
92     where
93         F: FnOnce() -> R,
94     {
95         let (old_tys, old_lts, old_cts) = (
96             mem::replace(&mut *self.ty_substs.borrow_mut(), ty_substs),
97             mem::replace(&mut *self.lt_substs.borrow_mut(), lt_substs),
98             mem::replace(&mut *self.ct_substs.borrow_mut(), ct_substs),
99         );
100         let r = f();
101         *self.ty_substs.borrow_mut() = old_tys;
102         *self.lt_substs.borrow_mut() = old_lts;
103         *self.ct_substs.borrow_mut() = old_cts;
104         r
105     }
106
107     // This is an ugly hack, but it's the simplest way to handle synthetic impls without greatly
108     // refactoring either librustdoc or librustc. In particular, allowing new DefIds to be
109     // registered after the AST is constructed would require storing the defid mapping in a
110     // RefCell, decreasing the performance for normal compilation for very little gain.
111     //
112     // Instead, we construct 'fake' def ids, which start immediately after the last DefId.
113     // In the Debug impl for clean::Item, we explicitly check for fake
114     // def ids, as we'll end up with a panic if we use the DefId Debug impl for fake DefIds
115     pub fn next_def_id(&self, crate_num: CrateNum) -> DefId {
116         let start_def_id = {
117             let next_id = if crate_num == LOCAL_CRATE {
118                 self.tcx.hir().definitions().def_path_table().next_id()
119             } else {
120                 self.enter_resolver(|r| r.cstore().def_path_table(crate_num).next_id())
121             };
122
123             DefId { krate: crate_num, index: next_id }
124         };
125
126         let mut fake_ids = self.fake_def_ids.borrow_mut();
127
128         let def_id = fake_ids.entry(crate_num).or_insert(start_def_id).clone();
129         fake_ids.insert(
130             crate_num,
131             DefId { krate: crate_num, index: DefIndex::from(def_id.index.index() + 1) },
132         );
133
134         MAX_DEF_ID.with(|m| {
135             m.borrow_mut().entry(def_id.krate.clone()).or_insert(start_def_id);
136         });
137
138         self.all_fake_def_ids.borrow_mut().insert(def_id);
139
140         def_id.clone()
141     }
142
143     /// Like the function of the same name on the HIR map, but skips calling it on fake DefIds.
144     /// (This avoids a slice-index-out-of-bounds panic.)
145     pub fn as_local_hir_id(&self, def_id: DefId) -> Option<HirId> {
146         if self.all_fake_def_ids.borrow().contains(&def_id) {
147             None
148         } else {
149             self.tcx.hir().as_local_hir_id(def_id)
150         }
151     }
152
153     pub fn stability(&self, id: HirId) -> Option<attr::Stability> {
154         self.tcx
155             .hir()
156             .opt_local_def_id(id)
157             .and_then(|def_id| self.tcx.lookup_stability(def_id))
158             .cloned()
159     }
160
161     pub fn deprecation(&self, id: HirId) -> Option<attr::Deprecation> {
162         self.tcx.hir().opt_local_def_id(id).and_then(|def_id| self.tcx.lookup_deprecation(def_id))
163     }
164 }
165
166 /// Creates a new diagnostic `Handler` that can be used to emit warnings and errors.
167 ///
168 /// If the given `error_format` is `ErrorOutputType::Json` and no `SourceMap` is given, a new one
169 /// will be created for the handler.
170 pub fn new_handler(
171     error_format: ErrorOutputType,
172     source_map: Option<Lrc<source_map::SourceMap>>,
173     debugging_opts: &DebuggingOptions,
174 ) -> rustc_errors::Handler {
175     let emitter: Box<dyn Emitter + sync::Send> = match error_format {
176         ErrorOutputType::HumanReadable(kind) => {
177             let (short, color_config) = kind.unzip();
178             Box::new(
179                 EmitterWriter::stderr(
180                     color_config,
181                     source_map.map(|cm| cm as _),
182                     short,
183                     debugging_opts.teach,
184                     debugging_opts.terminal_width,
185                     false,
186                 )
187                 .ui_testing(debugging_opts.ui_testing()),
188             )
189         }
190         ErrorOutputType::Json { pretty, json_rendered } => {
191             let source_map = source_map.unwrap_or_else(|| {
192                 Lrc::new(source_map::SourceMap::new(source_map::FilePathMapping::empty()))
193             });
194             Box::new(
195                 JsonEmitter::stderr(None, source_map, pretty, json_rendered, false)
196                     .ui_testing(debugging_opts.ui_testing()),
197             )
198         }
199     };
200
201     rustc_errors::Handler::with_emitter_and_flags(
202         emitter,
203         debugging_opts.diagnostic_handler_flags(true),
204     )
205 }
206
207 pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOptions) {
208     // Parse, resolve, and typecheck the given crate.
209
210     let RustdocOptions {
211         input,
212         crate_name,
213         proc_macro_crate,
214         error_format,
215         libs,
216         externs,
217         mut cfgs,
218         codegen_options,
219         debugging_options,
220         target,
221         edition,
222         maybe_sysroot,
223         lint_opts,
224         describe_lints,
225         lint_cap,
226         mut default_passes,
227         mut document_private,
228         document_hidden,
229         mut manual_passes,
230         display_warnings,
231         render_options,
232         ..
233     } = options;
234
235     let extern_names: Vec<String> = externs
236         .iter()
237         .filter(|(_, entry)| entry.add_prelude)
238         .map(|(name, _)| name)
239         .cloned()
240         .collect();
241
242     // Add the doc cfg into the doc build.
243     cfgs.push("doc".to_string());
244
245     let cpath = Some(input.clone());
246     let input = Input::File(input);
247
248     let intra_link_resolution_failure_name = lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE.name;
249     let warnings_lint_name = lint::builtin::WARNINGS.name;
250     let missing_docs = rustc_lint::builtin::MISSING_DOCS.name;
251     let missing_doc_example = rustc_lint::builtin::MISSING_DOC_CODE_EXAMPLES.name;
252     let private_doc_tests = rustc_lint::builtin::PRIVATE_DOC_TESTS.name;
253
254     // In addition to those specific lints, we also need to whitelist those given through
255     // command line, otherwise they'll get ignored and we don't want that.
256     let mut whitelisted_lints = vec![
257         warnings_lint_name.to_owned(),
258         intra_link_resolution_failure_name.to_owned(),
259         missing_docs.to_owned(),
260         missing_doc_example.to_owned(),
261         private_doc_tests.to_owned(),
262     ];
263
264     whitelisted_lints.extend(lint_opts.iter().map(|(lint, _)| lint).cloned());
265
266     let lints = || {
267         lint::builtin::HardwiredLints::get_lints()
268             .into_iter()
269             .chain(rustc_lint::SoftLints::get_lints().into_iter())
270     };
271
272     let lint_opts = lints()
273         .filter_map(|lint| {
274             if lint.name == warnings_lint_name || lint.name == intra_link_resolution_failure_name {
275                 None
276             } else {
277                 Some((lint.name_lower(), lint::Allow))
278             }
279         })
280         .chain(lint_opts.into_iter())
281         .collect::<Vec<_>>();
282
283     let lint_caps = lints()
284         .filter_map(|lint| {
285             // We don't want to whitelist *all* lints so let's
286             // ignore those ones.
287             if whitelisted_lints.iter().any(|l| &lint.name == l) {
288                 None
289             } else {
290                 Some((lint::LintId::of(lint), lint::Allow))
291             }
292         })
293         .collect();
294
295     let crate_types = if proc_macro_crate {
296         vec![config::CrateType::ProcMacro]
297     } else {
298         vec![config::CrateType::Rlib]
299     };
300     // plays with error output here!
301     let sessopts = config::Options {
302         maybe_sysroot,
303         search_paths: libs,
304         crate_types,
305         lint_opts: if !display_warnings { lint_opts } else { vec![] },
306         lint_cap: Some(lint_cap.unwrap_or_else(|| lint::Forbid)),
307         cg: codegen_options,
308         externs,
309         target_triple: target,
310         unstable_features: UnstableFeatures::from_environment(),
311         actually_rustdoc: true,
312         debugging_opts: debugging_options,
313         error_format,
314         edition,
315         describe_lints,
316         ..Options::default()
317     };
318
319     let config = interface::Config {
320         opts: sessopts,
321         crate_cfg: interface::parse_cfgspecs(cfgs),
322         input,
323         input_path: cpath,
324         output_file: None,
325         output_dir: None,
326         file_loader: None,
327         diagnostic_output: DiagnosticOutput::Default,
328         stderr: None,
329         crate_name,
330         lint_caps,
331         register_lints: None,
332         override_queries: None,
333         registry: rustc_driver::diagnostics_registry(),
334     };
335
336     interface::run_compiler_in_existing_thread_pool(config, |compiler| {
337         compiler.enter(|queries| {
338             let sess = compiler.session();
339
340             // We need to hold on to the complete resolver, so we cause everything to be
341             // cloned for the analysis passes to use. Suboptimal, but necessary in the
342             // current architecture.
343             let resolver = {
344                 let parts = abort_on_err(queries.expansion(), sess).peek();
345                 let resolver = parts.1.borrow();
346
347                 // Before we actually clone it, let's force all the extern'd crates to
348                 // actually be loaded, just in case they're only referred to inside
349                 // intra-doc-links
350                 resolver.borrow_mut().access(|resolver| {
351                     for extern_name in &extern_names {
352                         resolver
353                             .resolve_str_path_error(DUMMY_SP, extern_name, TypeNS, CRATE_NODE_ID)
354                             .unwrap_or_else(|()| {
355                                 panic!("Unable to resolve external crate {}", extern_name)
356                             });
357                     }
358                 });
359
360                 // Now we're good to clone the resolver because everything should be loaded
361                 resolver.clone()
362             };
363
364             if sess.has_errors() {
365                 sess.fatal("Compilation failed, aborting rustdoc");
366             }
367
368             let mut global_ctxt = abort_on_err(queries.global_ctxt(), sess).take();
369
370             global_ctxt.enter(|tcx| {
371                 tcx.analysis(LOCAL_CRATE).ok();
372
373                 // Abort if there were any errors so far
374                 sess.abort_if_errors();
375
376                 let access_levels = tcx.privacy_access_levels(LOCAL_CRATE);
377                 // Convert from a HirId set to a DefId set since we don't always have easy access
378                 // to the map from defid -> hirid
379                 let access_levels = AccessLevels {
380                     map: access_levels
381                         .map
382                         .iter()
383                         .map(|(&k, &v)| (tcx.hir().local_def_id(k), v))
384                         .collect(),
385                 };
386
387                 let mut renderinfo = RenderInfo::default();
388                 renderinfo.access_levels = access_levels;
389
390                 let mut ctxt = DocContext {
391                     tcx,
392                     resolver,
393                     external_traits: Default::default(),
394                     active_extern_traits: Default::default(),
395                     renderinfo: RefCell::new(renderinfo),
396                     ty_substs: Default::default(),
397                     lt_substs: Default::default(),
398                     ct_substs: Default::default(),
399                     impl_trait_bounds: Default::default(),
400                     fake_def_ids: Default::default(),
401                     all_fake_def_ids: Default::default(),
402                     generated_synthetics: Default::default(),
403                     auto_traits: tcx
404                         .all_traits(LOCAL_CRATE)
405                         .iter()
406                         .cloned()
407                         .filter(|trait_def_id| tcx.trait_is_auto(*trait_def_id))
408                         .collect(),
409                 };
410                 debug!("crate: {:?}", tcx.hir().krate());
411
412                 let mut krate = clean::krate(&mut ctxt);
413
414                 fn report_deprecated_attr(name: &str, diag: &rustc_errors::Handler) {
415                     let mut msg = diag.struct_warn(&format!(
416                         "the `#![doc({})]` attribute is \
417                                                          considered deprecated",
418                         name
419                     ));
420                     msg.warn("please see https://github.com/rust-lang/rust/issues/44136");
421
422                     if name == "no_default_passes" {
423                         msg.help("you may want to use `#![doc(document_private_items)]`");
424                     }
425
426                     msg.emit();
427                 }
428
429                 // Process all of the crate attributes, extracting plugin metadata along
430                 // with the passes which we are supposed to run.
431                 for attr in krate.module.as_ref().unwrap().attrs.lists(sym::doc) {
432                     let diag = ctxt.sess().diagnostic();
433
434                     let name = attr.name_or_empty();
435                     if attr.is_word() {
436                         if name == sym::no_default_passes {
437                             report_deprecated_attr("no_default_passes", diag);
438                             if default_passes == passes::DefaultPassOption::Default {
439                                 default_passes = passes::DefaultPassOption::None;
440                             }
441                         }
442                     } else if let Some(value) = attr.value_str() {
443                         let sink = match name {
444                             sym::passes => {
445                                 report_deprecated_attr("passes = \"...\"", diag);
446                                 &mut manual_passes
447                             }
448                             sym::plugins => {
449                                 report_deprecated_attr("plugins = \"...\"", diag);
450                                 eprintln!(
451                                     "WARNING: `#![doc(plugins = \"...\")]` \
452                                       no longer functions; see CVE-2018-1000622"
453                                 );
454                                 continue;
455                             }
456                             _ => continue,
457                         };
458                         for name in value.as_str().split_whitespace() {
459                             sink.push(name.to_string());
460                         }
461                     }
462
463                     if attr.is_word() && name == sym::document_private_items {
464                         document_private = true;
465                     }
466                 }
467
468                 let passes = passes::defaults(default_passes).iter().copied().chain(
469                     manual_passes.into_iter().flat_map(|name| {
470                         if let Some(pass) = passes::find_pass(&name) {
471                             Some(ConditionalPass::always(pass))
472                         } else {
473                             error!("unknown pass {}, skipping", name);
474                             None
475                         }
476                     }),
477                 );
478
479                 info!("Executing passes");
480
481                 for p in passes {
482                     let run = match p.condition {
483                         Always => true,
484                         WhenDocumentPrivate => document_private,
485                         WhenNotDocumentPrivate => !document_private,
486                         WhenNotDocumentHidden => !document_hidden,
487                     };
488                     if run {
489                         debug!("running pass {}", p.pass.name);
490                         krate = (p.pass.run)(krate, &ctxt);
491                     }
492                 }
493
494                 ctxt.sess().abort_if_errors();
495
496                 (krate, ctxt.renderinfo.into_inner(), render_options)
497             })
498         })
499     })
500 }
501
502 /// `DefId` or parameter index (`ty::ParamTy.index`) of a synthetic type parameter
503 /// for `impl Trait` in argument position.
504 #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
505 pub enum ImplTraitParam {
506     DefId(DefId),
507     ParamIndex(u32),
508 }
509
510 impl From<DefId> for ImplTraitParam {
511     fn from(did: DefId) -> Self {
512         ImplTraitParam::DefId(did)
513     }
514 }
515
516 impl From<u32> for ImplTraitParam {
517     fn from(idx: u32) -> Self {
518         ImplTraitParam::ParamIndex(idx)
519     }
520 }