|
1 | 1 | use super::{
|
2 |
| - EvaluationResult, Obligation, ObligationCause, ObligationCauseCode, PredicateObligation, |
3 |
| - SelectionContext, |
| 2 | + DerivedObligationCause, EvaluationResult, ImplDerivedObligationCause, Obligation, |
| 3 | + ObligationCause, ObligationCauseCode, PredicateObligation, SelectionContext, |
4 | 4 | };
|
5 | 5 |
|
6 | 6 | use crate::autoderef::Autoderef;
|
@@ -496,50 +496,78 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
496 | 496 | trait_pred: ty::PolyTraitPredicate<'tcx>,
|
497 | 497 | ) -> bool {
|
498 | 498 | // It only make sense when suggesting dereferences for arguments
|
499 |
| - let code = if let ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } = |
500 |
| - obligation.cause.code() |
501 |
| - { |
502 |
| - parent_code.clone() |
503 |
| - } else { |
| 499 | + let ObligationCauseCode::FunctionArgumentObligation { .. } = obligation.cause.code() else { |
504 | 500 | return false;
|
505 | 501 | };
|
506 | 502 | let param_env = obligation.param_env;
|
507 | 503 | let body_id = obligation.cause.body_id;
|
508 | 504 | let span = obligation.cause.span;
|
509 |
| - let real_trait_pred = match &*code { |
510 |
| - ObligationCauseCode::ImplDerivedObligation(cause) => cause.derived.parent_trait_pred, |
511 |
| - ObligationCauseCode::DerivedObligation(cause) |
512 |
| - | ObligationCauseCode::BuiltinDerivedObligation(cause) => cause.parent_trait_pred, |
513 |
| - _ => trait_pred, |
514 |
| - }; |
515 |
| - let Some(real_ty) = real_trait_pred.self_ty().no_bound_vars() else { |
516 |
| - return false; |
517 |
| - }; |
| 505 | + let mut real_trait_pred = trait_pred; |
| 506 | + let mut code = obligation.cause.code(); |
| 507 | + loop { |
| 508 | + match &code { |
| 509 | + ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } => { |
| 510 | + code = &parent_code; |
| 511 | + } |
| 512 | + ObligationCauseCode::ImplDerivedObligation(box ImplDerivedObligationCause { |
| 513 | + derived: DerivedObligationCause { parent_code, parent_trait_pred }, |
| 514 | + .. |
| 515 | + }) |
| 516 | + | ObligationCauseCode::BuiltinDerivedObligation(DerivedObligationCause { |
| 517 | + parent_code, |
| 518 | + parent_trait_pred, |
| 519 | + }) |
| 520 | + | ObligationCauseCode::DerivedObligation(DerivedObligationCause { |
| 521 | + parent_code, |
| 522 | + parent_trait_pred, |
| 523 | + }) => { |
| 524 | + code = &parent_code; |
| 525 | + real_trait_pred = *parent_trait_pred; |
| 526 | + } |
| 527 | + _ => break, |
| 528 | + }; |
| 529 | + let Some(real_ty) = real_trait_pred.self_ty().no_bound_vars() else { |
| 530 | + continue; |
| 531 | + }; |
518 | 532 |
|
519 |
| - if let ty::Ref(region, base_ty, mutbl) = *real_ty.kind() { |
520 |
| - let mut autoderef = Autoderef::new(self, param_env, body_id, span, base_ty, span); |
521 |
| - if let Some(steps) = autoderef.find_map(|(ty, steps)| { |
522 |
| - // Re-add the `&` |
523 |
| - let ty = self.tcx.mk_ref(region, TypeAndMut { ty, mutbl }); |
524 |
| - let obligation = |
525 |
| - self.mk_trait_obligation_with_new_self_ty(param_env, real_trait_pred, ty); |
526 |
| - Some(steps).filter(|_| self.predicate_may_hold(&obligation)) |
527 |
| - }) { |
528 |
| - if steps > 0 { |
529 |
| - if let Ok(src) = self.tcx.sess.source_map().span_to_snippet(span) { |
530 |
| - // Don't care about `&mut` because `DerefMut` is used less |
531 |
| - // often and user will not expect autoderef happens. |
532 |
| - if src.starts_with('&') && !src.starts_with("&mut ") { |
533 |
| - let derefs = "*".repeat(steps); |
534 |
| - err.span_suggestion( |
535 |
| - span, |
536 |
| - "consider adding dereference here", |
537 |
| - format!("&{}{}", derefs, &src[1..]), |
538 |
| - Applicability::MachineApplicable, |
539 |
| - ); |
540 |
| - return true; |
| 533 | + if let ty::Ref(region, base_ty, mutbl) = *real_ty.kind() { |
| 534 | + let mut autoderef = Autoderef::new(self, param_env, body_id, span, base_ty, span); |
| 535 | + if let Some(steps) = autoderef.find_map(|(ty, steps)| { |
| 536 | + // Re-add the `&` |
| 537 | + let ty = self.tcx.mk_ref(region, TypeAndMut { ty, mutbl }); |
| 538 | + let obligation = |
| 539 | + self.mk_trait_obligation_with_new_self_ty(param_env, real_trait_pred, ty); |
| 540 | + Some(steps).filter(|_| self.predicate_may_hold(&obligation)) |
| 541 | + }) { |
| 542 | + if steps > 0 { |
| 543 | + if let Ok(src) = self.tcx.sess.source_map().span_to_snippet(span) { |
| 544 | + // Don't care about `&mut` because `DerefMut` is used less |
| 545 | + // often and user will not expect autoderef happens. |
| 546 | + if src.starts_with('&') && !src.starts_with("&mut ") { |
| 547 | + let derefs = "*".repeat(steps); |
| 548 | + err.span_suggestion( |
| 549 | + span, |
| 550 | + "consider dereferencing here", |
| 551 | + format!("&{}{}", derefs, &src[1..]), |
| 552 | + Applicability::MachineApplicable, |
| 553 | + ); |
| 554 | + return true; |
| 555 | + } |
541 | 556 | }
|
542 | 557 | }
|
| 558 | + } else if real_trait_pred != trait_pred { |
| 559 | + // This branch addresses #87437. |
| 560 | + let obligation = |
| 561 | + self.mk_trait_obligation_with_new_self_ty(param_env, real_trait_pred, base_ty); |
| 562 | + if self.predicate_may_hold(&obligation) { |
| 563 | + err.span_suggestion_verbose( |
| 564 | + span.shrink_to_lo(), |
| 565 | + "consider dereferencing here", |
| 566 | + "*".to_string(), |
| 567 | + Applicability::MachineApplicable, |
| 568 | + ); |
| 569 | + return true; |
| 570 | + } |
543 | 571 | }
|
544 | 572 | }
|
545 | 573 | }
|
|
0 commit comments