Skip to content

Commit a1404a9

Browse files
authored
Rollup merge of #72456 - ldm0:dereftrait, r=estebank
Try to suggest dereferences on trait selection failed Fixes #39029 Fixes #62530 This PR consists of two parts: 1. Decouple `Autoderef` with `FnCtxt` and move `Autoderef` to `librustc_trait_selection`. 2. Try to suggest dereferences when trait selection failed. The first is needed because: 1. For suggesting dereferences, the struct `Autoderef` should be used. But before this PR, it is placed in `librustc_typeck`, which depends on `librustc_trait_selection`. But trait selection error emitting happens in `librustc_trait_selection`, if we want to use `Autoderef` in it, dependency loop is inevitable. So I moved the `Autoderef` to `librustc_trait_selection`. 2. Before this PR, `FnCtxt` is coupled to `Autoderef`, and `FnCtxt` only exists in `librustc_typeck`. So decoupling is needed. After this PR, we can get suggestion like this: ``` error[E0277]: the trait bound `&Baz: Happy` is not satisfied --> $DIR/trait-suggest-deferences-multiple.rs:34:9 | LL | fn foo<T>(_: T) where T: Happy {} | ----- required by this bound in `foo` ... LL | foo(&baz); | ^^^^ | | | the trait `Happy` is not implemented for `&Baz` | help: consider adding dereference here: `&***baz` error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. ``` r? @estebank
2 parents 033013c + f1e0710 commit a1404a9

23 files changed

+594
-250
lines changed
+229
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
use crate::traits::query::evaluate_obligation::InferCtxtExt;
2+
use crate::traits::{self, TraitEngine};
3+
use rustc_errors::struct_span_err;
4+
use rustc_hir as hir;
5+
use rustc_infer::infer::InferCtxt;
6+
use rustc_middle::ty::{self, TraitRef, Ty, TyCtxt, WithConstness};
7+
use rustc_middle::ty::{ToPredicate, TypeFoldable};
8+
use rustc_session::DiagnosticMessageId;
9+
use rustc_span::symbol::Ident;
10+
use rustc_span::Span;
11+
12+
#[derive(Copy, Clone, Debug)]
13+
pub enum AutoderefKind {
14+
Builtin,
15+
Overloaded,
16+
}
17+
18+
struct AutoderefSnapshot<'tcx> {
19+
at_start: bool,
20+
reached_recursion_limit: bool,
21+
steps: Vec<(Ty<'tcx>, AutoderefKind)>,
22+
cur_ty: Ty<'tcx>,
23+
obligations: Vec<traits::PredicateObligation<'tcx>>,
24+
}
25+
26+
pub struct Autoderef<'a, 'tcx> {
27+
// Meta infos:
28+
infcx: &'a InferCtxt<'a, 'tcx>,
29+
span: Span,
30+
body_id: hir::HirId,
31+
param_env: ty::ParamEnv<'tcx>,
32+
33+
// Current state:
34+
state: AutoderefSnapshot<'tcx>,
35+
36+
// Configurations:
37+
include_raw_pointers: bool,
38+
silence_errors: bool,
39+
}
40+
41+
impl<'a, 'tcx> Iterator for Autoderef<'a, 'tcx> {
42+
type Item = (Ty<'tcx>, usize);
43+
44+
fn next(&mut self) -> Option<Self::Item> {
45+
let tcx = self.infcx.tcx;
46+
47+
debug!("autoderef: steps={:?}, cur_ty={:?}", self.state.steps, self.state.cur_ty);
48+
if self.state.at_start {
49+
self.state.at_start = false;
50+
debug!("autoderef stage #0 is {:?}", self.state.cur_ty);
51+
return Some((self.state.cur_ty, 0));
52+
}
53+
54+
// If we have reached the recursion limit, error gracefully.
55+
if !tcx.sess.recursion_limit().value_within_limit(self.state.steps.len()) {
56+
if !self.silence_errors {
57+
report_autoderef_recursion_limit_error(tcx, self.span, self.state.cur_ty);
58+
}
59+
self.state.reached_recursion_limit = true;
60+
return None;
61+
}
62+
63+
if self.state.cur_ty.is_ty_var() {
64+
return None;
65+
}
66+
67+
// Otherwise, deref if type is derefable:
68+
let (kind, new_ty) =
69+
if let Some(mt) = self.state.cur_ty.builtin_deref(self.include_raw_pointers) {
70+
(AutoderefKind::Builtin, mt.ty)
71+
} else if let Some(ty) = self.overloaded_deref_ty(self.state.cur_ty) {
72+
(AutoderefKind::Overloaded, ty)
73+
} else {
74+
return None;
75+
};
76+
77+
if new_ty.references_error() {
78+
return None;
79+
}
80+
81+
self.state.steps.push((self.state.cur_ty, kind));
82+
debug!(
83+
"autoderef stage #{:?} is {:?} from {:?}",
84+
self.step_count(),
85+
new_ty,
86+
(self.state.cur_ty, kind)
87+
);
88+
self.state.cur_ty = new_ty;
89+
90+
Some((self.state.cur_ty, self.step_count()))
91+
}
92+
}
93+
94+
impl<'a, 'tcx> Autoderef<'a, 'tcx> {
95+
pub fn new(
96+
infcx: &'a InferCtxt<'a, 'tcx>,
97+
param_env: ty::ParamEnv<'tcx>,
98+
body_id: hir::HirId,
99+
span: Span,
100+
base_ty: Ty<'tcx>,
101+
) -> Autoderef<'a, 'tcx> {
102+
Autoderef {
103+
infcx,
104+
span,
105+
body_id,
106+
param_env,
107+
state: AutoderefSnapshot {
108+
steps: vec![],
109+
cur_ty: infcx.resolve_vars_if_possible(&base_ty),
110+
obligations: vec![],
111+
at_start: true,
112+
reached_recursion_limit: false,
113+
},
114+
include_raw_pointers: false,
115+
silence_errors: false,
116+
}
117+
}
118+
119+
fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
120+
debug!("overloaded_deref_ty({:?})", ty);
121+
122+
let tcx = self.infcx.tcx;
123+
124+
// <ty as Deref>
125+
let trait_ref = TraitRef {
126+
def_id: tcx.lang_items().deref_trait()?,
127+
substs: tcx.mk_substs_trait(ty, &[]),
128+
};
129+
130+
let cause = traits::ObligationCause::misc(self.span, self.body_id);
131+
132+
let obligation = traits::Obligation::new(
133+
cause.clone(),
134+
self.param_env,
135+
trait_ref.without_const().to_predicate(tcx),
136+
);
137+
if !self.infcx.predicate_may_hold(&obligation) {
138+
debug!("overloaded_deref_ty: cannot match obligation");
139+
return None;
140+
}
141+
142+
let mut fulfillcx = traits::FulfillmentContext::new_in_snapshot();
143+
let normalized_ty = fulfillcx.normalize_projection_type(
144+
&self.infcx,
145+
self.param_env,
146+
ty::ProjectionTy::from_ref_and_name(tcx, trait_ref, Ident::from_str("Target")),
147+
cause,
148+
);
149+
if let Err(e) = fulfillcx.select_where_possible(&self.infcx) {
150+
// This shouldn't happen, except for evaluate/fulfill mismatches,
151+
// but that's not a reason for an ICE (`predicate_may_hold` is conservative
152+
// by design).
153+
debug!("overloaded_deref_ty: encountered errors {:?} while fulfilling", e);
154+
return None;
155+
}
156+
let obligations = fulfillcx.pending_obligations();
157+
debug!("overloaded_deref_ty({:?}) = ({:?}, {:?})", ty, normalized_ty, obligations);
158+
self.state.obligations.extend(obligations);
159+
160+
Some(self.infcx.resolve_vars_if_possible(&normalized_ty))
161+
}
162+
163+
/// Returns the final type we ended up with, which may be an inference
164+
/// variable (we will resolve it first, if we want).
165+
pub fn final_ty(&self, resolve: bool) -> Ty<'tcx> {
166+
if resolve {
167+
self.infcx.resolve_vars_if_possible(&self.state.cur_ty)
168+
} else {
169+
self.state.cur_ty
170+
}
171+
}
172+
173+
pub fn step_count(&self) -> usize {
174+
self.state.steps.len()
175+
}
176+
177+
pub fn into_obligations(self) -> Vec<traits::PredicateObligation<'tcx>> {
178+
self.state.obligations
179+
}
180+
181+
pub fn steps(&self) -> &[(Ty<'tcx>, AutoderefKind)] {
182+
&self.state.steps
183+
}
184+
185+
pub fn span(&self) -> Span {
186+
self.span.clone()
187+
}
188+
189+
pub fn reached_recursion_limit(&self) -> bool {
190+
self.state.reached_recursion_limit
191+
}
192+
193+
/// also dereference through raw pointer types
194+
/// e.g., assuming ptr_to_Foo is the type `*const Foo`
195+
/// fcx.autoderef(span, ptr_to_Foo) => [*const Foo]
196+
/// fcx.autoderef(span, ptr_to_Foo).include_raw_ptrs() => [*const Foo, Foo]
197+
pub fn include_raw_pointers(mut self) -> Self {
198+
self.include_raw_pointers = true;
199+
self
200+
}
201+
202+
pub fn silence_errors(mut self) -> Self {
203+
self.silence_errors = true;
204+
self
205+
}
206+
}
207+
208+
pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Span, ty: Ty<'tcx>) {
209+
// We've reached the recursion limit, error gracefully.
210+
let suggested_limit = tcx.sess.recursion_limit() * 2;
211+
let msg = format!("reached the recursion limit while auto-dereferencing `{:?}`", ty);
212+
let error_id = (DiagnosticMessageId::ErrorId(55), Some(span), msg);
213+
let fresh = tcx.sess.one_time_diagnostics.borrow_mut().insert(error_id);
214+
if fresh {
215+
struct_span_err!(
216+
tcx.sess,
217+
span,
218+
E0055,
219+
"reached the recursion limit while auto-dereferencing `{:?}`",
220+
ty
221+
)
222+
.span_label(span, "deref recursion limit reached")
223+
.help(&format!(
224+
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate (`{}`)",
225+
suggested_limit, tcx.crate_name,
226+
))
227+
.emit();
228+
}
229+
}

src/librustc_trait_selection/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ extern crate log;
2828
#[macro_use]
2929
extern crate rustc_middle;
3030

31+
pub mod autoderef;
3132
pub mod infer;
3233
pub mod opaque_types;
3334
pub mod traits;

src/librustc_trait_selection/traits/error_reporting/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
402402
err.span_label(enclosing_scope_span, s.as_str());
403403
}
404404

405+
self.suggest_dereferences(&obligation, &mut err, &trait_ref, points_at_arg);
405406
self.suggest_borrow_on_unsized_slice(&obligation.cause.code, &mut err);
406407
self.suggest_fn_call(&obligation, &mut err, &trait_ref, points_at_arg);
407408
self.suggest_remove_reference(&obligation, &mut err, &trait_ref);

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

+66-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::{
33
SelectionContext,
44
};
55

6+
use crate::autoderef::Autoderef;
67
use crate::infer::InferCtxt;
78
use crate::traits::normalize_projection_type;
89

@@ -13,11 +14,11 @@ use rustc_hir::def_id::DefId;
1314
use rustc_hir::intravisit::Visitor;
1415
use rustc_hir::lang_items;
1516
use rustc_hir::{AsyncGeneratorKind, GeneratorKind, Node};
16-
use rustc_middle::ty::TypeckTables;
1717
use rustc_middle::ty::{
1818
self, suggest_constraining_type_param, AdtKind, DefIdTree, Infer, InferTy, ToPredicate, Ty,
1919
TyCtxt, TypeFoldable, WithConstness,
2020
};
21+
use rustc_middle::ty::{TypeAndMut, TypeckTables};
2122
use rustc_span::symbol::{kw, sym, Ident, Symbol};
2223
use rustc_span::{MultiSpan, Span, DUMMY_SP};
2324
use std::fmt;
@@ -48,6 +49,14 @@ pub trait InferCtxtExt<'tcx> {
4849
err: &mut DiagnosticBuilder<'_>,
4950
);
5051

52+
fn suggest_dereferences(
53+
&self,
54+
obligation: &PredicateObligation<'tcx>,
55+
err: &mut DiagnosticBuilder<'tcx>,
56+
trait_ref: &ty::PolyTraitRef<'tcx>,
57+
points_at_arg: bool,
58+
);
59+
5160
fn get_closure_name(
5261
&self,
5362
def_id: DefId,
@@ -450,6 +459,62 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
450459
}
451460
}
452461

462+
/// When after several dereferencing, the reference satisfies the trait
463+
/// binding. This function provides dereference suggestion for this
464+
/// specific situation.
465+
fn suggest_dereferences(
466+
&self,
467+
obligation: &PredicateObligation<'tcx>,
468+
err: &mut DiagnosticBuilder<'tcx>,
469+
trait_ref: &ty::PolyTraitRef<'tcx>,
470+
points_at_arg: bool,
471+
) {
472+
// It only make sense when suggesting dereferences for arguments
473+
if !points_at_arg {
474+
return;
475+
}
476+
let param_env = obligation.param_env;
477+
let body_id = obligation.cause.body_id;
478+
let span = obligation.cause.span;
479+
let real_trait_ref = match &obligation.cause.code {
480+
ObligationCauseCode::ImplDerivedObligation(cause)
481+
| ObligationCauseCode::DerivedObligation(cause)
482+
| ObligationCauseCode::BuiltinDerivedObligation(cause) => &cause.parent_trait_ref,
483+
_ => trait_ref,
484+
};
485+
let real_ty = match real_trait_ref.self_ty().no_bound_vars() {
486+
Some(ty) => ty,
487+
None => return,
488+
};
489+
490+
if let ty::Ref(region, base_ty, mutbl) = real_ty.kind {
491+
let mut autoderef = Autoderef::new(self, param_env, body_id, span, base_ty);
492+
if let Some(steps) = autoderef.find_map(|(ty, steps)| {
493+
// Re-add the `&`
494+
let ty = self.tcx.mk_ref(region, TypeAndMut { ty, mutbl });
495+
let obligation =
496+
self.mk_trait_obligation_with_new_self_ty(param_env, real_trait_ref, ty);
497+
Some(steps).filter(|_| self.predicate_may_hold(&obligation))
498+
}) {
499+
if steps > 0 {
500+
if let Ok(src) = self.tcx.sess.source_map().span_to_snippet(span) {
501+
// Don't care about `&mut` because `DerefMut` is used less
502+
// often and user will not expect autoderef happens.
503+
if src.starts_with("&") && !src.starts_with("&mut ") {
504+
let derefs = "*".repeat(steps);
505+
err.span_suggestion(
506+
span,
507+
"consider adding dereference here",
508+
format!("&{}{}", derefs, &src[1..]),
509+
Applicability::MachineApplicable,
510+
);
511+
}
512+
}
513+
}
514+
}
515+
}
516+
}
517+
453518
/// When encountering an assignment of an unsized trait, like `let x = ""[..];`, provide a
454519
/// suggestion to borrow the initializer in order to use have a slice instead.
455520
fn suggest_borrow_on_unsized_slice(

0 commit comments

Comments
 (0)