Skip to content

Commit c0ce0f3

Browse files
committed
Display short types for unimplemented trait
1 parent ef32456 commit c0ce0f3

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ impl<'tcx> OnUnimplementedDirective {
669669
options.iter().filter_map(|(k, v)| v.clone().map(|v| (*k, v))).collect();
670670

671671
for command in self.subcommands.iter().chain(Some(self)).rev() {
672+
debug!(?command);
672673
if let Some(ref condition) = command.condition
673674
&& !attr::eval_condition(condition, &tcx.sess, Some(tcx.features()), &mut |cfg| {
674675
let value = cfg.value.map(|v| {
@@ -824,7 +825,11 @@ impl<'tcx> OnUnimplementedFormatString {
824825
.filter_map(|param| {
825826
let value = match param.kind {
826827
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
827-
trait_ref.args[param.index as usize].to_string()
828+
if let Some(ty) = trait_ref.args[param.index as usize].as_type() {
829+
tcx.short_ty_string(ty, &mut None)
830+
} else {
831+
trait_ref.args[param.index as usize].to_string()
832+
}
828833
}
829834
GenericParamDefKind::Lifetime => return None,
830835
};

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3507,7 +3507,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
35073507
}
35083508
ObligationCauseCode::OpaqueReturnType(expr_info) => {
35093509
if let Some((expr_ty, expr_span)) = expr_info {
3510-
let expr_ty = with_forced_trimmed_paths!(self.ty_to_string(expr_ty));
3510+
let expr_ty =
3511+
with_forced_trimmed_paths!(self.tcx.short_ty_string(expr_ty, &mut None));
35113512
err.span_label(
35123513
expr_span,
35133514
with_forced_trimmed_paths!(format!(

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+1
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
389389
kind: _,
390390
} = *obligation.cause.code()
391391
{
392+
debug!("ObligationCauseCode::CompareImplItemObligation");
392393
return self.report_extra_impl_obligation(
393394
span,
394395
impl_item_def_id,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ compile-flags: --diagnostic-width=60 -Z write-long-types-to-disk=yes
2+
//@ normalize-stderr-test: "long-type-\d+" -> "long-type-hash"
3+
4+
pub fn foo() -> impl std::fmt::Display {
5+
//~^ ERROR doesn't implement `std::fmt::Display`
6+
Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(
7+
Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(
8+
Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(
9+
Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(
10+
Some(Some(Some(Some(Some(Some(Some(Some(())))))))),
11+
))))))))))),
12+
))))))))))),
13+
))))))))))),
14+
)))))))))))
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0277]: `Option<Option<Option<...>>>` doesn't implement `std::fmt::Display`
2+
--> $DIR/on_unimplemented_long_types.rs:4:17
3+
|
4+
LL | pub fn foo() -> impl std::fmt::Display {
5+
| ^^^^^^^^^^^^^^^^^^^^^^ `Option<Option<Option<...>>>` cannot be formatted with the default formatter
6+
LL |
7+
LL | / Some(Some(Some(Some(Some(Some(Some(Some(Some(S...
8+
LL | | Some(Some(Some(Some(Some(Some(Some(Some(So...
9+
LL | | Some(Some(Some(Some(Some(Some(Some(Som...
10+
LL | | Some(Some(Some(Some(Some(Some(Some...
11+
... |
12+
LL | | ))))))))))),
13+
LL | | )))))))))))
14+
| |_______________- return type was inferred to be `Option<Option<Option<...>>>` here
15+
|
16+
= help: the trait `std::fmt::Display` is not implemented for `Option<Option<Option<...>>>`
17+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
18+
19+
error: aborting due to 1 previous error
20+
21+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)