Skip to content

Commit 0712fde

Browse files
committed
Reduce verbosity of error
1 parent 43871a9 commit 0712fde

File tree

3 files changed

+24
-43
lines changed

3 files changed

+24
-43
lines changed

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

+21-16
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub trait TypeErrCtxtExt<'tcx> {
106106
obligation: &PredicateObligation<'tcx>,
107107
trait_ref: ty::TraitRef<'tcx>,
108108
err: &mut Diagnostic,
109-
);
109+
) -> bool;
110110

111111
fn report_const_param_not_wf(
112112
&self,
@@ -503,8 +503,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
503503

504504
let mut err = struct_span_err!(self.tcx.sess, span, E0277, "{}", err_msg);
505505

506+
let mut suggested = false;
506507
if is_try_conversion {
507-
self.try_conversion_context(&obligation, trait_ref.skip_binder(), &mut err);
508+
suggested = self.try_conversion_context(&obligation, trait_ref.skip_binder(), &mut err);
508509
}
509510

510511
if is_try_conversion && let Some(ret_span) = self.return_type_span(&obligation) {
@@ -605,8 +606,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
605606

606607
self.suggest_floating_point_literal(&obligation, &mut err, &trait_ref);
607608
self.suggest_dereferencing_index(&obligation, &mut err, trait_predicate);
608-
let mut suggested =
609-
self.suggest_dereferences(&obligation, &mut err, trait_predicate);
609+
suggested |= self.suggest_dereferences(&obligation, &mut err, trait_predicate);
610610
suggested |= self.suggest_fn_call(&obligation, &mut err, trait_predicate);
611611
let impl_candidates = self.find_similar_impl_candidates(trait_predicate);
612612
suggested = if let &[cand] = &impl_candidates[..] {
@@ -961,7 +961,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
961961
obligation: &PredicateObligation<'tcx>,
962962
trait_ref: ty::TraitRef<'tcx>,
963963
err: &mut Diagnostic,
964-
) {
964+
) -> bool {
965965
let span = obligation.cause.span;
966966
struct V<'v> {
967967
search_span: Span,
@@ -986,22 +986,22 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
986986
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. })) => {
987987
body_id
988988
}
989-
_ => return,
989+
_ => return false,
990990
};
991991
let mut v = V { search_span: span, found: None };
992992
v.visit_body(self.tcx.hir().body(*body_id));
993993
let Some(expr) = v.found else {
994-
return;
994+
return false;
995995
};
996996
let Some(typeck) = &self.typeck_results else {
997-
return;
997+
return false;
998998
};
999999
let Some((ObligationCauseCode::QuestionMark, Some(y))) = obligation.cause.code().parent()
10001000
else {
1001-
return;
1001+
return false;
10021002
};
10031003
if !self.tcx.is_diagnostic_item(sym::FromResidual, y.def_id()) {
1004-
return;
1004+
return false;
10051005
}
10061006
let self_ty = trait_ref.self_ty();
10071007
let found_ty = trait_ref.args.get(1).and_then(|a| a.as_type());
@@ -1026,6 +1026,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10261026
Some(arg.as_type()?)
10271027
};
10281028

1029+
let mut suggested = false;
10291030
let mut chain = vec![];
10301031

10311032
// The following logic is simlar to `point_at_chain`, but that's focused on associated types
@@ -1090,6 +1091,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10901091
)
10911092
.must_apply_modulo_regions()
10921093
{
1094+
suggested = true;
10931095
err.span_suggestion_short(
10941096
stmt.span.with_lo(expr.span.hi()),
10951097
"remove this semicolon",
@@ -1146,17 +1148,20 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
11461148
)
11471149
.must_apply_modulo_regions()
11481150
{
1149-
err.span_label(span, format!("this has type `Result<_, {err_ty}>`"));
1151+
if !suggested {
1152+
err.span_label(span, format!("this has type `Result<_, {err_ty}>`"));
1153+
}
11501154
} else {
11511155
err.span_label(
1152-
span,
1153-
format!(
1154-
"this can't be annotated with `?` because it has type `Result<_, {err_ty}>`",
1155-
),
1156-
);
1156+
span,
1157+
format!(
1158+
"this can't be annotated with `?` because it has type `Result<_, {err_ty}>`",
1159+
),
1160+
);
11571161
}
11581162
prev = Some(err_ty);
11591163
}
1164+
suggested
11601165
}
11611166

11621167
fn report_const_param_not_wf(

tests/ui/traits/question-mark-result-err-mismatch.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ fn foo() -> Result<String, String> { //~ NOTE expected `String` because of this
33
let x = test
44
.split_whitespace()
55
.next()
6-
.ok_or_else(|| { //~ NOTE this has type `Result<_, &str>`
6+
.ok_or_else(|| {
77
"Couldn't split the test string"
88
});
99
let one = x
@@ -15,11 +15,9 @@ fn foo() -> Result<String, String> { //~ NOTE expected `String` because of this
1515
//~^ NOTE in this expansion of desugaring of operator `?`
1616
//~| NOTE in this expansion of desugaring of operator `?`
1717
//~| NOTE in this expansion of desugaring of operator `?`
18-
//~| NOTE in this expansion of desugaring of operator `?`
1918
//~| NOTE the trait `From<()>` is not implemented for `String`
2019
//~| NOTE the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
2120
//~| NOTE required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>`
22-
//~| HELP the following other types implement trait `From<T>`:
2321
Ok(one.to_string())
2422
}
2523

@@ -52,11 +50,9 @@ fn baz() -> Result<String, String> { //~ NOTE expected `String` because of this
5250
//~| NOTE in this expansion of desugaring of operator `?`
5351
//~| NOTE in this expansion of desugaring of operator `?`
5452
//~| NOTE in this expansion of desugaring of operator `?`
55-
//~| NOTE in this expansion of desugaring of operator `?`
5653
//~| NOTE the trait `From<()>` is not implemented for `String`
5754
//~| NOTE the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
5855
//~| NOTE required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>`
59-
//~| HELP the following other types implement trait `From<T>`:
6056
Ok(one.to_string())
6157
}
6258

tests/ui/traits/question-mark-result-err-mismatch.stderr

+2-22
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ error[E0277]: `?` couldn't convert the error to `String`
44
LL | fn foo() -> Result<String, String> {
55
| ---------------------- expected `String` because of this
66
...
7-
LL | .ok_or_else(|| {
8-
| __________-
9-
LL | | "Couldn't split the test string"
10-
LL | | });
11-
| |__________- this has type `Result<_, &str>`
12-
...
137
LL | .map_err(|e| {
148
| __________-
159
LL | | e;
@@ -20,17 +14,10 @@ LL | .map(|()| "")?;
2014
| ^ the trait `From<()>` is not implemented for `String`
2115
|
2216
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
23-
= help: the following other types implement trait `From<T>`:
24-
<String as From<char>>
25-
<String as From<Box<str>>>
26-
<String as From<Cow<'a, str>>>
27-
<String as From<&str>>
28-
<String as From<&mut str>>
29-
<String as From<&String>>
3017
= note: required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>`
3118

3219
error[E0277]: `?` couldn't convert the error to `String`
33-
--> $DIR/question-mark-result-err-mismatch.rs:30:25
20+
--> $DIR/question-mark-result-err-mismatch.rs:28:25
3421
|
3522
LL | fn bar() -> Result<(), String> {
3623
| ------------------ expected `String` because of this
@@ -53,7 +40,7 @@ LL | .map_err(|_| ())?;
5340
= note: required for `Result<(), String>` to implement `FromResidual<Result<Infallible, ()>>`
5441

5542
error[E0277]: `?` couldn't convert the error to `String`
56-
--> $DIR/question-mark-result-err-mismatch.rs:50:11
43+
--> $DIR/question-mark-result-err-mismatch.rs:48:11
5744
|
5845
LL | fn baz() -> Result<String, String> {
5946
| ---------------------- expected `String` because of this
@@ -68,13 +55,6 @@ LL | | })?;
6855
| this can't be annotated with `?` because it has type `Result<_, ()>`
6956
|
7057
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
71-
= help: the following other types implement trait `From<T>`:
72-
<String as From<char>>
73-
<String as From<Box<str>>>
74-
<String as From<Cow<'a, str>>>
75-
<String as From<&str>>
76-
<String as From<&mut str>>
77-
<String as From<&String>>
7858
= note: required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>`
7959

8060
error: aborting due to 3 previous errors

0 commit comments

Comments
 (0)