From f93aa75426ef42d31deacbe145db50d5f5d75a17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 26 Oct 2023 19:03:29 +0000 Subject: [PATCH] address review comments --- .../src/traits/error_reporting/suggestions.rs | 48 ++++++++----------- .../sealed-traits/sealed-trait-local.stderr | 13 +++-- 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 8232eef9451c4..67a13de0062b1 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -2682,35 +2682,27 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { .flatten() .chain(impls_of.blanket_impls().iter()) .collect::>(); - match &impls[..] { - [] => {} - [only] => { - err.help(with_no_trimmed_paths!(format!( - "type `{}` implements the trait", - tcx.type_of(*only).instantiate_identity(), - ))); - } - impls => { - let mut types = impls.iter() - .map(|t| with_no_trimmed_paths!(format!( - " - {}\n", - tcx.type_of(*t).instantiate_identity(), - ))) - .collect::>(); - let post = if types.len() > 9 { - let len = types.len(); - types.truncate(8); - format!("and {} others", len - 8) - } else { - String::new() - }; - err.help(format!( - "the following types implement the trait:\n{}{post}", - types.join(""), - )); - } + if !impls.is_empty() { + let len = impls.len(); + let mut types = impls.iter() + .map(|t| with_no_trimmed_paths!(format!( + " {}", + tcx.type_of(*t).instantiate_identity(), + ))) + .collect::>(); + let post = if types.len() > 9 { + types.truncate(8); + format!("\nand {} others", len - 8) + } else { + String::new() + }; + err.help(format!( + "the following type{} implement{} the trait:\n{}{post}", + pluralize!(len), + if len == 1 { "s" } else { "" }, + types.join("\n"), + )); } - } } } else { diff --git a/tests/ui/privacy/sealed-traits/sealed-trait-local.stderr b/tests/ui/privacy/sealed-traits/sealed-trait-local.stderr index 6f9777be9b61d..a7f77a1c0c020 100644 --- a/tests/ui/privacy/sealed-traits/sealed-trait-local.stderr +++ b/tests/ui/privacy/sealed-traits/sealed-trait-local.stderr @@ -10,7 +10,8 @@ note: required by a bound in `a::Sealed` LL | pub trait Sealed: self::b::Hidden { | ^^^^^^^^^^^^^^^ required by this bound in `Sealed` = note: `Sealed` is a "sealed trait", because to implement it you also need to implement `a::b::Hidden`, which is not accessible; this is usually done to force you to use one of the provided types that already implement it - = help: type `a::X` implements the trait + = help: the following type implements the trait: + a::X error[E0277]: the trait bound `S: d::Hidden` is not satisfied --> $DIR/sealed-trait-local.rs:53:20 @@ -25,9 +26,8 @@ LL | pub trait Sealed: self::d::Hidden { | ^^^^^^^^^^^^^^^ required by this bound in `Sealed` = note: `Sealed` is a "sealed trait", because to implement it you also need to implement `c::d::Hidden`, which is not accessible; this is usually done to force you to use one of the provided types that already implement it = help: the following types implement the trait: - - c::X - - c::Y - + c::X + c::Y error[E0277]: the trait bound `S: f::Hidden` is not satisfied --> $DIR/sealed-trait-local.rs:54:20 @@ -42,9 +42,8 @@ LL | pub trait Sealed: self::f::Hidden { | ^^^^^^^^^^^^^^^ required by this bound in `Sealed` = note: `Sealed` is a "sealed trait", because to implement it you also need to implement `e::f::Hidden`, which is not accessible; this is usually done to force you to use one of the provided types that already implement it = help: the following types implement the trait: - - e::X - - e::Y - + e::X + e::Y error: aborting due to 3 previous errors