2019-12-24 05:02:53 +01:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2020-10-31 03:14:32 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2022-10-27 13:23:26 +00:00
|
|
|
use rustc_data_structures::unord::UnordSet;
|
2020-10-31 03:14:32 +01:00
|
|
|
use rustc_errors::emitter::{DynEmitter, EmitterWriter};
|
2020-03-11 12:49:08 +01:00
|
|
|
use rustc_errors::json::JsonEmitter;
|
2023-02-09 10:16:00 +00:00
|
|
|
use rustc_errors::TerminalUrl;
|
2019-11-30 02:50:47 +01:00
|
|
|
use rustc_feature::UnstableFeatures;
|
2022-02-01 20:30:32 +08:00
|
|
|
use rustc_hir::def::Res;
|
2023-01-22 01:33:53 +04:00
|
|
|
use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LocalDefId};
|
2021-11-03 18:03:12 -05:00
|
|
|
use rustc_hir::intravisit::{self, Visitor};
|
2022-02-01 20:30:32 +08:00
|
|
|
use rustc_hir::{HirId, Path};
|
2021-08-29 21:41:46 +03:00
|
|
|
use rustc_interface::interface;
|
2023-07-15 16:43:45 +00:00
|
|
|
use rustc_lint::{late_lint_mod, MissingDoc};
|
2021-11-03 18:03:12 -05:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2020-10-18 11:27:16 -04:00
|
|
|
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
|
2023-02-06 21:57:45 +04:00
|
|
|
use rustc_session::config::{self, CrateType, ErrorOutputType, ResolveDocLinks};
|
2020-05-02 01:30:23 +03:00
|
|
|
use rustc_session::Session;
|
2023-06-23 05:56:09 +08:00
|
|
|
use rustc_session::{lint, EarlyErrorHandler};
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::sym;
|
2023-02-05 23:05:46 +04:00
|
|
|
use rustc_span::{source_map, Span};
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2020-12-29 23:16:16 -05:00
|
|
|
use std::cell::RefCell;
|
2016-09-01 10:21:12 +03:00
|
|
|
use std::mem;
|
2018-12-08 20:30:23 +01:00
|
|
|
use std::rc::Rc;
|
2022-06-16 19:39:39 +04:00
|
|
|
use std::sync::LazyLock;
|
2023-10-16 22:11:57 +02:00
|
|
|
use std::sync::{atomic::AtomicBool, Arc};
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2021-02-18 20:46:07 +01:00
|
|
|
use crate::clean::inline::build_external_trait;
|
2022-09-27 12:27:04 -07:00
|
|
|
use crate::clean::{self, ItemId};
|
2021-02-12 00:03:24 -05:00
|
|
|
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
|
2021-01-12 23:36:04 +01:00
|
|
|
use crate::formats::cache::Cache;
|
2021-12-13 23:49:29 -07:00
|
|
|
use crate::passes::{self, Condition::*};
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
pub(crate) use rustc_session::config::{Input, Options, UnstableOptions};
|
2015-01-17 21:23:05 -08:00
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) struct DocContext<'tcx> {
|
|
|
|
|
pub(crate) tcx: TyCtxt<'tcx>,
|
2020-10-18 11:27:16 -04:00
|
|
|
/// Used for normalization.
|
|
|
|
|
///
|
|
|
|
|
/// Most of this logic is copied from rustc_lint::late.
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) param_env: ParamEnv<'tcx>,
|
2021-01-12 23:36:04 +01:00
|
|
|
/// Later on moved through `clean::Crate` into `cache`
|
2022-09-27 12:27:04 -07:00
|
|
|
pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
|
2018-02-21 18:33:42 -06:00
|
|
|
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
|
|
|
|
|
/// the same time.
|
2023-01-22 01:33:53 +04:00
|
|
|
pub(crate) active_extern_traits: DefIdSet,
|
2021-10-30 21:42:17 -07:00
|
|
|
// The current set of parameter substitutions,
|
2016-09-01 10:21:12 +03:00
|
|
|
// for expanding type aliases at the HIR level:
|
2021-10-30 21:42:17 -07:00
|
|
|
/// Table `DefId` of type, lifetime, or const parameter -> substituted type, lifetime, or const
|
2023-07-11 22:35:29 +01:00
|
|
|
pub(crate) args: DefIdMap<clean::SubstParam>,
|
2023-04-20 18:08:06 -07:00
|
|
|
pub(crate) current_type_aliases: DefIdMap<usize>,
|
2019-06-21 12:23:05 +09:00
|
|
|
/// Table synthetic type parameter for `impl Trait` in argument position -> bounds
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) impl_trait_bounds: FxHashMap<ImplTraitParam, Vec<clean::GenericBound>>,
|
2019-04-22 22:52:51 +03:00
|
|
|
/// Auto-trait or blanket impls processed so far, as `(self_ty, trait_def_id)`.
|
|
|
|
|
// FIXME(eddyb) make this a `ty::TraitRef<'tcx>` set.
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) generated_synthetics: FxHashSet<(Ty<'tcx>, DefId)>,
|
|
|
|
|
pub(crate) auto_traits: Vec<DefId>,
|
2020-05-30 11:35:35 -04:00
|
|
|
/// The options given to rustdoc that could be relevant to a pass.
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) render_options: RenderOptions,
|
2021-02-11 21:29:22 -05:00
|
|
|
/// This same cache is used throughout rustdoc, including in [`crate::html::render`].
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) cache: Cache,
|
2021-02-11 21:29:22 -05:00
|
|
|
/// Used by [`clean::inline`] to tell if an item has already been inlined.
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) inlined: FxHashSet<ItemId>,
|
2021-02-11 21:29:22 -05:00
|
|
|
/// Used by `calculate_doc_coverage`.
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) output_format: OutputFormat,
|
2022-07-15 18:10:52 +02:00
|
|
|
/// Used by `strip_private`.
|
|
|
|
|
pub(crate) show_coverage: bool,
|
2014-03-05 16:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
2018-12-08 20:30:23 +01:00
|
|
|
impl<'tcx> DocContext<'tcx> {
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) fn sess(&self) -> &'tcx Session {
|
2021-10-01 17:12:39 +02:00
|
|
|
self.tcx.sess
|
2014-06-26 11:37:39 -07:00
|
|
|
}
|
2016-09-01 10:21:12 +03:00
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) fn with_param_env<T, F: FnOnce(&mut Self) -> T>(
|
|
|
|
|
&mut self,
|
|
|
|
|
def_id: DefId,
|
|
|
|
|
f: F,
|
|
|
|
|
) -> T {
|
2021-02-20 17:12:22 -08:00
|
|
|
let old_param_env = mem::replace(&mut self.param_env, self.tcx.param_env(def_id));
|
2021-02-12 01:59:20 -05:00
|
|
|
let ret = f(self);
|
2021-02-20 17:12:22 -08:00
|
|
|
self.param_env = old_param_env;
|
2020-10-18 11:27:16 -04:00
|
|
|
ret
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-01 10:21:12 +03:00
|
|
|
/// Call the closure with the given parameters set as
|
|
|
|
|
/// the substitutions for a type alias' RHS.
|
2023-04-20 18:08:06 -07:00
|
|
|
pub(crate) fn enter_alias<F, R>(
|
|
|
|
|
&mut self,
|
2023-07-11 22:35:29 +01:00
|
|
|
args: DefIdMap<clean::SubstParam>,
|
2023-04-20 18:08:06 -07:00
|
|
|
def_id: DefId,
|
|
|
|
|
f: F,
|
|
|
|
|
) -> R
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
2021-02-12 01:59:20 -05:00
|
|
|
F: FnOnce(&mut Self) -> R,
|
2019-12-22 17:42:04 -05:00
|
|
|
{
|
2023-07-11 22:35:29 +01:00
|
|
|
let old_args = mem::replace(&mut self.args, args);
|
2023-04-20 18:08:06 -07:00
|
|
|
*self.current_type_aliases.entry(def_id).or_insert(0) += 1;
|
2021-02-12 01:59:20 -05:00
|
|
|
let r = f(self);
|
2023-07-11 22:35:29 +01:00
|
|
|
self.args = old_args;
|
2023-04-20 18:08:06 -07:00
|
|
|
if let Some(count) = self.current_type_aliases.get_mut(&def_id) {
|
|
|
|
|
*count -= 1;
|
|
|
|
|
if *count == 0 {
|
|
|
|
|
self.current_type_aliases.remove(&def_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-01 10:21:12 +03:00
|
|
|
r
|
|
|
|
|
}
|
2018-08-03 22:13:05 +02:00
|
|
|
|
2020-08-12 12:22:56 +02:00
|
|
|
/// Like `hir().local_def_id_to_hir_id()`, but skips calling it on fake DefIds.
|
2018-09-19 09:28:49 -05:00
|
|
|
/// (This avoids a slice-index-out-of-bounds panic.)
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) fn as_local_hir_id(tcx: TyCtxt<'_>, item_id: ItemId) -> Option<HirId> {
|
2022-04-16 14:28:09 +02:00
|
|
|
match item_id {
|
2021-06-26 13:52:31 +02:00
|
|
|
ItemId::DefId(real_id) => {
|
2021-04-29 21:36:54 +02:00
|
|
|
real_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
|
|
|
|
|
}
|
2021-06-26 13:52:31 +02:00
|
|
|
// FIXME: Can this be `Some` for `Auto` or `Blanket`?
|
|
|
|
|
_ => None,
|
2019-03-04 09:00:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
2018-04-24 18:29:04 -05:00
|
|
|
/// Creates a new diagnostic `Handler` that can be used to emit warnings and errors.
|
|
|
|
|
///
|
2018-08-18 12:13:35 +02:00
|
|
|
/// If the given `error_format` is `ErrorOutputType::Json` and no `SourceMap` is given, a new one
|
2018-04-24 18:29:04 -05:00
|
|
|
/// will be created for the handler.
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) fn new_handler(
|
2019-12-22 17:42:04 -05:00
|
|
|
error_format: ErrorOutputType,
|
|
|
|
|
source_map: Option<Lrc<source_map::SourceMap>>,
|
2022-07-06 11:57:41 +01:00
|
|
|
diagnostic_width: Option<usize>,
|
2022-07-06 07:44:47 -05:00
|
|
|
unstable_opts: &UnstableOptions,
|
2020-01-09 11:18:47 +01:00
|
|
|
) -> rustc_errors::Handler {
|
2022-10-17 14:11:26 +01:00
|
|
|
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
|
|
|
|
|
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
|
|
|
|
|
false,
|
|
|
|
|
);
|
2020-10-31 03:14:32 +01:00
|
|
|
let emitter: Box<DynEmitter> = match error_format {
|
2019-03-25 11:16:58 +01:00
|
|
|
ErrorOutputType::HumanReadable(kind) => {
|
|
|
|
|
let (short, color_config) = kind.unzip();
|
|
|
|
|
Box::new(
|
2023-07-25 13:09:53 +00:00
|
|
|
EmitterWriter::stderr(color_config, fallback_bundle)
|
|
|
|
|
.sm(source_map.map(|sm| sm as _))
|
|
|
|
|
.short_message(short)
|
|
|
|
|
.teach(unstable_opts.teach)
|
|
|
|
|
.diagnostic_width(diagnostic_width)
|
|
|
|
|
.track_diagnostics(unstable_opts.track_diagnostics)
|
|
|
|
|
.ui_testing(unstable_opts.ui_testing),
|
2019-03-25 11:16:58 +01:00
|
|
|
)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-03-25 11:16:58 +01:00
|
|
|
ErrorOutputType::Json { pretty, json_rendered } => {
|
2019-12-22 17:42:04 -05:00
|
|
|
let source_map = source_map.unwrap_or_else(|| {
|
2019-12-29 23:07:23 +03:00
|
|
|
Lrc::new(source_map::SourceMap::new(source_map::FilePathMapping::empty()))
|
2019-12-22 17:42:04 -05:00
|
|
|
});
|
2018-04-24 18:29:04 -05:00
|
|
|
Box::new(
|
2020-06-26 13:18:16 +01:00
|
|
|
JsonEmitter::stderr(
|
|
|
|
|
None,
|
|
|
|
|
source_map,
|
2022-03-28 09:36:20 +01:00
|
|
|
None,
|
2022-03-26 07:27:43 +00:00
|
|
|
fallback_bundle,
|
2020-06-26 13:18:16 +01:00
|
|
|
pretty,
|
|
|
|
|
json_rendered,
|
2022-07-06 11:57:41 +01:00
|
|
|
diagnostic_width,
|
2020-06-26 13:18:16 +01:00
|
|
|
false,
|
2022-10-24 20:52:51 +02:00
|
|
|
unstable_opts.track_diagnostics,
|
2023-02-09 10:16:00 +00:00
|
|
|
TerminalUrl::No,
|
2020-06-26 13:18:16 +01:00
|
|
|
)
|
2022-07-06 07:44:47 -05:00
|
|
|
.ui_testing(unstable_opts.ui_testing),
|
2018-04-24 18:29:04 -05:00
|
|
|
)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2018-04-24 18:29:04 -05:00
|
|
|
};
|
|
|
|
|
|
2023-07-25 10:27:34 +00:00
|
|
|
rustc_errors::Handler::with_emitter(emitter)
|
|
|
|
|
.with_flags(unstable_opts.diagnostic_handler_flags(true))
|
2018-04-24 18:29:04 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-11 21:37:14 -05:00
|
|
|
/// Parse, resolve, and typecheck the given crate.
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) fn create_config(
|
2023-06-23 05:56:09 +08:00
|
|
|
handler: &EarlyErrorHandler,
|
2020-12-11 21:37:14 -05:00
|
|
|
RustdocOptions {
|
2018-10-30 13:35:10 -05:00
|
|
|
input,
|
|
|
|
|
crate_name,
|
2019-07-20 16:34:41 -04:00
|
|
|
proc_macro_crate,
|
2018-10-30 13:35:10 -05:00
|
|
|
error_format,
|
2022-07-06 11:57:41 +01:00
|
|
|
diagnostic_width,
|
2018-10-30 13:35:10 -05:00
|
|
|
libs,
|
|
|
|
|
externs,
|
2019-09-25 17:08:40 +02:00
|
|
|
mut cfgs,
|
2022-02-19 14:31:20 +01:00
|
|
|
check_cfgs,
|
2018-10-30 13:35:10 -05:00
|
|
|
codegen_options,
|
2022-07-06 07:44:47 -05:00
|
|
|
unstable_opts,
|
2018-10-30 13:35:10 -05:00
|
|
|
target,
|
|
|
|
|
edition,
|
|
|
|
|
maybe_sysroot,
|
|
|
|
|
lint_opts,
|
|
|
|
|
describe_lints,
|
|
|
|
|
lint_cap,
|
2022-02-11 21:48:59 -08:00
|
|
|
scrape_examples_options,
|
2023-07-03 13:11:27 +02:00
|
|
|
expanded_args,
|
2018-10-30 13:35:10 -05:00
|
|
|
..
|
2020-12-11 21:37:14 -05:00
|
|
|
}: RustdocOptions,
|
2023-02-06 21:57:45 +04:00
|
|
|
RenderOptions { document_private, .. }: &RenderOptions,
|
2023-10-16 22:11:57 +02:00
|
|
|
using_internal_features: Arc<AtomicBool>,
|
2020-12-11 21:37:14 -05:00
|
|
|
) -> rustc_interface::Config {
|
2019-11-06 14:48:10 +01:00
|
|
|
// Add the doc cfg into the doc build.
|
2019-11-06 18:32:51 +01:00
|
|
|
cfgs.push("doc".to_string());
|
2019-09-25 17:08:40 +02:00
|
|
|
|
2018-10-30 13:35:10 -05:00
|
|
|
let input = Input::File(input);
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2020-12-30 14:11:15 -05:00
|
|
|
// By default, rustdoc ignores all lints.
|
|
|
|
|
// Specifically unblock lints relevant to documentation or the lint machinery itself.
|
2020-12-29 23:16:16 -05:00
|
|
|
let mut lints_to_show = vec![
|
2021-05-19 23:13:26 -04:00
|
|
|
// it's unclear whether these should be part of rustdoc directly (#77364)
|
2020-12-29 23:16:16 -05:00
|
|
|
rustc_lint::builtin::MISSING_DOCS.name.to_string(),
|
2021-05-19 23:13:26 -04:00
|
|
|
rustc_lint::builtin::INVALID_DOC_ATTRIBUTES.name.to_string(),
|
2020-12-29 23:16:16 -05:00
|
|
|
// these are definitely not part of rustdoc, but we want to warn on them anyway.
|
|
|
|
|
rustc_lint::builtin::RENAMED_AND_REMOVED_LINTS.name.to_string(),
|
|
|
|
|
rustc_lint::builtin::UNKNOWN_LINTS.name.to_string(),
|
2022-02-19 14:31:20 +01:00
|
|
|
rustc_lint::builtin::UNEXPECTED_CFGS.name.to_string(),
|
2022-03-31 22:36:12 +02:00
|
|
|
// this lint is needed to support `#[expect]` attributes
|
|
|
|
|
rustc_lint::builtin::UNFULFILLED_LINT_EXPECTATIONS.name.to_string(),
|
2019-12-22 17:42:04 -05:00
|
|
|
];
|
2020-12-30 14:11:15 -05:00
|
|
|
lints_to_show.extend(crate::lint::RUSTDOC_LINTS.iter().map(|lint| lint.name.to_string()));
|
2018-07-11 00:36:31 +02:00
|
|
|
|
2020-12-30 14:11:15 -05:00
|
|
|
let (lint_opts, lint_caps) = crate::lint::init_lints(lints_to_show, lint_opts, |lint| {
|
2021-04-22 23:40:43 -04:00
|
|
|
Some((lint.name_lower(), lint::Allow))
|
2020-04-26 14:07:13 +02:00
|
|
|
});
|
2014-06-04 14:35:58 -07:00
|
|
|
|
2020-05-02 01:30:23 +03:00
|
|
|
let crate_types =
|
|
|
|
|
if proc_macro_crate { vec![CrateType::ProcMacro] } else { vec![CrateType::Rlib] };
|
2023-02-11 19:14:25 +04:00
|
|
|
let resolve_doc_links =
|
|
|
|
|
if *document_private { ResolveDocLinks::All } else { ResolveDocLinks::Exported };
|
2022-02-11 21:48:59 -08:00
|
|
|
let test = scrape_examples_options.map(|opts| opts.scrape_tests).unwrap_or(false);
|
2018-04-01 00:09:00 +02:00
|
|
|
// plays with error output here!
|
2014-05-18 16:56:13 +03:00
|
|
|
let sessopts = config::Options {
|
2017-08-06 22:54:09 -07:00
|
|
|
maybe_sysroot,
|
2018-10-30 13:35:10 -05:00
|
|
|
search_paths: libs,
|
2019-07-20 16:34:41 -04:00
|
|
|
crate_types,
|
2021-09-09 16:52:19 +02:00
|
|
|
lint_opts,
|
2020-08-25 09:18:57 -04:00
|
|
|
lint_cap,
|
2018-10-30 13:35:10 -05:00
|
|
|
cg: codegen_options,
|
2017-08-06 22:54:09 -07:00
|
|
|
externs,
|
2019-06-11 11:06:34 -07:00
|
|
|
target_triple: target,
|
2020-10-10 14:27:52 -04:00
|
|
|
unstable_features: UnstableFeatures::from_environment(crate_name.as_deref()),
|
2016-09-29 19:10:29 -07:00
|
|
|
actually_rustdoc: true,
|
2023-02-06 21:57:45 +04:00
|
|
|
resolve_doc_links,
|
2022-07-06 07:44:47 -05:00
|
|
|
unstable_opts,
|
2018-04-01 00:09:00 +02:00
|
|
|
error_format,
|
2022-07-06 11:57:41 +01:00
|
|
|
diagnostic_width,
|
2018-04-20 14:47:23 -07:00
|
|
|
edition,
|
2018-06-23 15:09:21 +02:00
|
|
|
describe_lints,
|
2020-11-30 22:54:20 -05:00
|
|
|
crate_name,
|
2022-02-11 21:48:59 -08:00
|
|
|
test,
|
2018-07-26 12:36:11 -06:00
|
|
|
..Options::default()
|
2013-08-15 16:28:54 -04:00
|
|
|
};
|
2018-07-01 00:27:44 +02:00
|
|
|
|
2020-12-11 21:37:14 -05:00
|
|
|
interface::Config {
|
2018-12-08 20:30:23 +01:00
|
|
|
opts: sessopts,
|
Clean up config mess.
`parse_cfgspecs` and `parse_check_cfg` run very early, before the main
interner is running. They each use a short-lived interner and convert
all interned symbols to strings in their output data structures. Once
the main interner starts up, these data structures get converted into
new data structures that are identical except with the strings converted
to symbols.
All is not obvious from the current code, which is a mess, particularly
with inconsistent naming that obscures the parallel string/symbol data
structures. This commit clean things up a lot.
- The existing `CheckCfg` type is generic, allowing both
`CheckCfg<String>` and `CheckCfg<Symbol>` forms. This is really
useful, but it defaults to `String`. The commit removes the default so
we have to use `CheckCfg<String>` and `CheckCfg<Symbol>` explicitly,
which makes things clearer.
- Introduces `Cfg`, which is generic over `String` and `Symbol`, similar
to `CheckCfg`.
- Renames some things.
- `parse_cfgspecs` -> `parse_cfg`
- `CfgSpecs` -> `Cfg<String>`, plus it's used in more places, rather
than the underlying `FxHashSet` type.
- `CrateConfig` -> `Cfg<Symbol>`.
- `CrateCheckConfig` -> `CheckCfg<Symbol>`
- Adds some comments explaining the string-to-symbol conversions.
- `to_crate_check_config`, which converts `CheckCfg<String>` to
`CheckCfg<Symbol>`, is inlined and removed and combined with the
overly-general `CheckCfg::map_data` to produce
`CheckCfg::<String>::intern`.
- `build_configuration` now does the `Cfg<String>`-to-`Cfg<Symbol>`
conversion, so callers don't need to, which removes the need for
`to_crate_config`.
The diff for two of the fields in `Config` is a good example of the
improved clarity:
```
- pub crate_cfg: FxHashSet<(String, Option<String>)>,
- pub crate_check_cfg: CheckCfg,
+ pub crate_cfg: Cfg<String>,
+ pub crate_check_cfg: CheckCfg<String>,
```
Compare that with the diff for the corresponding fields in `ParseSess`,
and the relationship to `Config` is much clearer than before:
```
- pub config: CrateConfig,
- pub check_config: CrateCheckConfig,
+ pub config: Cfg<Symbol>,
+ pub check_config: CheckCfg<Symbol>,
```
2023-10-27 15:58:02 +11:00
|
|
|
crate_cfg: interface::parse_cfg(handler, cfgs),
|
2023-06-23 05:56:09 +08:00
|
|
|
crate_check_cfg: interface::parse_check_cfg(handler, check_cfgs),
|
2018-12-08 20:30:23 +01:00
|
|
|
input,
|
|
|
|
|
output_file: None,
|
|
|
|
|
output_dir: None,
|
|
|
|
|
file_loader: None,
|
2022-10-13 10:13:02 +01:00
|
|
|
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
|
2018-12-08 20:30:23 +01:00
|
|
|
lint_caps,
|
2021-03-15 17:57:53 +08:00
|
|
|
parse_sess_created: None,
|
2023-10-13 17:28:34 +00:00
|
|
|
hash_untracked_state: None,
|
2022-07-11 00:25:25 +02:00
|
|
|
register_lints: Some(Box::new(crate::lint::register_lints)),
|
2023-09-22 16:38:31 +00:00
|
|
|
override_queries: Some(|_sess, providers| {
|
2023-07-19 10:52:12 +00:00
|
|
|
// We do not register late module lints, so this only runs `MissingDoc`.
|
2020-07-11 00:28:42 -04:00
|
|
|
// Most lints will require typechecking, so just don't run them.
|
2023-07-15 16:43:45 +00:00
|
|
|
providers.lint_mod = |tcx, module_def_id| late_lint_mod(tcx, module_def_id, MissingDoc);
|
2020-07-17 08:47:04 +00:00
|
|
|
// hack so that `used_trait_imports` won't try to call typeck
|
2020-07-11 21:50:25 -04:00
|
|
|
providers.used_trait_imports = |_, _| {
|
2022-10-27 13:23:26 +00:00
|
|
|
static EMPTY_SET: LazyLock<UnordSet<LocalDefId>> = LazyLock::new(UnordSet::default);
|
2020-07-11 21:50:25 -04:00
|
|
|
&EMPTY_SET
|
|
|
|
|
};
|
2020-07-11 00:28:42 -04:00
|
|
|
// In case typeck does end up being called, don't ICE in case there were name resolution errors
|
2020-07-17 08:47:04 +00:00
|
|
|
providers.typeck = move |tcx, def_id| {
|
2020-07-09 09:13:59 -04:00
|
|
|
// Closures' tables come from their outermost function,
|
|
|
|
|
// as they are part of the same "inference environment".
|
2020-07-17 08:47:04 +00:00
|
|
|
// This avoids emitting errors for the parent twice (see similar code in `typeck_with_fallback`)
|
2021-11-06 20:01:35 +00:00
|
|
|
let typeck_root_def_id = tcx.typeck_root_def_id(def_id.to_def_id()).expect_local();
|
|
|
|
|
if typeck_root_def_id != def_id {
|
|
|
|
|
return tcx.typeck(typeck_root_def_id);
|
2020-07-09 09:13:59 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-03 18:41:23 -04:00
|
|
|
let hir = tcx.hir();
|
2022-07-15 23:13:04 -04:00
|
|
|
let body = hir.body(hir.body_owned_by(def_id));
|
2023-08-14 22:25:32 +02:00
|
|
|
debug!("visiting body for {def_id:?}");
|
2020-11-30 14:39:00 -05:00
|
|
|
EmitIgnoredResolutionErrors::new(tcx).visit_body(body);
|
2023-09-22 16:26:20 +00:00
|
|
|
(rustc_interface::DEFAULT_QUERY_PROVIDERS.typeck)(tcx, def_id)
|
2020-07-03 18:41:23 -04:00
|
|
|
};
|
2020-06-20 16:41:39 -04:00
|
|
|
}),
|
2020-09-08 13:44:41 +02:00
|
|
|
make_codegen_backend: None,
|
2019-11-15 19:41:50 +01:00
|
|
|
registry: rustc_driver::diagnostics_registry(),
|
2023-03-03 22:25:18 +00:00
|
|
|
ice_file: None,
|
2023-10-16 22:11:57 +02:00
|
|
|
using_internal_features,
|
2023-07-03 13:11:27 +02:00
|
|
|
expanded_args,
|
2020-12-11 21:37:14 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) fn run_global_ctxt(
|
2020-08-16 17:56:02 -04:00
|
|
|
tcx: TyCtxt<'_>,
|
2021-12-13 23:49:29 -07:00
|
|
|
show_coverage: bool,
|
2020-08-16 17:56:02 -04:00
|
|
|
render_options: RenderOptions,
|
2021-01-28 18:00:07 -08:00
|
|
|
output_format: OutputFormat,
|
2021-02-11 21:29:22 -05:00
|
|
|
) -> (clean::Crate, RenderOptions, Cache) {
|
2020-08-23 22:57:47 -04:00
|
|
|
// Certain queries assume that some checks were run elsewhere
|
|
|
|
|
// (see https://github.com/rust-lang/rust/pull/73566#issuecomment-656954425),
|
|
|
|
|
// so type-check everything other than function bodies in this crate before running lints.
|
|
|
|
|
|
|
|
|
|
// NOTE: this does not call `tcx.analysis()` so that we won't
|
|
|
|
|
// typeck function bodies or run the default rustc lints.
|
|
|
|
|
// (see `override_queries` in the `config`)
|
|
|
|
|
|
|
|
|
|
// HACK(jynelson) this calls an _extremely_ limited subset of `typeck`
|
|
|
|
|
// and might break if queries change their assumptions in the future.
|
2023-02-28 19:13:21 +01:00
|
|
|
tcx.sess.time("type_collecting", || {
|
|
|
|
|
tcx.hir().for_each_module(|module| tcx.ensure().collect_mod_item_types(module))
|
|
|
|
|
});
|
2020-08-23 22:57:47 -04:00
|
|
|
|
|
|
|
|
// NOTE: This is copy/pasted from typeck/lib.rs and should be kept in sync with those changes.
|
|
|
|
|
tcx.sess.time("item_types_checking", || {
|
2021-07-18 18:12:17 +02:00
|
|
|
tcx.hir().for_each_module(|module| tcx.ensure().check_mod_item_types(module))
|
2020-08-23 22:57:47 -04:00
|
|
|
});
|
|
|
|
|
tcx.sess.abort_if_errors();
|
2023-07-15 16:43:45 +00:00
|
|
|
tcx.sess.time("missing_docs", || rustc_lint::check_crate(tcx));
|
2020-08-16 17:40:47 -04:00
|
|
|
tcx.sess.time("check_mod_attrs", || {
|
2021-07-18 18:12:17 +02:00
|
|
|
tcx.hir().for_each_module(|module| tcx.ensure().check_mod_attrs(module))
|
2020-08-16 17:40:47 -04:00
|
|
|
});
|
2021-02-27 22:02:41 +01:00
|
|
|
rustc_passes::stability::check_unused_or_stable_features(tcx);
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2022-10-18 15:49:04 +04:00
|
|
|
let auto_traits =
|
|
|
|
|
tcx.all_traits().filter(|&trait_def_id| tcx.trait_is_auto(trait_def_id)).collect();
|
2020-08-23 22:57:47 -04:00
|
|
|
|
|
|
|
|
let mut ctxt = DocContext {
|
|
|
|
|
tcx,
|
2021-02-20 17:12:22 -08:00
|
|
|
param_env: ParamEnv::empty(),
|
2020-08-23 22:57:47 -04:00
|
|
|
external_traits: Default::default(),
|
|
|
|
|
active_extern_traits: Default::default(),
|
2023-07-11 22:35:29 +01:00
|
|
|
args: Default::default(),
|
2023-04-20 18:08:06 -07:00
|
|
|
current_type_aliases: Default::default(),
|
2020-08-23 22:57:47 -04:00
|
|
|
impl_trait_bounds: Default::default(),
|
|
|
|
|
generated_synthetics: Default::default(),
|
2021-08-29 21:41:46 +03:00
|
|
|
auto_traits,
|
2023-07-14 16:44:41 +02:00
|
|
|
cache: Cache::new(render_options.document_private, render_options.document_hidden),
|
2021-02-11 21:29:22 -05:00
|
|
|
inlined: FxHashSet::default(),
|
|
|
|
|
output_format,
|
|
|
|
|
render_options,
|
2022-07-15 18:10:52 +02:00
|
|
|
show_coverage,
|
2020-08-23 22:57:47 -04:00
|
|
|
};
|
2021-02-18 20:46:07 +01:00
|
|
|
|
2023-02-05 23:05:46 +04:00
|
|
|
for cnum in tcx.crates(()) {
|
|
|
|
|
crate::visit_lib::lib_embargo_visit_item(&mut ctxt, cnum.as_def_id());
|
|
|
|
|
}
|
2023-01-17 01:09:23 +04:00
|
|
|
|
2021-02-18 20:46:07 +01:00
|
|
|
// Small hack to force the Sized trait to be present.
|
|
|
|
|
//
|
|
|
|
|
// Note that in case of `#![no_core]`, the trait is not available.
|
|
|
|
|
if let Some(sized_trait_did) = ctxt.tcx.lang_items().sized_trait() {
|
2022-07-22 14:26:05 +02:00
|
|
|
let sized_trait = build_external_trait(&mut ctxt, sized_trait_did);
|
2022-09-27 12:27:04 -07:00
|
|
|
ctxt.external_traits.borrow_mut().insert(sized_trait_did, sized_trait);
|
2021-02-18 20:46:07 +01:00
|
|
|
}
|
|
|
|
|
|
2020-08-23 22:57:47 -04:00
|
|
|
debug!("crate: {:?}", tcx.hir().krate());
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2020-08-18 16:49:37 -04:00
|
|
|
let mut krate = tcx.sess.time("clean_crate", || clean::krate(&mut ctxt));
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2023-05-12 20:34:24 +03:00
|
|
|
if krate.module.doc_value().is_empty() {
|
2021-05-04 23:36:33 -04:00
|
|
|
let help = format!(
|
|
|
|
|
"The following guide may be of use:\n\
|
|
|
|
|
{}/rustdoc/how-to-write-documentation.html",
|
|
|
|
|
crate::DOC_RUST_LANG_ORG_CHANNEL
|
|
|
|
|
);
|
2021-03-23 11:04:09 -07:00
|
|
|
tcx.struct_lint_node(
|
|
|
|
|
crate::lint::MISSING_CRATE_LEVEL_DOCS,
|
2022-04-16 14:28:09 +02:00
|
|
|
DocContext::as_local_hir_id(tcx, krate.module.item_id).unwrap(),
|
2022-09-16 19:07:20 +04:00
|
|
|
"no documentation found for this crate's top-level module",
|
|
|
|
|
|lint| lint.help(help),
|
2021-03-23 11:04:09 -07:00
|
|
|
);
|
2020-08-23 22:57:47 -04:00
|
|
|
}
|
2019-12-01 20:12:49 +01:00
|
|
|
|
2021-02-28 22:41:05 -05:00
|
|
|
fn report_deprecated_attr(name: &str, diag: &rustc_errors::Handler, sp: Span) {
|
|
|
|
|
let mut msg =
|
2023-08-14 22:25:32 +02:00
|
|
|
diag.struct_span_warn(sp, format!("the `#![doc({name})]` attribute is deprecated"));
|
2021-02-28 22:41:05 -05:00
|
|
|
msg.note(
|
2020-08-23 22:57:47 -04:00
|
|
|
"see issue #44136 <https://github.com/rust-lang/rust/issues/44136> \
|
2021-12-13 23:49:29 -07:00
|
|
|
for more information",
|
2020-08-23 22:57:47 -04:00
|
|
|
);
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2020-08-23 22:57:47 -04:00
|
|
|
if name == "no_default_passes" {
|
2021-12-13 23:49:29 -07:00
|
|
|
msg.help("`#![doc(no_default_passes)]` no longer functions; you may want to use `#![doc(document_private_items)]`");
|
|
|
|
|
} else if name.starts_with("passes") {
|
|
|
|
|
msg.help("`#![doc(passes = \"...\")]` no longer functions; you may want to use `#![doc(document_private_items)]`");
|
2021-02-28 22:51:35 -05:00
|
|
|
} else if name.starts_with("plugins") {
|
|
|
|
|
msg.warn("`#![doc(plugins = \"...\")]` no longer functions; see CVE-2018-1000622 <https://nvd.nist.gov/vuln/detail/CVE-2018-1000622>");
|
2020-08-23 22:57:47 -04:00
|
|
|
}
|
2018-07-27 10:22:16 -05:00
|
|
|
|
2020-08-23 22:57:47 -04:00
|
|
|
msg.emit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process all of the crate attributes, extracting plugin metadata along
|
|
|
|
|
// with the passes which we are supposed to run.
|
2021-03-23 11:04:09 -07:00
|
|
|
for attr in krate.module.attrs.lists(sym::doc) {
|
2020-08-23 22:57:47 -04:00
|
|
|
let diag = ctxt.sess().diagnostic();
|
|
|
|
|
|
|
|
|
|
let name = attr.name_or_empty();
|
2021-12-13 23:49:29 -07:00
|
|
|
// `plugins = "..."`, `no_default_passes`, and `passes = "..."` have no effect
|
|
|
|
|
if attr.is_word() && name == sym::no_default_passes {
|
|
|
|
|
report_deprecated_attr("no_default_passes", diag, attr.span());
|
|
|
|
|
} else if attr.value_str().is_some() {
|
2021-02-28 22:41:05 -05:00
|
|
|
match name {
|
2020-08-23 22:57:47 -04:00
|
|
|
sym::passes => {
|
2021-02-28 22:41:05 -05:00
|
|
|
report_deprecated_attr("passes = \"...\"", diag, attr.span());
|
2018-07-27 10:22:16 -05:00
|
|
|
}
|
2020-08-23 22:57:47 -04:00
|
|
|
sym::plugins => {
|
2021-02-28 22:41:05 -05:00
|
|
|
report_deprecated_attr("plugins = \"...\"", diag, attr.span());
|
2020-08-23 22:57:47 -04:00
|
|
|
}
|
2021-12-13 23:49:29 -07:00
|
|
|
_ => (),
|
2020-08-23 22:57:47 -04:00
|
|
|
}
|
|
|
|
|
}
|
2018-07-27 10:22:16 -05:00
|
|
|
|
2020-08-23 22:57:47 -04:00
|
|
|
if attr.is_word() && name == sym::document_private_items {
|
|
|
|
|
ctxt.render_options.document_private = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-27 10:22:16 -05:00
|
|
|
|
2020-08-23 22:57:47 -04:00
|
|
|
info!("Executing passes");
|
|
|
|
|
|
2021-12-13 23:49:29 -07:00
|
|
|
for p in passes::defaults(show_coverage) {
|
2020-08-23 22:57:47 -04:00
|
|
|
let run = match p.condition {
|
|
|
|
|
Always => true,
|
|
|
|
|
WhenDocumentPrivate => ctxt.render_options.document_private,
|
|
|
|
|
WhenNotDocumentPrivate => !ctxt.render_options.document_private,
|
|
|
|
|
WhenNotDocumentHidden => !ctxt.render_options.document_hidden,
|
|
|
|
|
};
|
|
|
|
|
if run {
|
|
|
|
|
debug!("running pass {}", p.pass.name);
|
2021-09-16 05:35:23 +00:00
|
|
|
krate = tcx.sess.time(p.pass.name, || (p.pass.run)(krate, &mut ctxt));
|
2020-08-23 22:57:47 -04:00
|
|
|
}
|
|
|
|
|
}
|
2018-07-27 10:22:16 -05:00
|
|
|
|
2022-03-31 22:36:12 +02:00
|
|
|
tcx.sess.time("check_lint_expectations", || tcx.check_expectations(Some(sym::rustdoc)));
|
|
|
|
|
|
2022-01-22 18:49:12 -06:00
|
|
|
if tcx.sess.diagnostic().has_errors_or_lint_errors().is_some() {
|
2021-09-16 05:35:23 +00:00
|
|
|
rustc_errors::FatalError.raise();
|
|
|
|
|
}
|
2018-07-28 00:06:51 -05:00
|
|
|
|
2021-11-19 17:51:45 -05:00
|
|
|
krate = tcx.sess.time("create_format_cache", || Cache::populate(&mut ctxt, krate));
|
2021-02-11 21:29:22 -05:00
|
|
|
|
2021-11-19 17:51:45 -05:00
|
|
|
(krate, ctxt.render_options, ctxt.cache)
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
2019-06-21 12:23:05 +09:00
|
|
|
|
2020-11-05 14:33:23 +01:00
|
|
|
/// Due to <https://github.com/rust-lang/rust/pull/73566>,
|
2020-07-03 18:41:23 -04:00
|
|
|
/// the name resolution pass may find errors that are never emitted.
|
|
|
|
|
/// If typeck is called after this happens, then we'll get an ICE:
|
|
|
|
|
/// 'Res::Error found but not reported'. To avoid this, emit the errors now.
|
2020-07-10 18:07:31 -04:00
|
|
|
struct EmitIgnoredResolutionErrors<'tcx> {
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-07-03 18:41:23 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-10 18:07:31 -04:00
|
|
|
impl<'tcx> EmitIgnoredResolutionErrors<'tcx> {
|
|
|
|
|
fn new(tcx: TyCtxt<'tcx>) -> Self {
|
2020-07-10 17:59:29 -04:00
|
|
|
Self { tcx }
|
2020-07-03 18:41:23 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 18:07:31 -04:00
|
|
|
impl<'tcx> Visitor<'tcx> for EmitIgnoredResolutionErrors<'tcx> {
|
2021-11-03 18:03:12 -05:00
|
|
|
type NestedFilter = nested_filter::OnlyBodies;
|
2020-07-03 18:41:23 -04:00
|
|
|
|
2021-11-03 18:03:12 -05:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
2020-07-09 09:13:59 -04:00
|
|
|
// We need to recurse into nested closures,
|
|
|
|
|
// since those will fallback to the parent for type checking.
|
2021-11-03 18:03:12 -05:00
|
|
|
self.tcx.hir()
|
2020-07-03 18:41:23 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-25 11:26:36 +03:00
|
|
|
fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) {
|
2023-08-14 22:25:32 +02:00
|
|
|
debug!("visiting path {path:?}");
|
2020-07-03 18:41:23 -04:00
|
|
|
if path.res == Res::Err {
|
|
|
|
|
// We have less context here than in rustc_resolve,
|
|
|
|
|
// so we can only emit the name and span.
|
|
|
|
|
// However we can give a hint that rustc_resolve will have more info.
|
|
|
|
|
let label = format!(
|
|
|
|
|
"could not resolve path `{}`",
|
|
|
|
|
path.segments
|
|
|
|
|
.iter()
|
2022-01-15 18:06:45 -05:00
|
|
|
.map(|segment| segment.ident.as_str())
|
|
|
|
|
.intersperse("::")
|
|
|
|
|
.collect::<String>()
|
2020-07-03 18:41:23 -04:00
|
|
|
);
|
|
|
|
|
let mut err = rustc_errors::struct_span_err!(
|
2020-07-10 17:59:29 -04:00
|
|
|
self.tcx.sess,
|
2020-07-03 18:41:23 -04:00
|
|
|
path.span,
|
|
|
|
|
E0433,
|
2023-08-14 22:25:32 +02:00
|
|
|
"failed to resolve: {label}",
|
2020-07-03 18:41:23 -04:00
|
|
|
);
|
|
|
|
|
err.span_label(path.span, label);
|
|
|
|
|
err.note("this error was originally ignored because you are running `rustdoc`");
|
2020-07-10 17:51:38 -04:00
|
|
|
err.note("try running again with `rustc` or `cargo check` and you may get a more detailed error");
|
2020-07-03 18:41:23 -04:00
|
|
|
err.emit();
|
|
|
|
|
}
|
2020-07-10 17:50:03 -04:00
|
|
|
// We could have an outer resolution that succeeded,
|
|
|
|
|
// but with generic parameters that failed.
|
|
|
|
|
// Recurse into the segments so we catch those too.
|
|
|
|
|
intravisit::walk_path(self, path);
|
2020-07-03 18:41:23 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 12:23:05 +09:00
|
|
|
/// `DefId` or parameter index (`ty::ParamTy.index`) of a synthetic type parameter
|
|
|
|
|
/// for `impl Trait` in argument position.
|
2019-07-09 16:37:55 +09:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) enum ImplTraitParam {
|
2021-05-08 10:04:03 +02:00
|
|
|
DefId(DefId),
|
2019-06-21 12:23:05 +09:00
|
|
|
ParamIndex(u32),
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 10:04:03 +02:00
|
|
|
impl From<DefId> for ImplTraitParam {
|
|
|
|
|
fn from(did: DefId) -> Self {
|
2019-06-21 12:23:05 +09:00
|
|
|
ImplTraitParam::DefId(did)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<u32> for ImplTraitParam {
|
|
|
|
|
fn from(idx: u32) -> Self {
|
|
|
|
|
ImplTraitParam::ParamIndex(idx)
|
|
|
|
|
}
|
|
|
|
|
}
|