Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 1 | use rustc_data_structures::fx::{FxHashMap, FxHashSet}; |
Chris Wailes | 951ae7a | 2023-12-07 10:11:13 -0800 | [diff] [blame^] | 2 | use rustc_data_structures::sync::Lrc; |
Charisee | f7ad1c4 | 2023-01-30 22:46:42 +0000 | [diff] [blame] | 3 | use rustc_data_structures::unord::UnordSet; |
Chris Wailes | 951ae7a | 2023-12-07 10:11:13 -0800 | [diff] [blame^] | 4 | use rustc_errors::emitter::{DynEmitter, EmitterWriter}; |
Jeff Vander Stoep | ad6790c | 2020-06-24 15:34:31 +0200 | [diff] [blame] | 5 | use rustc_errors::json::JsonEmitter; |
Chris Wailes | 5c0824a | 2023-04-24 16:30:59 -0700 | [diff] [blame] | 6 | use rustc_errors::TerminalUrl; |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 7 | use rustc_feature::UnstableFeatures; |
Chris Wailes | 5c0824a | 2023-04-24 16:30:59 -0700 | [diff] [blame] | 8 | use rustc_hir::def::Res; |
| 9 | use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LocalDefId}; |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 10 | use rustc_hir::intravisit::{self, Visitor}; |
Chris Wailes | 5c0824a | 2023-04-24 16:30:59 -0700 | [diff] [blame] | 11 | use rustc_hir::{HirId, Path}; |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 12 | use rustc_interface::interface; |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 13 | use rustc_lint::{late_lint_mod, MissingDoc}; |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 14 | use rustc_middle::hir::nested_filter; |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 15 | use rustc_middle::ty::{ParamEnv, Ty, TyCtxt}; |
Chris Wailes | 5c0824a | 2023-04-24 16:30:59 -0700 | [diff] [blame] | 16 | use rustc_session::config::{self, CrateType, ErrorOutputType, ResolveDocLinks}; |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 17 | use rustc_session::Session; |
James Farrell | 91b821d | 2023-08-24 14:18:40 +0000 | [diff] [blame] | 18 | use rustc_session::{lint, EarlyErrorHandler}; |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 19 | use rustc_span::symbol::sym; |
Chris Wailes | 5c0824a | 2023-04-24 16:30:59 -0700 | [diff] [blame] | 20 | use rustc_span::{source_map, Span}; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 21 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 22 | use std::cell::RefCell; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 23 | use std::mem; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 24 | use std::rc::Rc; |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 25 | use std::sync::LazyLock; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 26 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 27 | use crate::clean::inline::build_external_trait; |
Charisee | f7ad1c4 | 2023-01-30 22:46:42 +0000 | [diff] [blame] | 28 | use crate::clean::{self, ItemId}; |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 29 | use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions}; |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 30 | use crate::formats::cache::Cache; |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 31 | use crate::passes::{self, Condition::*}; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 32 | |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 33 | pub(crate) use rustc_session::config::{Input, Options, UnstableOptions}; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 34 | |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 35 | pub(crate) struct DocContext<'tcx> { |
| 36 | pub(crate) tcx: TyCtxt<'tcx>, |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 37 | /// Used for normalization. |
| 38 | /// |
| 39 | /// Most of this logic is copied from rustc_lint::late. |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 40 | pub(crate) param_env: ParamEnv<'tcx>, |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 41 | /// Later on moved through `clean::Crate` into `cache` |
Charisee | f7ad1c4 | 2023-01-30 22:46:42 +0000 | [diff] [blame] | 42 | pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 43 | /// Used while populating `external_traits` to ensure we don't process the same trait twice at |
| 44 | /// the same time. |
Chris Wailes | 5c0824a | 2023-04-24 16:30:59 -0700 | [diff] [blame] | 45 | pub(crate) active_extern_traits: DefIdSet, |
Chris Wailes | 356b57e | 2022-01-13 10:08:24 -0800 | [diff] [blame] | 46 | // The current set of parameter substitutions, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 47 | // for expanding type aliases at the HIR level: |
Chris Wailes | 356b57e | 2022-01-13 10:08:24 -0800 | [diff] [blame] | 48 | /// Table `DefId` of type, lifetime, or const parameter -> substituted type, lifetime, or const |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 49 | pub(crate) args: DefIdMap<clean::SubstParam>, |
Chris Wailes | cd1aefd | 2023-07-13 13:36:21 -0700 | [diff] [blame] | 50 | pub(crate) current_type_aliases: DefIdMap<usize>, |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 51 | /// Table synthetic type parameter for `impl Trait` in argument position -> bounds |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 52 | pub(crate) impl_trait_bounds: FxHashMap<ImplTraitParam, Vec<clean::GenericBound>>, |
Chih-Hung Hsieh | fd666f2 | 2019-12-19 14:34:18 -0800 | [diff] [blame] | 53 | /// Auto-trait or blanket impls processed so far, as `(self_ty, trait_def_id)`. |
| 54 | // FIXME(eddyb) make this a `ty::TraitRef<'tcx>` set. |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 55 | pub(crate) generated_synthetics: FxHashSet<(Ty<'tcx>, DefId)>, |
| 56 | pub(crate) auto_traits: Vec<DefId>, |
Jeff Vander Stoep | 983137f | 2020-09-16 14:50:30 +0200 | [diff] [blame] | 57 | /// The options given to rustdoc that could be relevant to a pass. |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 58 | pub(crate) render_options: RenderOptions, |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 59 | /// This same cache is used throughout rustdoc, including in [`crate::html::render`]. |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 60 | pub(crate) cache: Cache, |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 61 | /// Used by [`clean::inline`] to tell if an item has already been inlined. |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 62 | pub(crate) inlined: FxHashSet<ItemId>, |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 63 | /// Used by `calculate_doc_coverage`. |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 64 | pub(crate) output_format: OutputFormat, |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 65 | /// Used by `strip_private`. |
| 66 | pub(crate) show_coverage: bool, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | impl<'tcx> DocContext<'tcx> { |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 70 | pub(crate) fn sess(&self) -> &'tcx Session { |
Chris Wailes | 356b57e | 2022-01-13 10:08:24 -0800 | [diff] [blame] | 71 | self.tcx.sess |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 74 | pub(crate) fn with_param_env<T, F: FnOnce(&mut Self) -> T>( |
| 75 | &mut self, |
| 76 | def_id: DefId, |
| 77 | f: F, |
| 78 | ) -> T { |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 79 | let old_param_env = mem::replace(&mut self.param_env, self.tcx.param_env(def_id)); |
| 80 | let ret = f(self); |
| 81 | self.param_env = old_param_env; |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 82 | ret |
| 83 | } |
| 84 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 85 | /// Call the closure with the given parameters set as |
| 86 | /// the substitutions for a type alias' RHS. |
Chris Wailes | cd1aefd | 2023-07-13 13:36:21 -0700 | [diff] [blame] | 87 | pub(crate) fn enter_alias<F, R>( |
| 88 | &mut self, |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 89 | args: DefIdMap<clean::SubstParam>, |
Chris Wailes | cd1aefd | 2023-07-13 13:36:21 -0700 | [diff] [blame] | 90 | def_id: DefId, |
| 91 | f: F, |
| 92 | ) -> R |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 93 | where |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 94 | F: FnOnce(&mut Self) -> R, |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 95 | { |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 96 | let old_args = mem::replace(&mut self.args, args); |
Chris Wailes | cd1aefd | 2023-07-13 13:36:21 -0700 | [diff] [blame] | 97 | *self.current_type_aliases.entry(def_id).or_insert(0) += 1; |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 98 | let r = f(self); |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 99 | self.args = old_args; |
Chris Wailes | cd1aefd | 2023-07-13 13:36:21 -0700 | [diff] [blame] | 100 | if let Some(count) = self.current_type_aliases.get_mut(&def_id) { |
| 101 | *count -= 1; |
| 102 | if *count == 0 { |
| 103 | self.current_type_aliases.remove(&def_id); |
| 104 | } |
| 105 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 106 | r |
| 107 | } |
| 108 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 109 | /// Like `hir().local_def_id_to_hir_id()`, but skips calling it on fake DefIds. |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 110 | /// (This avoids a slice-index-out-of-bounds panic.) |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 111 | pub(crate) fn as_local_hir_id(tcx: TyCtxt<'_>, item_id: ItemId) -> Option<HirId> { |
Charisee | 9cf6780 | 2022-06-30 20:04:09 +0000 | [diff] [blame] | 112 | match item_id { |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 113 | ItemId::DefId(real_id) => { |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 114 | real_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) |
| 115 | } |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 116 | // FIXME: Can this be `Some` for `Auto` or `Blanket`? |
| 117 | _ => None, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /// Creates a new diagnostic `Handler` that can be used to emit warnings and errors. |
| 123 | /// |
| 124 | /// If the given `error_format` is `ErrorOutputType::Json` and no `SourceMap` is given, a new one |
| 125 | /// will be created for the handler. |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 126 | pub(crate) fn new_handler( |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 127 | error_format: ErrorOutputType, |
| 128 | source_map: Option<Lrc<source_map::SourceMap>>, |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 129 | diagnostic_width: Option<usize>, |
| 130 | unstable_opts: &UnstableOptions, |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 131 | ) -> rustc_errors::Handler { |
Chris Wailes | 5c0824a | 2023-04-24 16:30:59 -0700 | [diff] [blame] | 132 | let fallback_bundle = rustc_errors::fallback_fluent_bundle( |
| 133 | rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(), |
| 134 | false, |
| 135 | ); |
Chris Wailes | 951ae7a | 2023-12-07 10:11:13 -0800 | [diff] [blame^] | 136 | let emitter: Box<DynEmitter> = match error_format { |
Chih-Hung Hsieh | fd666f2 | 2019-12-19 14:34:18 -0800 | [diff] [blame] | 137 | ErrorOutputType::HumanReadable(kind) => { |
| 138 | let (short, color_config) = kind.unzip(); |
| 139 | Box::new( |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 140 | EmitterWriter::stderr(color_config, fallback_bundle) |
| 141 | .sm(source_map.map(|sm| sm as _)) |
| 142 | .short_message(short) |
| 143 | .teach(unstable_opts.teach) |
| 144 | .diagnostic_width(diagnostic_width) |
| 145 | .track_diagnostics(unstable_opts.track_diagnostics) |
| 146 | .ui_testing(unstable_opts.ui_testing), |
Chih-Hung Hsieh | fd666f2 | 2019-12-19 14:34:18 -0800 | [diff] [blame] | 147 | ) |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 148 | } |
Chih-Hung Hsieh | fd666f2 | 2019-12-19 14:34:18 -0800 | [diff] [blame] | 149 | ErrorOutputType::Json { pretty, json_rendered } => { |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 150 | let source_map = source_map.unwrap_or_else(|| { |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 151 | Lrc::new(source_map::SourceMap::new(source_map::FilePathMapping::empty())) |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 152 | }); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 153 | Box::new( |
Jeff Vander Stoep | 983137f | 2020-09-16 14:50:30 +0200 | [diff] [blame] | 154 | JsonEmitter::stderr( |
| 155 | None, |
| 156 | source_map, |
Charisee | 9cf6780 | 2022-06-30 20:04:09 +0000 | [diff] [blame] | 157 | None, |
| 158 | fallback_bundle, |
Jeff Vander Stoep | 983137f | 2020-09-16 14:50:30 +0200 | [diff] [blame] | 159 | pretty, |
| 160 | json_rendered, |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 161 | diagnostic_width, |
Jeff Vander Stoep | 983137f | 2020-09-16 14:50:30 +0200 | [diff] [blame] | 162 | false, |
Chris Wailes | 977026a | 2023-02-13 09:13:10 -0800 | [diff] [blame] | 163 | unstable_opts.track_diagnostics, |
Chris Wailes | 5c0824a | 2023-04-24 16:30:59 -0700 | [diff] [blame] | 164 | TerminalUrl::No, |
Jeff Vander Stoep | 983137f | 2020-09-16 14:50:30 +0200 | [diff] [blame] | 165 | ) |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 166 | .ui_testing(unstable_opts.ui_testing), |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 167 | ) |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 168 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 171 | rustc_errors::Handler::with_emitter(emitter) |
| 172 | .with_flags(unstable_opts.diagnostic_handler_flags(true)) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 175 | /// Parse, resolve, and typecheck the given crate. |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 176 | pub(crate) fn create_config( |
James Farrell | 91b821d | 2023-08-24 14:18:40 +0000 | [diff] [blame] | 177 | handler: &EarlyErrorHandler, |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 178 | RustdocOptions { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 179 | input, |
| 180 | crate_name, |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 181 | proc_macro_crate, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 182 | error_format, |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 183 | diagnostic_width, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 184 | libs, |
| 185 | externs, |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 186 | mut cfgs, |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 187 | check_cfgs, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 188 | codegen_options, |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 189 | unstable_opts, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 190 | target, |
| 191 | edition, |
| 192 | maybe_sysroot, |
| 193 | lint_opts, |
| 194 | describe_lints, |
| 195 | lint_cap, |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 196 | scrape_examples_options, |
Chris Wailes | 951ae7a | 2023-12-07 10:11:13 -0800 | [diff] [blame^] | 197 | expanded_args, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 198 | .. |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 199 | }: RustdocOptions, |
Chris Wailes | 5c0824a | 2023-04-24 16:30:59 -0700 | [diff] [blame] | 200 | RenderOptions { document_private, .. }: &RenderOptions, |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 201 | ) -> rustc_interface::Config { |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 202 | // Add the doc cfg into the doc build. |
| 203 | cfgs.push("doc".to_string()); |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 204 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 205 | let input = Input::File(input); |
| 206 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 207 | // By default, rustdoc ignores all lints. |
| 208 | // Specifically unblock lints relevant to documentation or the lint machinery itself. |
| 209 | let mut lints_to_show = vec![ |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 210 | // it's unclear whether these should be part of rustdoc directly (#77364) |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 211 | rustc_lint::builtin::MISSING_DOCS.name.to_string(), |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 212 | rustc_lint::builtin::INVALID_DOC_ATTRIBUTES.name.to_string(), |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 213 | // these are definitely not part of rustdoc, but we want to warn on them anyway. |
| 214 | rustc_lint::builtin::RENAMED_AND_REMOVED_LINTS.name.to_string(), |
| 215 | rustc_lint::builtin::UNKNOWN_LINTS.name.to_string(), |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 216 | rustc_lint::builtin::UNEXPECTED_CFGS.name.to_string(), |
Charisee | 9cf6780 | 2022-06-30 20:04:09 +0000 | [diff] [blame] | 217 | // this lint is needed to support `#[expect]` attributes |
| 218 | rustc_lint::builtin::UNFULFILLED_LINT_EXPECTATIONS.name.to_string(), |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 219 | ]; |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 220 | lints_to_show.extend(crate::lint::RUSTDOC_LINTS.iter().map(|lint| lint.name.to_string())); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 221 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 222 | let (lint_opts, lint_caps) = crate::lint::init_lints(lints_to_show, lint_opts, |lint| { |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 223 | Some((lint.name_lower(), lint::Allow)) |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 224 | }); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 225 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 226 | let crate_types = |
| 227 | if proc_macro_crate { vec![CrateType::ProcMacro] } else { vec![CrateType::Rlib] }; |
Charisee | 635618d | 2023-06-01 20:46:00 +0000 | [diff] [blame] | 228 | let resolve_doc_links = |
| 229 | if *document_private { ResolveDocLinks::All } else { ResolveDocLinks::Exported }; |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 230 | let test = scrape_examples_options.map(|opts| opts.scrape_tests).unwrap_or(false); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 231 | // plays with error output here! |
| 232 | let sessopts = config::Options { |
| 233 | maybe_sysroot, |
| 234 | search_paths: libs, |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 235 | crate_types, |
Chris Wailes | a153842 | 2021-12-02 10:37:12 -0800 | [diff] [blame] | 236 | lint_opts, |
Thiébaud Weksteen | 3b664ca | 2020-11-26 14:41:59 +0100 | [diff] [blame] | 237 | lint_cap, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 238 | cg: codegen_options, |
| 239 | externs, |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 240 | target_triple: target, |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 241 | unstable_features: UnstableFeatures::from_environment(crate_name.as_deref()), |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 242 | actually_rustdoc: true, |
Chris Wailes | 5c0824a | 2023-04-24 16:30:59 -0700 | [diff] [blame] | 243 | resolve_doc_links, |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 244 | unstable_opts, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 245 | error_format, |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 246 | diagnostic_width, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 247 | edition, |
| 248 | describe_lints, |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 249 | crate_name, |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 250 | test, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 251 | ..Options::default() |
| 252 | }; |
| 253 | |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 254 | interface::Config { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 255 | opts: sessopts, |
James Farrell | 91b821d | 2023-08-24 14:18:40 +0000 | [diff] [blame] | 256 | crate_cfg: interface::parse_cfgspecs(handler, cfgs), |
| 257 | crate_check_cfg: interface::parse_check_cfg(handler, check_cfgs), |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 258 | input, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 259 | output_file: None, |
| 260 | output_dir: None, |
| 261 | file_loader: None, |
Chris Wailes | 5c0824a | 2023-04-24 16:30:59 -0700 | [diff] [blame] | 262 | locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 263 | lint_caps, |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 264 | parse_sess_created: None, |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 265 | register_lints: Some(Box::new(crate::lint::register_lints)), |
Chris Wailes | 951ae7a | 2023-12-07 10:11:13 -0800 | [diff] [blame^] | 266 | override_queries: Some(|_sess, providers| { |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 267 | // We do not register late module lints, so this only runs `MissingDoc`. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 268 | // Most lints will require typechecking, so just don't run them. |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 269 | providers.lint_mod = |tcx, module_def_id| late_lint_mod(tcx, module_def_id, MissingDoc); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 270 | // hack so that `used_trait_imports` won't try to call typeck |
| 271 | providers.used_trait_imports = |_, _| { |
Charisee | f7ad1c4 | 2023-01-30 22:46:42 +0000 | [diff] [blame] | 272 | static EMPTY_SET: LazyLock<UnordSet<LocalDefId>> = LazyLock::new(UnordSet::default); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 273 | &EMPTY_SET |
| 274 | }; |
| 275 | // In case typeck does end up being called, don't ICE in case there were name resolution errors |
| 276 | providers.typeck = move |tcx, def_id| { |
| 277 | // Closures' tables come from their outermost function, |
| 278 | // as they are part of the same "inference environment". |
| 279 | // This avoids emitting errors for the parent twice (see similar code in `typeck_with_fallback`) |
Chris Wailes | 356b57e | 2022-01-13 10:08:24 -0800 | [diff] [blame] | 280 | let typeck_root_def_id = tcx.typeck_root_def_id(def_id.to_def_id()).expect_local(); |
| 281 | if typeck_root_def_id != def_id { |
| 282 | return tcx.typeck(typeck_root_def_id); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | let hir = tcx.hir(); |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 286 | let body = hir.body(hir.body_owned_by(def_id)); |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 287 | debug!("visiting body for {def_id:?}"); |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 288 | EmitIgnoredResolutionErrors::new(tcx).visit_body(body); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 289 | (rustc_interface::DEFAULT_QUERY_PROVIDERS.typeck)(tcx, def_id) |
| 290 | }; |
| 291 | }), |
Thiébaud Weksteen | 3b664ca | 2020-11-26 14:41:59 +0100 | [diff] [blame] | 292 | make_codegen_backend: None, |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 293 | registry: rustc_driver::diagnostics_registry(), |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 294 | ice_file: None, |
Chris Wailes | 951ae7a | 2023-12-07 10:11:13 -0800 | [diff] [blame^] | 295 | expanded_args, |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 296 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 297 | } |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 298 | |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 299 | pub(crate) fn run_global_ctxt( |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 300 | tcx: TyCtxt<'_>, |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 301 | show_coverage: bool, |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 302 | render_options: RenderOptions, |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 303 | output_format: OutputFormat, |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 304 | ) -> (clean::Crate, RenderOptions, Cache) { |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 305 | // Certain queries assume that some checks were run elsewhere |
| 306 | // (see https://github.com/rust-lang/rust/pull/73566#issuecomment-656954425), |
| 307 | // so type-check everything other than function bodies in this crate before running lints. |
| 308 | |
| 309 | // NOTE: this does not call `tcx.analysis()` so that we won't |
| 310 | // typeck function bodies or run the default rustc lints. |
| 311 | // (see `override_queries` in the `config`) |
| 312 | |
| 313 | // HACK(jynelson) this calls an _extremely_ limited subset of `typeck` |
| 314 | // and might break if queries change their assumptions in the future. |
Charisee | 635618d | 2023-06-01 20:46:00 +0000 | [diff] [blame] | 315 | tcx.sess.time("type_collecting", || { |
| 316 | tcx.hir().for_each_module(|module| tcx.ensure().collect_mod_item_types(module)) |
| 317 | }); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 318 | |
| 319 | // NOTE: This is copy/pasted from typeck/lib.rs and should be kept in sync with those changes. |
| 320 | tcx.sess.time("item_types_checking", || { |
Chris Wailes | a153842 | 2021-12-02 10:37:12 -0800 | [diff] [blame] | 321 | tcx.hir().for_each_module(|module| tcx.ensure().check_mod_item_types(module)) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 322 | }); |
| 323 | tcx.sess.abort_if_errors(); |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 324 | tcx.sess.time("missing_docs", || rustc_lint::check_crate(tcx)); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 325 | tcx.sess.time("check_mod_attrs", || { |
Chris Wailes | a153842 | 2021-12-02 10:37:12 -0800 | [diff] [blame] | 326 | tcx.hir().for_each_module(|module| tcx.ensure().check_mod_attrs(module)) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 327 | }); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 328 | rustc_passes::stability::check_unused_or_stable_features(tcx); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 329 | |
Charisee | f7ad1c4 | 2023-01-30 22:46:42 +0000 | [diff] [blame] | 330 | let auto_traits = |
| 331 | tcx.all_traits().filter(|&trait_def_id| tcx.trait_is_auto(trait_def_id)).collect(); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 332 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 333 | let mut ctxt = DocContext { |
| 334 | tcx, |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 335 | param_env: ParamEnv::empty(), |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 336 | external_traits: Default::default(), |
| 337 | active_extern_traits: Default::default(), |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 338 | args: Default::default(), |
Chris Wailes | cd1aefd | 2023-07-13 13:36:21 -0700 | [diff] [blame] | 339 | current_type_aliases: Default::default(), |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 340 | impl_trait_bounds: Default::default(), |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 341 | generated_synthetics: Default::default(), |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 342 | auto_traits, |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 343 | cache: Cache::new(render_options.document_private, render_options.document_hidden), |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 344 | inlined: FxHashSet::default(), |
| 345 | output_format, |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 346 | render_options, |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 347 | show_coverage, |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 348 | }; |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 349 | |
Chris Wailes | 5c0824a | 2023-04-24 16:30:59 -0700 | [diff] [blame] | 350 | for cnum in tcx.crates(()) { |
| 351 | crate::visit_lib::lib_embargo_visit_item(&mut ctxt, cnum.as_def_id()); |
| 352 | } |
| 353 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 354 | // Small hack to force the Sized trait to be present. |
| 355 | // |
| 356 | // Note that in case of `#![no_core]`, the trait is not available. |
| 357 | if let Some(sized_trait_did) = ctxt.tcx.lang_items().sized_trait() { |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 358 | let sized_trait = build_external_trait(&mut ctxt, sized_trait_did); |
Charisee | f7ad1c4 | 2023-01-30 22:46:42 +0000 | [diff] [blame] | 359 | ctxt.external_traits.borrow_mut().insert(sized_trait_did, sized_trait); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 362 | debug!("crate: {:?}", tcx.hir().krate()); |
| 363 | |
| 364 | let mut krate = tcx.sess.time("clean_crate", || clean::krate(&mut ctxt)); |
| 365 | |
Chris Wailes | cd1aefd | 2023-07-13 13:36:21 -0700 | [diff] [blame] | 366 | if krate.module.doc_value().is_empty() { |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 367 | let help = format!( |
| 368 | "The following guide may be of use:\n\ |
| 369 | {}/rustdoc/how-to-write-documentation.html", |
| 370 | crate::DOC_RUST_LANG_ORG_CHANNEL |
| 371 | ); |
Chris Wailes | 32f7835 | 2021-07-20 14:04:55 -0700 | [diff] [blame] | 372 | tcx.struct_lint_node( |
| 373 | crate::lint::MISSING_CRATE_LEVEL_DOCS, |
Charisee | 9cf6780 | 2022-06-30 20:04:09 +0000 | [diff] [blame] | 374 | DocContext::as_local_hir_id(tcx, krate.module.item_id).unwrap(), |
Charisee | f7ad1c4 | 2023-01-30 22:46:42 +0000 | [diff] [blame] | 375 | "no documentation found for this crate's top-level module", |
| 376 | |lint| lint.help(help), |
Chris Wailes | 32f7835 | 2021-07-20 14:04:55 -0700 | [diff] [blame] | 377 | ); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 380 | fn report_deprecated_attr(name: &str, diag: &rustc_errors::Handler, sp: Span) { |
| 381 | let mut msg = |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 382 | diag.struct_span_warn(sp, format!("the `#![doc({name})]` attribute is deprecated")); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 383 | msg.note( |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 384 | "see issue #44136 <https://github.com/rust-lang/rust/issues/44136> \ |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 385 | for more information", |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 386 | ); |
| 387 | |
| 388 | if name == "no_default_passes" { |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 389 | msg.help("`#![doc(no_default_passes)]` no longer functions; you may want to use `#![doc(document_private_items)]`"); |
| 390 | } else if name.starts_with("passes") { |
| 391 | msg.help("`#![doc(passes = \"...\")]` no longer functions; you may want to use `#![doc(document_private_items)]`"); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 392 | } else if name.starts_with("plugins") { |
| 393 | msg.warn("`#![doc(plugins = \"...\")]` no longer functions; see CVE-2018-1000622 <https://nvd.nist.gov/vuln/detail/CVE-2018-1000622>"); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | msg.emit(); |
| 397 | } |
| 398 | |
| 399 | // Process all of the crate attributes, extracting plugin metadata along |
| 400 | // with the passes which we are supposed to run. |
Chris Wailes | 32f7835 | 2021-07-20 14:04:55 -0700 | [diff] [blame] | 401 | for attr in krate.module.attrs.lists(sym::doc) { |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 402 | let diag = ctxt.sess().diagnostic(); |
| 403 | |
| 404 | let name = attr.name_or_empty(); |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 405 | // `plugins = "..."`, `no_default_passes`, and `passes = "..."` have no effect |
| 406 | if attr.is_word() && name == sym::no_default_passes { |
| 407 | report_deprecated_attr("no_default_passes", diag, attr.span()); |
| 408 | } else if attr.value_str().is_some() { |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 409 | match name { |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 410 | sym::passes => { |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 411 | report_deprecated_attr("passes = \"...\"", diag, attr.span()); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 412 | } |
| 413 | sym::plugins => { |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 414 | report_deprecated_attr("plugins = \"...\"", diag, attr.span()); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 415 | } |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 416 | _ => (), |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
| 420 | if attr.is_word() && name == sym::document_private_items { |
| 421 | ctxt.render_options.document_private = true; |
| 422 | } |
| 423 | } |
| 424 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 425 | info!("Executing passes"); |
| 426 | |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 427 | for p in passes::defaults(show_coverage) { |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 428 | let run = match p.condition { |
| 429 | Always => true, |
| 430 | WhenDocumentPrivate => ctxt.render_options.document_private, |
| 431 | WhenNotDocumentPrivate => !ctxt.render_options.document_private, |
| 432 | WhenNotDocumentHidden => !ctxt.render_options.document_hidden, |
| 433 | }; |
| 434 | if run { |
| 435 | debug!("running pass {}", p.pass.name); |
Chris Wailes | 356b57e | 2022-01-13 10:08:24 -0800 | [diff] [blame] | 436 | krate = tcx.sess.time(p.pass.name, || (p.pass.run)(krate, &mut ctxt)); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Charisee | 9cf6780 | 2022-06-30 20:04:09 +0000 | [diff] [blame] | 440 | tcx.sess.time("check_lint_expectations", || tcx.check_expectations(Some(sym::rustdoc))); |
| 441 | |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 442 | if tcx.sess.diagnostic().has_errors_or_lint_errors().is_some() { |
Chris Wailes | 356b57e | 2022-01-13 10:08:24 -0800 | [diff] [blame] | 443 | rustc_errors::FatalError.raise(); |
| 444 | } |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 445 | |
Chris Wailes | 356b57e | 2022-01-13 10:08:24 -0800 | [diff] [blame] | 446 | krate = tcx.sess.time("create_format_cache", || Cache::populate(&mut ctxt, krate)); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 447 | |
Chris Wailes | 356b57e | 2022-01-13 10:08:24 -0800 | [diff] [blame] | 448 | (krate, ctxt.render_options, ctxt.cache) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 449 | } |
| 450 | |
Thiébaud Weksteen | 5bd94c1 | 2021-01-06 15:18:42 +0100 | [diff] [blame] | 451 | /// Due to <https://github.com/rust-lang/rust/pull/73566>, |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 452 | /// the name resolution pass may find errors that are never emitted. |
| 453 | /// If typeck is called after this happens, then we'll get an ICE: |
| 454 | /// 'Res::Error found but not reported'. To avoid this, emit the errors now. |
| 455 | struct EmitIgnoredResolutionErrors<'tcx> { |
| 456 | tcx: TyCtxt<'tcx>, |
| 457 | } |
| 458 | |
| 459 | impl<'tcx> EmitIgnoredResolutionErrors<'tcx> { |
| 460 | fn new(tcx: TyCtxt<'tcx>) -> Self { |
| 461 | Self { tcx } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | impl<'tcx> Visitor<'tcx> for EmitIgnoredResolutionErrors<'tcx> { |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 466 | type NestedFilter = nested_filter::OnlyBodies; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 467 | |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 468 | fn nested_visit_map(&mut self) -> Self::Map { |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 469 | // We need to recurse into nested closures, |
| 470 | // since those will fallback to the parent for type checking. |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 471 | self.tcx.hir() |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 472 | } |
| 473 | |
Chris Wailes | 977026a | 2023-02-13 09:13:10 -0800 | [diff] [blame] | 474 | fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) { |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 475 | debug!("visiting path {path:?}"); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 476 | if path.res == Res::Err { |
| 477 | // We have less context here than in rustc_resolve, |
| 478 | // so we can only emit the name and span. |
| 479 | // However we can give a hint that rustc_resolve will have more info. |
| 480 | let label = format!( |
| 481 | "could not resolve path `{}`", |
| 482 | path.segments |
| 483 | .iter() |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 484 | .map(|segment| segment.ident.as_str()) |
| 485 | .intersperse("::") |
| 486 | .collect::<String>() |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 487 | ); |
| 488 | let mut err = rustc_errors::struct_span_err!( |
| 489 | self.tcx.sess, |
| 490 | path.span, |
| 491 | E0433, |
Charisee | 8788044 | 2023-11-01 01:09:07 +0000 | [diff] [blame] | 492 | "failed to resolve: {label}", |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 493 | ); |
| 494 | err.span_label(path.span, label); |
| 495 | err.note("this error was originally ignored because you are running `rustdoc`"); |
| 496 | err.note("try running again with `rustc` or `cargo check` and you may get a more detailed error"); |
| 497 | err.emit(); |
| 498 | } |
| 499 | // We could have an outer resolution that succeeded, |
| 500 | // but with generic parameters that failed. |
| 501 | // Recurse into the segments so we catch those too. |
| 502 | intravisit::walk_path(self, path); |
| 503 | } |
| 504 | } |
| 505 | |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 506 | /// `DefId` or parameter index (`ty::ParamTy.index`) of a synthetic type parameter |
| 507 | /// for `impl Trait` in argument position. |
| 508 | #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 509 | pub(crate) enum ImplTraitParam { |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 510 | DefId(DefId), |
| 511 | ParamIndex(u32), |
| 512 | } |
| 513 | |
| 514 | impl From<DefId> for ImplTraitParam { |
| 515 | fn from(did: DefId) -> Self { |
| 516 | ImplTraitParam::DefId(did) |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | impl From<u32> for ImplTraitParam { |
| 521 | fn from(idx: u32) -> Self { |
| 522 | ImplTraitParam::ParamIndex(idx) |
| 523 | } |
| 524 | } |