review comment: move error logic to different method
This commit is contained in:
parent
d8456855f5
commit
1e8c0951e9
@ -36,9 +36,7 @@
|
|||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
use crate::FnCtxt;
|
use crate::FnCtxt;
|
||||||
use rustc_errors::{
|
use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan};
|
||||||
struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
|
|
||||||
};
|
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_hir::def_id::DefId;
|
use rustc_hir::def_id::DefId;
|
||||||
use rustc_hir::intravisit::{self, Visitor};
|
use rustc_hir::intravisit::{self, Visitor};
|
||||||
@ -1740,7 +1738,6 @@ fn report_return_mismatched_types<'a>(
|
|||||||
// label pointing out the cause for the type coercion will be wrong
|
// label pointing out the cause for the type coercion will be wrong
|
||||||
// as prior return coercions would not be relevant (#57664).
|
// as prior return coercions would not be relevant (#57664).
|
||||||
let fn_decl = if let (Some(expr), Some(blk_id)) = (expression, blk_id) {
|
let fn_decl = if let (Some(expr), Some(blk_id)) = (expression, blk_id) {
|
||||||
self.explain_self_literal(fcx, &mut err, expr, expected, found);
|
|
||||||
let pointing_at_return_type =
|
let pointing_at_return_type =
|
||||||
fcx.suggest_mismatched_types_on_tail(&mut err, expr, expected, found, blk_id);
|
fcx.suggest_mismatched_types_on_tail(&mut err, expr, expected, found, blk_id);
|
||||||
if let (Some(cond_expr), true, false) = (
|
if let (Some(cond_expr), true, false) = (
|
||||||
@ -1813,60 +1810,6 @@ fn report_return_mismatched_types<'a>(
|
|||||||
err
|
err
|
||||||
}
|
}
|
||||||
|
|
||||||
fn explain_self_literal(
|
|
||||||
&self,
|
|
||||||
fcx: &FnCtxt<'_, 'tcx>,
|
|
||||||
err: &mut Diagnostic,
|
|
||||||
expr: &'tcx hir::Expr<'tcx>,
|
|
||||||
expected: Ty<'tcx>,
|
|
||||||
found: Ty<'tcx>,
|
|
||||||
) {
|
|
||||||
match expr.peel_drop_temps().kind {
|
|
||||||
hir::ExprKind::Struct(
|
|
||||||
hir::QPath::Resolved(
|
|
||||||
None,
|
|
||||||
hir::Path { res: hir::def::Res::SelfTyAlias { alias_to, .. }, span, .. },
|
|
||||||
),
|
|
||||||
..,
|
|
||||||
)
|
|
||||||
| hir::ExprKind::Call(
|
|
||||||
hir::Expr {
|
|
||||||
kind:
|
|
||||||
hir::ExprKind::Path(hir::QPath::Resolved(
|
|
||||||
None,
|
|
||||||
hir::Path {
|
|
||||||
res: hir::def::Res::SelfTyAlias { alias_to, .. },
|
|
||||||
span,
|
|
||||||
..
|
|
||||||
},
|
|
||||||
)),
|
|
||||||
..
|
|
||||||
},
|
|
||||||
..,
|
|
||||||
) => {
|
|
||||||
if let Some(hir::Node::Item(hir::Item {
|
|
||||||
kind: hir::ItemKind::Impl(hir::Impl { self_ty, .. }),
|
|
||||||
..
|
|
||||||
})) = fcx.tcx.hir().get_if_local(*alias_to)
|
|
||||||
{
|
|
||||||
err.span_label(self_ty.span, "this is the type of the `Self` literal");
|
|
||||||
}
|
|
||||||
if let ty::Adt(e_def, e_args) = expected.kind()
|
|
||||||
&& let ty::Adt(f_def, _f_args) = found.kind()
|
|
||||||
&& e_def == f_def
|
|
||||||
{
|
|
||||||
err.span_suggestion_verbose(
|
|
||||||
*span,
|
|
||||||
"use the type name directly",
|
|
||||||
fcx.tcx.value_path_str_with_args(*alias_to, e_args),
|
|
||||||
Applicability::MaybeIncorrect,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Checks whether the return type is unsized via an obligation, which makes
|
/// Checks whether the return type is unsized via an obligation, which makes
|
||||||
/// sure we consider `dyn Trait: Sized` where clauses, which are trivially
|
/// sure we consider `dyn Trait: Sized` where clauses, which are trivially
|
||||||
/// false but technically valid for typeck.
|
/// false but technically valid for typeck.
|
||||||
|
@ -31,6 +31,7 @@ pub fn emit_type_mismatch_suggestions(
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.annotate_alternative_method_deref(err, expr, error);
|
self.annotate_alternative_method_deref(err, expr, error);
|
||||||
|
self.explain_self_literal(err, expr, expected, expr_ty);
|
||||||
|
|
||||||
// Use `||` to give these suggestions a precedence
|
// Use `||` to give these suggestions a precedence
|
||||||
let suggested = self.suggest_missing_parentheses(err, expr)
|
let suggested = self.suggest_missing_parentheses(err, expr)
|
||||||
@ -1027,6 +1028,59 @@ pub(crate) fn is_destruct_assignment_desugaring(&self, expr: &hir::Expr<'_>) ->
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn explain_self_literal(
|
||||||
|
&self,
|
||||||
|
err: &mut Diagnostic,
|
||||||
|
expr: &hir::Expr<'tcx>,
|
||||||
|
expected: Ty<'tcx>,
|
||||||
|
found: Ty<'tcx>,
|
||||||
|
) {
|
||||||
|
match expr.peel_drop_temps().kind {
|
||||||
|
hir::ExprKind::Struct(
|
||||||
|
hir::QPath::Resolved(
|
||||||
|
None,
|
||||||
|
hir::Path { res: hir::def::Res::SelfTyAlias { alias_to, .. }, span, .. },
|
||||||
|
),
|
||||||
|
..,
|
||||||
|
)
|
||||||
|
| hir::ExprKind::Call(
|
||||||
|
hir::Expr {
|
||||||
|
kind:
|
||||||
|
hir::ExprKind::Path(hir::QPath::Resolved(
|
||||||
|
None,
|
||||||
|
hir::Path {
|
||||||
|
res: hir::def::Res::SelfTyAlias { alias_to, .. },
|
||||||
|
span,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
)),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..,
|
||||||
|
) => {
|
||||||
|
if let Some(hir::Node::Item(hir::Item {
|
||||||
|
kind: hir::ItemKind::Impl(hir::Impl { self_ty, .. }),
|
||||||
|
..
|
||||||
|
})) = self.tcx.hir().get_if_local(*alias_to)
|
||||||
|
{
|
||||||
|
err.span_label(self_ty.span, "this is the type of the `Self` literal");
|
||||||
|
}
|
||||||
|
if let ty::Adt(e_def, e_args) = expected.kind()
|
||||||
|
&& let ty::Adt(f_def, _f_args) = found.kind()
|
||||||
|
&& e_def == f_def
|
||||||
|
{
|
||||||
|
err.span_suggestion_verbose(
|
||||||
|
*span,
|
||||||
|
"use the type name directly",
|
||||||
|
self.tcx.value_path_str_with_args(*alias_to, e_args),
|
||||||
|
Applicability::MaybeIncorrect,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn note_wrong_return_ty_due_to_generic_arg(
|
fn note_wrong_return_ty_due_to_generic_arg(
|
||||||
&self,
|
&self,
|
||||||
err: &mut Diagnostic,
|
err: &mut Diagnostic,
|
||||||
|
Loading…
Reference in New Issue
Block a user