1use std::fmt::Debug;
2
3use rustc_hir::def_id::DefId;
4use rustc_middle::ty::{self, Ty, TyVid, Upcast};
5use thin_vec::{ThinVec, thin_vec};
6
7use super::{ObligationCause, PredicateObligation, PredicateObligations};
8use crate::infer::InferCtxt;
9use crate::traits::Obligation;
10
11#[derive(#[automatically_derived]impl<'tcx> ::core::clone::Clone for
ScrubbedTraitError<'tcx> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
match self {
Self::TrueError => ScrubbedTraitError::TrueError,
Self::Ambiguity => ScrubbedTraitError::Ambiguity,
Self::Cycle(__self_0) =>
ScrubbedTraitError::Cycle(::core::clone::Clone::clone(__self_0)),
}
}
}Clone, #[automatically_derived]impl<'tcx> ::core::fmt::Debug for
ScrubbedTraitError<'tcx> {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::TrueError =>
::core::fmt::Formatter::write_str(f, "TrueError"),
Self::Ambiguity =>
::core::fmt::Formatter::write_str(f, "Ambiguity"),
Self::Cycle(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f, "Cycle",
&__self_0),
}
}
}Debug)]
18pub enum ScrubbedTraitError<'tcx> {
19 TrueError,
21 Ambiguity,
23 Cycle(PredicateObligations<'tcx>),
26}
27
28impl<'tcx> ScrubbedTraitError<'tcx> {
29 pub fn is_true_error(&self) -> bool {
30 match self {
31 ScrubbedTraitError::TrueError => true,
32 ScrubbedTraitError::Ambiguity | ScrubbedTraitError::Cycle(_) => false,
33 }
34 }
35}
36
37impl<'tcx> EngineError<'tcx> for ScrubbedTraitError<'tcx> {
38 fn try_report_errors(_infcx: &InferCtxt<'tcx>, _errors: ThinVec<Self>) {
39 }
43}
44
45#[derive(#[automatically_derived]impl<E: ::core::fmt::Debug> ::core::fmt::Debug for
TraitErrors<E> {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::HasErrors(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"HasErrors", &__self_0),
Self::NoErrors =>
::core::fmt::Formatter::write_str(f, "NoErrors"),
}
}
}Debug, #[automatically_derived]impl<E: ::core::clone::Clone> ::core::clone::Clone for
TraitErrors<E> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
match self {
Self::HasErrors(__self_0) =>
TraitErrors::HasErrors(::core::clone::Clone::clone(__self_0)),
Self::NoErrors => TraitErrors::NoErrors,
}
}
}Clone)]
46#[must_use]
47pub enum TraitErrors<E> {
48 HasErrors(ThinVec<E>),
49 NoErrors,
50}
51
52impl<E> TraitErrors<E> {
53 #[inline]
54 pub fn from_iter(iter: impl ExactSizeIterator<Item = E>) -> TraitErrors<E> {
55 if iter.len() == 0 { TraitErrors::NoErrors } else { TraitErrors::HasErrors(iter.collect()) }
56 }
57
58 #[inline]
59 pub fn has_errors(&self) -> bool {
60 #[allow(non_exhaustive_omitted_patterns)] match self {
TraitErrors::HasErrors(_) => true,
_ => false,
}matches!(self, TraitErrors::HasErrors(_))
61 }
62
63 #[inline]
64 pub fn no_errors(&self) -> bool {
65 #[allow(non_exhaustive_omitted_patterns)] match self {
TraitErrors::NoErrors => true,
_ => false,
}matches!(self, TraitErrors::NoErrors)
66 }
67
68 #[inline]
69 pub fn as_slice(&self) -> &[E] {
70 match self {
71 TraitErrors::HasErrors(errors) => errors.as_slice(),
72 TraitErrors::NoErrors => &[],
73 }
74 }
75
76 #[inline]
77 pub fn as_mut_slice(&mut self) -> &mut [E] {
78 match self {
79 TraitErrors::HasErrors(errors) => errors.as_mut_slice(),
80 TraitErrors::NoErrors => &mut [],
81 }
82 }
83
84 #[inline]
85 pub fn into_thin_vec(self) -> ThinVec<E> {
86 match self {
87 TraitErrors::HasErrors(errors) => errors,
88 TraitErrors::NoErrors => ThinVec::new(),
89 }
90 }
91
92 #[cold]
93 pub fn push(&mut self, err: E) {
94 match self {
95 TraitErrors::HasErrors(errors) => errors.push(err),
96 TraitErrors::NoErrors => *self = TraitErrors::HasErrors({
let len = [()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(err);
vec
}thin_vec![err]),
97 }
98 }
99
100 #[inline]
101 pub fn len(&self) -> usize {
102 match self {
103 TraitErrors::HasErrors(errors) => errors.len(),
104 TraitErrors::NoErrors => 0,
105 }
106 }
107}
108
109impl<E> IntoIterator for TraitErrors<E> {
110 type Item = E;
111 type IntoIter = thin_vec::IntoIter<E>;
112
113 #[inline]
114 fn into_iter(self) -> Self::IntoIter {
115 self.into_thin_vec().into_iter()
116 }
117}
118
119impl<'a, E> IntoIterator for &'a TraitErrors<E> {
120 type Item = &'a E;
121 type IntoIter = std::slice::Iter<'a, E>;
122
123 #[inline]
124 fn into_iter(self) -> Self::IntoIter {
125 self.as_slice().iter()
126 }
127}
128
129pub trait TraitEngine<'tcx, E: 'tcx>: 'tcx {
130 fn register_bound(
134 &mut self,
135 infcx: &InferCtxt<'tcx>,
136 param_env: ty::ParamEnv<'tcx>,
137 ty: Ty<'tcx>,
138 def_id: DefId,
139 cause: ObligationCause<'tcx>,
140 ) {
141 let trait_ref = ty::TraitRef::new(infcx.tcx, def_id, [ty]);
142 self.register_predicate_obligation(
143 infcx,
144 Obligation {
145 cause,
146 recursion_depth: 0,
147 param_env,
148 predicate: trait_ref.upcast(infcx.tcx),
149 },
150 );
151 }
152
153 fn register_predicate_obligation(
154 &mut self,
155 infcx: &InferCtxt<'tcx>,
156 obligation: PredicateObligation<'tcx>,
157 );
158
159 fn register_predicate_obligations(
160 &mut self,
161 infcx: &InferCtxt<'tcx>,
162 obligations: PredicateObligations<'tcx>,
163 ) {
164 for obligation in obligations {
165 self.register_predicate_obligation(infcx, obligation);
166 }
167 }
168
169 #[must_use]
178 fn try_evaluate_obligations(&mut self, infcx: &InferCtxt<'tcx>) -> TraitErrors<E>;
179
180 fn collect_remaining_errors(&mut self, infcx: &InferCtxt<'tcx>) -> TraitErrors<E>;
181
182 #[must_use]
191 fn evaluate_obligations_error_on_ambiguity(
192 &mut self,
193 infcx: &InferCtxt<'tcx>,
194 ) -> TraitErrors<E> {
195 let errors = self.try_evaluate_obligations(infcx);
196 if errors.has_errors() {
197 return errors;
198 }
199
200 self.collect_remaining_errors(infcx)
201 }
202
203 fn has_pending_obligations(&self) -> bool;
204
205 fn pending_obligations(&self) -> PredicateObligations<'tcx>;
206
207 fn pending_obligations_potentially_referencing_sub_root(
212 &self,
213 _infcx: &InferCtxt<'tcx>,
214 _sub_root: TyVid,
215 ) -> PredicateObligations<'tcx> {
216 self.pending_obligations()
217 }
218
219 fn pending_obligations_potentially_referencing_float_infer(
224 &self,
225 _infcx: &InferCtxt<'tcx>,
226 ) -> PredicateObligations<'tcx> {
227 self.pending_obligations()
228 }
229
230 fn drain_stalled_obligations_for_coroutines(
234 &mut self,
235 infcx: &InferCtxt<'tcx>,
236 ) -> PredicateObligations<'tcx>;
237}
238
239pub trait EngineError<'tcx>: Sized + 'tcx {
240 fn try_report_errors(infcx: &InferCtxt<'tcx>, errors: ThinVec<Self>);
241}
242
243pub trait FromSolverError<'tcx, E>: EngineError<'tcx> + Debug {
244 fn from_solver_error(infcx: &InferCtxt<'tcx>, error: E) -> Self;
245}