Skip to content

Remove RunCompiler and related things. #102759

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 5 additions & 71 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ use rustc_session::config::{ErrorOutputType, Input, OutputType, PrintRequest, Tr
use rustc_session::cstore::MetadataLoader;
use rustc_session::getopts;
use rustc_session::lint::{Lint, LintId};
use rustc_session::{config, DiagnosticOutput, Session};
use rustc_session::{config, Session};
use rustc_session::{early_error, early_error_no_abort, early_warn};
use rustc_span::source_map::{FileLoader, FileName};
use rustc_span::source_map::FileName;
use rustc_span::symbol::sym;
use rustc_target::json::ToJson;

Expand Down Expand Up @@ -139,76 +139,13 @@ pub fn diagnostics_registry() -> Registry {
Registry::new(rustc_error_codes::DIAGNOSTICS)
}

/// This is the primary entry point for rustc.
pub struct RunCompiler<'a, 'b> {
at_args: &'a [String],
callbacks: &'b mut (dyn Callbacks + Send),
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
emitter: Option<Box<dyn Write + Send>>,
make_codegen_backend:
Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>,
}

impl<'a, 'b> RunCompiler<'a, 'b> {
pub fn new(at_args: &'a [String], callbacks: &'b mut (dyn Callbacks + Send)) -> Self {
Self { at_args, callbacks, file_loader: None, emitter: None, make_codegen_backend: None }
}

/// Set a custom codegen backend.
///
/// Used by cg_clif.
pub fn set_make_codegen_backend(
&mut self,
make_codegen_backend: Option<
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
>,
) -> &mut Self {
self.make_codegen_backend = make_codegen_backend;
self
}

/// Emit diagnostics to the specified location.
///
/// Used by RLS.
pub fn set_emitter(&mut self, emitter: Option<Box<dyn Write + Send>>) -> &mut Self {
self.emitter = emitter;
self
}

/// Load files from sources other than the file system.
///
/// Used by RLS.
pub fn set_file_loader(
&mut self,
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
) -> &mut Self {
self.file_loader = file_loader;
self
}

/// Parse args and run the compiler.
pub fn run(self) -> interface::Result<()> {
run_compiler(
self.at_args,
self.callbacks,
self.file_loader,
self.emitter,
self.make_codegen_backend,
)
}
}
fn run_compiler(
// Primary entry point used by rustc, clippy, and miri.
pub fn run_compiler(
at_args: &[String],
callbacks: &mut (dyn Callbacks + Send),
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
emitter: Option<Box<dyn Write + Send>>,
make_codegen_backend: Option<
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still have a use case for both file_loader (using it for hooking rust-analyzers VFS into rustc at some point for running rustc without having to save) and make_codegen_backend (setting the codegen backend from a custom driver where the custom codegen backend has arbitrary data. used by the hotswapping branch of cg_clif)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh. This is frustrating when there is no way to tell if an interface like this is being used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah

) -> interface::Result<()> {
let args = args::arg_expand_all(at_args);

let diagnostic_output = emitter.map_or(DiagnosticOutput::Default, DiagnosticOutput::Raw);
let Some(matches) = handle_options(&args) else { return Ok(()) };

let sopts = config::build_session_options(&matches);
Expand All @@ -229,13 +166,10 @@ fn run_compiler(
input_path: None,
output_file: ofile,
output_dir: odir,
file_loader,
diagnostic_output,
lint_caps: Default::default(),
parse_sess_created: None,
register_lints: None,
override_queries: None,
make_codegen_backend,
registry: diagnostics_registry(),
};

Expand Down Expand Up @@ -1371,7 +1305,7 @@ pub fn main() -> ! {
})
})
.collect::<Vec<_>>();
RunCompiler::new(&args, &mut callbacks).run()
run_compiler(&args, &mut callbacks)
});

if callbacks.time_passes {
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_expand/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use rustc_ast::{token, AttrVec, Attribute, Inline, Item, ModSpans};
use rustc_errors::{struct_span_err, DiagnosticBuilder, ErrorGuaranteed};
use rustc_parse::new_parser_from_file;
use rustc_parse::validate_attr;
use rustc_session::parse::ParseSess;
use rustc_session::Session;
use rustc_span::symbol::{sym, Ident};
use rustc_span::Span;
Expand Down Expand Up @@ -151,7 +150,7 @@ fn mod_file_path<'a>(
DirOwnership::Owned { relative } => relative,
DirOwnership::UnownedViaBlock => None,
};
let result = default_submod_path(&sess.parse_sess, ident, relative, dir_path);
let result = default_submod_path(ident, relative, dir_path);
match dir_ownership {
DirOwnership::Owned { .. } => result,
DirOwnership::UnownedViaBlock => Err(ModError::ModInBlock(match result {
Expand Down Expand Up @@ -201,7 +200,6 @@ fn mod_file_path_from_attr(
/// Returns a path to a module.
// Public for rustfmt usage.
pub fn default_submod_path<'a>(
sess: &'a ParseSess,
ident: Ident,
relative: Option<Ident>,
dir_path: &Path,
Expand All @@ -223,8 +221,8 @@ pub fn default_submod_path<'a>(
format!("{}{}{}mod.rs", relative_prefix, ident.name, path::MAIN_SEPARATOR);
let default_path = dir_path.join(&default_path_str);
let secondary_path = dir_path.join(&secondary_path_str);
let default_exists = sess.source_map().file_exists(&default_path);
let secondary_exists = sess.source_map().file_exists(&secondary_path);
let default_exists = default_path.exists();
let secondary_exists = secondary_path.exists();

match (default_exists, secondary_exists) {
(true, false) => Ok(ModulePathSuccess {
Expand Down
13 changes: 2 additions & 11 deletions compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use rustc_session::config::{self, CheckCfg, ErrorOutputType, Input, OutputFilena
use rustc_session::early_error;
use rustc_session::lint;
use rustc_session::parse::{CrateConfig, ParseSess};
use rustc_session::{DiagnosticOutput, Session};
use rustc_span::source_map::{FileLoader, FileName};
use rustc_session::Session;
use rustc_span::source_map::FileName;
use rustc_span::symbol::sym;
use std::path::PathBuf;
use std::result;
Expand Down Expand Up @@ -246,8 +246,6 @@ pub struct Config {
pub input_path: Option<PathBuf>,
pub output_dir: Option<PathBuf>,
pub output_file: Option<PathBuf>,
pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
pub diagnostic_output: DiagnosticOutput,

pub lint_caps: FxHashMap<lint::LintId, lint::Level>,

Expand All @@ -268,10 +266,6 @@ pub struct Config {
pub override_queries:
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::ExternProviders)>,

/// This is a callback from the driver that is called to create a codegen backend.
pub make_codegen_backend:
Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>,

/// Registry of diagnostics codes.
pub registry: Registry,
}
Expand All @@ -284,11 +278,8 @@ pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R
config.opts,
config.crate_cfg,
config.crate_check_cfg,
config.diagnostic_output,
config.file_loader,
config.input_path.clone(),
config.lint_caps,
config.make_codegen_backend,
registry.clone(),
);

Expand Down
13 changes: 2 additions & 11 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rustc_session::config::{CFGuard, ExternEntry, LinkerPluginLto, LtoCli, Switc
use rustc_session::lint::Level;
use rustc_session::search_paths::SearchPath;
use rustc_session::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
use rustc_session::{build_session, getopts, DiagnosticOutput, Session};
use rustc_session::{build_session, getopts, Session};
use rustc_span::edition::{Edition, DEFAULT_EDITION};
use rustc_span::symbol::sym;
use rustc_span::SourceFileHashAlgorithm;
Expand All @@ -40,16 +40,7 @@ fn build_session_options_and_crate_config(matches: getopts::Matches) -> (Options
fn mk_session(matches: getopts::Matches) -> (Session, CfgSpecs) {
let registry = registry::Registry::new(&[]);
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
let sess = build_session(
sessopts,
None,
None,
registry,
DiagnosticOutput::Default,
Default::default(),
None,
None,
);
let sess = build_session(sessopts, None, None, registry, Default::default(), None);
(sess, cfg)
}

Expand Down
32 changes: 7 additions & 25 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ use rustc_session::config::{self, CrateType};
use rustc_session::config::{ErrorOutputType, Input, OutputFilenames};
use rustc_session::lint::{self, BuiltinLintDiagnostics, LintBuffer};
use rustc_session::parse::CrateConfig;
use rustc_session::{early_error, filesearch, output, DiagnosticOutput, Session};
use rustc_session::{early_error, filesearch, output, Session};
use rustc_span::edition::Edition;
use rustc_span::lev_distance::find_best_match_for_name;
use rustc_span::source_map::FileLoader;
use rustc_span::symbol::{sym, Symbol};
use std::env;
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
Expand Down Expand Up @@ -65,23 +64,14 @@ pub fn create_session(
sopts: config::Options,
cfg: FxHashSet<(String, Option<String>)>,
check_cfg: CheckCfg,
diagnostic_output: DiagnosticOutput,
file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>,
input_path: Option<PathBuf>,
lint_caps: FxHashMap<lint::LintId, lint::Level>,
make_codegen_backend: Option<
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
>,
descriptions: Registry,
) -> (Lrc<Session>, Lrc<Box<dyn CodegenBackend>>) {
let codegen_backend = if let Some(make_codegen_backend) = make_codegen_backend {
make_codegen_backend(&sopts)
} else {
get_codegen_backend(
&sopts.maybe_sysroot,
sopts.unstable_opts.codegen_backend.as_ref().map(|name| &name[..]),
)
};
let codegen_backend = get_codegen_backend(
&sopts.maybe_sysroot,
sopts.unstable_opts.codegen_backend.as_ref().map(|name| &name[..]),
);

// target_override is documented to be called before init(), so this is okay
let target_override = codegen_backend.target_override(&sopts);
Expand All @@ -99,16 +89,8 @@ pub fn create_session(
}
};

let mut sess = session::build_session(
sopts,
input_path,
bundle,
descriptions,
diagnostic_output,
lint_caps,
file_loader,
target_override,
);
let mut sess =
session::build_session(sopts, input_path, bundle, descriptions, lint_caps, target_override);

codegen_backend.init(&sess);

Expand Down
Loading