Skip to content

Commit

Permalink
Auto merge of rust-lang#122131 - workingjubilee:rollup-901aab1, r=wor…
Browse files Browse the repository at this point in the history
…kingjubilee

Rollup of 13 pull requests

Successful merges:

 - rust-lang#113525 (Dynamically size sigaltstk in std)
 - rust-lang#114009 (compiler: allow transmute of ZST arrays with generics)
 - rust-lang#116793 (Allow targets to override default codegen backend)
 - rust-lang#118623 (Improve std::fs::read_to_string example)
 - rust-lang#120504 (Vec::try_with_capacity)
 - rust-lang#121089 (Remove `feed_local_def_id`)
 - rust-lang#121280 (Implement MaybeUninit::fill{,_with,_from})
 - rust-lang#122087 (Add missing background color for top-level rust documentation page and increase contrast by setting text color to black)
 - rust-lang#122104 (Rust is a proper name: rust → Rust)
 - rust-lang#122110 (Make `x t miri` respect `MIRI_TEMP`)
 - rust-lang#122114 (Make not finding core a fatal error)
 - rust-lang#122115 (Cancel parsing ever made during recovery)
 - rust-lang#122126 (Fix `tidy --bless` on  ̶X̶e̶n̶i̶x̶ Windows)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Mar 7, 2024
2 parents 51f4839 + c22cf59 commit 18f5391
Show file tree
Hide file tree
Showing 66 changed files with 1,032 additions and 271 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
tcx.ensure_with_value().early_lint_checks(());
tcx.ensure_with_value().debugger_visualizers(LOCAL_CRATE);
tcx.ensure_with_value().get_lang_items(());
let (mut resolver, krate) = tcx.resolver_for_lowering(()).steal();
let (mut resolver, krate) = tcx.resolver_for_lowering().steal();

let ast_index = index_crate(&resolver.node_id_to_def_id, &krate);
let mut owners = IndexVec::from_fn_n(
Expand Down
17 changes: 13 additions & 4 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use rustc_session::config::{nightly_options, CG_OPTIONS, Z_OPTIONS};
use rustc_session::config::{ErrorOutputType, Input, OutFileName, OutputType};
use rustc_session::getopts::{self, Matches};
use rustc_session::lint::{Lint, LintId};
use rustc_session::{config, EarlyDiagCtxt, Session};
use rustc_session::{config, filesearch, EarlyDiagCtxt, Session};
use rustc_span::def_id::LOCAL_CRATE;
use rustc_span::source_map::FileLoader;
use rustc_span::symbol::sym;
Expand Down Expand Up @@ -418,7 +418,7 @@ fn run_compiler(
}

// Make sure name resolution and macro expansion is run.
queries.global_ctxt()?.enter(|tcx| tcx.resolver_for_lowering(()));
queries.global_ctxt()?.enter(|tcx| tcx.resolver_for_lowering());

if callbacks.after_expansion(compiler, queries) == Compilation::Stop {
return early_exit();
Expand Down Expand Up @@ -886,7 +886,11 @@ pub fn version_at_macro_invocation(

let debug_flags = matches.opt_strs("Z");
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
get_codegen_backend(early_dcx, &None, backend_name).print_version();
let opts = config::Options::default();
let sysroot = filesearch::materialize_sysroot(opts.maybe_sysroot.clone());
let target = config::build_target_config(early_dcx, &opts, None, &sysroot);

get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_version();
}
}

Expand Down Expand Up @@ -1091,7 +1095,12 @@ pub fn describe_flag_categories(early_dcx: &EarlyDiagCtxt, matches: &Matches) ->

if cg_flags.iter().any(|x| *x == "passes=list") {
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
get_codegen_backend(early_dcx, &None, backend_name).print_passes();

let opts = config::Options::default();
let sysroot = filesearch::materialize_sysroot(opts.maybe_sysroot.clone());
let target = config::build_target_config(early_dcx, &opts, None, &sysroot);

get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_passes();
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_driver_impl/src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl<'tcx> PrintExtra<'tcx> {
{
match self {
PrintExtra::AfterParsing { krate, .. } => f(krate),
PrintExtra::NeedsAstMap { tcx } => f(&tcx.resolver_for_lowering(()).borrow().1),
PrintExtra::NeedsAstMap { tcx } => f(&tcx.resolver_for_lowering().borrow().1),
}
}

Expand Down Expand Up @@ -281,7 +281,7 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
}
AstTreeExpanded => {
debug!("pretty-printing expanded AST");
format!("{:#?}", ex.tcx().resolver_for_lowering(()).borrow().1)
format!("{:#?}", ex.tcx().resolver_for_lowering().borrow().1)
}
Hir(s) => {
debug!("pretty printing HIR {:?}", s);
Expand Down
61 changes: 48 additions & 13 deletions compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_parse::maybe_new_parser_from_source_str;
use rustc_query_impl::QueryCtxt;
use rustc_query_system::query::print_query_stack;
use rustc_session::config::{self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName};
use rustc_session::filesearch::sysroot_candidates;
use rustc_session::filesearch::{self, sysroot_candidates};
use rustc_session::parse::ParseSess;
use rustc_session::{lint, CompilerIO, EarlyDiagCtxt, Session};
use rustc_span::source_map::FileLoader;
Expand Down Expand Up @@ -339,16 +339,53 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se

let early_dcx = EarlyDiagCtxt::new(config.opts.error_format);

let codegen_backend = if let Some(make_codegen_backend) = config.make_codegen_backend {
make_codegen_backend(&config.opts)
} else {
util::get_codegen_backend(
&early_dcx,
&config.opts.maybe_sysroot,
config.opts.unstable_opts.codegen_backend.as_deref(),
)
let sysroot = filesearch::materialize_sysroot(config.opts.maybe_sysroot.clone());

let (codegen_backend, target_override) = match config.make_codegen_backend {
None => {
// Build a target without override, so that it can override the backend if needed
let target =
config::build_target_config(&early_dcx, &config.opts, None, &sysroot);

let backend = util::get_codegen_backend(
&early_dcx,
&sysroot,
config.opts.unstable_opts.codegen_backend.as_deref(),
&target,
);

// target_override is documented to be called before init(), so this is okay
let target_override = backend.target_override(&config.opts);

// Assert that we don't use target's override of the backend and
// backend's override of the target at the same time
if config.opts.unstable_opts.codegen_backend.is_none()
&& target.default_codegen_backend.is_some()
&& target_override.is_some()
{
rustc_middle::bug!(
"Codegen backend requested target override even though the target requested the backend"
);
}

(backend, target_override)
}
Some(make_codegen_backend) => {
// N.B. `make_codegen_backend` takes precedence over `target.default_codegen_backend`,
// which is ignored in this case.
let backend = make_codegen_backend(&config.opts);

// target_override is documented to be called before init(), so this is okay
let target_override = backend.target_override(&config.opts);

(backend, target_override)
}
};

// Re-build target with the (potential) override
let target_cfg =
config::build_target_config(&early_dcx, &config.opts, target_override, &sysroot);

let temps_dir = config.opts.unstable_opts.temps_dir.as_deref().map(PathBuf::from);

let bundle = match rustc_errors::fluent_bundle(
Expand All @@ -367,9 +404,6 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
let mut locale_resources = Vec::from(config.locale_resources);
locale_resources.push(codegen_backend.locale_resource());

// target_override is documented to be called before init(), so this is okay
let target_override = codegen_backend.target_override(&config.opts);

let mut sess = rustc_session::build_session(
early_dcx,
config.opts,
Expand All @@ -384,7 +418,8 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
locale_resources,
config.lint_caps,
config.file_loader,
target_override,
target_cfg,
sysroot,
util::rustc_version_str().unwrap_or("unknown"),
config.ice_file,
config.using_internal_features,
Expand Down
18 changes: 10 additions & 8 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ fn configure_and_expand(

fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
let sess = tcx.sess;
let (resolver, krate) = &*tcx.resolver_for_lowering(()).borrow();
let (resolver, krate) = &*tcx.resolver_for_lowering().borrow();
let mut lint_buffer = resolver.lint_buffer.steal();

if sess.opts.unstable_opts.input_stats {
Expand Down Expand Up @@ -531,10 +531,10 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
}
}

fn resolver_for_lowering<'tcx>(
fn resolver_for_lowering_raw<'tcx>(
tcx: TyCtxt<'tcx>,
(): (),
) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)> {
) -> (&'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)>, &'tcx ty::ResolverGlobalCtxt) {
let arenas = Resolver::arenas();
let _ = tcx.registered_tools(()); // Uses `crate_for_resolver`.
let (krate, pre_configured_attrs) = tcx.crate_for_resolver(()).steal();
Expand All @@ -549,16 +549,15 @@ fn resolver_for_lowering<'tcx>(
ast_lowering: untracked_resolver_for_lowering,
} = resolver.into_outputs();

let feed = tcx.feed_unit_query();
feed.resolutions(tcx.arena.alloc(untracked_resolutions));
tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, Lrc::new(krate))))
let resolutions = tcx.arena.alloc(untracked_resolutions);
(tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, Lrc::new(krate)))), resolutions)
}

pub(crate) fn write_dep_info(tcx: TyCtxt<'_>) {
// Make sure name resolution and macro expansion is run for
// the side-effect of providing a complete set of all
// accessed files and env vars.
let _ = tcx.resolver_for_lowering(());
let _ = tcx.resolver_for_lowering();

let sess = tcx.sess;
let _timer = sess.timer("write_dep_info");
Expand Down Expand Up @@ -607,7 +606,10 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
let providers = &mut Providers::default();
providers.analysis = analysis;
providers.hir_crate = rustc_ast_lowering::lower_to_hir;
providers.resolver_for_lowering = resolver_for_lowering;
providers.resolver_for_lowering_raw = resolver_for_lowering_raw;
providers.stripped_cfg_items =
|tcx, _| tcx.arena.alloc_from_iter(tcx.resolutions(()).stripped_cfg_items.steal());
providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1;
providers.early_lint_checks = early_lint_checks;
proc_macro_decls::provide(providers);
rustc_const_eval::provide(providers);
Expand Down
12 changes: 3 additions & 9 deletions compiler/rustc_interface/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use rustc_codegen_ssa::CodegenResults;
use rustc_data_structures::steal::Steal;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, OnceLock, WorkerLocal};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{StableCrateId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
use rustc_hir::definitions::Definitions;
use rustc_incremental::setup_dep_graph;
use rustc_metadata::creader::CStore;
Expand Down Expand Up @@ -144,10 +143,8 @@ impl<'tcx> Queries<'tcx> {
stable_crate_id,
)) as _);
let definitions = FreezeLock::new(Definitions::new(stable_crate_id));
let source_span = AppendOnlyIndexVec::new();
let _id = source_span.push(krate.spans.inner_span);
debug_assert_eq!(_id, CRATE_DEF_ID);
let untracked = Untracked { cstore, source_span, definitions };
let untracked =
Untracked { cstore, source_span: AppendOnlyIndexVec::new(), definitions };

let qcx = passes::create_global_ctxt(
self.compiler,
Expand All @@ -172,9 +169,6 @@ impl<'tcx> Queries<'tcx> {
)));
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
feed.output_filenames(Arc::new(outputs));

let feed = tcx.feed_local_def_id(CRATE_DEF_ID);
feed.def_kind(DefKind::Mod);
});
Ok(qcx)
})
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_session::config::{
use rustc_session::lint::Level;
use rustc_session::search_paths::SearchPath;
use rustc_session::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
use rustc_session::{build_session, getopts, CompilerIO, EarlyDiagCtxt, Session};
use rustc_session::{build_session, filesearch, getopts, CompilerIO, EarlyDiagCtxt, Session};
use rustc_span::edition::{Edition, DEFAULT_EDITION};
use rustc_span::symbol::sym;
use rustc_span::{FileName, SourceFileHashAlgorithm};
Expand All @@ -37,6 +37,12 @@ fn mk_session(matches: getopts::Matches) -> (Session, Cfg) {
output_file: None,
temps_dir,
};

let sysroot = filesearch::materialize_sysroot(sessopts.maybe_sysroot.clone());

let target_cfg =
rustc_session::config::build_target_config(&early_dcx, &sessopts, None, &sysroot);

let sess = build_session(
early_dcx,
sessopts,
Expand All @@ -46,7 +52,8 @@ fn mk_session(matches: getopts::Matches) -> (Session, Cfg) {
vec![],
Default::default(),
None,
None,
target_cfg,
sysroot,
"",
None,
Arc::default(),
Expand Down
47 changes: 26 additions & 21 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ use rustc_session::{filesearch, output, Session};
use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::edition::Edition;
use rustc_span::symbol::{sym, Symbol};
use rustc_target::spec::Target;
use session::EarlyDiagCtxt;
use std::env;
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::OnceLock;
use std::thread;
use std::{env, iter};

/// Function pointer type that constructs a new CodegenBackend.
pub type MakeBackendFn = fn() -> Box<dyn CodegenBackend>;
Expand Down Expand Up @@ -182,21 +183,25 @@ fn load_backend_from_dylib(early_dcx: &EarlyDiagCtxt, path: &Path) -> MakeBacken
/// A name of `None` indicates that the default backend should be used.
pub fn get_codegen_backend(
early_dcx: &EarlyDiagCtxt,
maybe_sysroot: &Option<PathBuf>,
sysroot: &Path,
backend_name: Option<&str>,
target: &Target,
) -> Box<dyn CodegenBackend> {
static LOAD: OnceLock<unsafe fn() -> Box<dyn CodegenBackend>> = OnceLock::new();

let load = LOAD.get_or_init(|| {
let default_codegen_backend = option_env!("CFG_DEFAULT_CODEGEN_BACKEND").unwrap_or("llvm");
let backend = backend_name
.or(target.default_codegen_backend.as_deref())
.or(option_env!("CFG_DEFAULT_CODEGEN_BACKEND"))
.unwrap_or("llvm");

match backend_name.unwrap_or(default_codegen_backend) {
match backend {
filename if filename.contains('.') => {
load_backend_from_dylib(early_dcx, filename.as_ref())
}
#[cfg(feature = "llvm")]
"llvm" => rustc_codegen_llvm::LlvmCodegenBackend::new,
backend_name => get_codegen_sysroot(early_dcx, maybe_sysroot, backend_name),
backend_name => get_codegen_sysroot(early_dcx, sysroot, backend_name),
}
});

Expand Down Expand Up @@ -231,7 +236,7 @@ fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
fn get_codegen_sysroot(
early_dcx: &EarlyDiagCtxt,
maybe_sysroot: &Option<PathBuf>,
sysroot: &Path,
backend_name: &str,
) -> MakeBackendFn {
// For now we only allow this function to be called once as it'll dlopen a
Expand All @@ -248,28 +253,28 @@ fn get_codegen_sysroot(
let target = session::config::host_triple();
let sysroot_candidates = sysroot_candidates();

let sysroot = maybe_sysroot
.iter()
.chain(sysroot_candidates.iter())
let sysroot = iter::once(sysroot)
.chain(sysroot_candidates.iter().map(<_>::as_ref))
.map(|sysroot| {
filesearch::make_target_lib_path(sysroot, target).with_file_name("codegen-backends")
})
.find(|f| {
info!("codegen backend candidate: {}", f.display());
f.exists()
});
let sysroot = sysroot.unwrap_or_else(|| {
let candidates = sysroot_candidates
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join("\n* ");
let err = format!(
"failed to find a `codegen-backends` folder \
})
.unwrap_or_else(|| {
let candidates = sysroot_candidates
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join("\n* ");
let err = format!(
"failed to find a `codegen-backends` folder \
in the sysroot candidates:\n* {candidates}"
);
early_dcx.early_fatal(err);
});
);
early_dcx.early_fatal(err);
});

info!("probing {} for a codegen backend", sysroot.display());

let d = sysroot.read_dir().unwrap_or_else(|e| {
Expand Down
Loading

0 comments on commit 18f5391

Please sign in to comment.