2020-06-20 05:29:13 -05:00
|
|
|
use crate::traits::query::evaluate_obligation::InferCtxtExt;
|
|
|
|
use crate::traits::{self, TraitEngine};
|
|
|
|
use rustc_errors::struct_span_err;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_infer::infer::InferCtxt;
|
2021-12-11 22:34:46 -06:00
|
|
|
use rustc_middle::ty::{self, TraitRef, Ty, TyCtxt};
|
2022-06-17 07:15:00 -05:00
|
|
|
use rustc_middle::ty::{ToPredicate, TypeVisitable};
|
2022-03-20 14:02:18 -05:00
|
|
|
use rustc_session::Limit;
|
2021-05-10 11:23:32 -05:00
|
|
|
use rustc_span::def_id::LOCAL_CRATE;
|
2020-06-20 05:29:13 -05:00
|
|
|
use rustc_span::Span;
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum AutoderefKind {
|
|
|
|
Builtin,
|
|
|
|
Overloaded,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AutoderefSnapshot<'tcx> {
|
|
|
|
at_start: bool,
|
|
|
|
reached_recursion_limit: bool,
|
|
|
|
steps: Vec<(Ty<'tcx>, AutoderefKind)>,
|
|
|
|
cur_ty: Ty<'tcx>,
|
|
|
|
obligations: Vec<traits::PredicateObligation<'tcx>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Autoderef<'a, 'tcx> {
|
|
|
|
// Meta infos:
|
|
|
|
infcx: &'a InferCtxt<'a, 'tcx>,
|
|
|
|
span: Span,
|
2020-07-25 06:04:13 -05:00
|
|
|
overloaded_span: Span,
|
2020-06-20 05:29:13 -05:00
|
|
|
body_id: hir::HirId,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
|
|
|
|
// Current state:
|
|
|
|
state: AutoderefSnapshot<'tcx>,
|
|
|
|
|
|
|
|
// Configurations:
|
|
|
|
include_raw_pointers: bool,
|
|
|
|
silence_errors: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Iterator for Autoderef<'a, 'tcx> {
|
|
|
|
type Item = (Ty<'tcx>, usize);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
|
|
|
|
debug!("autoderef: steps={:?}, cur_ty={:?}", self.state.steps, self.state.cur_ty);
|
|
|
|
if self.state.at_start {
|
|
|
|
self.state.at_start = false;
|
|
|
|
debug!("autoderef stage #0 is {:?}", self.state.cur_ty);
|
|
|
|
return Some((self.state.cur_ty, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have reached the recursion limit, error gracefully.
|
2021-07-04 13:02:51 -05:00
|
|
|
if !tcx.recursion_limit().value_within_limit(self.state.steps.len()) {
|
2020-06-20 05:29:13 -05:00
|
|
|
if !self.silence_errors {
|
|
|
|
report_autoderef_recursion_limit_error(tcx, self.span, self.state.cur_ty);
|
|
|
|
}
|
|
|
|
self.state.reached_recursion_limit = true;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.state.cur_ty.is_ty_var() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, deref if type is derefable:
|
|
|
|
let (kind, new_ty) =
|
|
|
|
if let Some(mt) = self.state.cur_ty.builtin_deref(self.include_raw_pointers) {
|
|
|
|
(AutoderefKind::Builtin, mt.ty)
|
|
|
|
} else if let Some(ty) = self.overloaded_deref_ty(self.state.cur_ty) {
|
|
|
|
(AutoderefKind::Overloaded, ty)
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
if new_ty.references_error() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.state.steps.push((self.state.cur_ty, kind));
|
|
|
|
debug!(
|
|
|
|
"autoderef stage #{:?} is {:?} from {:?}",
|
|
|
|
self.step_count(),
|
|
|
|
new_ty,
|
|
|
|
(self.state.cur_ty, kind)
|
|
|
|
);
|
|
|
|
self.state.cur_ty = new_ty;
|
|
|
|
|
|
|
|
Some((self.state.cur_ty, self.step_count()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Autoderef<'a, 'tcx> {
|
|
|
|
pub fn new(
|
|
|
|
infcx: &'a InferCtxt<'a, 'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
body_id: hir::HirId,
|
|
|
|
span: Span,
|
|
|
|
base_ty: Ty<'tcx>,
|
2020-07-25 06:04:13 -05:00
|
|
|
overloaded_span: Span,
|
2020-06-20 05:29:13 -05:00
|
|
|
) -> Autoderef<'a, 'tcx> {
|
|
|
|
Autoderef {
|
|
|
|
infcx,
|
|
|
|
span,
|
2020-07-25 06:04:13 -05:00
|
|
|
overloaded_span,
|
2020-06-20 05:29:13 -05:00
|
|
|
body_id,
|
|
|
|
param_env,
|
|
|
|
state: AutoderefSnapshot {
|
|
|
|
steps: vec![],
|
2020-10-23 19:21:18 -05:00
|
|
|
cur_ty: infcx.resolve_vars_if_possible(base_ty),
|
2020-06-20 05:29:13 -05:00
|
|
|
obligations: vec![],
|
|
|
|
at_start: true,
|
|
|
|
reached_recursion_limit: false,
|
|
|
|
},
|
|
|
|
include_raw_pointers: false,
|
|
|
|
silence_errors: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
|
|
|
|
debug!("overloaded_deref_ty({:?})", ty);
|
|
|
|
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
|
|
|
|
// <ty as Deref>
|
|
|
|
let trait_ref = TraitRef {
|
|
|
|
def_id: tcx.lang_items().deref_trait()?,
|
|
|
|
substs: tcx.mk_substs_trait(ty, &[]),
|
|
|
|
};
|
|
|
|
|
|
|
|
let cause = traits::ObligationCause::misc(self.span, self.body_id);
|
|
|
|
|
|
|
|
let obligation = traits::Obligation::new(
|
|
|
|
cause.clone(),
|
|
|
|
self.param_env,
|
2021-09-15 19:54:50 -05:00
|
|
|
ty::Binder::dummy(trait_ref).without_const().to_predicate(tcx),
|
2020-06-20 05:29:13 -05:00
|
|
|
);
|
|
|
|
if !self.infcx.predicate_may_hold(&obligation) {
|
|
|
|
debug!("overloaded_deref_ty: cannot match obligation");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut fulfillcx = traits::FulfillmentContext::new_in_snapshot();
|
|
|
|
let normalized_ty = fulfillcx.normalize_projection_type(
|
|
|
|
&self.infcx,
|
|
|
|
self.param_env,
|
2021-02-11 15:02:27 -06:00
|
|
|
ty::ProjectionTy {
|
|
|
|
item_def_id: tcx.lang_items().deref_target()?,
|
|
|
|
substs: trait_ref.substs,
|
|
|
|
},
|
2020-06-20 05:29:13 -05:00
|
|
|
cause,
|
|
|
|
);
|
2021-11-08 09:35:23 -06:00
|
|
|
let errors = fulfillcx.select_where_possible(&self.infcx);
|
|
|
|
if !errors.is_empty() {
|
2020-06-20 05:29:13 -05:00
|
|
|
// This shouldn't happen, except for evaluate/fulfill mismatches,
|
|
|
|
// but that's not a reason for an ICE (`predicate_may_hold` is conservative
|
|
|
|
// by design).
|
2021-11-08 09:35:23 -06:00
|
|
|
debug!("overloaded_deref_ty: encountered errors {:?} while fulfilling", errors);
|
2020-06-20 05:29:13 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let obligations = fulfillcx.pending_obligations();
|
|
|
|
debug!("overloaded_deref_ty({:?}) = ({:?}, {:?})", ty, normalized_ty, obligations);
|
|
|
|
self.state.obligations.extend(obligations);
|
|
|
|
|
2020-10-23 19:21:18 -05:00
|
|
|
Some(self.infcx.resolve_vars_if_possible(normalized_ty))
|
2020-06-20 05:29:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the final type we ended up with, which may be an inference
|
|
|
|
/// variable (we will resolve it first, if we want).
|
|
|
|
pub fn final_ty(&self, resolve: bool) -> Ty<'tcx> {
|
|
|
|
if resolve {
|
2020-10-23 19:21:18 -05:00
|
|
|
self.infcx.resolve_vars_if_possible(self.state.cur_ty)
|
2020-06-20 05:29:13 -05:00
|
|
|
} else {
|
|
|
|
self.state.cur_ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn step_count(&self) -> usize {
|
|
|
|
self.state.steps.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn into_obligations(self) -> Vec<traits::PredicateObligation<'tcx>> {
|
|
|
|
self.state.obligations
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn steps(&self) -> &[(Ty<'tcx>, AutoderefKind)] {
|
|
|
|
&self.state.steps
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn span(&self) -> Span {
|
2020-08-07 17:27:46 -05:00
|
|
|
self.span
|
2020-06-20 05:29:13 -05:00
|
|
|
}
|
|
|
|
|
2020-07-25 06:04:13 -05:00
|
|
|
pub fn overloaded_span(&self) -> Span {
|
|
|
|
self.overloaded_span
|
|
|
|
}
|
|
|
|
|
2020-06-20 05:29:13 -05:00
|
|
|
pub fn reached_recursion_limit(&self) -> bool {
|
|
|
|
self.state.reached_recursion_limit
|
|
|
|
}
|
|
|
|
|
|
|
|
/// also dereference through raw pointer types
|
|
|
|
/// e.g., assuming ptr_to_Foo is the type `*const Foo`
|
|
|
|
/// fcx.autoderef(span, ptr_to_Foo) => [*const Foo]
|
|
|
|
/// fcx.autoderef(span, ptr_to_Foo).include_raw_ptrs() => [*const Foo, Foo]
|
|
|
|
pub fn include_raw_pointers(mut self) -> Self {
|
|
|
|
self.include_raw_pointers = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn silence_errors(mut self) -> Self {
|
|
|
|
self.silence_errors = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Span, ty: Ty<'tcx>) {
|
|
|
|
// We've reached the recursion limit, error gracefully.
|
2021-09-28 15:16:42 -05:00
|
|
|
let suggested_limit = match tcx.recursion_limit() {
|
|
|
|
Limit(0) => Limit(2),
|
|
|
|
limit => limit * 2,
|
|
|
|
};
|
2022-03-20 14:02:18 -05:00
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
span,
|
|
|
|
E0055,
|
|
|
|
"reached the recursion limit while auto-dereferencing `{:?}`",
|
|
|
|
ty
|
|
|
|
)
|
|
|
|
.span_label(span, "deref recursion limit reached")
|
|
|
|
.help(&format!(
|
|
|
|
"consider increasing the recursion limit by adding a \
|
2021-09-28 15:16:42 -05:00
|
|
|
`#![recursion_limit = \"{}\"]` attribute to your crate (`{}`)",
|
2022-03-20 14:02:18 -05:00
|
|
|
suggested_limit,
|
|
|
|
tcx.crate_name(LOCAL_CRATE),
|
|
|
|
))
|
|
|
|
.emit();
|
2020-06-20 05:29:13 -05:00
|
|
|
}
|