From 1091774c7470b36584b15c6091060367c9f7374e Mon Sep 17 00:00:00 2001 From: sjwang05 <63834813+sjwang05@users.noreply.github.com> Date: Sun, 3 Dec 2023 13:40:33 -0800 Subject: [PATCH] Improve comment phrasing Co-authored-by: oliver <151407407+kwfn@users.noreply.github.com> --- .../src/traits/error_reporting/suggestions.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 8652b6e478f5..7b98706ad53e 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -724,6 +724,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { && let hir::Node::Expr(expr) = self.tcx.hir().get(*arg_hir_id) && let Some(arg_ty) = typeck_results.expr_ty_adjusted_opt(expr) { + // Suggest dereferencing the argument to a function/method call if possible + let mut real_trait_pred = trait_pred; while let Some((parent_code, parent_trait_pred)) = code.parent() { code = parent_code; @@ -763,7 +765,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { { if steps > 0 { // Don't care about `&mut` because `DerefMut` is used less - // often and user will not expect autoderef happens. + // often and user will not expect that an autoderef happens. if let Some(hir::Node::Expr(hir::Expr { kind: hir::ExprKind::AddrOf( @@ -850,6 +852,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { && let hir::Node::Expr(rhs) = self.tcx.hir().get(*rhs_hir_id) && let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs) { + // Suggest dereferencing the LHS, RHS, or both terms of a binop if possible + let trait_pred = predicate.unwrap_or(trait_pred); let lhs_ty = self.tcx.instantiate_bound_regions_with_erased(trait_pred.self_ty()); let lhs_autoderef = (self.autoderef_steps)(lhs_ty); @@ -869,6 +873,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { .rev(); if let Some((lsteps, rsteps)) = autoderefs.find_map(|((lsteps, (l_ty, _)), (rsteps, (r_ty, _)))| { + // Create a new predicate with the dereferenced LHS and RHS + // We simultaneously dereference both sides rather than doing them + // one at a time to account for cases such as &Box == &&T let trait_pred_and_ty = trait_pred.map_bound(|inner| { ( ty::TraitPredicate { @@ -909,6 +916,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { expr = inner; steps -= 1; } + // Empty suggestions with empty spans ICE with debug assertions if steps == 0 { return ( msg.trim_end_matches(" and dereferencing instead"), @@ -936,6 +944,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { format!("{derefs}"), )] }; + // Empty suggestions with empty spans ICE with debug assertions if !prefix_span.is_empty() { suggestion.push((prefix_span, String::new())); }