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 if self.infcx.next_trait_solver()
94 && let ty::Alias(..) = ty.kind()
95 {
96 let (normalized_ty, obligations) =
97 self.structurally_normalize_ty(Unnormalized::new_wip(ty))?;
98 self.state.obligations.extend(obligations);
99 (AutoderefKind::Builtin, normalized_ty)
100 } else {
101 (AutoderefKind::Builtin, ty)
102 }
103 } else if let Some(ty) = self.overloaded_deref_ty(self.state.cur_ty) {
104 (AutoderefKind::Overloaded, ty)
106 } else {
107 return None;
108 };
109
110 self.state.steps.push((self.state.cur_ty, kind));
111 {
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:111",
"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(111u32),
::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!(
112 "autoderef stage #{:?} is {:?} from {:?}",
113 self.step_count(),
114 new_ty,
115 (self.state.cur_ty, kind)
116 );
117 self.state.cur_ty = new_ty;
118
119 Some((self.state.cur_ty, self.step_count()))
120 }
121}
122
123impl<'a, 'tcx> Autoderef<'a, 'tcx> {
124 pub fn new(
125 infcx: &'a InferCtxt<'tcx>,
126 param_env: ty::ParamEnv<'tcx>,
127 body_def_id: LocalDefId,
128 span: Span,
129 base_ty: Ty<'tcx>,
130 ) -> Self {
131 Autoderef {
132 infcx,
133 span,
134 body_id: body_def_id,
135 param_env,
136 state: AutoderefSnapshot {
137 steps: ::alloc::vec::Vec::new()vec![],
138 cur_ty: infcx.resolve_vars_if_possible(base_ty),
139 obligations: PredicateObligations::new(),
140 at_start: true,
141 reached_recursion_limit: false,
142 },
143 include_raw_pointers: false,
144 use_receiver_trait: false,
145 silence_errors: false,
146 }
147 }
148
149 fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
150 {
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:150",
"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(150u32),
::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);
151 let tcx = self.infcx.tcx;
152
153 if ty.references_error() {
154 return None;
155 }
156
157 let (trait_def_id, trait_target_def_id) = if self.use_receiver_trait {
159 (tcx.lang_items().receiver_trait()?, tcx.lang_items().receiver_target()?)
160 } else {
161 (tcx.lang_items().deref_trait()?, tcx.lang_items().deref_target()?)
162 };
163 let trait_ref = ty::TraitRef::new(tcx, trait_def_id, [ty]);
164 let cause = traits::ObligationCause::misc(self.span, self.body_id);
165 let obligation = traits::Obligation::new(
166 tcx,
167 cause.clone(),
168 self.param_env,
169 ty::Binder::dummy(trait_ref),
170 );
171 if !self.infcx.predicate_may_hold_opaque_types_jank(&obligation) {
176 {
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:176",
"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(176u32),
::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");
177 return None;
178 }
179
180 let (normalized_ty, obligations) = self.structurally_normalize_ty(Unnormalized::new(
181 Ty::new_projection(tcx, trait_target_def_id, [ty]),
182 ))?;
183 {
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:183",
"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(183u32),
::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);
184 self.state.obligations.extend(obligations);
185
186 Some(self.infcx.resolve_vars_if_possible(normalized_ty))
187 }
188
189 x;#[instrument(level = "debug", skip(self), ret)]
190 pub fn structurally_normalize_ty(
191 &self,
192 ty: Unnormalized<'tcx, Ty<'tcx>>,
193 ) -> Option<(Ty<'tcx>, PredicateObligations<'tcx>)> {
194 let ocx = ObligationCtxt::new(self.infcx);
195 let Ok(normalized_ty) = ocx.structurally_normalize_ty(
196 &traits::ObligationCause::misc(self.span, self.body_id),
197 self.param_env,
198 ty,
199 ) else {
200 return None;
203 };
204 let errors = ocx.try_evaluate_obligations();
205 if !errors.is_empty() {
206 if self.infcx.next_trait_solver() {
207 unreachable!();
208 }
209 debug!(?errors, "encountered errors while fulfilling");
212 return None;
213 }
214
215 Some((normalized_ty, ocx.into_pending_obligations()))
216 }
217
218 pub fn final_ty(&self) -> Ty<'tcx> {
221 self.state.cur_ty
222 }
223
224 pub fn step_count(&self) -> usize {
225 self.state.steps.len()
226 }
227
228 pub fn into_obligations(self) -> PredicateObligations<'tcx> {
229 self.state.obligations
230 }
231
232 pub fn current_obligations(&self) -> PredicateObligations<'tcx> {
233 self.state.obligations.clone()
234 }
235
236 pub fn steps(&self) -> &[(Ty<'tcx>, AutoderefKind)] {
237 &self.state.steps
238 }
239
240 pub fn span(&self) -> Span {
241 self.span
242 }
243
244 pub fn reached_recursion_limit(&self) -> bool {
245 self.state.reached_recursion_limit
246 }
247
248 pub fn include_raw_pointers(mut self) -> Self {
253 self.include_raw_pointers = true;
254 self
255 }
256
257 pub fn use_receiver_trait(mut self) -> Self {
261 self.use_receiver_trait = true;
262 self
263 }
264
265 pub fn silence_errors(mut self) -> Self {
266 self.silence_errors = true;
267 self
268 }
269}
270
271pub fn report_autoderef_recursion_limit_error<'tcx>(
272 tcx: TyCtxt<'tcx>,
273 span: Span,
274 ty: Ty<'tcx>,
275) -> ErrorGuaranteed {
276 let suggested_limit = match tcx.recursion_limit() {
278 Limit(0) => Limit(2),
279 limit => limit * 2,
280 };
281 tcx.dcx().emit_err(AutoDerefReachedRecursionLimit {
282 span,
283 ty,
284 suggested_limit,
285 crate_name: tcx.crate_name(LOCAL_CRATE),
286 })
287}