Avoid &format("...") calls in error message code.

Error message all end up passing into a function as an `impl
Into<{D,Subd}iagnosticMessage>`. If an error message is creatd as
`&format("...")` that means we allocate a string (in the `format!`
call), then take a reference, and then clone (allocating again) the
reference to produce the `{D,Subd}iagnosticMessage`, which is silly.

This commit removes the leading `&` from a lot of these cases. This
means the original `String` is moved into the
`{D,Subd}iagnosticMessage`, avoiding the double allocations. This
requires changing some function argument types from `&str` to `String`
(when all arguments are `String`) or `impl
Into<{D,Subd}iagnosticMessage>` (when some arguments are `String` and
some are `&str`).
This commit is contained in:
Nicholas Nethercote 2023-05-16 16:04:03 +10:00
parent 87a2bc027c
commit 01e33a3600
37 changed files with 139 additions and 133 deletions

View file

@ -88,7 +88,7 @@ pub fn create_session(
) {
Ok(bundle) => bundle,
Err(e) => {
early_error(sopts.error_format, &format!("failed to load fluent bundle: {e}"));
early_error(sopts.error_format, format!("failed to load fluent bundle: {e}"));
}
};
@ -220,13 +220,13 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
fn load_backend_from_dylib(path: &Path) -> MakeBackendFn {
let lib = unsafe { Library::new(path) }.unwrap_or_else(|err| {
let err = format!("couldn't load codegen backend {path:?}: {err}");
early_error(ErrorOutputType::default(), &err);
early_error(ErrorOutputType::default(), err);
});
let backend_sym = unsafe { lib.get::<MakeBackendFn>(b"__rustc_codegen_backend") }
.unwrap_or_else(|e| {
let err = format!("couldn't load codegen backend: {e}");
early_error(ErrorOutputType::default(), &err);
early_error(ErrorOutputType::default(), err);
});
// Intentionally leak the dynamic library. We can't ever unload it
@ -320,7 +320,7 @@ fn get_codegen_sysroot(maybe_sysroot: &Option<PathBuf>, backend_name: &str) -> M
"failed to find a `codegen-backends` folder \
in the sysroot candidates:\n* {candidates}"
);
early_error(ErrorOutputType::default(), &err);
early_error(ErrorOutputType::default(), err);
});
info!("probing {} for a codegen backend", sysroot.display());
@ -331,7 +331,7 @@ fn get_codegen_sysroot(maybe_sysroot: &Option<PathBuf>, backend_name: &str) -> M
sysroot.display(),
e
);
early_error(ErrorOutputType::default(), &err);
early_error(ErrorOutputType::default(), err);
});
let mut file: Option<PathBuf> = None;
@ -359,7 +359,7 @@ fn get_codegen_sysroot(maybe_sysroot: &Option<PathBuf>, backend_name: &str) -> M
prev.display(),
path.display()
);
early_error(ErrorOutputType::default(), &err);
early_error(ErrorOutputType::default(), err);
}
file = Some(path.clone());
}
@ -368,7 +368,7 @@ fn get_codegen_sysroot(maybe_sysroot: &Option<PathBuf>, backend_name: &str) -> M
Some(ref s) => load_backend_from_dylib(s),
None => {
let err = format!("unsupported builtin codegen backend `{backend_name}`");
early_error(ErrorOutputType::default(), &err);
early_error(ErrorOutputType::default(), err);
}
}
}