1use rustc_hir::limit::Limit;
2use rustc_infer::infer::InferCtxt;
3use rustc_infer::traits::PredicateObligations;
4use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, Unnormalized};
5use rustc_span::def_id::{LOCAL_CRATE, LocalDefId};
6use rustc_span::{ErrorGuaranteed, Span};
7use rustc_trait_selection::traits::ObligationCtxt;
8use tracing::{debug, instrument};
9
10use crate::errors::AutoDerefReachedRecursionLimit;
11use crate::traits;
12use crate::traits::query::evaluate_obligation::InferCtxtExt;
13
14#[derive(#[automatically_derived]
impl ::core::marker::Copy for AutoderefKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for AutoderefKind {
#[inline]
fn clone(&self) -> AutoderefKind { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for AutoderefKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
AutoderefKind::Builtin => "Builtin",
AutoderefKind::Overloaded => "Overloaded",
})
}
}Debug)]
15pub enum AutoderefKind {
16 Builtin,
18 Overloaded,
20}
21struct AutoderefSnapshot<'tcx> {
22 at_start: bool,
23 reached_recursion_limit: bool,
24 steps: Vec<(Ty<'tcx>, AutoderefKind)>,
25 cur_ty: Ty<'tcx>,
26 obligations: PredicateObligations<'tcx>,
27}
28
29pub struct Autoderef<'a, 'tcx> {
34 infcx: &'a InferCtxt<'tcx>,
36 span: Span,
37 body_id: LocalDefId,
38 param_env: ty::ParamEnv<'tcx>,
39
40 state: AutoderefSnapshot<'tcx>,
42
43 include_raw_pointers: bool,
45 use_receiver_trait: bool,
46 silence_errors: bool,
47}
48
49impl<'a, 'tcx> Iterator for Autoderef<'a, 'tcx> {
50 type Item = (Ty<'tcx>, usize);
51
52 fn next(&mut self) -> Option<Self::Item> {
53 let tcx = self.infcx.tcx;
54
55 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/autoderef.rs:55",
"rustc_hir_analysis::autoderef", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/autoderef.rs"),
::tracing_core::__macro_support::Option::Some(55u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::autoderef"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("autoderef: steps={0:?}, cur_ty={1:?}",
self.state.steps, self.state.cur_ty) as &dyn Value))])
});
} else { ; }
};debug!("autoderef: steps={:?}, cur_ty={:?}", self.state.steps, self.state.cur_ty);
56 if self.state.at_start {
57 self.state.at_start = false;
58 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/autoderef.rs:58",
"rustc_hir_analysis::autoderef", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/autoderef.rs"),
::tracing_core::__macro_support::Option::Some(58u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::autoderef"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("autoderef stage #0 is {0:?}",
self.state.cur_ty) as &dyn Value))])
});
} else { ; }
};debug!("autoderef stage #0 is {:?}", self.state.cur_ty);
59 return Some((self.state.cur_ty, 0));
60 }
61
62 if !tcx.recursion_limit().value_within_limit(self.state.steps.len()) {
64 if !self.silence_errors {
65 report_autoderef_recursion_limit_error(tcx, self.span, self.state.cur_ty);
66 }
67 self.state.reached_recursion_limit = true;
68 return None;
69 }
70
71 if let &ty::Infer(ty::TyVar(vid)) = self.state.cur_ty.kind()
77 && !self.infcx.has_opaques_with_sub_unified_hidden_type(vid)
78 {
79 return None;
80 }
81
82 let (kind, new_ty) =
88 if let Some(ty) = self.state.cur_ty.builtin_deref(self.include_raw_pointers) {
89 if true {
match (&ty, &self.infcx.resolve_vars_if_possible(ty)) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(ty, self.infcx.resolve_vars_if_possible(ty));
90 (AutoderefKind::Builtin, ty)
91 } else if let Some(ty) = self.overloaded_deref_ty(self.state.cur_ty) {
92 (AutoderefKind::Overloaded, ty)
94 } else {
95 return None;
96 };
97
98 self.state.steps.push((self.state.cur_ty, kind));
99 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/autoderef.rs:99",
"rustc_hir_analysis::autoderef", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/autoderef.rs"),
::tracing_core::__macro_support::Option::Some(99u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::autoderef"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("autoderef stage #{0:?} is {1:?} from {2:?}",
self.step_count(), new_ty, (self.state.cur_ty, kind)) as
&dyn Value))])
});
} else { ; }
};debug!(
100 "autoderef stage #{:?} is {:?} from {:?}",
101 self.step_count(),
102 new_ty,
103 (self.state.cur_ty, kind)
104 );
105 self.state.cur_ty = new_ty;
106
107 Some((self.state.cur_ty, self.step_count()))
108 }
109}
110
111impl<'a, 'tcx> Autoderef<'a, 'tcx> {
112 pub fn new(
113 infcx: &'a InferCtxt<'tcx>,
114 param_env: ty::ParamEnv<'tcx>,
115 body_def_id: LocalDefId,
116 span: Span,
117 base_ty: Ty<'tcx>,
118 ) -> Self {
119 Autoderef {
120 infcx,
121 span,
122 body_id: body_def_id,
123 param_env,
124 state: AutoderefSnapshot {
125 steps: ::alloc::vec::Vec::new()vec![],
126 cur_ty: infcx.resolve_vars_if_possible(base_ty),
127 obligations: PredicateObligations::new(),
128 at_start: true,
129 reached_recursion_limit: false,
130 },
131 include_raw_pointers: false,
132 use_receiver_trait: false,
133 silence_errors: false,
134 }
135 }
136
137 fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
138 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/autoderef.rs:138",
"rustc_hir_analysis::autoderef", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/autoderef.rs"),
::tracing_core::__macro_support::Option::Some(138u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::autoderef"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("overloaded_deref_ty({0:?})",
ty) as &dyn Value))])
});
} else { ; }
};debug!("overloaded_deref_ty({:?})", ty);
139 let tcx = self.infcx.tcx;
140
141 if ty.references_error() {
142 return None;
143 }
144
145 let (trait_def_id, trait_target_def_id) = if self.use_receiver_trait {
147 (tcx.lang_items().receiver_trait()?, tcx.lang_items().receiver_target()?)
148 } else {
149 (tcx.lang_items().deref_trait()?, tcx.lang_items().deref_target()?)
150 };
151 let trait_ref = ty::TraitRef::new(tcx, trait_def_id, [ty]);
152 let cause = traits::ObligationCause::misc(self.span, self.body_id);
153 let obligation = traits::Obligation::new(
154 tcx,
155 cause.clone(),
156 self.param_env,
157 ty::Binder::dummy(trait_ref),
158 );
159 if !self.infcx.predicate_may_hold_opaque_types_jank(&obligation) {
164 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/autoderef.rs:164",
"rustc_hir_analysis::autoderef", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/autoderef.rs"),
::tracing_core::__macro_support::Option::Some(164u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::autoderef"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("overloaded_deref_ty: cannot match obligation")
as &dyn Value))])
});
} else { ; }
};debug!("overloaded_deref_ty: cannot match obligation");
165 return None;
166 }
167
168 let (normalized_ty, obligations) = self
169 .normalize_ty(Unnormalized::new(Ty::new_projection(tcx, trait_target_def_id, [ty])))?;
170 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/autoderef.rs:170",
"rustc_hir_analysis::autoderef", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/autoderef.rs"),
::tracing_core::__macro_support::Option::Some(170u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::autoderef"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("overloaded_deref_ty({0:?}) = ({1:?}, {2:?})",
ty, normalized_ty, obligations) as &dyn Value))])
});
} else { ; }
};debug!("overloaded_deref_ty({:?}) = ({:?}, {:?})", ty, normalized_ty, obligations);
171 self.state.obligations.extend(obligations);
172
173 Some(self.infcx.resolve_vars_if_possible(normalized_ty))
174 }
175
176 x;#[instrument(level = "debug", skip(self), ret)]
177 pub fn normalize_ty(
178 &self,
179 ty: Unnormalized<'tcx, Ty<'tcx>>,
180 ) -> Option<(Ty<'tcx>, PredicateObligations<'tcx>)> {
181 let ocx = ObligationCtxt::new(self.infcx);
182 let normalized_ty = ocx.normalize(
183 &traits::ObligationCause::misc(self.span, self.body_id),
184 self.param_env,
185 ty,
186 );
187 let errors = ocx.try_evaluate_obligations();
188 if !errors.is_empty() {
189 debug!(?errors, "encountered errors while fulfilling");
192 return None;
193 }
194
195 Some((normalized_ty, ocx.into_pending_obligations()))
196 }
197
198 pub fn final_ty(&self) -> Ty<'tcx> {
201 self.state.cur_ty
202 }
203
204 pub fn step_count(&self) -> usize {
205 self.state.steps.len()
206 }
207
208 pub fn into_obligations(self) -> PredicateObligations<'tcx> {
209 self.state.obligations
210 }
211
212 pub fn current_obligations(&self) -> PredicateObligations<'tcx> {
213 self.state.obligations.clone()
214 }
215
216 pub fn steps(&self) -> &[(Ty<'tcx>, AutoderefKind)] {
217 &self.state.steps
218 }
219
220 pub fn span(&self) -> Span {
221 self.span
222 }
223
224 pub fn reached_recursion_limit(&self) -> bool {
225 self.state.reached_recursion_limit
226 }
227
228 pub fn include_raw_pointers(mut self) -> Self {
233 self.include_raw_pointers = true;
234 self
235 }
236
237 pub fn use_receiver_trait(mut self) -> Self {
241 self.use_receiver_trait = true;
242 self
243 }
244
245 pub fn silence_errors(mut self) -> Self {
246 self.silence_errors = true;
247 self
248 }
249}
250
251pub fn report_autoderef_recursion_limit_error<'tcx>(
252 tcx: TyCtxt<'tcx>,
253 span: Span,
254 ty: Ty<'tcx>,
255) -> ErrorGuaranteed {
256 let suggested_limit = match tcx.recursion_limit() {
258 Limit(0) => Limit(2),
259 limit => limit * 2,
260 };
261 tcx.dcx().emit_err(AutoDerefReachedRecursionLimit {
262 span,
263 ty,
264 suggested_limit,
265 crate_name: tcx.crate_name(LOCAL_CRATE),
266 })
267}