]> git.lizzy.rs Git - rust.git/blob - src/librustc_trait_selection/traits/fulfill.rs
Rollup merge of #74197 - estebank:self-sugg, r=petrochenkov
[rust.git] / src / librustc_trait_selection / traits / fulfill.rs
1 use crate::infer::{InferCtxt, TyOrConstInferVar};
2 use rustc_data_structures::obligation_forest::ProcessResult;
3 use rustc_data_structures::obligation_forest::{DoCompleted, Error, ForestObligation};
4 use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor};
5 use rustc_errors::ErrorReported;
6 use rustc_infer::traits::{TraitEngine, TraitEngineExt as _};
7 use rustc_middle::mir::interpret::ErrorHandled;
8 use rustc_middle::ty::error::ExpectedFound;
9 use rustc_middle::ty::{self, Const, ToPolyTraitRef, Ty, TypeFoldable};
10 use std::marker::PhantomData;
11
12 use super::project;
13 use super::select::SelectionContext;
14 use super::wf;
15 use super::CodeAmbiguity;
16 use super::CodeProjectionError;
17 use super::CodeSelectionError;
18 use super::{ConstEvalFailure, Unimplemented};
19 use super::{FulfillmentError, FulfillmentErrorCode};
20 use super::{ObligationCause, PredicateObligation};
21
22 use crate::traits::error_reporting::InferCtxtExt as _;
23 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
24
25 impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
26     /// Note that we include both the `ParamEnv` and the `Predicate`,
27     /// as the `ParamEnv` can influence whether fulfillment succeeds
28     /// or fails.
29     type CacheKey = ty::ParamEnvAnd<'tcx, ty::Predicate<'tcx>>;
30
31     fn as_cache_key(&self) -> Self::CacheKey {
32         self.obligation.param_env.and(self.obligation.predicate)
33     }
34 }
35
36 /// The fulfillment context is used to drive trait resolution. It
37 /// consists of a list of obligations that must be (eventually)
38 /// satisfied. The job is to track which are satisfied, which yielded
39 /// errors, and which are still pending. At any point, users can call
40 /// `select_where_possible`, and the fulfillment context will try to do
41 /// selection, retaining only those obligations that remain
42 /// ambiguous. This may be helpful in pushing type inference
43 /// along. Once all type inference constraints have been generated, the
44 /// method `select_all_or_error` can be used to report any remaining
45 /// ambiguous cases as errors.
46 pub struct FulfillmentContext<'tcx> {
47     // A list of all obligations that have been registered with this
48     // fulfillment context.
49     predicates: ObligationForest<PendingPredicateObligation<'tcx>>,
50     // Should this fulfillment context register type-lives-for-region
51     // obligations on its parent infcx? In some cases, region
52     // obligations are either already known to hold (normalization) or
53     // hopefully verifed elsewhere (type-impls-bound), and therefore
54     // should not be checked.
55     //
56     // Note that if we are normalizing a type that we already
57     // know is well-formed, there should be no harm setting this
58     // to true - all the region variables should be determinable
59     // using the RFC 447 rules, which don't depend on
60     // type-lives-for-region constraints, and because the type
61     // is well-formed, the constraints should hold.
62     register_region_obligations: bool,
63     // Is it OK to register obligations into this infcx inside
64     // an infcx snapshot?
65     //
66     // The "primary fulfillment" in many cases in typeck lives
67     // outside of any snapshot, so any use of it inside a snapshot
68     // will lead to trouble and therefore is checked against, but
69     // other fulfillment contexts sometimes do live inside of
70     // a snapshot (they don't *straddle* a snapshot, so there
71     // is no trouble there).
72     usable_in_snapshot: bool,
73 }
74
75 #[derive(Clone, Debug)]
76 pub struct PendingPredicateObligation<'tcx> {
77     pub obligation: PredicateObligation<'tcx>,
78     // This is far more often read than modified, meaning that we
79     // should mostly optimize for reading speed, while modifying is not as relevant.
80     //
81     // For whatever reason using a boxed slice is slower than using a `Vec` here.
82     pub stalled_on: Vec<TyOrConstInferVar<'tcx>>,
83 }
84
85 // `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
86 #[cfg(target_arch = "x86_64")]
87 static_assert_size!(PendingPredicateObligation<'_>, 64);
88
89 impl<'a, 'tcx> FulfillmentContext<'tcx> {
90     /// Creates a new fulfillment context.
91     pub fn new() -> FulfillmentContext<'tcx> {
92         FulfillmentContext {
93             predicates: ObligationForest::new(),
94             register_region_obligations: true,
95             usable_in_snapshot: false,
96         }
97     }
98
99     pub fn new_in_snapshot() -> FulfillmentContext<'tcx> {
100         FulfillmentContext {
101             predicates: ObligationForest::new(),
102             register_region_obligations: true,
103             usable_in_snapshot: true,
104         }
105     }
106
107     pub fn new_ignoring_regions() -> FulfillmentContext<'tcx> {
108         FulfillmentContext {
109             predicates: ObligationForest::new(),
110             register_region_obligations: false,
111             usable_in_snapshot: false,
112         }
113     }
114
115     /// Attempts to select obligations using `selcx`.
116     fn select(
117         &mut self,
118         selcx: &mut SelectionContext<'a, 'tcx>,
119     ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
120         debug!("select(obligation-forest-size={})", self.predicates.len());
121
122         let mut errors = Vec::new();
123
124         loop {
125             debug!("select: starting another iteration");
126
127             // Process pending obligations.
128             let outcome = self.predicates.process_obligations(
129                 &mut FulfillProcessor {
130                     selcx,
131                     register_region_obligations: self.register_region_obligations,
132                 },
133                 DoCompleted::No,
134             );
135             debug!("select: outcome={:#?}", outcome);
136
137             // FIXME: if we kept the original cache key, we could mark projection
138             // obligations as complete for the projection cache here.
139
140             errors.extend(outcome.errors.into_iter().map(to_fulfillment_error));
141
142             // If nothing new was added, no need to keep looping.
143             if outcome.stalled {
144                 break;
145             }
146         }
147
148         debug!(
149             "select({} predicates remaining, {} errors) done",
150             self.predicates.len(),
151             errors.len()
152         );
153
154         if errors.is_empty() { Ok(()) } else { Err(errors) }
155     }
156 }
157
158 impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
159     /// "Normalize" a projection type `<SomeType as SomeTrait>::X` by
160     /// creating a fresh type variable `$0` as well as a projection
161     /// predicate `<SomeType as SomeTrait>::X == $0`. When the
162     /// inference engine runs, it will attempt to find an impl of
163     /// `SomeTrait` or a where-clause that lets us unify `$0` with
164     /// something concrete. If this fails, we'll unify `$0` with
165     /// `projection_ty` again.
166     fn normalize_projection_type(
167         &mut self,
168         infcx: &InferCtxt<'_, 'tcx>,
169         param_env: ty::ParamEnv<'tcx>,
170         projection_ty: ty::ProjectionTy<'tcx>,
171         cause: ObligationCause<'tcx>,
172     ) -> Ty<'tcx> {
173         debug!("normalize_projection_type(projection_ty={:?})", projection_ty);
174
175         debug_assert!(!projection_ty.has_escaping_bound_vars());
176
177         // FIXME(#20304) -- cache
178
179         let mut selcx = SelectionContext::new(infcx);
180         let mut obligations = vec![];
181         let normalized_ty = project::normalize_projection_type(
182             &mut selcx,
183             param_env,
184             projection_ty,
185             cause,
186             0,
187             &mut obligations,
188         );
189         self.register_predicate_obligations(infcx, obligations);
190
191         debug!("normalize_projection_type: result={:?}", normalized_ty);
192
193         normalized_ty
194     }
195
196     fn register_predicate_obligation(
197         &mut self,
198         infcx: &InferCtxt<'_, 'tcx>,
199         obligation: PredicateObligation<'tcx>,
200     ) {
201         // this helps to reduce duplicate errors, as well as making
202         // debug output much nicer to read and so on.
203         let obligation = infcx.resolve_vars_if_possible(&obligation);
204
205         debug!("register_predicate_obligation(obligation={:?})", obligation);
206
207         assert!(!infcx.is_in_snapshot() || self.usable_in_snapshot);
208
209         self.predicates
210             .register_obligation(PendingPredicateObligation { obligation, stalled_on: vec![] });
211     }
212
213     fn select_all_or_error(
214         &mut self,
215         infcx: &InferCtxt<'_, 'tcx>,
216     ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
217         self.select_where_possible(infcx)?;
218
219         let errors: Vec<_> = self
220             .predicates
221             .to_errors(CodeAmbiguity)
222             .into_iter()
223             .map(to_fulfillment_error)
224             .collect();
225         if errors.is_empty() { Ok(()) } else { Err(errors) }
226     }
227
228     fn select_where_possible(
229         &mut self,
230         infcx: &InferCtxt<'_, 'tcx>,
231     ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
232         let mut selcx = SelectionContext::new(infcx);
233         self.select(&mut selcx)
234     }
235
236     fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
237         self.predicates.map_pending_obligations(|o| o.obligation.clone())
238     }
239 }
240
241 struct FulfillProcessor<'a, 'b, 'tcx> {
242     selcx: &'a mut SelectionContext<'b, 'tcx>,
243     register_region_obligations: bool,
244 }
245
246 fn mk_pending(os: Vec<PredicateObligation<'tcx>>) -> Vec<PendingPredicateObligation<'tcx>> {
247     os.into_iter()
248         .map(|o| PendingPredicateObligation { obligation: o, stalled_on: vec![] })
249         .collect()
250 }
251
252 impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
253     type Obligation = PendingPredicateObligation<'tcx>;
254     type Error = FulfillmentErrorCode<'tcx>;
255
256     /// Processes a predicate obligation and returns either:
257     /// - `Changed(v)` if the predicate is true, presuming that `v` are also true
258     /// - `Unchanged` if we don't have enough info to be sure
259     /// - `Error(e)` if the predicate does not hold
260     ///
261     /// This is always inlined, despite its size, because it has a single
262     /// callsite and it is called *very* frequently.
263     #[inline(always)]
264     fn process_obligation(
265         &mut self,
266         pending_obligation: &mut Self::Obligation,
267     ) -> ProcessResult<Self::Obligation, Self::Error> {
268         // If we were stalled on some unresolved variables, first check whether
269         // any of them have been resolved; if not, don't bother doing more work
270         // yet.
271         let change = match pending_obligation.stalled_on.len() {
272             // Match arms are in order of frequency, which matters because this
273             // code is so hot. 1 and 0 dominate; 2+ is fairly rare.
274             1 => {
275                 let infer_var = pending_obligation.stalled_on[0];
276                 self.selcx.infcx().ty_or_const_infer_var_changed(infer_var)
277             }
278             0 => {
279                 // In this case we haven't changed, but wish to make a change.
280                 true
281             }
282             _ => {
283                 // This `for` loop was once a call to `all()`, but this lower-level
284                 // form was a perf win. See #64545 for details.
285                 (|| {
286                     for &infer_var in &pending_obligation.stalled_on {
287                         if self.selcx.infcx().ty_or_const_infer_var_changed(infer_var) {
288                             return true;
289                         }
290                     }
291                     false
292                 })()
293             }
294         };
295
296         if !change {
297             debug!(
298                 "process_predicate: pending obligation {:?} still stalled on {:?}",
299                 self.selcx.infcx().resolve_vars_if_possible(&pending_obligation.obligation),
300                 pending_obligation.stalled_on
301             );
302             return ProcessResult::Unchanged;
303         }
304
305         // This part of the code is much colder.
306
307         pending_obligation.stalled_on.truncate(0);
308
309         let obligation = &mut pending_obligation.obligation;
310
311         if obligation.predicate.has_infer_types_or_consts() {
312             obligation.predicate =
313                 self.selcx.infcx().resolve_vars_if_possible(&obligation.predicate);
314         }
315
316         debug!("process_obligation: obligation = {:?} cause = {:?}", obligation, obligation.cause);
317
318         let infcx = self.selcx.infcx();
319
320         match obligation.predicate.kind() {
321             ty::PredicateKind::Trait(ref data, _) => {
322                 let trait_obligation = obligation.with(*data);
323
324                 if obligation.predicate.is_global() {
325                     // no type variables present, can use evaluation for better caching.
326                     // FIXME: consider caching errors too.
327                     if infcx.predicate_must_hold_considering_regions(&obligation) {
328                         debug!(
329                             "selecting trait `{:?}` at depth {} evaluated to holds",
330                             data, obligation.recursion_depth
331                         );
332                         return ProcessResult::Changed(vec![]);
333                     }
334                 }
335
336                 match self.selcx.select(&trait_obligation) {
337                     Ok(Some(impl_source)) => {
338                         debug!(
339                             "selecting trait `{:?}` at depth {} yielded Ok(Some)",
340                             data, obligation.recursion_depth
341                         );
342                         ProcessResult::Changed(mk_pending(impl_source.nested_obligations()))
343                     }
344                     Ok(None) => {
345                         debug!(
346                             "selecting trait `{:?}` at depth {} yielded Ok(None)",
347                             data, obligation.recursion_depth
348                         );
349
350                         // This is a bit subtle: for the most part, the
351                         // only reason we can fail to make progress on
352                         // trait selection is because we don't have enough
353                         // information about the types in the trait.
354                         pending_obligation.stalled_on =
355                             trait_ref_infer_vars(self.selcx, data.to_poly_trait_ref());
356
357                         debug!(
358                             "process_predicate: pending obligation {:?} now stalled on {:?}",
359                             infcx.resolve_vars_if_possible(obligation),
360                             pending_obligation.stalled_on
361                         );
362
363                         ProcessResult::Unchanged
364                     }
365                     Err(selection_err) => {
366                         info!(
367                             "selecting trait `{:?}` at depth {} yielded Err",
368                             data, obligation.recursion_depth
369                         );
370
371                         ProcessResult::Error(CodeSelectionError(selection_err))
372                     }
373                 }
374             }
375
376             &ty::PredicateKind::RegionOutlives(binder) => {
377                 match infcx.region_outlives_predicate(&obligation.cause, binder) {
378                     Ok(()) => ProcessResult::Changed(vec![]),
379                     Err(_) => ProcessResult::Error(CodeSelectionError(Unimplemented)),
380                 }
381             }
382
383             ty::PredicateKind::TypeOutlives(ref binder) => {
384                 // Check if there are higher-ranked vars.
385                 match binder.no_bound_vars() {
386                     // If there are, inspect the underlying type further.
387                     None => {
388                         // Convert from `Binder<OutlivesPredicate<Ty, Region>>` to `Binder<Ty>`.
389                         let binder = binder.map_bound_ref(|pred| pred.0);
390
391                         // Check if the type has any bound vars.
392                         match binder.no_bound_vars() {
393                             // If so, this obligation is an error (for now). Eventually we should be
394                             // able to support additional cases here, like `for<'a> &'a str: 'a`.
395                             // NOTE: this is duplicate-implemented between here and fulfillment.
396                             None => ProcessResult::Error(CodeSelectionError(Unimplemented)),
397                             // Otherwise, we have something of the form
398                             // `for<'a> T: 'a where 'a not in T`, which we can treat as
399                             // `T: 'static`.
400                             Some(t_a) => {
401                                 let r_static = self.selcx.tcx().lifetimes.re_static;
402                                 if self.register_region_obligations {
403                                     self.selcx.infcx().register_region_obligation_with_cause(
404                                         t_a,
405                                         r_static,
406                                         &obligation.cause,
407                                     );
408                                 }
409                                 ProcessResult::Changed(vec![])
410                             }
411                         }
412                     }
413                     // If there aren't, register the obligation.
414                     Some(ty::OutlivesPredicate(t_a, r_b)) => {
415                         if self.register_region_obligations {
416                             self.selcx.infcx().register_region_obligation_with_cause(
417                                 t_a,
418                                 r_b,
419                                 &obligation.cause,
420                             );
421                         }
422                         ProcessResult::Changed(vec![])
423                     }
424                 }
425             }
426
427             ty::PredicateKind::Projection(ref data) => {
428                 let project_obligation = obligation.with(*data);
429                 match project::poly_project_and_unify_type(self.selcx, &project_obligation) {
430                     Ok(None) => {
431                         let tcx = self.selcx.tcx();
432                         pending_obligation.stalled_on =
433                             trait_ref_infer_vars(self.selcx, data.to_poly_trait_ref(tcx));
434                         ProcessResult::Unchanged
435                     }
436                     Ok(Some(os)) => ProcessResult::Changed(mk_pending(os)),
437                     Err(e) => ProcessResult::Error(CodeProjectionError(e)),
438                 }
439             }
440
441             &ty::PredicateKind::ObjectSafe(trait_def_id) => {
442                 if !self.selcx.tcx().is_object_safe(trait_def_id) {
443                     ProcessResult::Error(CodeSelectionError(Unimplemented))
444                 } else {
445                     ProcessResult::Changed(vec![])
446                 }
447             }
448
449             &ty::PredicateKind::ClosureKind(_, closure_substs, kind) => {
450                 match self.selcx.infcx().closure_kind(closure_substs) {
451                     Some(closure_kind) => {
452                         if closure_kind.extends(kind) {
453                             ProcessResult::Changed(vec![])
454                         } else {
455                             ProcessResult::Error(CodeSelectionError(Unimplemented))
456                         }
457                     }
458                     None => ProcessResult::Unchanged,
459                 }
460             }
461
462             &ty::PredicateKind::WellFormed(arg) => {
463                 match wf::obligations(
464                     self.selcx.infcx(),
465                     obligation.param_env,
466                     obligation.cause.body_id,
467                     arg,
468                     obligation.cause.span,
469                 ) {
470                     None => {
471                         pending_obligation.stalled_on =
472                             vec![TyOrConstInferVar::maybe_from_generic_arg(arg).unwrap()];
473                         ProcessResult::Unchanged
474                     }
475                     Some(os) => ProcessResult::Changed(mk_pending(os)),
476                 }
477             }
478
479             &ty::PredicateKind::Subtype(subtype) => {
480                 match self.selcx.infcx().subtype_predicate(
481                     &obligation.cause,
482                     obligation.param_env,
483                     subtype,
484                 ) {
485                     None => {
486                         // None means that both are unresolved.
487                         pending_obligation.stalled_on = vec![
488                             TyOrConstInferVar::maybe_from_ty(subtype.skip_binder().a).unwrap(),
489                             TyOrConstInferVar::maybe_from_ty(subtype.skip_binder().b).unwrap(),
490                         ];
491                         ProcessResult::Unchanged
492                     }
493                     Some(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
494                     Some(Err(err)) => {
495                         let expected_found = ExpectedFound::new(
496                             subtype.skip_binder().a_is_expected,
497                             subtype.skip_binder().a,
498                             subtype.skip_binder().b,
499                         );
500                         ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
501                             expected_found,
502                             err,
503                         ))
504                     }
505                 }
506             }
507
508             &ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
509                 match self.selcx.infcx().const_eval_resolve(
510                     obligation.param_env,
511                     def_id,
512                     substs,
513                     None,
514                     Some(obligation.cause.span),
515                 ) {
516                     Ok(_) => ProcessResult::Changed(vec![]),
517                     Err(err) => ProcessResult::Error(CodeSelectionError(ConstEvalFailure(err))),
518                 }
519             }
520
521             ty::PredicateKind::ConstEquate(c1, c2) => {
522                 debug!("equating consts: c1={:?} c2={:?}", c1, c2);
523
524                 let stalled_on = &mut pending_obligation.stalled_on;
525
526                 let mut evaluate = |c: &'tcx Const<'tcx>| {
527                     if let ty::ConstKind::Unevaluated(def_id, substs, promoted) = c.val {
528                         match self.selcx.infcx().const_eval_resolve(
529                             obligation.param_env,
530                             def_id,
531                             substs,
532                             promoted,
533                             Some(obligation.cause.span),
534                         ) {
535                             Ok(val) => Ok(Const::from_value(self.selcx.tcx(), val, c.ty)),
536                             Err(ErrorHandled::TooGeneric) => {
537                                 stalled_on.append(
538                                     &mut substs
539                                         .types()
540                                         .filter_map(|ty| TyOrConstInferVar::maybe_from_ty(ty))
541                                         .collect(),
542                                 );
543                                 Err(ErrorHandled::TooGeneric)
544                             }
545                             Err(err) => Err(err),
546                         }
547                     } else {
548                         Ok(c)
549                     }
550                 };
551
552                 match (evaluate(c1), evaluate(c2)) {
553                     (Ok(c1), Ok(c2)) => {
554                         match self
555                             .selcx
556                             .infcx()
557                             .at(&obligation.cause, obligation.param_env)
558                             .eq(c1, c2)
559                         {
560                             Ok(_) => ProcessResult::Changed(vec![]),
561                             Err(err) => {
562                                 ProcessResult::Error(FulfillmentErrorCode::CodeConstEquateError(
563                                     ExpectedFound::new(true, c1, c2),
564                                     err,
565                                 ))
566                             }
567                         }
568                     }
569                     (Err(ErrorHandled::Reported(ErrorReported)), _)
570                     | (_, Err(ErrorHandled::Reported(ErrorReported))) => ProcessResult::Error(
571                         CodeSelectionError(ConstEvalFailure(ErrorHandled::Reported(ErrorReported))),
572                     ),
573                     (Err(ErrorHandled::Linted), _) | (_, Err(ErrorHandled::Linted)) => span_bug!(
574                         obligation.cause.span(self.selcx.tcx()),
575                         "ConstEquate: const_eval_resolve returned an unexpected error"
576                     ),
577                     (Err(ErrorHandled::TooGeneric), _) | (_, Err(ErrorHandled::TooGeneric)) => {
578                         ProcessResult::Unchanged
579                     }
580                 }
581             }
582         }
583     }
584
585     fn process_backedge<'c, I>(
586         &mut self,
587         cycle: I,
588         _marker: PhantomData<&'c PendingPredicateObligation<'tcx>>,
589     ) where
590         I: Clone + Iterator<Item = &'c PendingPredicateObligation<'tcx>>,
591     {
592         if self.selcx.coinductive_match(cycle.clone().map(|s| s.obligation.predicate)) {
593             debug!("process_child_obligations: coinductive match");
594         } else {
595             let cycle: Vec<_> = cycle.map(|c| c.obligation.clone()).collect();
596             self.selcx.infcx().report_overflow_error_cycle(&cycle);
597         }
598     }
599 }
600
601 /// Returns the set of inference variables contained in a trait ref.
602 fn trait_ref_infer_vars<'a, 'tcx>(
603     selcx: &mut SelectionContext<'a, 'tcx>,
604     trait_ref: ty::PolyTraitRef<'tcx>,
605 ) -> Vec<TyOrConstInferVar<'tcx>> {
606     selcx
607         .infcx()
608         .resolve_vars_if_possible(&trait_ref)
609         .skip_binder() // ok b/c this check doesn't care about regions
610         .substs
611         .iter()
612         // FIXME(eddyb) try using `skip_current_subtree` to skip everything that
613         // doesn't contain inference variables, not just the outermost level.
614         .filter(|arg| arg.has_infer_types_or_consts())
615         .flat_map(|arg| arg.walk())
616         .filter_map(TyOrConstInferVar::maybe_from_generic_arg)
617         .collect()
618 }
619
620 fn to_fulfillment_error<'tcx>(
621     error: Error<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>>,
622 ) -> FulfillmentError<'tcx> {
623     let obligation = error.backtrace.into_iter().next().unwrap().obligation;
624     FulfillmentError::new(obligation, error.error)
625 }