Skip to content

Commit c4ebd27

Browse files
committedMay 29, 2023
Auto merge of #111748 - nnethercote:Cow-DiagnosticMessage, r=WaffleLapkin
Use `Cow` in `{D,Subd}iagnosticMessage`. Each of `{D,Subd}iagnosticMessage::{Str,Eager}` has a comment: ``` // FIXME(davidtwco): can a `Cow<'static, str>` be used here? ``` This commit answers that question in the affirmative. It's not the most compelling change ever, but it might be worth merging. This requires changing the `impl<'a> From<&'a str>` impls to `impl From<&'static str>`, which involves a bunch of knock-on changes that require/result in call sites being a little more precise about exactly what kind of string they use to create errors, and not just `&str`. This will result in fewer unnecessary allocations, though this will not have any notable perf effects given that these are error paths. Note that I was lazy within Clippy, using `to_string` in a few places to preserve the existing string imprecision. I could have used `impl Into<{D,Subd}iagnosticMessage>` in various places as is done in the compiler, but that would have required changes to *many* call sites (mostly changing `&format("...")` to `format!("...")`) which didn't seem worthwhile. r? `@WaffleLapkin`
2 parents f3cd05d + 6fabcda commit c4ebd27

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed
 

‎src/diagnostics.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ pub fn report_error<'tcx, 'mir>(
306306
msg.insert(0, e.to_string());
307307
report_msg(
308308
DiagLevel::Error,
309-
&if let Some(title) = title { format!("{title}: {}", msg[0]) } else { msg[0].clone() },
309+
if let Some(title) = title { format!("{title}: {}", msg[0]) } else { msg[0].clone() },
310310
msg,
311311
vec![],
312312
helps,
@@ -359,7 +359,7 @@ pub fn report_leaks<'mir, 'tcx>(
359359
any_pruned |= pruned;
360360
report_msg(
361361
DiagLevel::Error,
362-
&format!(
362+
format!(
363363
"memory leaked: {id:?} ({}, size: {:?}, align: {:?}), allocated here:",
364364
kind,
365365
alloc.size().bytes(),
@@ -386,7 +386,7 @@ pub fn report_leaks<'mir, 'tcx>(
386386
/// additional `span_label` or `note` call.
387387
pub fn report_msg<'tcx>(
388388
diag_level: DiagLevel,
389-
title: &str,
389+
title: String,
390390
span_msg: Vec<String>,
391391
notes: Vec<(Option<SpanData>, String)>,
392392
helps: Vec<(Option<SpanData>, String)>,
@@ -463,15 +463,16 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
463463
let (stacktrace, _was_pruned) = prune_stacktrace(stacktrace, self);
464464

465465
let (title, diag_level) = match &e {
466-
RejectedIsolatedOp(_) => ("operation rejected by isolation", DiagLevel::Warning),
467-
Int2Ptr { .. } => ("integer-to-pointer cast", DiagLevel::Warning),
466+
RejectedIsolatedOp(_) =>
467+
("operation rejected by isolation".to_string(), DiagLevel::Warning),
468+
Int2Ptr { .. } => ("integer-to-pointer cast".to_string(), DiagLevel::Warning),
468469
CreatedPointerTag(..)
469470
| PoppedPointerTag(..)
470471
| CreatedCallId(..)
471472
| CreatedAlloc(..)
472473
| FreedAlloc(..)
473474
| ProgressReport { .. }
474-
| WeakMemoryOutdatedLoad => ("tracking was triggered", DiagLevel::Note),
475+
| WeakMemoryOutdatedLoad => ("tracking was triggered".to_string(), DiagLevel::Note),
475476
};
476477

477478
let msg = match &e {
@@ -571,7 +572,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
571572
let stacktrace = this.generate_stacktrace();
572573
report_msg(
573574
DiagLevel::Note,
574-
"the place in the program where the ICE was triggered",
575+
"the place in the program where the ICE was triggered".to_string(),
575576
vec![],
576577
vec![],
577578
vec![],

0 commit comments

Comments
 (0)
Please sign in to comment.