6b62f37402
Currently a `{D,Subd}iagnosticMessage` can be created from any type that impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static, str>`, which are reasonable. It also includes `&String`, which is pretty weird, and results in many places making unnecessary allocations for patterns like this: ``` self.fatal(&format!(...)) ``` This creates a string with `format!`, takes a reference, passes the reference to `fatal`, which does an `into()`, which clones the reference, doing a second allocation. Two allocations for a single string, bleh. This commit changes the `From` impls so that you can only create a `{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static, str>`. This requires changing all the places that currently create one from a `&String`. Most of these are of the `&format!(...)` form described above; each one removes an unnecessary static `&`, plus an allocation when executed. There are also a few places where the existing use of `&String` was more reasonable; these now just use `clone()` at the call site. As well as making the code nicer and more efficient, this is a step towards possibly using `Cow<'static, str>` in `{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing the `From<&'a str>` impls to `From<&'static str>`, which is doable, but I'm not yet sure if it's worthwhile.
85 lines
2.7 KiB
Rust
85 lines
2.7 KiB
Rust
#![feature(rustc_private)]
|
|
#![deny(warnings)]
|
|
|
|
extern crate rustc_codegen_ssa;
|
|
extern crate rustc_data_structures;
|
|
extern crate rustc_driver;
|
|
extern crate rustc_errors;
|
|
extern crate rustc_hir;
|
|
extern crate rustc_metadata;
|
|
extern crate rustc_middle;
|
|
extern crate rustc_session;
|
|
extern crate rustc_span;
|
|
extern crate rustc_symbol_mangling;
|
|
extern crate rustc_target;
|
|
|
|
use rustc_codegen_ssa::traits::CodegenBackend;
|
|
use rustc_codegen_ssa::{CodegenResults, CrateInfo};
|
|
use rustc_data_structures::fx::FxHashMap;
|
|
use rustc_errors::ErrorGuaranteed;
|
|
use rustc_metadata::EncodedMetadata;
|
|
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
|
|
use rustc_middle::ty::TyCtxt;
|
|
use rustc_session::config::OutputFilenames;
|
|
use rustc_session::Session;
|
|
use std::any::Any;
|
|
|
|
struct TheBackend;
|
|
|
|
impl CodegenBackend for TheBackend {
|
|
fn locale_resource(&self) -> &'static str { "" }
|
|
|
|
fn codegen_crate<'a, 'tcx>(
|
|
&self,
|
|
tcx: TyCtxt<'tcx>,
|
|
metadata: EncodedMetadata,
|
|
_need_metadata_module: bool,
|
|
) -> Box<dyn Any> {
|
|
Box::new(CodegenResults {
|
|
modules: vec![],
|
|
allocator_module: None,
|
|
metadata_module: None,
|
|
metadata,
|
|
crate_info: CrateInfo::new(tcx, "fake_target_cpu".to_string()),
|
|
})
|
|
}
|
|
|
|
fn join_codegen(
|
|
&self,
|
|
ongoing_codegen: Box<dyn Any>,
|
|
_sess: &Session,
|
|
_outputs: &OutputFilenames,
|
|
) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorGuaranteed> {
|
|
let codegen_results = ongoing_codegen
|
|
.downcast::<CodegenResults>()
|
|
.expect("in join_codegen: ongoing_codegen is not a CodegenResults");
|
|
Ok((*codegen_results, FxHashMap::default()))
|
|
}
|
|
|
|
fn link(
|
|
&self,
|
|
sess: &Session,
|
|
codegen_results: CodegenResults,
|
|
outputs: &OutputFilenames,
|
|
) -> Result<(), ErrorGuaranteed> {
|
|
use rustc_session::{config::CrateType, output::out_filename};
|
|
use std::io::Write;
|
|
let crate_name = codegen_results.crate_info.local_crate_name;
|
|
for &crate_type in sess.opts.crate_types.iter() {
|
|
if crate_type != CrateType::Rlib {
|
|
sess.fatal(format!("Crate type is {:?}", crate_type));
|
|
}
|
|
let output_name = out_filename(sess, crate_type, &outputs, crate_name);
|
|
let mut out_file = ::std::fs::File::create(output_name).unwrap();
|
|
write!(out_file, "This has been \"compiled\" successfully.").unwrap();
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
/// This is the entrypoint for a hot plugged rustc_codegen_llvm
|
|
#[no_mangle]
|
|
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
|
|
Box::new(TheBackend)
|
|
}
|