|
7 | 7 | //! [rustc dev guide]:https://rustc-dev-guide.rust-lang.org/traits/resolution.html#candidate-assembly
|
8 | 8 | use rustc_hir as hir;
|
9 | 9 | use rustc_infer::traits::{Obligation, SelectionError, TraitObligation};
|
| 10 | +use rustc_middle::ty::print::with_no_trimmed_paths; |
10 | 11 | use rustc_middle::ty::{self, TypeFoldable};
|
11 | 12 | use rustc_target::spec::abi::Abi;
|
12 | 13 |
|
| 14 | +use crate::traits::coherence::Conflict; |
13 | 15 | use crate::traits::{util, SelectionResult};
|
| 16 | +use crate::traits::{Overflow, Unimplemented}; |
14 | 17 |
|
15 | 18 | use super::BuiltinImplConditions;
|
| 19 | +use super::IntercrateAmbiguityCause; |
| 20 | +use super::OverflowError; |
16 | 21 | use super::SelectionCandidate::{self, *};
|
17 |
| -use super::{SelectionCandidateSet, SelectionContext, TraitObligationStack}; |
| 22 | +use super::{EvaluatedCandidate, SelectionCandidateSet, SelectionContext, TraitObligationStack}; |
18 | 23 |
|
19 | 24 | impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
20 | 25 | pub(super) fn candidate_from_obligation<'o>(
|
@@ -62,6 +67,161 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
62 | 67 | candidate
|
63 | 68 | }
|
64 | 69 |
|
| 70 | + fn candidate_from_obligation_no_cache<'o>( |
| 71 | + &mut self, |
| 72 | + stack: &TraitObligationStack<'o, 'tcx>, |
| 73 | + ) -> SelectionResult<'tcx, SelectionCandidate<'tcx>> { |
| 74 | + if let Some(conflict) = self.is_knowable(stack) { |
| 75 | + debug!("coherence stage: not knowable"); |
| 76 | + if self.intercrate_ambiguity_causes.is_some() { |
| 77 | + debug!("evaluate_stack: intercrate_ambiguity_causes is some"); |
| 78 | + // Heuristics: show the diagnostics when there are no candidates in crate. |
| 79 | + if let Ok(candidate_set) = self.assemble_candidates(stack) { |
| 80 | + let mut no_candidates_apply = true; |
| 81 | + |
| 82 | + for c in candidate_set.vec.iter() { |
| 83 | + if self.evaluate_candidate(stack, &c)?.may_apply() { |
| 84 | + no_candidates_apply = false; |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if !candidate_set.ambiguous && no_candidates_apply { |
| 90 | + let trait_ref = stack.obligation.predicate.skip_binder().trait_ref; |
| 91 | + let self_ty = trait_ref.self_ty(); |
| 92 | + let (trait_desc, self_desc) = with_no_trimmed_paths(|| { |
| 93 | + let trait_desc = trait_ref.print_only_trait_path().to_string(); |
| 94 | + let self_desc = if self_ty.has_concrete_skeleton() { |
| 95 | + Some(self_ty.to_string()) |
| 96 | + } else { |
| 97 | + None |
| 98 | + }; |
| 99 | + (trait_desc, self_desc) |
| 100 | + }); |
| 101 | + let cause = if let Conflict::Upstream = conflict { |
| 102 | + IntercrateAmbiguityCause::UpstreamCrateUpdate { trait_desc, self_desc } |
| 103 | + } else { |
| 104 | + IntercrateAmbiguityCause::DownstreamCrate { trait_desc, self_desc } |
| 105 | + }; |
| 106 | + debug!("evaluate_stack: pushing cause = {:?}", cause); |
| 107 | + self.intercrate_ambiguity_causes.as_mut().unwrap().push(cause); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + return Ok(None); |
| 112 | + } |
| 113 | + |
| 114 | + let candidate_set = self.assemble_candidates(stack)?; |
| 115 | + |
| 116 | + if candidate_set.ambiguous { |
| 117 | + debug!("candidate set contains ambig"); |
| 118 | + return Ok(None); |
| 119 | + } |
| 120 | + |
| 121 | + let mut candidates = candidate_set.vec; |
| 122 | + |
| 123 | + debug!("assembled {} candidates for {:?}: {:?}", candidates.len(), stack, candidates); |
| 124 | + |
| 125 | + // At this point, we know that each of the entries in the |
| 126 | + // candidate set is *individually* applicable. Now we have to |
| 127 | + // figure out if they contain mutual incompatibilities. This |
| 128 | + // frequently arises if we have an unconstrained input type -- |
| 129 | + // for example, we are looking for `$0: Eq` where `$0` is some |
| 130 | + // unconstrained type variable. In that case, we'll get a |
| 131 | + // candidate which assumes $0 == int, one that assumes `$0 == |
| 132 | + // usize`, etc. This spells an ambiguity. |
| 133 | + |
| 134 | + // If there is more than one candidate, first winnow them down |
| 135 | + // by considering extra conditions (nested obligations and so |
| 136 | + // forth). We don't winnow if there is exactly one |
| 137 | + // candidate. This is a relatively minor distinction but it |
| 138 | + // can lead to better inference and error-reporting. An |
| 139 | + // example would be if there was an impl: |
| 140 | + // |
| 141 | + // impl<T:Clone> Vec<T> { fn push_clone(...) { ... } } |
| 142 | + // |
| 143 | + // and we were to see some code `foo.push_clone()` where `boo` |
| 144 | + // is a `Vec<Bar>` and `Bar` does not implement `Clone`. If |
| 145 | + // we were to winnow, we'd wind up with zero candidates. |
| 146 | + // Instead, we select the right impl now but report "`Bar` does |
| 147 | + // not implement `Clone`". |
| 148 | + if candidates.len() == 1 { |
| 149 | + return self.filter_negative_and_reservation_impls(candidates.pop().unwrap()); |
| 150 | + } |
| 151 | + |
| 152 | + // Winnow, but record the exact outcome of evaluation, which |
| 153 | + // is needed for specialization. Propagate overflow if it occurs. |
| 154 | + let mut candidates = candidates |
| 155 | + .into_iter() |
| 156 | + .map(|c| match self.evaluate_candidate(stack, &c) { |
| 157 | + Ok(eval) if eval.may_apply() => { |
| 158 | + Ok(Some(EvaluatedCandidate { candidate: c, evaluation: eval })) |
| 159 | + } |
| 160 | + Ok(_) => Ok(None), |
| 161 | + Err(OverflowError) => Err(Overflow), |
| 162 | + }) |
| 163 | + .flat_map(Result::transpose) |
| 164 | + .collect::<Result<Vec<_>, _>>()?; |
| 165 | + |
| 166 | + debug!("winnowed to {} candidates for {:?}: {:?}", candidates.len(), stack, candidates); |
| 167 | + |
| 168 | + let needs_infer = stack.obligation.predicate.needs_infer(); |
| 169 | + |
| 170 | + // If there are STILL multiple candidates, we can further |
| 171 | + // reduce the list by dropping duplicates -- including |
| 172 | + // resolving specializations. |
| 173 | + if candidates.len() > 1 { |
| 174 | + let mut i = 0; |
| 175 | + while i < candidates.len() { |
| 176 | + let is_dup = (0..candidates.len()).filter(|&j| i != j).any(|j| { |
| 177 | + self.candidate_should_be_dropped_in_favor_of( |
| 178 | + &candidates[i], |
| 179 | + &candidates[j], |
| 180 | + needs_infer, |
| 181 | + ) |
| 182 | + }); |
| 183 | + if is_dup { |
| 184 | + debug!("Dropping candidate #{}/{}: {:?}", i, candidates.len(), candidates[i]); |
| 185 | + candidates.swap_remove(i); |
| 186 | + } else { |
| 187 | + debug!("Retaining candidate #{}/{}: {:?}", i, candidates.len(), candidates[i]); |
| 188 | + i += 1; |
| 189 | + |
| 190 | + // If there are *STILL* multiple candidates, give up |
| 191 | + // and report ambiguity. |
| 192 | + if i > 1 { |
| 193 | + debug!("multiple matches, ambig"); |
| 194 | + return Ok(None); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + // If there are *NO* candidates, then there are no impls -- |
| 201 | + // that we know of, anyway. Note that in the case where there |
| 202 | + // are unbound type variables within the obligation, it might |
| 203 | + // be the case that you could still satisfy the obligation |
| 204 | + // from another crate by instantiating the type variables with |
| 205 | + // a type from another crate that does have an impl. This case |
| 206 | + // is checked for in `evaluate_stack` (and hence users |
| 207 | + // who might care about this case, like coherence, should use |
| 208 | + // that function). |
| 209 | + if candidates.is_empty() { |
| 210 | + // If there's an error type, 'downgrade' our result from |
| 211 | + // `Err(Unimplemented)` to `Ok(None)`. This helps us avoid |
| 212 | + // emitting additional spurious errors, since we're guaranteed |
| 213 | + // to have emitted at least one. |
| 214 | + if stack.obligation.references_error() { |
| 215 | + debug!("no results for error type, treating as ambiguous"); |
| 216 | + return Ok(None); |
| 217 | + } |
| 218 | + return Err(Unimplemented); |
| 219 | + } |
| 220 | + |
| 221 | + // Just one candidate left. |
| 222 | + self.filter_negative_and_reservation_impls(candidates.pop().unwrap().candidate) |
| 223 | + } |
| 224 | + |
65 | 225 | pub(super) fn assemble_candidates<'o>(
|
66 | 226 | &mut self,
|
67 | 227 | stack: &TraitObligationStack<'o, 'tcx>,
|
|
0 commit comments