1
Fork 0

Reduce verbosity of error

This commit is contained in:
Esteban Küber 2023-10-07 03:58:45 +00:00
parent 98e5317173
commit 70fe624b3d
3 changed files with 24 additions and 43 deletions

View file

@ -112,7 +112,7 @@ pub trait TypeErrCtxtExt<'tcx> {
obligation: &PredicateObligation<'tcx>, obligation: &PredicateObligation<'tcx>,
trait_ref: ty::TraitRef<'tcx>, trait_ref: ty::TraitRef<'tcx>,
err: &mut Diagnostic, err: &mut Diagnostic,
); ) -> bool;
fn report_const_param_not_wf( fn report_const_param_not_wf(
&self, &self,
@ -517,8 +517,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
let mut err = struct_span_err!(self.tcx.sess, span, E0277, "{}", err_msg); let mut err = struct_span_err!(self.tcx.sess, span, E0277, "{}", err_msg);
let mut suggested = false;
if is_try_conversion { if is_try_conversion {
self.try_conversion_context(&obligation, trait_ref.skip_binder(), &mut err); suggested = self.try_conversion_context(&obligation, trait_ref.skip_binder(), &mut err);
} }
if is_try_conversion && let Some(ret_span) = self.return_type_span(&obligation) { if is_try_conversion && let Some(ret_span) = self.return_type_span(&obligation) {
@ -621,8 +622,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
self.suggest_floating_point_literal(&obligation, &mut err, &trait_ref); self.suggest_floating_point_literal(&obligation, &mut err, &trait_ref);
self.suggest_dereferencing_index(&obligation, &mut err, trait_predicate); self.suggest_dereferencing_index(&obligation, &mut err, trait_predicate);
let mut suggested = suggested |= self.suggest_dereferences(&obligation, &mut err, trait_predicate);
self.suggest_dereferences(&obligation, &mut err, trait_predicate);
suggested |= self.suggest_fn_call(&obligation, &mut err, trait_predicate); suggested |= self.suggest_fn_call(&obligation, &mut err, trait_predicate);
let impl_candidates = self.find_similar_impl_candidates(trait_predicate); let impl_candidates = self.find_similar_impl_candidates(trait_predicate);
suggested = if let &[cand] = &impl_candidates[..] { suggested = if let &[cand] = &impl_candidates[..] {
@ -1002,7 +1002,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
obligation: &PredicateObligation<'tcx>, obligation: &PredicateObligation<'tcx>,
trait_ref: ty::TraitRef<'tcx>, trait_ref: ty::TraitRef<'tcx>,
err: &mut Diagnostic, err: &mut Diagnostic,
) { ) -> bool {
let span = obligation.cause.span; let span = obligation.cause.span;
struct V<'v> { struct V<'v> {
search_span: Span, search_span: Span,
@ -1027,22 +1027,22 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. })) => { Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. })) => {
body_id body_id
} }
_ => return, _ => return false,
}; };
let mut v = V { search_span: span, found: None }; let mut v = V { search_span: span, found: None };
v.visit_body(self.tcx.hir().body(*body_id)); v.visit_body(self.tcx.hir().body(*body_id));
let Some(expr) = v.found else { let Some(expr) = v.found else {
return; return false;
}; };
let Some(typeck) = &self.typeck_results else { let Some(typeck) = &self.typeck_results else {
return; return false;
}; };
let Some((ObligationCauseCode::QuestionMark, Some(y))) = obligation.cause.code().parent() let Some((ObligationCauseCode::QuestionMark, Some(y))) = obligation.cause.code().parent()
else { else {
return; return false;
}; };
if !self.tcx.is_diagnostic_item(sym::FromResidual, y.def_id()) { if !self.tcx.is_diagnostic_item(sym::FromResidual, y.def_id()) {
return; return false;
} }
let self_ty = trait_ref.self_ty(); let self_ty = trait_ref.self_ty();
let found_ty = trait_ref.args.get(1).and_then(|a| a.as_type()); let found_ty = trait_ref.args.get(1).and_then(|a| a.as_type());
@ -1067,6 +1067,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
Some(arg.as_type()?) Some(arg.as_type()?)
}; };
let mut suggested = false;
let mut chain = vec![]; let mut chain = vec![];
// The following logic is simlar to `point_at_chain`, but that's focused on associated types // The following logic is simlar to `point_at_chain`, but that's focused on associated types
@ -1135,6 +1136,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
) )
.must_apply_modulo_regions() .must_apply_modulo_regions()
{ {
suggested = true;
err.span_suggestion_short( err.span_suggestion_short(
stmt.span.with_lo(expr.span.hi()), stmt.span.with_lo(expr.span.hi()),
"remove this semicolon", "remove this semicolon",
@ -1193,17 +1195,20 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
) )
.must_apply_modulo_regions() .must_apply_modulo_regions()
{ {
err.span_label(span, format!("this has type `Result<_, {err_ty}>`")); if !suggested {
err.span_label(span, format!("this has type `Result<_, {err_ty}>`"));
}
} else { } else {
err.span_label( err.span_label(
span, span,
format!( format!(
"this can't be annotated with `?` because it has type `Result<_, {err_ty}>`", "this can't be annotated with `?` because it has type `Result<_, {err_ty}>`",
), ),
); );
} }
prev = Some(err_ty); prev = Some(err_ty);
} }
suggested
} }
fn report_const_param_not_wf( fn report_const_param_not_wf(

View file

@ -3,7 +3,7 @@ fn foo() -> Result<String, String> { //~ NOTE expected `String` because of this
let x = test let x = test
.split_whitespace() .split_whitespace()
.next() .next()
.ok_or_else(|| { //~ NOTE this has type `Result<_, &str>` .ok_or_else(|| {
"Couldn't split the test string" "Couldn't split the test string"
}); });
let one = x let one = x
@ -15,11 +15,9 @@ fn foo() -> Result<String, String> { //~ NOTE expected `String` because of this
//~^ NOTE in this expansion of desugaring of operator `?` //~^ NOTE in this expansion of desugaring of operator `?`
//~| NOTE in this expansion of desugaring of operator `?` //~| NOTE in this expansion of desugaring of operator `?`
//~| NOTE in this expansion of desugaring of operator `?` //~| NOTE in this expansion of desugaring of operator `?`
//~| NOTE in this expansion of desugaring of operator `?`
//~| NOTE the trait `From<()>` is not implemented for `String` //~| NOTE the trait `From<()>` is not implemented for `String`
//~| NOTE the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait //~| NOTE the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
//~| NOTE required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>` //~| NOTE required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>`
//~| HELP the following other types implement trait `From<T>`:
Ok(one.to_string()) Ok(one.to_string())
} }
@ -52,11 +50,9 @@ fn baz() -> Result<String, String> { //~ NOTE expected `String` because of this
//~| NOTE in this expansion of desugaring of operator `?` //~| NOTE in this expansion of desugaring of operator `?`
//~| NOTE in this expansion of desugaring of operator `?` //~| NOTE in this expansion of desugaring of operator `?`
//~| NOTE in this expansion of desugaring of operator `?` //~| NOTE in this expansion of desugaring of operator `?`
//~| NOTE in this expansion of desugaring of operator `?`
//~| NOTE the trait `From<()>` is not implemented for `String` //~| NOTE the trait `From<()>` is not implemented for `String`
//~| NOTE the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait //~| NOTE the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
//~| NOTE required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>` //~| NOTE required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>`
//~| HELP the following other types implement trait `From<T>`:
Ok(one.to_string()) Ok(one.to_string())
} }

View file

@ -4,12 +4,6 @@ error[E0277]: `?` couldn't convert the error to `String`
LL | fn foo() -> Result<String, String> { LL | fn foo() -> Result<String, String> {
| ---------------------- expected `String` because of this | ---------------------- expected `String` because of this
... ...
LL | .ok_or_else(|| {
| __________-
LL | | "Couldn't split the test string"
LL | | });
| |__________- this has type `Result<_, &str>`
...
LL | .map_err(|e| { LL | .map_err(|e| {
| __________- | __________-
LL | | e; LL | | e;
@ -20,17 +14,10 @@ LL | .map(|()| "")?;
| ^ the trait `From<()>` is not implemented for `String` | ^ the trait `From<()>` is not implemented for `String`
| |
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= help: the following other types implement trait `From<T>`:
<String as From<char>>
<String as From<Box<str>>>
<String as From<Cow<'a, str>>>
<String as From<&str>>
<String as From<&mut str>>
<String as From<&String>>
= note: required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>` = note: required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>`
error[E0277]: `?` couldn't convert the error to `String` error[E0277]: `?` couldn't convert the error to `String`
--> $DIR/question-mark-result-err-mismatch.rs:30:25 --> $DIR/question-mark-result-err-mismatch.rs:28:25
| |
LL | fn bar() -> Result<(), String> { LL | fn bar() -> Result<(), String> {
| ------------------ expected `String` because of this | ------------------ expected `String` because of this
@ -53,7 +40,7 @@ LL | .map_err(|_| ())?;
= note: required for `Result<(), String>` to implement `FromResidual<Result<Infallible, ()>>` = note: required for `Result<(), String>` to implement `FromResidual<Result<Infallible, ()>>`
error[E0277]: `?` couldn't convert the error to `String` error[E0277]: `?` couldn't convert the error to `String`
--> $DIR/question-mark-result-err-mismatch.rs:50:11 --> $DIR/question-mark-result-err-mismatch.rs:48:11
| |
LL | fn baz() -> Result<String, String> { LL | fn baz() -> Result<String, String> {
| ---------------------- expected `String` because of this | ---------------------- expected `String` because of this
@ -68,13 +55,6 @@ LL | | })?;
| this can't be annotated with `?` because it has type `Result<_, ()>` | this can't be annotated with `?` because it has type `Result<_, ()>`
| |
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= help: the following other types implement trait `From<T>`:
<String as From<char>>
<String as From<Box<str>>>
<String as From<Cow<'a, str>>>
<String as From<&str>>
<String as From<&mut str>>
<String as From<&String>>
= note: required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>` = note: required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>`
error: aborting due to 3 previous errors error: aborting due to 3 previous errors