]> git.lizzy.rs Git - rust.git/commitdiff
review comments
authorEsteban Küber <esteban@kuber.com.ar>
Fri, 6 Jan 2023 00:53:31 +0000 (00:53 +0000)
committerEsteban Küber <esteban@kuber.com.ar>
Mon, 30 Jan 2023 20:12:21 +0000 (20:12 +0000)
compiler/rustc_hir_analysis/src/check/check.rs
compiler/rustc_infer/src/infer/error_reporting/mod.rs
compiler/rustc_middle/src/ty/error.rs

index ccd75ad0ec5a1f28f3933327bc1053d3242c0a34..f7ebacaa8546f4155dbf4e11be18a39499f6f2f8 100644 (file)
@@ -442,12 +442,10 @@ fn check_opaque_meets_bounds<'tcx>(
     match ocx.eq(&misc_cause, param_env, opaque_ty, hidden_ty) {
         Ok(()) => {}
         Err(ty_err) => {
+            let ty_err = ty_err.to_string(tcx);
             tcx.sess.delay_span_bug(
                 span,
-                &format!(
-                    "could not unify `{hidden_ty}` with revealed type:\n{}",
-                    ty_err.to_string(tcx)
-                ),
+                &format!("could not unify `{hidden_ty}` with revealed type:\n{ty_err}"),
             );
         }
     }
index d58a5ceef966b54c0f61d4989da9dfbc0e99b8b5..8ebcd3d54aa7ff7827b9980813a41b9f2d49f5ed 100644 (file)
@@ -1613,7 +1613,7 @@ enum Mismatch<'a> {
                 {
                     format!("expected this to be `{}`", expected)
                 } else {
-                    terr.to_string(self.tcx)
+                    terr.to_string(self.tcx).to_string()
                 };
                 label_or_note(sp, &terr);
                 label_or_note(span, &msg);
index bb87b0eea378b8422ab760745c7e854e78ed42a0..6229c0072f2a79f5397e7d869468e2852db5d2de 100644 (file)
@@ -86,7 +86,7 @@ pub fn involves_regions(self) -> bool {
 /// afterwards to present additional details, particularly when it comes to lifetime-related
 /// errors.
 impl<'tcx> TypeError<'tcx> {
-    pub fn to_string(self, tcx: TyCtxt<'tcx>) -> String {
+    pub fn to_string(self, tcx: TyCtxt<'tcx>) -> Cow<'static, str> {
         use self::TypeError::*;
         fn report_maybe_different(expected: &str, found: &str) -> String {
             // A naive approach to making sure that we're not reporting silly errors such as:
@@ -104,48 +104,52 @@ fn report_maybe_different(expected: &str, found: &str) -> String {
         };
 
         match self {
-            CyclicTy(_) => format!("cyclic type of infinite size"),
-            CyclicConst(_) => format!("encountered a self-referencing constant"),
-            Mismatch => format!("types differ"),
+            CyclicTy(_) => "cyclic type of infinite size".into(),
+            CyclicConst(_) => "encountered a self-referencing constant".into(),
+            Mismatch => "types differ".into(),
             ConstnessMismatch(values) => {
-                format!("expected {} bound, found {} bound", values.expected, values.found)
+                format!("expected {} bound, found {} bound", values.expected, values.found).into()
             }
             PolarityMismatch(values) => {
                 format!("expected {} polarity, found {} polarity", values.expected, values.found)
+                    .into()
             }
             UnsafetyMismatch(values) => {
-                format!("expected {} fn, found {} fn", values.expected, values.found)
+                format!("expected {} fn, found {} fn", values.expected, values.found).into()
             }
             AbiMismatch(values) => {
-                format!("expected {} fn, found {} fn", values.expected, values.found)
+                format!("expected {} fn, found {} fn", values.expected, values.found).into()
             }
-            ArgumentMutability(_) | Mutability => format!("types differ in mutability"),
+            ArgumentMutability(_) | Mutability => "types differ in mutability".into(),
             TupleSize(values) => format!(
                 "expected a tuple with {} element{}, found one with {} element{}",
                 values.expected,
                 pluralize!(values.expected),
                 values.found,
                 pluralize!(values.found)
-            ),
+            )
+            .into(),
             FixedArraySize(values) => format!(
                 "expected an array with a fixed size of {} element{}, found one with {} element{}",
                 values.expected,
                 pluralize!(values.expected),
                 values.found,
                 pluralize!(values.found)
-            ),
-            ArgCount => format!("incorrect number of function parameters"),
-            FieldMisMatch(adt, field) => format!("field type mismatch: {}.{}", adt, field),
-            RegionsDoesNotOutlive(..) => format!("lifetime mismatch"),
+            )
+            .into(),
+            ArgCount => "incorrect number of function parameters".into(),
+            FieldMisMatch(adt, field) => format!("field type mismatch: {}.{}", adt, field).into(),
+            RegionsDoesNotOutlive(..) => "lifetime mismatch".into(),
             // Actually naming the region here is a bit confusing because context is lacking
             RegionsInsufficientlyPolymorphic(..) => {
-                format!("one type is more general than the other")
+                "one type is more general than the other".into()
             }
             RegionsOverlyPolymorphic(br, _) => format!(
                 "expected concrete lifetime, found bound lifetime parameter{}",
                 br_string(br)
-            ),
-            RegionsPlaceholderMismatch => format!("one type is more general than the other"),
+            )
+            .into(),
+            RegionsPlaceholderMismatch => "one type is more general than the other".into(),
             ArgumentSorts(values, _) | Sorts(values) => {
                 let mut expected = values.expected.sort_string(tcx);
                 let mut found = values.found.sort_string(tcx);
@@ -153,7 +157,7 @@ fn report_maybe_different(expected: &str, found: &str) -> String {
                     expected = values.expected.sort_string(tcx);
                     found = values.found.sort_string(tcx);
                 }
-                report_maybe_different(&expected, &found)
+                report_maybe_different(&expected, &found).into()
             }
             Traits(values) => {
                 let (mut expected, mut found) = with_forced_trimmed_paths!((
@@ -165,6 +169,7 @@ fn report_maybe_different(expected: &str, found: &str) -> String {
                     found = tcx.def_path_str(values.found);
                 }
                 report_maybe_different(&format!("trait `{expected}`"), &format!("trait `{found}`"))
+                    .into()
             }
             IntMismatch(ref values) => {
                 let expected = match values.expected {
@@ -175,36 +180,38 @@ fn report_maybe_different(expected: &str, found: &str) -> String {
                     ty::IntVarValue::IntType(ty) => ty.name_str(),
                     ty::IntVarValue::UintType(ty) => ty.name_str(),
                 };
-                format!("expected `{}`, found `{}`", expected, found)
-            }
-            FloatMismatch(ref values) => {
-                format!(
-                    "expected `{}`, found `{}`",
-                    values.expected.name_str(),
-                    values.found.name_str()
-                )
+                format!("expected `{}`, found `{}`", expected, found).into()
             }
+            FloatMismatch(ref values) => format!(
+                "expected `{}`, found `{}`",
+                values.expected.name_str(),
+                values.found.name_str()
+            )
+            .into(),
             VariadicMismatch(ref values) => format!(
                 "expected {} fn, found {} function",
                 if values.expected { "variadic" } else { "non-variadic" },
                 if values.found { "variadic" } else { "non-variadic" }
-            ),
+            )
+            .into(),
             ProjectionMismatched(ref values) => format!(
                 "expected {}, found {}",
                 tcx.def_path_str(values.expected),
                 tcx.def_path_str(values.found)
-            ),
+            )
+            .into(),
             ExistentialMismatch(ref values) => report_maybe_different(
                 &format!("trait `{}`", values.expected),
                 &format!("trait `{}`", values.found),
-            ),
+            )
+            .into(),
             ConstMismatch(ref values) => {
-                format!("expected `{}`, found `{}`", values.expected, values.found)
+                format!("expected `{}`, found `{}`", values.expected, values.found).into()
+            }
+            IntrinsicCast => "cannot coerce intrinsics to function pointers".into(),
+            TargetFeatureCast(_) => {
+                "cannot coerce functions with `#[target_feature]` to safe function pointers".into()
             }
-            IntrinsicCast => format!("cannot coerce intrinsics to function pointers"),
-            TargetFeatureCast(_) => format!(
-                "cannot coerce functions with `#[target_feature]` to safe function pointers"
-            ),
         }
     }
 }
@@ -237,7 +244,7 @@ pub fn must_include_note(self) -> bool {
 }
 
 impl<'tcx> Ty<'tcx> {
-    pub fn sort_string(self, tcx: TyCtxt<'tcx>) -> String {
+    pub fn sort_string(self, tcx: TyCtxt<'tcx>) -> Cow<'static, str> {
         match *self.kind() {
             ty::Foreign(def_id) => format!("extern type `{}`", tcx.def_path_str(def_id)).into(),
             ty::FnDef(def_id, ..) => match tcx.def_kind(def_id) {
@@ -247,7 +254,7 @@ pub fn sort_string(self, tcx: TyCtxt<'tcx>) -> String {
             },
             ty::FnPtr(_) => "fn pointer".into(),
             ty::Dynamic(ref inner, ..) if let Some(principal) = inner.principal() => {
-                format!("`dyn {}`", tcx.def_path_str(principal.def_id()))
+                format!("`dyn {}`", tcx.def_path_str(principal.def_id())).into()
             }
             ty::Dynamic(..) => "trait object".into(),
             ty::Closure(..) => "closure".into(),
@@ -269,7 +276,7 @@ pub fn sort_string(self, tcx: TyCtxt<'tcx>) -> String {
             _ => {
                 let width = tcx.sess.diagnostic_width();
                 let length_limit = std::cmp::max(width / 4, 15);
-                format!("`{}`", tcx.ty_string_with_limit(self, length_limit))
+                format!("`{}`", tcx.ty_string_with_limit(self, length_limit)).into()
             }
         }
     }