1
Fork 0

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

@ -24,7 +24,7 @@ fn generic_arg_mismatch_err(
arg: &GenericArg<'_>,
param: &GenericParamDef,
possible_ordering_error: bool,
help: Option<&str>,
help: Option<String>,
) -> ErrorGuaranteed {
let sess = tcx.sess;
let mut err = struct_span_err!(
@ -300,7 +300,7 @@ pub fn create_substs_for_generic_args<'tcx, 'a>(
arg,
param,
!args_iter.clone().is_sorted_by_key(|arg| arg.to_ord()),
Some(&format!(
Some(format!(
"reorder the arguments: {}: `<{}>`",
param_types_present
.into_iter()

View file

@ -164,7 +164,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
let Some(type_dependent_def) = tables.type_dependent_def_id(parent_node_id) else {
return tcx.ty_error_with_message(
tcx.def_span(def_id),
&format!("unable to find type-dependent def for {:?}", parent_node_id),
format!("unable to find type-dependent def for {:?}", parent_node_id),
);
};
let idx = segment
@ -205,14 +205,14 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
} else {
return tcx.ty_error_with_message(
tcx.def_span(def_id),
&format!("unable to find const parent for {} in pat {:?}", hir_id, pat),
format!("unable to find const parent for {} in pat {:?}", hir_id, pat),
);
}
}
_ => {
return tcx.ty_error_with_message(
tcx.def_span(def_id),
&format!("unexpected const parent path {:?}", parent_node),
format!("unexpected const parent path {:?}", parent_node),
);
}
};
@ -243,7 +243,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
None => {
return tcx.ty_error_with_message(
tcx.def_span(def_id),
&format!("unexpected anon const res {:?} in path: {:?}", segment.res, path),
format!("unexpected anon const res {:?} in path: {:?}", segment.res, path),
);
}
};
@ -253,7 +253,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
_ => return tcx.ty_error_with_message(
tcx.def_span(def_id),
&format!("unexpected const parent in type_of(): {parent_node:?}"),
format!("unexpected const parent in type_of(): {parent_node:?}"),
),
};
@ -279,7 +279,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
} else {
return tcx.ty_error_with_message(
tcx.def_span(def_id),
&format!("const generic parameter not found in {generics:?} at position {arg_idx:?}"),
format!("const generic parameter not found in {generics:?} at position {arg_idx:?}"),
);
}
}