|
1 | | -use crate::solve::FulfillmentCtxt; |
2 | 1 | use crate::traits::query::evaluate_obligation::InferCtxtExt as _; |
3 | | -use crate::traits::{self, DefiningAnchor, ObligationCtxt}; |
| 2 | +use crate::traits::{self, DefiningAnchor, ObligationCtxt, SelectionContext}; |
4 | 3 |
|
| 4 | +use crate::traits::TraitEngineExt as _; |
5 | 5 | use rustc_hir::def_id::DefId; |
6 | 6 | use rustc_hir::lang_items::LangItem; |
7 | | -use rustc_infer::traits::{TraitEngine, TraitEngineExt}; |
| 7 | +use rustc_infer::traits::{Obligation, TraitEngine, TraitEngineExt as _}; |
8 | 8 | use rustc_middle::arena::ArenaAllocatable; |
9 | 9 | use rustc_middle::infer::canonical::{Canonical, CanonicalQueryResponse, QueryResponse}; |
10 | 10 | use rustc_middle::traits::query::NoSolution; |
| 11 | +use rustc_middle::traits::ObligationCause; |
11 | 12 | use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeVisitableExt}; |
12 | 13 | use rustc_middle::ty::{GenericArg, ToPredicate}; |
13 | 14 | use rustc_span::DUMMY_SP; |
@@ -38,7 +39,10 @@ pub trait InferCtxtExt<'tcx> { |
38 | 39 | param_env: ty::ParamEnv<'tcx>, |
39 | 40 | ) -> traits::EvaluationResult; |
40 | 41 |
|
41 | | - fn could_impl_trait( |
| 42 | + /// Returns `Some` if a type implements a trait a trait shallowly, |
| 43 | + /// returning any errors that may be reported upon further obligation |
| 44 | + /// processing. |
| 45 | + fn type_implements_trait_shallow( |
42 | 46 | &self, |
43 | 47 | trait_def_id: DefId, |
44 | 48 | ty: Ty<'tcx>, |
@@ -86,64 +90,26 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> { |
86 | 90 | self.evaluate_obligation(&obligation).unwrap_or(traits::EvaluationResult::EvaluatedToErr) |
87 | 91 | } |
88 | 92 |
|
89 | | - fn could_impl_trait( |
| 93 | + fn type_implements_trait_shallow( |
90 | 94 | &self, |
91 | 95 | trait_def_id: DefId, |
92 | 96 | ty: Ty<'tcx>, |
93 | 97 | param_env: ty::ParamEnv<'tcx>, |
94 | 98 | ) -> Option<Vec<traits::FulfillmentError<'tcx>>> { |
95 | 99 | self.probe(|_snapshot| { |
96 | | - if let ty::Adt(def, args) = ty.kind() |
97 | | - && let Some((impl_def_id, _)) = self |
98 | | - .tcx |
99 | | - .all_impls(trait_def_id) |
100 | | - .filter_map(|impl_def_id| { |
101 | | - self.tcx.impl_trait_ref(impl_def_id).map(|r| (impl_def_id, r)) |
102 | | - }) |
103 | | - .map(|(impl_def_id, imp)| (impl_def_id, imp.skip_binder())) |
104 | | - .find(|(_, imp)| match imp.self_ty().peel_refs().kind() { |
105 | | - ty::Adt(i_def, _) if i_def.did() == def.did() => true, |
106 | | - _ => false, |
107 | | - }) |
108 | | - { |
109 | | - let mut fulfill_cx = FulfillmentCtxt::new(self); |
110 | | - // We get all obligations from the impl to talk about specific |
111 | | - // trait bounds. |
112 | | - let obligations = self |
113 | | - .tcx |
114 | | - .predicates_of(impl_def_id) |
115 | | - .instantiate(self.tcx, args) |
116 | | - .into_iter() |
117 | | - .map(|(clause, span)| { |
118 | | - traits::Obligation::new( |
119 | | - self.tcx, |
120 | | - traits::ObligationCause::dummy_with_span(span), |
121 | | - param_env, |
122 | | - clause, |
123 | | - ) |
124 | | - }) |
125 | | - .collect::<Vec<_>>(); |
126 | | - fulfill_cx.register_predicate_obligations(self, obligations); |
127 | | - let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, [ty]); |
128 | | - let obligation = traits::Obligation::new( |
129 | | - self.tcx, |
130 | | - traits::ObligationCause::dummy(), |
131 | | - param_env, |
132 | | - trait_ref, |
133 | | - ); |
134 | | - fulfill_cx.register_predicate_obligation(self, obligation); |
135 | | - let mut errors = fulfill_cx.select_all_or_error(self); |
136 | | - // We remove the last predicate failure, which corresponds to |
137 | | - // the top-level obligation, because most of the type we only |
138 | | - // care about the other ones, *except* when it is the only one. |
139 | | - // This seems to only be relevant for arbitrary self-types. |
140 | | - // Look at `tests/ui/moves/move-fn-self-receiver.rs`. |
141 | | - if errors.len() > 1 { |
142 | | - errors.truncate(errors.len() - 1); |
| 100 | + let mut selcx = SelectionContext::new(self); |
| 101 | + match selcx.select(&Obligation::new( |
| 102 | + self.tcx, |
| 103 | + ObligationCause::dummy(), |
| 104 | + param_env, |
| 105 | + ty::TraitRef::new(self.tcx, trait_def_id, [ty]), |
| 106 | + )) { |
| 107 | + Ok(Some(selection)) => { |
| 108 | + let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new(self); |
| 109 | + fulfill_cx.register_predicate_obligations(self, selection.nested_obligations()); |
| 110 | + Some(fulfill_cx.select_all_or_error(self)) |
143 | 111 | } |
144 | | - Some(errors) |
145 | | - } else { |
146 | | - None |
| 112 | + Ok(None) | Err(_) => None, |
147 | 113 | } |
148 | 114 | }) |
149 | 115 | } |
|
0 commit comments