Skip to content

Commit bb20f12

Browse files
committed
Reduce verbosity of error
1 parent 53a0ae0 commit bb20f12

File tree

3 files changed

+20
-35
lines changed

3 files changed

+20
-35
lines changed

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

+18-15
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub trait TypeErrCtxtExt<'tcx> {
110110
obligation: &PredicateObligation<'tcx>,
111111
trait_ref: ty::TraitRef<'tcx>,
112112
err: &mut Diagnostic,
113-
);
113+
) -> bool;
114114

115115
fn report_const_param_not_wf(
116116
&self,
@@ -506,8 +506,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
506506

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

509+
let mut suggested = false;
509510
if is_try_conversion {
510-
self.try_conversion_context(&obligation, trait_ref.skip_binder(), &mut err);
511+
suggested = self.try_conversion_context(&obligation, trait_ref.skip_binder(), &mut err);
511512
}
512513

513514
if is_try_conversion && let Some(ret_span) = self.return_type_span(&obligation) {
@@ -608,8 +609,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
608609

609610
self.suggest_floating_point_literal(&obligation, &mut err, &trait_ref);
610611
self.suggest_dereferencing_index(&obligation, &mut err, trait_predicate);
611-
let mut suggested =
612-
self.suggest_dereferences(&obligation, &mut err, trait_predicate);
612+
suggested |= self.suggest_dereferences(&obligation, &mut err, trait_predicate);
613613
suggested |= self.suggest_fn_call(&obligation, &mut err, trait_predicate);
614614
let impl_candidates = self.find_similar_impl_candidates(trait_predicate);
615615
suggested = if let &[cand] = &impl_candidates[..] {
@@ -944,7 +944,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
944944
obligation: &PredicateObligation<'tcx>,
945945
trait_ref: ty::TraitRef<'tcx>,
946946
err: &mut Diagnostic,
947-
) {
947+
) -> bool {
948948
let span = obligation.cause.span;
949949
struct V<'v> {
950950
search_span: Span,
@@ -969,22 +969,22 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
969969
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. })) => {
970970
body_id
971971
}
972-
_ => return,
972+
_ => return false,
973973
};
974974
let mut v = V { search_span: span, found: None };
975975
v.visit_body(self.tcx.hir().body(*body_id));
976976
let Some(expr) = v.found else {
977-
return;
977+
return false;
978978
};
979979
let Some(typeck) = &self.typeck_results else {
980-
return;
980+
return false;
981981
};
982982
let Some((ObligationCauseCode::QuestionMark, Some(y))) = obligation.cause.code().parent()
983983
else {
984-
return;
984+
return false;
985985
};
986986
if !self.tcx.is_diagnostic_item(sym::FromResidual, y.def_id()) {
987-
return;
987+
return false;
988988
}
989989
let self_ty = trait_ref.self_ty();
990990
let found_ty = trait_ref.args.get(1).and_then(|a| a.as_type());
@@ -1009,6 +1009,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10091009
Some(arg.as_type()?)
10101010
};
10111011

1012+
let mut suggested = false;
10121013
let mut chain = vec![];
10131014

10141015
// The following logic is simlar to `point_at_chain`, but that's focused on associated types
@@ -1073,6 +1074,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10731074
)
10741075
.must_apply_modulo_regions()
10751076
{
1077+
suggested = true;
10761078
err.span_suggestion_short(
10771079
stmt.span.with_lo(expr.span.hi()),
10781080
"remove this semicolon",
@@ -1132,14 +1134,15 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
11321134
err.span_label(span, format!("this has type `Result<_, {err_ty}>`"));
11331135
} else {
11341136
err.span_label(
1135-
span,
1136-
format!(
1137-
"this can't be annotated with `?` because it has type `Result<_, {err_ty}>`",
1138-
),
1139-
);
1137+
span,
1138+
format!(
1139+
"this can't be annotated with `?` because it has type `Result<_, {err_ty}>`",
1140+
),
1141+
);
11401142
}
11411143
prev = Some(err_ty);
11421144
}
1145+
suggested
11431146
}
11441147

11451148
fn report_const_param_not_wf(

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

-4
Original file line numberDiff line numberDiff line change
@@ -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-16
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,10 @@ LL | .map(|()| "")?;
2020
| ^ the trait `From<()>` is not implemented for `String`
2121
|
2222
= 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>>
3023
= note: required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>`
3124

3225
error[E0277]: `?` couldn't convert the error to `String`
33-
--> $DIR/question-mark-result-err-mismatch.rs:30:25
26+
--> $DIR/question-mark-result-err-mismatch.rs:28:25
3427
|
3528
LL | fn bar() -> Result<(), String> {
3629
| ------------------ expected `String` because of this
@@ -53,7 +46,7 @@ LL | .map_err(|_| ())?;
5346
= note: required for `Result<(), String>` to implement `FromResidual<Result<Infallible, ()>>`
5447

5548
error[E0277]: `?` couldn't convert the error to `String`
56-
--> $DIR/question-mark-result-err-mismatch.rs:50:11
49+
--> $DIR/question-mark-result-err-mismatch.rs:48:11
5750
|
5851
LL | fn baz() -> Result<String, String> {
5952
| ---------------------- expected `String` because of this
@@ -68,13 +61,6 @@ LL | | })?;
6861
| this can't be annotated with `?` because it has type `Result<_, ()>`
6962
|
7063
= 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>>
7864
= note: required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>`
7965

8066
error: aborting due to 3 previous errors

0 commit comments

Comments
 (0)