Skip to content

Commit

Permalink
review comment: use body_id
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Apr 22, 2020
1 parent 8e67570 commit c0a9d52
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions src/librustc_typeck/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else if let ty::Param(p) = lhs_ty.kind {
suggest_constraining_param(
self.tcx,
self.body_id,
&mut err,
lhs_ty,
rhs_ty,
&expr,
missing_trait,
p,
false,
Expand Down Expand Up @@ -481,10 +481,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else if let ty::Param(p) = lhs_ty.kind {
suggest_constraining_param(
self.tcx,
self.body_id,
&mut err,
lhs_ty,
rhs_ty,
&expr,
missing_trait,
p,
use_output,
Expand Down Expand Up @@ -938,10 +938,10 @@ fn suggest_impl_missing(err: &mut DiagnosticBuilder<'_>, ty: Ty<'_>, missing_tra

fn suggest_constraining_param(
tcx: TyCtxt<'_>,
body_id: hir::HirId,
mut err: &mut DiagnosticBuilder<'_>,
lhs_ty: Ty<'_>,
rhs_ty: Ty<'_>,
expr: &hir::Expr<'_>,
missing_trait: &str,
p: ty::ParamTy,
set_output: bool,
Expand All @@ -951,33 +951,26 @@ fn suggest_constraining_param(
// Try to find the def-id and details for the parameter p. We have only the index,
// so we have to find the enclosing function's def-id, then look through its declared
// generic parameters to get the declaration.
if let Some(def_id) = hir
.find(hir.get_parent_item(expr.hir_id))
.and_then(|node| node.hir_id())
.and_then(|hir_id| hir.opt_local_def_id(hir_id))
let def_id = hir.body_owner_def_id(hir::BodyId { hir_id: body_id });
let generics = tcx.generics_of(def_id);
let param_def_id = generics.type_param(&p, tcx).def_id;
if let Some(generics) = hir
.as_local_hir_id(param_def_id)
.and_then(|id| hir.find(hir.get_parent_item(id)))
.as_ref()
.and_then(|node| node.generics())
{
let generics = tcx.generics_of(def_id);
let param_def_id = generics.type_param(&p, tcx).def_id;
if let Some(generics) = hir
.as_local_hir_id(param_def_id)
.and_then(|id| hir.find(hir.get_parent_item(id)))
.as_ref()
.and_then(|node| node.generics())
{
let output = if set_output { format!("<Output = {}>", rhs_ty) } else { String::new() };
suggest_constraining_type_param(
tcx,
generics,
&mut err,
&format!("{}", lhs_ty),
&format!("{}{}", missing_trait, output),
None,
);
} else {
let span = tcx.def_span(param_def_id);
err.span_label(span, msg);
}
let output = if set_output { format!("<Output = {}>", rhs_ty) } else { String::new() };
suggest_constraining_type_param(
tcx,
generics,
&mut err,
&format!("{}", lhs_ty),
&format!("{}{}", missing_trait, output),
None,
);
} else {
err.note(&msg);
let span = tcx.def_span(param_def_id);
err.span_label(span, msg);
}
}

0 comments on commit c0a9d52

Please sign in to comment.