Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various cleanups around pre-TyCtxt queries and functions #106810

Merged
merged 9 commits into from
Jan 19, 2023
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl DebugContext {
.working_dir
.to_string_lossy(FileNameDisplayPreference::Remapped)
.into_owned();
let (name, file_info) = match tcx.sess.local_crate_source_file.clone() {
let (name, file_info) = match tcx.sess.local_crate_source_file() {
Some(path) => {
let name = path.to_string_lossy().into_owned();
(name, None)
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,10 +782,10 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
codegen_unit_name: &str,
debug_context: &CodegenUnitDebugContext<'ll, 'tcx>,
) -> &'ll DIDescriptor {
let mut name_in_debuginfo = match tcx.sess.local_crate_source_file {
Some(ref path) => path.clone(),
None => PathBuf::from(tcx.crate_name(LOCAL_CRATE).as_str()),
};
let mut name_in_debuginfo = tcx
.sess
.local_crate_source_file()
.unwrap_or_else(|| PathBuf::from(tcx.crate_name(LOCAL_CRATE).as_str()));

// To avoid breaking split DWARF, we need to ensure that each codegen unit
// has a unique `DW_AT_name`. This is because there's a remote chance that
Expand Down
110 changes: 32 additions & 78 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ fn run_compiler(
crate_cfg: cfg,
crate_check_cfg: check_cfg,
input: Input::File(PathBuf::new()),
input_path: None,
output_file: ofile,
output_dir: odir,
file_loader,
Expand All @@ -237,9 +236,8 @@ fn run_compiler(

match make_input(config.opts.error_format, &matches.free) {
Err(reported) => return Err(reported),
Ok(Some((input, input_file_path))) => {
Ok(Some(input)) => {
config.input = input;
config.input_path = input_file_path;

callbacks.config(&mut config);
}
Expand All @@ -261,14 +259,8 @@ fn run_compiler(
describe_lints(compiler.session(), &lint_store, registered_lints);
return;
}
let should_stop = print_crate_info(
&***compiler.codegen_backend(),
compiler.session(),
None,
compiler.output_dir(),
compiler.output_file(),
compiler.temps_dir(),
);
let should_stop =
print_crate_info(&***compiler.codegen_backend(), compiler.session(), false);

if should_stop == Compilation::Stop {
return;
Expand All @@ -290,18 +282,9 @@ fn run_compiler(

interface::run_compiler(config, |compiler| {
let sess = compiler.session();
let should_stop = print_crate_info(
&***compiler.codegen_backend(),
sess,
Some(compiler.input()),
compiler.output_dir(),
compiler.output_file(),
compiler.temps_dir(),
)
.and_then(|| {
list_metadata(sess, &*compiler.codegen_backend().metadata_loader(), compiler.input())
})
.and_then(|| try_process_rlink(sess, compiler));
let should_stop = print_crate_info(&***compiler.codegen_backend(), sess, true)
.and_then(|| list_metadata(sess, &*compiler.codegen_backend().metadata_loader()))
.and_then(|| try_process_rlink(sess, compiler));

if should_stop == Compilation::Stop {
return sess.compile_status();
Expand All @@ -315,24 +298,12 @@ fn run_compiler(
if ppm.needs_ast_map() {
let expanded_crate = queries.expansion()?.borrow().0.clone();
queries.global_ctxt()?.enter(|tcx| {
pretty::print_after_hir_lowering(
tcx,
compiler.input(),
&*expanded_crate,
*ppm,
compiler.output_file().as_deref(),
);
pretty::print_after_hir_lowering(tcx, &*expanded_crate, *ppm);
Ok(())
})?;
} else {
let krate = queries.parse()?.steal();
pretty::print_after_parsing(
sess,
compiler.input(),
&krate,
*ppm,
compiler.output_file().as_deref(),
);
pretty::print_after_parsing(sess, &krate, *ppm);
}
trace!("finished pretty-printing");
return early_exit();
Expand All @@ -357,21 +328,17 @@ fn run_compiler(
}
}

queries.expansion()?;
queries.global_ctxt()?;
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
bjorn3 marked this conversation as resolved.
Show resolved Hide resolved
if callbacks.after_expansion(compiler, queries) == Compilation::Stop {
return early_exit();
}

queries.prepare_outputs()?;

if sess.opts.output_types.contains_key(&OutputType::DepInfo)
&& sess.opts.output_types.len() == 1
{
return early_exit();
}

queries.global_ctxt()?;

if sess.opts.unstable_opts.no_analysis {
return early_exit();
}
Expand All @@ -384,9 +351,9 @@ fn run_compiler(
save::process_crate(
tcx,
crate_name,
compiler.input(),
&sess.io.input,
None,
DumpHandler::new(compiler.output_dir().as_deref(), crate_name),
DumpHandler::new(sess.io.output_dir.as_deref(), crate_name),
)
});
}
Expand Down Expand Up @@ -439,7 +406,7 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<PathBuf>)
fn make_input(
error_format: ErrorOutputType,
free_matches: &[String],
) -> Result<Option<(Input, Option<PathBuf>)>, ErrorGuaranteed> {
) -> Result<Option<Input>, ErrorGuaranteed> {
if free_matches.len() == 1 {
let ifile = &free_matches[0];
if ifile == "-" {
Expand All @@ -461,12 +428,12 @@ fn make_input(
let line = isize::from_str_radix(&line, 10)
.expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number");
let file_name = FileName::doc_test_source_code(PathBuf::from(path), line);
Ok(Some((Input::Str { name: file_name, input: src }, None)))
Ok(Some(Input::Str { name: file_name, input: src }))
} else {
Ok(Some((Input::Str { name: FileName::anon_source_code(&src), input: src }, None)))
Ok(Some(Input::Str { name: FileName::anon_source_code(&src), input: src }))
}
} else {
Ok(Some((Input::File(PathBuf::from(ifile)), Some(PathBuf::from(ifile)))))
Ok(Some(Input::File(PathBuf::from(ifile))))
}
} else {
Ok(None)
Expand Down Expand Up @@ -560,7 +527,7 @@ fn show_content_with_pager(content: &str) {

pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Compilation {
if sess.opts.unstable_opts.link_only {
if let Input::File(file) = compiler.input() {
if let Input::File(file) = &sess.io.input {
// FIXME: #![crate_type] and #![crate_name] support not implemented yet
sess.init_crate_types(collect_crate_types(sess, &[]));
let outputs = compiler.build_output_filenames(sess, &[]);
Expand Down Expand Up @@ -601,13 +568,9 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp
}
}

pub fn list_metadata(
sess: &Session,
metadata_loader: &dyn MetadataLoader,
input: &Input,
) -> Compilation {
pub fn list_metadata(sess: &Session, metadata_loader: &dyn MetadataLoader) -> Compilation {
if sess.opts.unstable_opts.ls {
match *input {
match sess.io.input {
Input::File(ref ifile) => {
let path = &(*ifile);
let mut v = Vec::new();
Expand All @@ -627,10 +590,7 @@ pub fn list_metadata(
fn print_crate_info(
codegen_backend: &dyn CodegenBackend,
sess: &Session,
input: Option<&Input>,
odir: &Option<PathBuf>,
ofile: &Option<PathBuf>,
temps_dir: &Option<PathBuf>,
parse_attrs: bool,
) -> Compilation {
use rustc_session::config::PrintRequest::*;
// NativeStaticLibs and LinkArgs are special - printed during linking
Expand All @@ -639,18 +599,17 @@ fn print_crate_info(
return Compilation::Continue;
}

let attrs = match input {
None => None,
Some(input) => {
let result = parse_crate_attrs(sess, input);
match result {
Ok(attrs) => Some(attrs),
Err(mut parse_error) => {
parse_error.emit();
return Compilation::Stop;
}
let attrs = if parse_attrs {
let result = parse_crate_attrs(sess);
match result {
Ok(attrs) => Some(attrs),
Err(mut parse_error) => {
parse_error.emit();
return Compilation::Stop;
}
}
} else {
None
};
for req in &sess.opts.prints {
match *req {
Expand All @@ -665,14 +624,9 @@ fn print_crate_info(
println!("{}", serde_json::to_string_pretty(&sess.target.to_json()).unwrap());
}
FileNames | CrateName => {
let input = input.unwrap_or_else(|| {
early_error(ErrorOutputType::default(), "no input file provided")
});
let attrs = attrs.as_ref().unwrap();
let t_outputs = rustc_interface::util::build_output_filenames(
input, odir, ofile, temps_dir, attrs, sess,
);
let id = rustc_session::output::find_crate_name(sess, attrs, input);
let t_outputs = rustc_interface::util::build_output_filenames(attrs, sess);
let id = rustc_session::output::find_crate_name(sess, attrs);
if *req == PrintRequest::CrateName {
println!("{id}");
continue;
Expand Down Expand Up @@ -1108,8 +1062,8 @@ pub fn handle_options(args: &[String]) -> Option<getopts::Matches> {
Some(matches)
}

fn parse_crate_attrs<'a>(sess: &'a Session, input: &Input) -> PResult<'a, ast::AttrVec> {
match input {
fn parse_crate_attrs<'a>(sess: &'a Session) -> PResult<'a, ast::AttrVec> {
match &sess.io.input {
Input::File(ifile) => rustc_parse::parse_crate_attrs_from_file(ifile, &sess.parse_sess),
Input::Str { name, input } => rustc_parse::parse_crate_attrs_from_source_str(
name.clone(),
Expand Down
45 changes: 14 additions & 31 deletions compiler/rustc_driver/src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ use rustc_hir_pretty as pprust_hir;
use rustc_middle::hir::map as hir_map;
use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty};
use rustc_middle::ty::{self, TyCtxt};
use rustc_session::config::{Input, PpAstTreeMode, PpHirMode, PpMode, PpSourceMode};
use rustc_session::config::{PpAstTreeMode, PpHirMode, PpMode, PpSourceMode};
use rustc_session::Session;
use rustc_span::symbol::Ident;
use rustc_span::FileName;

use std::cell::Cell;
use std::fmt::Write;
use std::path::Path;

pub use self::PpMode::*;
pub use self::PpSourceMode::*;
Expand Down Expand Up @@ -345,8 +344,8 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
}
}

fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
let src_name = input.source_name();
fn get_source(sess: &Session) -> (String, FileName) {
let src_name = sess.io.input.source_name();
let src = String::clone(
sess.source_map()
.get_source_file(&src_name)
Expand All @@ -358,8 +357,8 @@ fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
(src, src_name)
}

fn write_or_print(out: &str, ofile: Option<&Path>, sess: &Session) {
match ofile {
fn write_or_print(out: &str, sess: &Session) {
match &sess.io.output_file {
None => print!("{out}"),
Some(p) => {
if let Err(e) = std::fs::write(p, out) {
Expand All @@ -372,14 +371,8 @@ fn write_or_print(out: &str, ofile: Option<&Path>, sess: &Session) {
}
}

pub fn print_after_parsing(
sess: &Session,
input: &Input,
krate: &ast::Crate,
ppm: PpMode,
ofile: Option<&Path>,
) {
let (src, src_name) = get_source(input, sess);
pub fn print_after_parsing(sess: &Session, krate: &ast::Crate, ppm: PpMode) {
let (src, src_name) = get_source(sess);

let out = match ppm {
Source(s) => {
Expand Down Expand Up @@ -407,22 +400,16 @@ pub fn print_after_parsing(
_ => unreachable!(),
};

write_or_print(&out, ofile, sess);
write_or_print(&out, sess);
}

pub fn print_after_hir_lowering<'tcx>(
tcx: TyCtxt<'tcx>,
input: &Input,
krate: &ast::Crate,
ppm: PpMode,
ofile: Option<&Path>,
) {
pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, krate: &ast::Crate, ppm: PpMode) {
if ppm.needs_analysis() {
abort_on_err(print_with_analysis(tcx, ppm, ofile), tcx.sess);
abort_on_err(print_with_analysis(tcx, ppm), tcx.sess);
return;
}

let (src, src_name) = get_source(input, tcx.sess);
let (src, src_name) = get_source(tcx.sess);

let out = match ppm {
Source(s) => {
Expand Down Expand Up @@ -474,18 +461,14 @@ pub fn print_after_hir_lowering<'tcx>(
_ => unreachable!(),
};

write_or_print(&out, ofile, tcx.sess);
write_or_print(&out, tcx.sess);
}

// In an ideal world, this would be a public function called by the driver after
// analysis is performed. However, we want to call `phase_3_run_analysis_passes`
// with a different callback than the standard driver, so that isn't easy.
// Instead, we call that function ourselves.
fn print_with_analysis(
tcx: TyCtxt<'_>,
ppm: PpMode,
ofile: Option<&Path>,
) -> Result<(), ErrorGuaranteed> {
fn print_with_analysis(tcx: TyCtxt<'_>, ppm: PpMode) -> Result<(), ErrorGuaranteed> {
tcx.analysis(())?;
let out = match ppm {
Mir => {
Expand Down Expand Up @@ -518,7 +501,7 @@ fn print_with_analysis(
_ => unreachable!(),
};

write_or_print(&out, ofile, tcx.sess);
write_or_print(&out, tcx.sess);

Ok(())
}
Loading