]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/core.rs
Fix font color for help button in ayu and dark themes
[rust.git] / src / librustdoc / core.rs
1 use rustc_attr as attr;
2 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
3 use rustc_data_structures::sync::{self, Lrc};
4 use rustc_driver::abort_on_err;
5 use rustc_errors::emitter::{Emitter, EmitterWriter};
6 use rustc_errors::json::JsonEmitter;
7 use rustc_feature::UnstableFeatures;
8 use rustc_hir::def::{Namespace::TypeNS, Res};
9 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
10 use rustc_hir::HirId;
11 use rustc_hir::{
12     intravisit::{self, NestedVisitorMap, Visitor},
13     Path,
14 };
15 use rustc_interface::interface;
16 use rustc_middle::hir::map::Map;
17 use rustc_middle::middle::cstore::CrateStore;
18 use rustc_middle::middle::privacy::AccessLevels;
19 use rustc_middle::ty::{Ty, TyCtxt};
20 use rustc_resolve as resolve;
21 use rustc_session::config::{self, CrateType, ErrorOutputType};
22 use rustc_session::lint;
23 use rustc_session::DiagnosticOutput;
24 use rustc_session::Session;
25 use rustc_span::source_map;
26 use rustc_span::symbol::sym;
27 use rustc_span::DUMMY_SP;
28
29 use std::cell::RefCell;
30 use std::mem;
31 use std::rc::Rc;
32
33 use crate::clean;
34 use crate::clean::{AttributesExt, MAX_DEF_ID};
35 use crate::config::RenderInfo;
36 use crate::config::{Options as RustdocOptions, RenderOptions};
37 use crate::passes::{self, Condition::*, ConditionalPass};
38
39 pub use rustc_session::config::{CodegenOptions, DebuggingOptions, Input, Options};
40 pub use rustc_session::search_paths::SearchPath;
41
42 pub type ExternalPaths = FxHashMap<DefId, (Vec<String>, clean::TypeKind)>;
43
44 pub struct DocContext<'tcx> {
45     pub tcx: TyCtxt<'tcx>,
46     pub resolver: Rc<RefCell<interface::BoxedResolver>>,
47     /// Later on moved into `CACHE_KEY`
48     pub renderinfo: RefCell<RenderInfo>,
49     /// Later on moved through `clean::Crate` into `CACHE_KEY`
50     pub external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
51     /// Used while populating `external_traits` to ensure we don't process the same trait twice at
52     /// the same time.
53     pub active_extern_traits: RefCell<FxHashSet<DefId>>,
54     // The current set of type and lifetime substitutions,
55     // for expanding type aliases at the HIR level:
56     /// Table `DefId` of type parameter -> substituted type
57     pub ty_substs: RefCell<FxHashMap<DefId, clean::Type>>,
58     /// Table `DefId` of lifetime parameter -> substituted lifetime
59     pub lt_substs: RefCell<FxHashMap<DefId, clean::Lifetime>>,
60     /// Table `DefId` of const parameter -> substituted const
61     pub ct_substs: RefCell<FxHashMap<DefId, clean::Constant>>,
62     /// Table synthetic type parameter for `impl Trait` in argument position -> bounds
63     pub impl_trait_bounds: RefCell<FxHashMap<ImplTraitParam, Vec<clean::GenericBound>>>,
64     pub fake_def_ids: RefCell<FxHashMap<CrateNum, DefId>>,
65     pub all_fake_def_ids: RefCell<FxHashSet<DefId>>,
66     /// Auto-trait or blanket impls processed so far, as `(self_ty, trait_def_id)`.
67     // FIXME(eddyb) make this a `ty::TraitRef<'tcx>` set.
68     pub generated_synthetics: RefCell<FxHashSet<(Ty<'tcx>, DefId)>>,
69     pub auto_traits: Vec<DefId>,
70     /// The options given to rustdoc that could be relevant to a pass.
71     pub render_options: RenderOptions,
72 }
73
74 impl<'tcx> DocContext<'tcx> {
75     pub fn sess(&self) -> &Session {
76         &self.tcx.sess
77     }
78
79     pub fn enter_resolver<F, R>(&self, f: F) -> R
80     where
81         F: FnOnce(&mut resolve::Resolver<'_>) -> R,
82     {
83         self.resolver.borrow_mut().access(f)
84     }
85
86     /// Call the closure with the given parameters set as
87     /// the substitutions for a type alias' RHS.
88     pub fn enter_alias<F, R>(
89         &self,
90         ty_substs: FxHashMap<DefId, clean::Type>,
91         lt_substs: FxHashMap<DefId, clean::Lifetime>,
92         ct_substs: FxHashMap<DefId, clean::Constant>,
93         f: F,
94     ) -> R
95     where
96         F: FnOnce() -> R,
97     {
98         let (old_tys, old_lts, old_cts) = (
99             mem::replace(&mut *self.ty_substs.borrow_mut(), ty_substs),
100             mem::replace(&mut *self.lt_substs.borrow_mut(), lt_substs),
101             mem::replace(&mut *self.ct_substs.borrow_mut(), ct_substs),
102         );
103         let r = f();
104         *self.ty_substs.borrow_mut() = old_tys;
105         *self.lt_substs.borrow_mut() = old_lts;
106         *self.ct_substs.borrow_mut() = old_cts;
107         r
108     }
109
110     // This is an ugly hack, but it's the simplest way to handle synthetic impls without greatly
111     // refactoring either librustdoc or librustc_middle. In particular, allowing new DefIds to be
112     // registered after the AST is constructed would require storing the defid mapping in a
113     // RefCell, decreasing the performance for normal compilation for very little gain.
114     //
115     // Instead, we construct 'fake' def ids, which start immediately after the last DefId.
116     // In the Debug impl for clean::Item, we explicitly check for fake
117     // def ids, as we'll end up with a panic if we use the DefId Debug impl for fake DefIds
118     pub fn next_def_id(&self, crate_num: CrateNum) -> DefId {
119         let start_def_id = {
120             let next_id = if crate_num == LOCAL_CRATE {
121                 self.tcx.hir().definitions().def_path_table().next_id()
122             } else {
123                 self.enter_resolver(|r| r.cstore().def_path_table(crate_num).next_id())
124             };
125
126             DefId { krate: crate_num, index: next_id }
127         };
128
129         let mut fake_ids = self.fake_def_ids.borrow_mut();
130
131         let def_id = *fake_ids.entry(crate_num).or_insert(start_def_id);
132         fake_ids.insert(
133             crate_num,
134             DefId { krate: crate_num, index: DefIndex::from(def_id.index.index() + 1) },
135         );
136
137         MAX_DEF_ID.with(|m| {
138             m.borrow_mut().entry(def_id.krate).or_insert(start_def_id);
139         });
140
141         self.all_fake_def_ids.borrow_mut().insert(def_id);
142
143         def_id
144     }
145
146     /// Like `hir().local_def_id_to_hir_id()`, but skips calling it on fake DefIds.
147     /// (This avoids a slice-index-out-of-bounds panic.)
148     pub fn as_local_hir_id(&self, def_id: DefId) -> Option<HirId> {
149         if self.all_fake_def_ids.borrow().contains(&def_id) {
150             None
151         } else {
152             def_id.as_local().map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id))
153         }
154     }
155
156     pub fn stability(&self, id: HirId) -> Option<attr::Stability> {
157         self.tcx
158             .hir()
159             .opt_local_def_id(id)
160             .and_then(|def_id| self.tcx.lookup_stability(def_id.to_def_id()))
161             .cloned()
162     }
163
164     pub fn deprecation(&self, id: HirId) -> Option<attr::Deprecation> {
165         self.tcx
166             .hir()
167             .opt_local_def_id(id)
168             .and_then(|def_id| self.tcx.lookup_deprecation(def_id.to_def_id()))
169     }
170 }
171
172 /// Creates a new diagnostic `Handler` that can be used to emit warnings and errors.
173 ///
174 /// If the given `error_format` is `ErrorOutputType::Json` and no `SourceMap` is given, a new one
175 /// will be created for the handler.
176 pub fn new_handler(
177     error_format: ErrorOutputType,
178     source_map: Option<Lrc<source_map::SourceMap>>,
179     debugging_opts: &DebuggingOptions,
180 ) -> rustc_errors::Handler {
181     let emitter: Box<dyn Emitter + sync::Send> = match error_format {
182         ErrorOutputType::HumanReadable(kind) => {
183             let (short, color_config) = kind.unzip();
184             Box::new(
185                 EmitterWriter::stderr(
186                     color_config,
187                     source_map.map(|sm| sm as _),
188                     short,
189                     debugging_opts.teach,
190                     debugging_opts.terminal_width,
191                     false,
192                 )
193                 .ui_testing(debugging_opts.ui_testing),
194             )
195         }
196         ErrorOutputType::Json { pretty, json_rendered } => {
197             let source_map = source_map.unwrap_or_else(|| {
198                 Lrc::new(source_map::SourceMap::new(source_map::FilePathMapping::empty()))
199             });
200             Box::new(
201                 JsonEmitter::stderr(
202                     None,
203                     source_map,
204                     pretty,
205                     json_rendered,
206                     debugging_opts.terminal_width,
207                     false,
208                 )
209                 .ui_testing(debugging_opts.ui_testing),
210             )
211         }
212     };
213
214     rustc_errors::Handler::with_emitter_and_flags(
215         emitter,
216         debugging_opts.diagnostic_handler_flags(true),
217     )
218 }
219
220 /// This function is used to setup the lint initialization. By default, in rustdoc, everything
221 /// is "allowed". Depending if we run in test mode or not, we want some of them to be at their
222 /// default level. For example, the "INVALID_CODEBLOCK_ATTRIBUTES" lint is activated in both
223 /// modes.
224 ///
225 /// A little detail easy to forget is that there is a way to set the lint level for all lints
226 /// through the "WARNINGS" lint. To prevent this to happen, we set it back to its "normal" level
227 /// inside this function.
228 ///
229 /// It returns a tuple containing:
230 ///  * Vector of tuples of lints' name and their associated "max" level
231 ///  * HashMap of lint id with their associated "max" level
232 pub fn init_lints<F>(
233     mut allowed_lints: Vec<String>,
234     lint_opts: Vec<(String, lint::Level)>,
235     filter_call: F,
236 ) -> (Vec<(String, lint::Level)>, FxHashMap<lint::LintId, lint::Level>)
237 where
238     F: Fn(&lint::Lint) -> Option<(String, lint::Level)>,
239 {
240     let warnings_lint_name = lint::builtin::WARNINGS.name;
241
242     allowed_lints.push(warnings_lint_name.to_owned());
243     allowed_lints.extend(lint_opts.iter().map(|(lint, _)| lint).cloned());
244
245     let lints = || {
246         lint::builtin::HardwiredLints::get_lints()
247             .into_iter()
248             .chain(rustc_lint::SoftLints::get_lints().into_iter())
249     };
250
251     let lint_opts = lints()
252         .filter_map(|lint| {
253             // Permit feature-gated lints to avoid feature errors when trying to
254             // allow all lints.
255             if lint.name == warnings_lint_name || lint.feature_gate.is_some() {
256                 None
257             } else {
258                 filter_call(lint)
259             }
260         })
261         .chain(lint_opts.into_iter())
262         .collect::<Vec<_>>();
263
264     let lint_caps = lints()
265         .filter_map(|lint| {
266             // We don't want to allow *all* lints so let's ignore
267             // those ones.
268             if allowed_lints.iter().any(|l| lint.name == l) {
269                 None
270             } else {
271                 Some((lint::LintId::of(lint), lint::Allow))
272             }
273         })
274         .collect();
275     (lint_opts, lint_caps)
276 }
277
278 pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOptions) {
279     // Parse, resolve, and typecheck the given crate.
280
281     let RustdocOptions {
282         input,
283         crate_name,
284         proc_macro_crate,
285         error_format,
286         libs,
287         externs,
288         mut cfgs,
289         codegen_options,
290         debugging_options,
291         target,
292         edition,
293         maybe_sysroot,
294         lint_opts,
295         describe_lints,
296         lint_cap,
297         mut default_passes,
298         mut manual_passes,
299         display_warnings,
300         render_options,
301         output_format,
302         ..
303     } = options;
304
305     let extern_names: Vec<String> = externs
306         .iter()
307         .filter(|(_, entry)| entry.add_prelude)
308         .map(|(name, _)| name)
309         .cloned()
310         .collect();
311
312     // Add the doc cfg into the doc build.
313     cfgs.push("doc".to_string());
314
315     let cpath = Some(input.clone());
316     let input = Input::File(input);
317
318     let intra_link_resolution_failure_name = lint::builtin::BROKEN_INTRA_DOC_LINKS.name;
319     let missing_docs = rustc_lint::builtin::MISSING_DOCS.name;
320     let missing_doc_example = rustc_lint::builtin::MISSING_DOC_CODE_EXAMPLES.name;
321     let private_doc_tests = rustc_lint::builtin::PRIVATE_DOC_TESTS.name;
322     let no_crate_level_docs = rustc_lint::builtin::MISSING_CRATE_LEVEL_DOCS.name;
323     let invalid_codeblock_attributes_name = rustc_lint::builtin::INVALID_CODEBLOCK_ATTRIBUTES.name;
324
325     // In addition to those specific lints, we also need to allow those given through
326     // command line, otherwise they'll get ignored and we don't want that.
327     let allowed_lints = vec![
328         intra_link_resolution_failure_name.to_owned(),
329         missing_docs.to_owned(),
330         missing_doc_example.to_owned(),
331         private_doc_tests.to_owned(),
332         no_crate_level_docs.to_owned(),
333         invalid_codeblock_attributes_name.to_owned(),
334     ];
335
336     let (lint_opts, lint_caps) = init_lints(allowed_lints, lint_opts, |lint| {
337         if lint.name == intra_link_resolution_failure_name
338             || lint.name == invalid_codeblock_attributes_name
339         {
340             None
341         } else {
342             Some((lint.name_lower(), lint::Allow))
343         }
344     });
345
346     let crate_types =
347         if proc_macro_crate { vec![CrateType::ProcMacro] } else { vec![CrateType::Rlib] };
348     // plays with error output here!
349     let sessopts = config::Options {
350         maybe_sysroot,
351         search_paths: libs,
352         crate_types,
353         lint_opts: if !display_warnings { lint_opts } else { vec![] },
354         lint_cap: Some(lint_cap.unwrap_or_else(|| lint::Forbid)),
355         cg: codegen_options,
356         externs,
357         target_triple: target,
358         unstable_features: UnstableFeatures::from_environment(),
359         actually_rustdoc: true,
360         debugging_opts: debugging_options,
361         error_format,
362         edition,
363         describe_lints,
364         ..Options::default()
365     };
366
367     let config = interface::Config {
368         opts: sessopts,
369         crate_cfg: interface::parse_cfgspecs(cfgs),
370         input,
371         input_path: cpath,
372         output_file: None,
373         output_dir: None,
374         file_loader: None,
375         diagnostic_output: DiagnosticOutput::Default,
376         stderr: None,
377         crate_name,
378         lint_caps,
379         register_lints: None,
380         override_queries: Some(|_sess, providers, _external_providers| {
381             // Most lints will require typechecking, so just don't run them.
382             providers.lint_mod = |_, _| {};
383             // Prevent `rustc_typeck::check_crate` from calling `typeck` on all bodies.
384             providers.typeck_item_bodies = |_, _| {};
385             // hack so that `used_trait_imports` won't try to call typeck
386             providers.used_trait_imports = |_, _| {
387                 lazy_static! {
388                     static ref EMPTY_SET: FxHashSet<LocalDefId> = FxHashSet::default();
389                 }
390                 &EMPTY_SET
391             };
392             // In case typeck does end up being called, don't ICE in case there were name resolution errors
393             providers.typeck = move |tcx, def_id| {
394                 // Closures' tables come from their outermost function,
395                 // as they are part of the same "inference environment".
396                 // This avoids emitting errors for the parent twice (see similar code in `typeck_with_fallback`)
397                 let outer_def_id = tcx.closure_base_def_id(def_id.to_def_id()).expect_local();
398                 if outer_def_id != def_id {
399                     return tcx.typeck(outer_def_id);
400                 }
401
402                 let hir = tcx.hir();
403                 let body = hir.body(hir.body_owned_by(hir.local_def_id_to_hir_id(def_id)));
404                 debug!("visiting body for {:?}", def_id);
405                 EmitIgnoredResolutionErrors::new(tcx).visit_body(body);
406                 (rustc_interface::DEFAULT_QUERY_PROVIDERS.typeck)(tcx, def_id)
407             };
408         }),
409         registry: rustc_driver::diagnostics_registry(),
410     };
411
412     interface::create_compiler_and_run(config, |compiler| {
413         compiler.enter(|queries| {
414             let sess = compiler.session();
415
416             // We need to hold on to the complete resolver, so we cause everything to be
417             // cloned for the analysis passes to use. Suboptimal, but necessary in the
418             // current architecture.
419             let resolver = {
420                 let parts = abort_on_err(queries.expansion(), sess).peek();
421                 let resolver = parts.1.borrow();
422
423                 // Before we actually clone it, let's force all the extern'd crates to
424                 // actually be loaded, just in case they're only referred to inside
425                 // intra-doc-links
426                 resolver.borrow_mut().access(|resolver| {
427                     for extern_name in &extern_names {
428                         resolver
429                             .resolve_str_path_error(
430                                 DUMMY_SP,
431                                 extern_name,
432                                 TypeNS,
433                                 LocalDefId { local_def_index: CRATE_DEF_INDEX }.to_def_id(),
434                             )
435                             .unwrap_or_else(|()| {
436                                 panic!("Unable to resolve external crate {}", extern_name)
437                             });
438                     }
439                 });
440
441                 // Now we're good to clone the resolver because everything should be loaded
442                 resolver.clone()
443             };
444
445             if sess.has_errors() {
446                 sess.fatal("Compilation failed, aborting rustdoc");
447             }
448
449             let mut global_ctxt = abort_on_err(queries.global_ctxt(), sess).take();
450
451             global_ctxt.enter(|tcx| {
452                 // Certain queries assume that some checks were run elsewhere
453                 // (see https://github.com/rust-lang/rust/pull/73566#issuecomment-656954425),
454                 // so type-check everything other than function bodies in this crate before running lints.
455
456                 // NOTE: this does not call `tcx.analysis()` so that we won't
457                 // typeck function bodies or run the default rustc lints.
458                 // (see `override_queries` in the `config`)
459
460                 // HACK(jynelson) this calls an _extremely_ limited subset of `typeck`
461                 // and might break if queries change their assumptions in the future.
462
463                 // NOTE: This is copy/pasted from typeck/lib.rs and should be kept in sync with those changes.
464                 tcx.sess.time("item_types_checking", || {
465                     for &module in tcx.hir().krate().modules.keys() {
466                         tcx.ensure().check_mod_item_types(tcx.hir().local_def_id(module));
467                     }
468                 });
469                 tcx.sess.abort_if_errors();
470                 sess.time("missing_docs", || {
471                     rustc_lint::check_crate(tcx, rustc_lint::builtin::MissingDoc::new);
472                 });
473                 for &module in tcx.hir().krate().modules.keys() {
474                     let local_def_id = tcx.hir().local_def_id(module);
475                     tcx.ensure().check_mod_attrs(local_def_id);
476                 }
477
478                 let access_levels = tcx.privacy_access_levels(LOCAL_CRATE);
479                 // Convert from a HirId set to a DefId set since we don't always have easy access
480                 // to the map from defid -> hirid
481                 let access_levels = AccessLevels {
482                     map: access_levels
483                         .map
484                         .iter()
485                         .map(|(&k, &v)| (tcx.hir().local_def_id(k).to_def_id(), v))
486                         .collect(),
487                 };
488
489                 let mut renderinfo = RenderInfo::default();
490                 renderinfo.access_levels = access_levels;
491                 renderinfo.output_format = output_format;
492
493                 let mut ctxt = DocContext {
494                     tcx,
495                     resolver,
496                     external_traits: Default::default(),
497                     active_extern_traits: Default::default(),
498                     renderinfo: RefCell::new(renderinfo),
499                     ty_substs: Default::default(),
500                     lt_substs: Default::default(),
501                     ct_substs: Default::default(),
502                     impl_trait_bounds: Default::default(),
503                     fake_def_ids: Default::default(),
504                     all_fake_def_ids: Default::default(),
505                     generated_synthetics: Default::default(),
506                     auto_traits: tcx
507                         .all_traits(LOCAL_CRATE)
508                         .iter()
509                         .cloned()
510                         .filter(|trait_def_id| tcx.trait_is_auto(*trait_def_id))
511                         .collect(),
512                     render_options,
513                 };
514                 debug!("crate: {:?}", tcx.hir().krate());
515
516                 let mut krate = clean::krate(&mut ctxt);
517
518                 if let Some(ref m) = krate.module {
519                     if let None | Some("") = m.doc_value() {
520                         let help = "The following guide may be of use:\n\
521                              https://doc.rust-lang.org/nightly/rustdoc/how-to-write-documentation\
522                              .html";
523                         tcx.struct_lint_node(
524                             rustc_lint::builtin::MISSING_CRATE_LEVEL_DOCS,
525                             ctxt.as_local_hir_id(m.def_id).unwrap(),
526                             |lint| {
527                                 let mut diag = lint.build(
528                                     "no documentation found for this crate's top-level module",
529                                 );
530                                 diag.help(help);
531                                 diag.emit();
532                             },
533                         );
534                     }
535                 }
536
537                 fn report_deprecated_attr(name: &str, diag: &rustc_errors::Handler) {
538                     let mut msg = diag.struct_warn(&format!(
539                         "the `#![doc({})]` attribute is considered deprecated",
540                         name
541                     ));
542                     msg.warn(
543                         "see issue #44136 <https://github.com/rust-lang/rust/issues/44136> \
544                          for more information",
545                     );
546
547                     if name == "no_default_passes" {
548                         msg.help("you may want to use `#![doc(document_private_items)]`");
549                     }
550
551                     msg.emit();
552                 }
553
554                 // Process all of the crate attributes, extracting plugin metadata along
555                 // with the passes which we are supposed to run.
556                 for attr in krate.module.as_ref().unwrap().attrs.lists(sym::doc) {
557                     let diag = ctxt.sess().diagnostic();
558
559                     let name = attr.name_or_empty();
560                     if attr.is_word() {
561                         if name == sym::no_default_passes {
562                             report_deprecated_attr("no_default_passes", diag);
563                             if default_passes == passes::DefaultPassOption::Default {
564                                 default_passes = passes::DefaultPassOption::None;
565                             }
566                         }
567                     } else if let Some(value) = attr.value_str() {
568                         let sink = match name {
569                             sym::passes => {
570                                 report_deprecated_attr("passes = \"...\"", diag);
571                                 &mut manual_passes
572                             }
573                             sym::plugins => {
574                                 report_deprecated_attr("plugins = \"...\"", diag);
575                                 eprintln!(
576                                     "WARNING: `#![doc(plugins = \"...\")]` \
577                                       no longer functions; see CVE-2018-1000622"
578                                 );
579                                 continue;
580                             }
581                             _ => continue,
582                         };
583                         for name in value.as_str().split_whitespace() {
584                             sink.push(name.to_string());
585                         }
586                     }
587
588                     if attr.is_word() && name == sym::document_private_items {
589                         ctxt.render_options.document_private = true;
590                     }
591                 }
592
593                 let passes = passes::defaults(default_passes).iter().copied().chain(
594                     manual_passes.into_iter().flat_map(|name| {
595                         if let Some(pass) = passes::find_pass(&name) {
596                             Some(ConditionalPass::always(pass))
597                         } else {
598                             error!("unknown pass {}, skipping", name);
599                             None
600                         }
601                     }),
602                 );
603
604                 info!("Executing passes");
605
606                 for p in passes {
607                     let run = match p.condition {
608                         Always => true,
609                         WhenDocumentPrivate => ctxt.render_options.document_private,
610                         WhenNotDocumentPrivate => !ctxt.render_options.document_private,
611                         WhenNotDocumentHidden => !ctxt.render_options.document_hidden,
612                     };
613                     if run {
614                         debug!("running pass {}", p.pass.name);
615                         krate = (p.pass.run)(krate, &ctxt);
616                     }
617                 }
618
619                 ctxt.sess().abort_if_errors();
620
621                 (krate, ctxt.renderinfo.into_inner(), ctxt.render_options)
622             })
623         })
624     })
625 }
626
627 /// Due to https://github.com/rust-lang/rust/pull/73566,
628 /// the name resolution pass may find errors that are never emitted.
629 /// If typeck is called after this happens, then we'll get an ICE:
630 /// 'Res::Error found but not reported'. To avoid this, emit the errors now.
631 struct EmitIgnoredResolutionErrors<'tcx> {
632     tcx: TyCtxt<'tcx>,
633 }
634
635 impl<'tcx> EmitIgnoredResolutionErrors<'tcx> {
636     fn new(tcx: TyCtxt<'tcx>) -> Self {
637         Self { tcx }
638     }
639 }
640
641 impl<'tcx> Visitor<'tcx> for EmitIgnoredResolutionErrors<'tcx> {
642     type Map = Map<'tcx>;
643
644     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
645         // We need to recurse into nested closures,
646         // since those will fallback to the parent for type checking.
647         NestedVisitorMap::OnlyBodies(self.tcx.hir())
648     }
649
650     fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) {
651         debug!("visiting path {:?}", path);
652         if path.res == Res::Err {
653             // We have less context here than in rustc_resolve,
654             // so we can only emit the name and span.
655             // However we can give a hint that rustc_resolve will have more info.
656             let label = format!(
657                 "could not resolve path `{}`",
658                 path.segments
659                     .iter()
660                     .map(|segment| segment.ident.as_str().to_string())
661                     .collect::<Vec<_>>()
662                     .join("::")
663             );
664             let mut err = rustc_errors::struct_span_err!(
665                 self.tcx.sess,
666                 path.span,
667                 E0433,
668                 "failed to resolve: {}",
669                 label
670             );
671             err.span_label(path.span, label);
672             err.note("this error was originally ignored because you are running `rustdoc`");
673             err.note("try running again with `rustc` or `cargo check` and you may get a more detailed error");
674             err.emit();
675         }
676         // We could have an outer resolution that succeeded,
677         // but with generic parameters that failed.
678         // Recurse into the segments so we catch those too.
679         intravisit::walk_path(self, path);
680     }
681 }
682
683 /// `DefId` or parameter index (`ty::ParamTy.index`) of a synthetic type parameter
684 /// for `impl Trait` in argument position.
685 #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
686 pub enum ImplTraitParam {
687     DefId(DefId),
688     ParamIndex(u32),
689 }
690
691 impl From<DefId> for ImplTraitParam {
692     fn from(did: DefId) -> Self {
693         ImplTraitParam::DefId(did)
694     }
695 }
696
697 impl From<u32> for ImplTraitParam {
698     fn from(idx: u32) -> Self {
699         ImplTraitParam::ParamIndex(idx)
700     }
701 }