Skip to content

Commit

Permalink
Auto merge of rust-lang#80090 - jyn514:tcx-in-render, r=GuillaumeGomez
Browse files Browse the repository at this point in the history
Pass a `TyCtxt` through to `FormatRender`

This is the next step after rust-lang#79957 for rust-lang#76382. Eventually I plan to use this to remove `stability`, `const_stability`, and `deprecation` from `Item`, but that needs more extensive changes (in particular, rust-lang#75355 or something like it).

This has no actual changes to behavior, it's just moving types around.

ccc rust-lang#80014 (comment)
  • Loading branch information
bors committed Dec 18, 2020
2 parents fee693d + 0c2f76a commit d8d3ab9
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 179 deletions.
122 changes: 45 additions & 77 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_hir::{
intravisit::{self, NestedVisitorMap, Visitor},
Path,
};
use rustc_interface::interface;
use rustc_interface::{interface, Queries};
use rustc_middle::hir::map::Map;
use rustc_middle::middle::privacy::AccessLevels;
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
Expand Down Expand Up @@ -273,12 +273,9 @@ where
(lint_opts, lint_caps)
}

crate fn run_core(
options: RustdocOptions,
) -> (clean::Crate, RenderInfo, RenderOptions, Lrc<Session>) {
// Parse, resolve, and typecheck the given crate.

let RustdocOptions {
/// Parse, resolve, and typecheck the given crate.
crate fn create_config(
RustdocOptions {
input,
crate_name,
proc_macro_crate,
Expand All @@ -294,21 +291,10 @@ crate fn run_core(
lint_opts,
describe_lints,
lint_cap,
default_passes,
manual_passes,
display_warnings,
render_options,
output_format,
..
} = options;

let extern_names: Vec<String> = externs
.iter()
.filter(|(_, entry)| entry.add_prelude)
.map(|(name, _)| name)
.cloned()
.collect();

}: RustdocOptions,
) -> rustc_interface::Config {
// Add the doc cfg into the doc build.
cfgs.push("doc".to_string());

Expand Down Expand Up @@ -374,7 +360,7 @@ crate fn run_core(
..Options::default()
};

let config = interface::Config {
interface::Config {
opts: sessopts,
crate_cfg: interface::parse_cfgspecs(cfgs),
input,
Expand Down Expand Up @@ -417,68 +403,50 @@ crate fn run_core(
}),
make_codegen_backend: None,
registry: rustc_driver::diagnostics_registry(),
};

interface::create_compiler_and_run(config, |compiler| {
compiler.enter(|queries| {
let sess = compiler.session();

// We need to hold on to the complete resolver, so we cause everything to be
// cloned for the analysis passes to use. Suboptimal, but necessary in the
// current architecture.
let resolver = {
let parts = abort_on_err(queries.expansion(), sess).peek();
let resolver = parts.1.borrow();

// Before we actually clone it, let's force all the extern'd crates to
// actually be loaded, just in case they're only referred to inside
// intra-doc-links
resolver.borrow_mut().access(|resolver| {
sess.time("load_extern_crates", || {
for extern_name in &extern_names {
debug!("loading extern crate {}", extern_name);
resolver
.resolve_str_path_error(
DUMMY_SP,
extern_name,
TypeNS,
LocalDefId { local_def_index: CRATE_DEF_INDEX }.to_def_id(),
)
.unwrap_or_else(|()| {
panic!("Unable to resolve external crate {}", extern_name)
});
}
});
});
}
}

// Now we're good to clone the resolver because everything should be loaded
resolver.clone()
};
crate fn create_resolver<'a>(
externs: config::Externs,
queries: &Queries<'a>,
sess: &Session,
) -> Rc<RefCell<interface::BoxedResolver>> {
let extern_names: Vec<String> = externs
.iter()
.filter(|(_, entry)| entry.add_prelude)
.map(|(name, _)| name)
.cloned()
.collect();

if sess.has_errors() {
sess.fatal("Compilation failed, aborting rustdoc");
let parts = abort_on_err(queries.expansion(), sess).peek();
let resolver = parts.1.borrow();

// Before we actually clone it, let's force all the extern'd crates to
// actually be loaded, just in case they're only referred to inside
// intra-doc-links
resolver.borrow_mut().access(|resolver| {
sess.time("load_extern_crates", || {
for extern_name in &extern_names {
debug!("loading extern crate {}", extern_name);
resolver
.resolve_str_path_error(
DUMMY_SP,
extern_name,
TypeNS,
LocalDefId { local_def_index: CRATE_DEF_INDEX }.to_def_id(),
)
.unwrap_or_else(|()| {
panic!("Unable to resolve external crate {}", extern_name)
});
}
});
});

let mut global_ctxt = abort_on_err(queries.global_ctxt(), sess).take();

let (krate, render_info, opts) = sess.time("run_global_ctxt", || {
global_ctxt.enter(|tcx| {
run_global_ctxt(
tcx,
resolver,
default_passes,
manual_passes,
render_options,
output_format,
)
})
});
(krate, render_info, opts, Lrc::clone(sess))
})
})
// Now we're good to clone the resolver because everything should be loaded
resolver.clone()
}

fn run_global_ctxt(
crate fn run_global_ctxt(
tcx: TyCtxt<'_>,
resolver: Rc<RefCell<interface::BoxedResolver>>,
mut default_passes: passes::DefaultPassOption,
Expand Down
13 changes: 6 additions & 7 deletions src/librustdoc/formats/renderer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::sync::Arc;

use rustc_data_structures::sync::Lrc;
use rustc_session::Session;
use rustc_middle::ty;
use rustc_span::edition::Edition;

use crate::clean;
Expand All @@ -12,7 +11,7 @@ use crate::formats::cache::{Cache, CACHE_KEY};
/// Allows for different backends to rustdoc to be used with the `run_format()` function. Each
/// backend renderer has hooks for initialization, documenting an item, entering and exiting a
/// module, and cleanup/finalizing output.
crate trait FormatRenderer: Clone {
crate trait FormatRenderer<'tcx>: Clone {
/// Sets up any state required for the renderer. When this is called the cache has already been
/// populated.
fn init(
Expand All @@ -21,7 +20,7 @@ crate trait FormatRenderer: Clone {
render_info: RenderInfo,
edition: Edition,
cache: &mut Cache,
sess: Lrc<Session>,
tcx: ty::TyCtxt<'tcx>,
) -> Result<(Self, clean::Crate), Error>;

/// Renders a single non-module item. This means no recursive sub-item rendering is required.
Expand All @@ -46,13 +45,13 @@ crate trait FormatRenderer: Clone {
}

/// Main method for rendering a crate.
crate fn run_format<T: FormatRenderer>(
crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
krate: clean::Crate,
options: RenderOptions,
render_info: RenderInfo,
diag: &rustc_errors::Handler,
edition: Edition,
sess: Lrc<Session>,
tcx: ty::TyCtxt<'tcx>,
) -> Result<(), Error> {
let (krate, mut cache) = Cache::from_krate(
render_info.clone(),
Expand All @@ -63,7 +62,7 @@ crate fn run_format<T: FormatRenderer>(
);

let (mut format_renderer, mut krate) =
T::init(krate, options, render_info, edition, &mut cache, sess)?;
T::init(krate, options, render_info, edition, &mut cache, tcx)?;

let cache = Arc::new(cache);
// Freeze the cache now that the index has been built. Put an Arc into TLS for future
Expand Down
Loading

0 comments on commit d8d3ab9

Please sign in to comment.