Skip to content

Commit 1e8c095

Browse files
committed
review comment: move error logic to different method
1 parent d845685 commit 1e8c095

File tree

2 files changed

+55
-58
lines changed

2 files changed

+55
-58
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

+1-58
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636
//! ```
3737
3838
use crate::FnCtxt;
39-
use rustc_errors::{
40-
struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
41-
};
39+
use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan};
4240
use rustc_hir as hir;
4341
use rustc_hir::def_id::DefId;
4442
use rustc_hir::intravisit::{self, Visitor};
@@ -1740,7 +1738,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
17401738
// label pointing out the cause for the type coercion will be wrong
17411739
// as prior return coercions would not be relevant (#57664).
17421740
let fn_decl = if let (Some(expr), Some(blk_id)) = (expression, blk_id) {
1743-
self.explain_self_literal(fcx, &mut err, expr, expected, found);
17441741
let pointing_at_return_type =
17451742
fcx.suggest_mismatched_types_on_tail(&mut err, expr, expected, found, blk_id);
17461743
if let (Some(cond_expr), true, false) = (
@@ -1813,60 +1810,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18131810
err
18141811
}
18151812

1816-
fn explain_self_literal(
1817-
&self,
1818-
fcx: &FnCtxt<'_, 'tcx>,
1819-
err: &mut Diagnostic,
1820-
expr: &'tcx hir::Expr<'tcx>,
1821-
expected: Ty<'tcx>,
1822-
found: Ty<'tcx>,
1823-
) {
1824-
match expr.peel_drop_temps().kind {
1825-
hir::ExprKind::Struct(
1826-
hir::QPath::Resolved(
1827-
None,
1828-
hir::Path { res: hir::def::Res::SelfTyAlias { alias_to, .. }, span, .. },
1829-
),
1830-
..,
1831-
)
1832-
| hir::ExprKind::Call(
1833-
hir::Expr {
1834-
kind:
1835-
hir::ExprKind::Path(hir::QPath::Resolved(
1836-
None,
1837-
hir::Path {
1838-
res: hir::def::Res::SelfTyAlias { alias_to, .. },
1839-
span,
1840-
..
1841-
},
1842-
)),
1843-
..
1844-
},
1845-
..,
1846-
) => {
1847-
if let Some(hir::Node::Item(hir::Item {
1848-
kind: hir::ItemKind::Impl(hir::Impl { self_ty, .. }),
1849-
..
1850-
})) = fcx.tcx.hir().get_if_local(*alias_to)
1851-
{
1852-
err.span_label(self_ty.span, "this is the type of the `Self` literal");
1853-
}
1854-
if let ty::Adt(e_def, e_args) = expected.kind()
1855-
&& let ty::Adt(f_def, _f_args) = found.kind()
1856-
&& e_def == f_def
1857-
{
1858-
err.span_suggestion_verbose(
1859-
*span,
1860-
"use the type name directly",
1861-
fcx.tcx.value_path_str_with_args(*alias_to, e_args),
1862-
Applicability::MaybeIncorrect,
1863-
);
1864-
}
1865-
}
1866-
_ => {}
1867-
}
1868-
}
1869-
18701813
/// Checks whether the return type is unsized via an obligation, which makes
18711814
/// sure we consider `dyn Trait: Sized` where clauses, which are trivially
18721815
/// false but technically valid for typeck.

compiler/rustc_hir_typeck/src/demand.rs

+54
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3131
}
3232

3333
self.annotate_alternative_method_deref(err, expr, error);
34+
self.explain_self_literal(err, expr, expected, expr_ty);
3435

3536
// Use `||` to give these suggestions a precedence
3637
let suggested = self.suggest_missing_parentheses(err, expr)
@@ -1027,6 +1028,59 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10271028
return false;
10281029
}
10291030

1031+
fn explain_self_literal(
1032+
&self,
1033+
err: &mut Diagnostic,
1034+
expr: &hir::Expr<'tcx>,
1035+
expected: Ty<'tcx>,
1036+
found: Ty<'tcx>,
1037+
) {
1038+
match expr.peel_drop_temps().kind {
1039+
hir::ExprKind::Struct(
1040+
hir::QPath::Resolved(
1041+
None,
1042+
hir::Path { res: hir::def::Res::SelfTyAlias { alias_to, .. }, span, .. },
1043+
),
1044+
..,
1045+
)
1046+
| hir::ExprKind::Call(
1047+
hir::Expr {
1048+
kind:
1049+
hir::ExprKind::Path(hir::QPath::Resolved(
1050+
None,
1051+
hir::Path {
1052+
res: hir::def::Res::SelfTyAlias { alias_to, .. },
1053+
span,
1054+
..
1055+
},
1056+
)),
1057+
..
1058+
},
1059+
..,
1060+
) => {
1061+
if let Some(hir::Node::Item(hir::Item {
1062+
kind: hir::ItemKind::Impl(hir::Impl { self_ty, .. }),
1063+
..
1064+
})) = self.tcx.hir().get_if_local(*alias_to)
1065+
{
1066+
err.span_label(self_ty.span, "this is the type of the `Self` literal");
1067+
}
1068+
if let ty::Adt(e_def, e_args) = expected.kind()
1069+
&& let ty::Adt(f_def, _f_args) = found.kind()
1070+
&& e_def == f_def
1071+
{
1072+
err.span_suggestion_verbose(
1073+
*span,
1074+
"use the type name directly",
1075+
self.tcx.value_path_str_with_args(*alias_to, e_args),
1076+
Applicability::MaybeIncorrect,
1077+
);
1078+
}
1079+
}
1080+
_ => {}
1081+
}
1082+
}
1083+
10301084
fn note_wrong_return_ty_due_to_generic_arg(
10311085
&self,
10321086
err: &mut Diagnostic,

0 commit comments

Comments
 (0)