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};
13 use rustc_interface::interface;
14 use rustc_resolve as resolve;
15 use rustc_session::lint;
17 use rustc_attr as attr;
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;
25 use rustc_data_structures::sync::{self, Lrc};
26 use std::cell::RefCell;
31 use crate::clean::{AttributesExt, MAX_DEF_ID};
32 use crate::config::{Options as RustdocOptions, RenderOptions};
33 use crate::html::render::RenderInfo;
35 use crate::passes::{self, Condition::*, ConditionalPass};
37 pub use rustc::session::config::{CodegenOptions, DebuggingOptions, Input, Options};
38 pub use rustc::session::search_paths::SearchPath;
40 pub type ExternalPaths = FxHashMap<DefId, (Vec<String>, clean::TypeKind)>;
42 pub struct DocContext<'tcx> {
43 pub tcx: TyCtxt<'tcx>,
44 pub resolver: Rc<RefCell<interface::BoxedResolver>>,
45 /// Later on moved into `html::render::CACHE_KEY`
46 pub renderinfo: RefCell<RenderInfo>,
47 /// Later on moved through `clean::Crate` into `html::render::CACHE_KEY`
48 pub external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
49 /// Used while populating `external_traits` to ensure we don't process the same trait twice at
51 pub active_extern_traits: RefCell<FxHashSet<DefId>>,
52 // The current set of type and lifetime substitutions,
53 // for expanding type aliases at the HIR level:
54 /// Table `DefId` of type parameter -> substituted type
55 pub ty_substs: RefCell<FxHashMap<DefId, clean::Type>>,
56 /// Table `DefId` of lifetime parameter -> substituted lifetime
57 pub lt_substs: RefCell<FxHashMap<DefId, clean::Lifetime>>,
58 /// Table `DefId` of const parameter -> substituted const
59 pub ct_substs: RefCell<FxHashMap<DefId, clean::Constant>>,
60 /// Table synthetic type parameter for `impl Trait` in argument position -> bounds
61 pub impl_trait_bounds: RefCell<FxHashMap<ImplTraitParam, Vec<clean::GenericBound>>>,
62 pub fake_def_ids: RefCell<FxHashMap<CrateNum, DefId>>,
63 pub all_fake_def_ids: RefCell<FxHashSet<DefId>>,
64 /// Auto-trait or blanket impls processed so far, as `(self_ty, trait_def_id)`.
65 // FIXME(eddyb) make this a `ty::TraitRef<'tcx>` set.
66 pub generated_synthetics: RefCell<FxHashSet<(Ty<'tcx>, DefId)>>,
67 pub auto_traits: Vec<DefId>,
70 impl<'tcx> DocContext<'tcx> {
71 pub fn sess(&self) -> &session::Session {
75 pub fn enter_resolver<F, R>(&self, f: F) -> R
77 F: FnOnce(&mut resolve::Resolver<'_>) -> R,
79 self.resolver.borrow_mut().access(f)
82 /// Call the closure with the given parameters set as
83 /// the substitutions for a type alias' RHS.
84 pub fn enter_alias<F, R>(
86 ty_substs: FxHashMap<DefId, clean::Type>,
87 lt_substs: FxHashMap<DefId, clean::Lifetime>,
88 ct_substs: FxHashMap<DefId, clean::Constant>,
94 let (old_tys, old_lts, old_cts) = (
95 mem::replace(&mut *self.ty_substs.borrow_mut(), ty_substs),
96 mem::replace(&mut *self.lt_substs.borrow_mut(), lt_substs),
97 mem::replace(&mut *self.ct_substs.borrow_mut(), ct_substs),
100 *self.ty_substs.borrow_mut() = old_tys;
101 *self.lt_substs.borrow_mut() = old_lts;
102 *self.ct_substs.borrow_mut() = old_cts;
106 // This is an ugly hack, but it's the simplest way to handle synthetic impls without greatly
107 // refactoring either librustdoc or librustc. In particular, allowing new DefIds to be
108 // registered after the AST is constructed would require storing the defid mapping in a
109 // RefCell, decreasing the performance for normal compilation for very little gain.
111 // Instead, we construct 'fake' def ids, which start immediately after the last DefId.
112 // In the Debug impl for clean::Item, we explicitly check for fake
113 // def ids, as we'll end up with a panic if we use the DefId Debug impl for fake DefIds
114 pub fn next_def_id(&self, crate_num: CrateNum) -> DefId {
116 let next_id = if crate_num == LOCAL_CRATE {
117 self.tcx.hir().definitions().def_path_table().next_id()
119 self.enter_resolver(|r| r.cstore().def_path_table(crate_num).next_id())
122 DefId { krate: crate_num, index: next_id }
125 let mut fake_ids = self.fake_def_ids.borrow_mut();
127 let def_id = *fake_ids.entry(crate_num).or_insert(start_def_id);
130 DefId { krate: crate_num, index: DefIndex::from(def_id.index.index() + 1) },
133 MAX_DEF_ID.with(|m| {
134 m.borrow_mut().entry(def_id.krate.clone()).or_insert(start_def_id);
137 self.all_fake_def_ids.borrow_mut().insert(def_id);
142 /// Like the function of the same name on the HIR map, but skips calling it on fake DefIds.
143 /// (This avoids a slice-index-out-of-bounds panic.)
144 pub fn as_local_hir_id(&self, def_id: DefId) -> Option<HirId> {
145 if self.all_fake_def_ids.borrow().contains(&def_id) {
148 self.tcx.hir().as_local_hir_id(def_id)
152 pub fn stability(&self, id: HirId) -> Option<attr::Stability> {
155 .opt_local_def_id(id)
156 .and_then(|def_id| self.tcx.lookup_stability(def_id))
160 pub fn deprecation(&self, id: HirId) -> Option<attr::Deprecation> {
161 self.tcx.hir().opt_local_def_id(id).and_then(|def_id| self.tcx.lookup_deprecation(def_id))
165 /// Creates a new diagnostic `Handler` that can be used to emit warnings and errors.
167 /// If the given `error_format` is `ErrorOutputType::Json` and no `SourceMap` is given, a new one
168 /// will be created for the handler.
170 error_format: ErrorOutputType,
171 source_map: Option<Lrc<source_map::SourceMap>>,
172 debugging_opts: &DebuggingOptions,
173 ) -> rustc_errors::Handler {
174 let emitter: Box<dyn Emitter + sync::Send> = match error_format {
175 ErrorOutputType::HumanReadable(kind) => {
176 let (short, color_config) = kind.unzip();
178 EmitterWriter::stderr(
180 source_map.map(|cm| cm as _),
182 debugging_opts.teach,
183 debugging_opts.terminal_width,
186 .ui_testing(debugging_opts.ui_testing()),
189 ErrorOutputType::Json { pretty, json_rendered } => {
190 let source_map = source_map.unwrap_or_else(|| {
191 Lrc::new(source_map::SourceMap::new(source_map::FilePathMapping::empty()))
194 JsonEmitter::stderr(None, source_map, pretty, json_rendered, false)
195 .ui_testing(debugging_opts.ui_testing()),
200 rustc_errors::Handler::with_emitter_and_flags(
202 debugging_opts.diagnostic_handler_flags(true),
206 pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOptions) {
207 // Parse, resolve, and typecheck the given crate.
226 mut document_private,
234 let extern_names: Vec<String> = externs
236 .filter(|(_, entry)| entry.add_prelude)
237 .map(|(name, _)| name)
241 // Add the doc cfg into the doc build.
242 cfgs.push("doc".to_string());
244 let cpath = Some(input.clone());
245 let input = Input::File(input);
247 let intra_link_resolution_failure_name = lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE.name;
248 let warnings_lint_name = lint::builtin::WARNINGS.name;
249 let missing_docs = rustc_lint::builtin::MISSING_DOCS.name;
250 let missing_doc_example = rustc_lint::builtin::MISSING_DOC_CODE_EXAMPLES.name;
251 let private_doc_tests = rustc_lint::builtin::PRIVATE_DOC_TESTS.name;
253 // In addition to those specific lints, we also need to whitelist those given through
254 // command line, otherwise they'll get ignored and we don't want that.
255 let mut whitelisted_lints = vec![
256 warnings_lint_name.to_owned(),
257 intra_link_resolution_failure_name.to_owned(),
258 missing_docs.to_owned(),
259 missing_doc_example.to_owned(),
260 private_doc_tests.to_owned(),
263 whitelisted_lints.extend(lint_opts.iter().map(|(lint, _)| lint).cloned());
266 lint::builtin::HardwiredLints::get_lints()
268 .chain(rustc_lint::SoftLints::get_lints().into_iter())
271 let lint_opts = lints()
273 if lint.name == warnings_lint_name || lint.name == intra_link_resolution_failure_name {
276 Some((lint.name_lower(), lint::Allow))
279 .chain(lint_opts.into_iter())
280 .collect::<Vec<_>>();
282 let lint_caps = lints()
284 // We don't want to whitelist *all* lints so let's
285 // ignore those ones.
286 if whitelisted_lints.iter().any(|l| &lint.name == l) {
289 Some((lint::LintId::of(lint), lint::Allow))
294 let crate_types = if proc_macro_crate {
295 vec![config::CrateType::ProcMacro]
297 vec![config::CrateType::Rlib]
299 // plays with error output here!
300 let sessopts = config::Options {
304 lint_opts: if !display_warnings { lint_opts } else { vec![] },
305 lint_cap: Some(lint_cap.unwrap_or_else(|| lint::Forbid)),
308 target_triple: target,
309 unstable_features: UnstableFeatures::from_environment(),
310 actually_rustdoc: true,
311 debugging_opts: debugging_options,
318 let config = interface::Config {
320 crate_cfg: interface::parse_cfgspecs(cfgs),
326 diagnostic_output: DiagnosticOutput::Default,
330 register_lints: None,
331 override_queries: None,
332 registry: rustc_driver::diagnostics_registry(),
335 interface::run_compiler_in_existing_thread_pool(config, |compiler| {
336 compiler.enter(|queries| {
337 let sess = compiler.session();
339 // We need to hold on to the complete resolver, so we cause everything to be
340 // cloned for the analysis passes to use. Suboptimal, but necessary in the
341 // current architecture.
343 let parts = abort_on_err(queries.expansion(), sess).peek();
344 let resolver = parts.1.borrow();
346 // Before we actually clone it, let's force all the extern'd crates to
347 // actually be loaded, just in case they're only referred to inside
349 resolver.borrow_mut().access(|resolver| {
350 for extern_name in &extern_names {
352 .resolve_str_path_error(DUMMY_SP, extern_name, TypeNS, CRATE_NODE_ID)
353 .unwrap_or_else(|()| {
354 panic!("Unable to resolve external crate {}", extern_name)
359 // Now we're good to clone the resolver because everything should be loaded
363 if sess.has_errors() {
364 sess.fatal("Compilation failed, aborting rustdoc");
367 let mut global_ctxt = abort_on_err(queries.global_ctxt(), sess).take();
369 global_ctxt.enter(|tcx| {
370 tcx.analysis(LOCAL_CRATE).ok();
372 // Abort if there were any errors so far
373 sess.abort_if_errors();
375 let access_levels = tcx.privacy_access_levels(LOCAL_CRATE);
376 // Convert from a HirId set to a DefId set since we don't always have easy access
377 // to the map from defid -> hirid
378 let access_levels = AccessLevels {
382 .map(|(&k, &v)| (tcx.hir().local_def_id(k), v))
386 let mut renderinfo = RenderInfo::default();
387 renderinfo.access_levels = access_levels;
389 let mut ctxt = DocContext {
392 external_traits: Default::default(),
393 active_extern_traits: Default::default(),
394 renderinfo: RefCell::new(renderinfo),
395 ty_substs: Default::default(),
396 lt_substs: Default::default(),
397 ct_substs: Default::default(),
398 impl_trait_bounds: Default::default(),
399 fake_def_ids: Default::default(),
400 all_fake_def_ids: Default::default(),
401 generated_synthetics: Default::default(),
403 .all_traits(LOCAL_CRATE)
406 .filter(|trait_def_id| tcx.trait_is_auto(*trait_def_id))
409 debug!("crate: {:?}", tcx.hir().krate());
411 let mut krate = clean::krate(&mut ctxt);
413 fn report_deprecated_attr(name: &str, diag: &rustc_errors::Handler) {
414 let mut msg = diag.struct_warn(&format!(
415 "the `#![doc({})]` attribute is \
416 considered deprecated",
420 "see issue #44136 <https://github.com/rust-lang/rust/issues/44136> \
421 for more information",
424 if name == "no_default_passes" {
425 msg.help("you may want to use `#![doc(document_private_items)]`");
431 // Process all of the crate attributes, extracting plugin metadata along
432 // with the passes which we are supposed to run.
433 for attr in krate.module.as_ref().unwrap().attrs.lists(sym::doc) {
434 let diag = ctxt.sess().diagnostic();
436 let name = attr.name_or_empty();
438 if name == sym::no_default_passes {
439 report_deprecated_attr("no_default_passes", diag);
440 if default_passes == passes::DefaultPassOption::Default {
441 default_passes = passes::DefaultPassOption::None;
444 } else if let Some(value) = attr.value_str() {
445 let sink = match name {
447 report_deprecated_attr("passes = \"...\"", diag);
451 report_deprecated_attr("plugins = \"...\"", diag);
453 "WARNING: `#![doc(plugins = \"...\")]` \
454 no longer functions; see CVE-2018-1000622"
460 for name in value.as_str().split_whitespace() {
461 sink.push(name.to_string());
465 if attr.is_word() && name == sym::document_private_items {
466 document_private = true;
470 let passes = passes::defaults(default_passes).iter().copied().chain(
471 manual_passes.into_iter().flat_map(|name| {
472 if let Some(pass) = passes::find_pass(&name) {
473 Some(ConditionalPass::always(pass))
475 error!("unknown pass {}, skipping", name);
481 info!("Executing passes");
484 let run = match p.condition {
486 WhenDocumentPrivate => document_private,
487 WhenNotDocumentPrivate => !document_private,
488 WhenNotDocumentHidden => !document_hidden,
491 debug!("running pass {}", p.pass.name);
492 krate = (p.pass.run)(krate, &ctxt);
496 ctxt.sess().abort_if_errors();
498 (krate, ctxt.renderinfo.into_inner(), render_options)
504 /// `DefId` or parameter index (`ty::ParamTy.index`) of a synthetic type parameter
505 /// for `impl Trait` in argument position.
506 #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
507 pub enum ImplTraitParam {
512 impl From<DefId> for ImplTraitParam {
513 fn from(did: DefId) -> Self {
514 ImplTraitParam::DefId(did)
518 impl From<u32> for ImplTraitParam {
519 fn from(idx: u32) -> Self {
520 ImplTraitParam::ParamIndex(idx)