1
Fork 0

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`
This commit is contained in:
bors 2023-05-29 07:10:44 +00:00
commit 70e04bd88d
45 changed files with 308 additions and 287 deletions

View file

@ -251,7 +251,7 @@ pub fn explain_lint_level_source(
}
LintLevelSource::Node { name: lint_attr_name, span, reason, .. } => {
if let Some(rationale) = reason {
err.note(rationale.as_str());
err.note(rationale.to_string());
}
err.span_note_once(span, "the lint level is defined here");
if lint_attr_name.as_str() != name {

View file

@ -104,7 +104,7 @@ pub fn report_unstable(
suggestion: Option<(Span, String, String, Applicability)>,
is_soft: bool,
span: Span,
soft_handler: impl FnOnce(&'static Lint, Span, &str),
soft_handler: impl FnOnce(&'static Lint, Span, String),
) {
let msg = match reason {
Some(r) => format!("use of unstable library feature '{}': {}", feature, r),
@ -112,7 +112,7 @@ pub fn report_unstable(
};
if is_soft {
soft_handler(SOFT_UNSTABLE, span, &msg)
soft_handler(SOFT_UNSTABLE, span, msg)
} else {
let mut err =
feature_err_issue(&sess.parse_sess, feature, span, GateIssue::Library(issue), msg);
@ -225,7 +225,7 @@ pub fn deprecation_message_and_lint(
pub fn early_report_deprecation(
lint_buffer: &mut LintBuffer,
message: &str,
message: String,
suggestion: Option<Symbol>,
lint: &'static Lint,
span: Span,
@ -241,7 +241,7 @@ pub fn early_report_deprecation(
fn late_report_deprecation(
tcx: TyCtxt<'_>,
message: &str,
message: String,
suggestion: Option<Symbol>,
lint: &'static Lint,
span: Span,
@ -396,7 +396,7 @@ impl<'tcx> TyCtxt<'tcx> {
late_report_deprecation(
self,
&deprecation_message(
deprecation_message(
is_in_effect,
depr_attr.since,
depr_attr.note,
@ -619,7 +619,7 @@ impl<'tcx> TyCtxt<'tcx> {
allow_unstable: AllowUnstable,
unmarked: impl FnOnce(Span, DefId),
) -> bool {
let soft_handler = |lint, span, msg: &_| {
let soft_handler = |lint, span, msg: String| {
self.struct_span_lint_hir(lint, id.unwrap_or(hir::CRATE_HIR_ID), span, msg, |lint| lint)
};
let eval_result =

View file

@ -732,7 +732,11 @@ impl<'tcx> TyCtxt<'tcx> {
/// Constructs a `RegionKind::ReError` lifetime and registers a `delay_span_bug` with the given
/// `msg` to ensure it gets used.
#[track_caller]
pub fn mk_re_error_with_message<S: Into<MultiSpan>>(self, span: S, msg: &str) -> Region<'tcx> {
pub fn mk_re_error_with_message<S: Into<MultiSpan>>(
self,
span: S,
msg: &'static str,
) -> Region<'tcx> {
let reported = self.sess.delay_span_bug(span, msg);
self.mk_re_error(reported)
}
@ -759,7 +763,7 @@ impl<'tcx> TyCtxt<'tcx> {
self,
ty: Ty<'tcx>,
span: S,
msg: &str,
msg: &'static str,
) -> Const<'tcx> {
let reported = self.sess.delay_span_bug(span, msg);
self.mk_const(ty::ConstKind::Error(reported), ty)

View file

@ -1,5 +1,6 @@
//! Diagnostics related methods for `Ty`.
use std::borrow::Cow;
use std::ops::ControlFlow;
use crate::ty::{
@ -384,22 +385,18 @@ pub fn suggest_constraining_type_params<'a>(
if suggestions.len() == 1 {
let (span, suggestion, msg) = suggestions.pop().unwrap();
let s;
let msg = match msg {
SuggestChangingConstraintsMessage::RestrictBoundFurther => {
"consider further restricting this bound"
Cow::from("consider further restricting this bound")
}
SuggestChangingConstraintsMessage::RestrictType { ty } => {
s = format!("consider restricting type parameter `{}`", ty);
&s
Cow::from(format!("consider restricting type parameter `{}`", ty))
}
SuggestChangingConstraintsMessage::RestrictTypeFurther { ty } => {
s = format!("consider further restricting type parameter `{}`", ty);
&s
Cow::from(format!("consider further restricting type parameter `{}`", ty))
}
SuggestChangingConstraintsMessage::RemovingQSized => {
"consider removing the `?Sized` bound to make the type parameter `Sized`"
Cow::from("consider removing the `?Sized` bound to make the type parameter `Sized`")
}
};