Skip to content

Commit

Permalink
review comment: move error logic to different method
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Nov 16, 2023
1 parent d845685 commit 1e8c095
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 58 deletions.
59 changes: 1 addition & 58 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
//! ```

use crate::FnCtxt;
use rustc_errors::{
struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
};
use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan};
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{self, Visitor};
Expand Down Expand Up @@ -1740,7 +1738,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
// label pointing out the cause for the type coercion will be wrong
// as prior return coercions would not be relevant (#57664).
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 =
fcx.suggest_mismatched_types_on_tail(&mut err, expr, expected, found, blk_id);
if let (Some(cond_expr), true, false) = (
Expand Down Expand Up @@ -1813,60 +1810,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
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
/// sure we consider `dyn Trait: Sized` where clauses, which are trivially
/// false but technically valid for typeck.
Expand Down
54 changes: 54 additions & 0 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}

self.annotate_alternative_method_deref(err, expr, error);
self.explain_self_literal(err, expr, expected, expr_ty);

// Use `||` to give these suggestions a precedence
let suggested = self.suggest_missing_parentheses(err, expr)
Expand Down Expand Up @@ -1027,6 +1028,59 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
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(
&self,
err: &mut Diagnostic,
Expand Down

0 comments on commit 1e8c095

Please sign in to comment.