1use rustc_data_structures::unord::UnordSet;
4use rustc_hir::def_id::DefId;
5use rustc_infer::traits::{self, ObligationCause, PredicateObligations};
6use rustc_middle::ty::{self, Ty, TypeVisitableExt};
7use rustc_span::Span;
8use rustc_trait_selection::solve::Certainty;
9use rustc_trait_selection::solve::inspect::{
10 InferCtxtProofTreeExt, InspectConfig, InspectGoal, ProofTreeVisitor,
11};
12use tracing::{debug, instrument, trace};
13
14use crate::FnCtxt;
15
16impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("obligations_for_self_ty",
"rustc_hir_typeck::fn_ctxt::inspect_obligations",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs"),
::tracing_core::__macro_support::Option::Some(19u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fn_ctxt::inspect_obligations"),
::tracing_core::field::FieldSet::new(&["self_ty"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self_ty)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: PredicateObligations<'tcx> =
loop {};
return __tracing_attr_fake_return;
}
{
if self.next_trait_solver() {
self.obligations_for_self_ty_next(self_ty)
} else {
let ty_var_root = self.root_var(self_ty);
let mut obligations =
self.fulfillment_cx.borrow().pending_obligations();
{
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_typeck/src/fn_ctxt/inspect_obligations.rs:26",
"rustc_hir_typeck::fn_ctxt::inspect_obligations",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs"),
::tracing_core::__macro_support::Option::Some(26u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fn_ctxt::inspect_obligations"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::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!("pending_obligations = {0:#?}",
obligations) as &dyn Value))])
});
} else { ; }
};
obligations.retain(|obligation|
self.predicate_has_self_ty(obligation.predicate,
ty_var_root));
obligations
}
}
}
}#[instrument(skip(self), level = "debug")]
20 pub(crate) fn obligations_for_self_ty(&self, self_ty: ty::TyVid) -> PredicateObligations<'tcx> {
21 if self.next_trait_solver() {
22 self.obligations_for_self_ty_next(self_ty)
23 } else {
24 let ty_var_root = self.root_var(self_ty);
25 let mut obligations = self.fulfillment_cx.borrow().pending_obligations();
26 trace!("pending_obligations = {:#?}", obligations);
27 obligations
28 .retain(|obligation| self.predicate_has_self_ty(obligation.predicate, ty_var_root));
29 obligations
30 }
31 }
32
33 x;#[instrument(level = "debug", skip(self), ret)]
34 fn predicate_has_self_ty(
35 &self,
36 predicate: ty::Predicate<'tcx>,
37 expected_vid: ty::TyVid,
38 ) -> bool {
39 match predicate.kind().skip_binder() {
40 ty::PredicateKind::Clause(ty::ClauseKind::Trait(data)) => {
41 self.type_matches_expected_vid(data.self_ty(), expected_vid)
42 }
43 ty::PredicateKind::Clause(ty::ClauseKind::Projection(data)) => {
44 if data.projection_term.kind.is_trait_projection() {
45 self.type_matches_expected_vid(data.self_ty(), expected_vid)
46 } else {
47 false
48 }
49 }
50 ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
51 | ty::PredicateKind::Subtype(..)
52 | ty::PredicateKind::Coerce(..)
53 | ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(..))
54 | ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(..))
55 | ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(..))
56 | ty::PredicateKind::DynCompatible(..)
57 | ty::PredicateKind::NormalizesTo(..)
58 | ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
59 | ty::PredicateKind::ConstEquate(..)
60 | ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(..))
61 | ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(_))
62 | ty::PredicateKind::Ambiguous => false,
63 }
64 }
65
66 x;#[instrument(level = "debug", skip(self), ret)]
67 fn type_matches_expected_vid(&self, ty: Ty<'tcx>, expected_vid: ty::TyVid) -> bool {
68 let ty = self.shallow_resolve(ty);
69 debug!(?ty);
70
71 match *ty.kind() {
72 ty::Infer(ty::TyVar(found_vid)) => {
73 self.root_var(expected_vid) == self.root_var(found_vid)
74 }
75 _ => false,
76 }
77 }
78
79 pub(crate) fn obligations_for_self_ty_next(
80 &self,
81 self_ty: ty::TyVid,
82 ) -> PredicateObligations<'tcx> {
83 let sub_root_var = self.sub_unification_table_root_var(self_ty);
91 let obligations = self
92 .fulfillment_cx
93 .borrow()
94 .pending_obligations_potentially_referencing_sub_root(&self.infcx, sub_root_var);
95 {
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_typeck/src/fn_ctxt/inspect_obligations.rs:95",
"rustc_hir_typeck::fn_ctxt::inspect_obligations",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs"),
::tracing_core::__macro_support::Option::Some(95u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fn_ctxt::inspect_obligations"),
::tracing_core::field::FieldSet::new(&["obligations"],
::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(&debug(&obligations)
as &dyn Value))])
});
} else { ; }
};debug!(?obligations);
96 let mut obligations_for_self_ty = PredicateObligations::new();
97 for obligation in obligations {
98 let mut visitor = NestedObligationsForSelfTy {
99 fcx: self,
100 self_ty,
101 obligations_for_self_ty: &mut obligations_for_self_ty,
102 root_cause: &obligation.cause,
103 };
104
105 let goal = obligation.as_goal();
106 self.visit_proof_tree(goal, &mut visitor);
107 }
108
109 obligations_for_self_ty.retain_mut(|obligation| {
110 obligation.predicate = self.resolve_vars_if_possible(obligation.predicate);
111 !obligation.predicate.has_placeholders()
112 });
113 obligations_for_self_ty
114 }
115
116 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("from_float_for_f32_root_vids",
"rustc_hir_typeck::fn_ctxt::inspect_obligations",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs"),
::tracing_core::__macro_support::Option::Some(117u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fn_ctxt::inspect_obligations"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set(&[]) })
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: UnordSet<ty::FloatVid> = loop {};
return __tracing_attr_fake_return;
}
{
if self.next_trait_solver() {
self.from_float_for_f32_root_vids_next()
} else {
let Some(from_trait) =
self.tcx.lang_items().from_trait() else {
return UnordSet::new();
};
self.fulfillment_cx.borrow_mut().pending_obligations().into_iter().filter_map(|obligation|
{
self.predicate_from_float_for_f32_root_vid(from_trait,
obligation.predicate)
}).collect()
}
}
}
}#[instrument(skip(self), level = "debug")]
118 pub(crate) fn from_float_for_f32_root_vids(&self) -> UnordSet<ty::FloatVid> {
119 if self.next_trait_solver() {
120 self.from_float_for_f32_root_vids_next()
121 } else {
122 let Some(from_trait) = self.tcx.lang_items().from_trait() else {
123 return UnordSet::new();
124 };
125 self.fulfillment_cx
126 .borrow_mut()
127 .pending_obligations()
128 .into_iter()
129 .filter_map(|obligation| {
130 self.predicate_from_float_for_f32_root_vid(from_trait, obligation.predicate)
131 })
132 .collect()
133 }
134 }
135
136 fn predicate_from_float_for_f32_root_vid(
137 &self,
138 from_trait: DefId,
139 predicate: ty::Predicate<'tcx>,
140 ) -> Option<ty::FloatVid> {
141 match predicate.kind().no_bound_vars() {
145 Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(ty::TraitPredicate {
146 polarity: ty::PredicatePolarity::Positive,
147 trait_ref,
148 }))) if trait_ref.def_id == from_trait
149 && self.shallow_resolve(trait_ref.self_ty()).kind()
150 == &ty::Float(ty::FloatTy::F32) =>
151 {
152 self.root_float_vid(trait_ref.args.type_at(1))
153 }
154 _ => None,
155 }
156 }
157
158 fn from_float_for_f32_root_vids_next(&self) -> UnordSet<ty::FloatVid> {
159 let Some(from_trait) = self.tcx.lang_items().from_trait() else {
160 return UnordSet::new();
161 };
162 let obligations = self.fulfillment_cx.borrow().pending_obligations();
163 {
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_typeck/src/fn_ctxt/inspect_obligations.rs:163",
"rustc_hir_typeck::fn_ctxt::inspect_obligations",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs"),
::tracing_core::__macro_support::Option::Some(163u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fn_ctxt::inspect_obligations"),
::tracing_core::field::FieldSet::new(&["obligations"],
::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(&debug(&obligations)
as &dyn Value))])
});
} else { ; }
};debug!(?obligations);
164 let mut vids = UnordSet::new();
165 for obligation in obligations {
166 let mut visitor = FindFromFloatForF32RootVids {
167 fcx: self,
168 from_trait,
169 vids: &mut vids,
170 span: obligation.cause.span,
171 };
172
173 let goal = obligation.as_goal();
174 self.visit_proof_tree(goal, &mut visitor);
175 }
176 vids
177 }
178}
179
180struct NestedObligationsForSelfTy<'a, 'tcx> {
181 fcx: &'a FnCtxt<'a, 'tcx>,
182 self_ty: ty::TyVid,
183 root_cause: &'a ObligationCause<'tcx>,
184 obligations_for_self_ty: &'a mut PredicateObligations<'tcx>,
185}
186
187impl<'tcx> ProofTreeVisitor<'tcx> for NestedObligationsForSelfTy<'_, 'tcx> {
188 fn span(&self) -> Span {
189 self.root_cause.span
190 }
191
192 fn config(&self) -> InspectConfig {
193 InspectConfig { max_depth: 5 }
197 }
198
199 fn visit_goal(&mut self, inspect_goal: &InspectGoal<'_, 'tcx>) {
200 if inspect_goal.result() == Ok(Certainty::Yes) {
203 return;
204 }
205
206 if !inspect_goal
209 .orig_values()
210 .iter()
211 .filter_map(|arg| arg.as_type())
212 .any(|ty| self.fcx.type_matches_expected_vid(ty, self.self_ty))
213 {
214 {
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_typeck/src/fn_ctxt/inspect_obligations.rs:214",
"rustc_hir_typeck::fn_ctxt::inspect_obligations",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs"),
::tracing_core::__macro_support::Option::Some(214u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fn_ctxt::inspect_obligations"),
::tracing_core::field::FieldSet::new(&["message", "goal"],
::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!("goal does not mention self type")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&inspect_goal.goal())
as &dyn Value))])
});
} else { ; }
};debug!(goal = ?inspect_goal.goal(), "goal does not mention self type");
215 return;
216 }
217
218 let tcx = self.fcx.tcx;
219 let goal = inspect_goal.goal();
220 if self.fcx.predicate_has_self_ty(goal.predicate, self.self_ty) {
221 self.obligations_for_self_ty.push(traits::Obligation::new(
222 tcx,
223 self.root_cause.clone(),
224 goal.param_env,
225 goal.predicate,
226 ));
227 }
228
229 if let Some(candidate) = inspect_goal.unique_applicable_candidate() {
234 candidate.visit_nested_no_probe(self)
235 }
236 }
237}
238
239struct FindFromFloatForF32RootVids<'a, 'tcx> {
240 fcx: &'a FnCtxt<'a, 'tcx>,
241 from_trait: DefId,
242 vids: &'a mut UnordSet<ty::FloatVid>,
243 span: Span,
244}
245
246impl<'tcx> ProofTreeVisitor<'tcx> for FindFromFloatForF32RootVids<'_, 'tcx> {
247 fn span(&self) -> Span {
248 self.span
249 }
250
251 fn config(&self) -> InspectConfig {
252 InspectConfig { max_depth: 3 }
255 }
256
257 fn visit_goal(&mut self, inspect_goal: &InspectGoal<'_, 'tcx>) {
258 if let Some(vid) = self
259 .fcx
260 .predicate_from_float_for_f32_root_vid(self.from_trait, inspect_goal.goal().predicate)
261 {
262 self.vids.insert(vid);
263 } else if let Some(candidate) = inspect_goal.unique_applicable_candidate() {
264 let start_len = self.vids.len();
265 let _ = candidate.goal().infcx().commit_if_ok(|_| {
266 candidate.visit_nested_no_probe(self);
267 if self.vids.len() > start_len { Ok(()) } else { Err(()) }
268 });
269 }
270 }
271}