@@ -8,6 +8,7 @@ use std::ops::ControlFlow;
8
8
use derive_where:: derive_where;
9
9
use rustc_type_ir:: inherent:: * ;
10
10
use rustc_type_ir:: lang_items:: TraitSolverLangItem ;
11
+ use rustc_type_ir:: search_graph:: CandidateUsages ;
11
12
use rustc_type_ir:: solve:: SizedTraitKind ;
12
13
use rustc_type_ir:: {
13
14
self as ty, Interner , TypeFlags , TypeFoldable , TypeSuperVisitable , TypeVisitable ,
@@ -33,10 +34,11 @@ enum AliasBoundKind {
33
34
///
34
35
/// It consists of both the `source`, which describes how that goal would be proven,
35
36
/// and the `result` when using the given `source`.
36
- #[ derive_where( Clone , Debug ; I : Interner ) ]
37
+ #[ derive_where( Debug ; I : Interner ) ]
37
38
pub ( super ) struct Candidate < I : Interner > {
38
39
pub ( super ) source : CandidateSource < I > ,
39
40
pub ( super ) result : CanonicalResponse < I > ,
41
+ pub ( super ) candidate_usages : CandidateUsages ,
40
42
}
41
43
42
44
/// Methods used to assemble candidates for either trait or projection goals.
@@ -116,8 +118,11 @@ where
116
118
ecx : & mut EvalCtxt < ' _ , D > ,
117
119
goal : Goal < I , Self > ,
118
120
assumption : I :: Clause ,
119
- ) -> Result < Candidate < I > , NoSolution > {
120
- Self :: fast_reject_assumption ( ecx, goal, assumption) ?;
121
+ ) -> Result < Candidate < I > , CandidateUsages > {
122
+ match Self :: fast_reject_assumption ( ecx, goal, assumption) {
123
+ Ok ( ( ) ) => { }
124
+ Err ( NoSolution ) => return Err ( CandidateUsages :: default ( ) ) ,
125
+ }
121
126
122
127
// Dealing with `ParamEnv` candidates is a bit of a mess as we need to lazily
123
128
// check whether the candidate is global while considering normalization.
@@ -126,18 +131,23 @@ where
126
131
// in `probe` even if the candidate does not apply before we get there. We handle this
127
132
// by using a `Cell` here. We only ever write into it inside of `match_assumption`.
128
133
let source = Cell :: new ( CandidateSource :: ParamEnv ( ParamEnvSource :: Global ) ) ;
129
- ecx. probe ( |result : & QueryResult < I > | inspect:: ProbeKind :: TraitCandidate {
130
- source : source. get ( ) ,
131
- result : * result,
132
- } )
133
- . enter ( |ecx| {
134
- Self :: match_assumption ( ecx, goal, assumption, |ecx| {
135
- ecx. try_evaluate_added_goals ( ) ?;
136
- source. set ( ecx. characterize_param_env_assumption ( goal. param_env , assumption) ?) ;
137
- ecx. evaluate_added_goals_and_make_canonical_response ( Certainty :: Yes )
134
+ let ( result, candidate_usages) = ecx
135
+ . probe ( |result : & QueryResult < I > | inspect:: ProbeKind :: TraitCandidate {
136
+ source : source. get ( ) ,
137
+ result : * result,
138
138
} )
139
- } )
140
- . map ( |result| Candidate { source : source. get ( ) , result } )
139
+ . enter_unique_candidate ( |ecx| {
140
+ Self :: match_assumption ( ecx, goal, assumption, |ecx| {
141
+ ecx. try_evaluate_added_goals ( ) ?;
142
+ source. set ( ecx. characterize_param_env_assumption ( goal. param_env , assumption) ?) ;
143
+ ecx. evaluate_added_goals_and_make_canonical_response ( Certainty :: Yes )
144
+ } )
145
+ } ) ;
146
+
147
+ match result {
148
+ Ok ( result) => Ok ( Candidate { source : source. get ( ) , result, candidate_usages } ) ,
149
+ Err ( NoSolution ) => Err ( candidate_usages) ,
150
+ }
141
151
}
142
152
143
153
/// Try equating an assumption predicate against a goal's predicate. If it
@@ -355,6 +365,12 @@ pub(super) enum AssembleCandidatesFrom {
355
365
EnvAndBounds ,
356
366
}
357
367
368
+ // TODO
369
+ #[ derive( Debug ) ]
370
+ pub ( super ) struct FailedCandidateInfo {
371
+ pub param_env_usages : CandidateUsages ,
372
+ }
373
+
358
374
impl < D , I > EvalCtxt < ' _ , D >
359
375
where
360
376
D : SolverDelegate < Interner = I > ,
@@ -364,16 +380,20 @@ where
364
380
& mut self ,
365
381
goal : Goal < I , G > ,
366
382
assemble_from : AssembleCandidatesFrom ,
367
- ) -> Vec < Candidate < I > > {
383
+ ) -> ( Vec < Candidate < I > > , FailedCandidateInfo ) {
384
+ let mut candidates = vec ! [ ] ;
385
+ let mut failed_candidate_info =
386
+ FailedCandidateInfo { param_env_usages : CandidateUsages :: default ( ) } ;
368
387
let Ok ( normalized_self_ty) =
369
388
self . structurally_normalize_ty ( goal. param_env , goal. predicate . self_ty ( ) )
370
389
else {
371
- return vec ! [ ] ;
390
+ return ( candidates , failed_candidate_info ) ;
372
391
} ;
373
392
374
393
if normalized_self_ty. is_ty_var ( ) {
375
394
debug ! ( "self type has been normalized to infer" ) ;
376
- return self . forced_ambiguity ( MaybeCause :: Ambiguity ) . into_iter ( ) . collect ( ) ;
395
+ candidates. extend ( self . forced_ambiguity ( MaybeCause :: Ambiguity ) ) ;
396
+ return ( candidates, failed_candidate_info) ;
377
397
}
378
398
379
399
let goal: Goal < I , G > = goal
@@ -382,16 +402,15 @@ where
382
402
// normalizing the self type as well, since type variables are not uniquified.
383
403
let goal = self . resolve_vars_if_possible ( goal) ;
384
404
385
- let mut candidates = vec ! [ ] ;
386
-
387
405
if let TypingMode :: Coherence = self . typing_mode ( )
388
406
&& let Ok ( candidate) = self . consider_coherence_unknowable_candidate ( goal)
389
407
{
390
- return vec ! [ candidate] ;
408
+ candidates. push ( candidate) ;
409
+ return ( candidates, failed_candidate_info) ;
391
410
}
392
411
393
412
self . assemble_alias_bound_candidates ( goal, & mut candidates) ;
394
- self . assemble_param_env_candidates ( goal, & mut candidates) ;
413
+ self . assemble_param_env_candidates ( goal, & mut candidates, & mut failed_candidate_info ) ;
395
414
396
415
match assemble_from {
397
416
AssembleCandidatesFrom :: All => {
@@ -423,7 +442,7 @@ where
423
442
AssembleCandidatesFrom :: EnvAndBounds => { }
424
443
}
425
444
426
- candidates
445
+ ( candidates, failed_candidate_info )
427
446
}
428
447
429
448
pub ( super ) fn forced_ambiguity (
@@ -584,9 +603,15 @@ where
584
603
& mut self ,
585
604
goal : Goal < I , G > ,
586
605
candidates : & mut Vec < Candidate < I > > ,
606
+ failed_candidate_info : & mut FailedCandidateInfo ,
587
607
) {
588
608
for assumption in goal. param_env . caller_bounds ( ) . iter ( ) {
589
- candidates. extend ( G :: probe_and_consider_param_env_candidate ( self , goal, assumption) ) ;
609
+ match G :: probe_and_consider_param_env_candidate ( self , goal, assumption) {
610
+ Ok ( candidate) => candidates. push ( candidate) ,
611
+ Err ( candidate_usages) => {
612
+ failed_candidate_info. param_env_usages . add_usages ( candidate_usages)
613
+ }
614
+ }
590
615
}
591
616
}
592
617
@@ -661,7 +686,11 @@ where
661
686
if let Ok ( result) =
662
687
self . evaluate_added_goals_and_make_canonical_response ( Certainty :: AMBIGUOUS )
663
688
{
664
- candidates. push ( Candidate { source : CandidateSource :: AliasBound , result } ) ;
689
+ candidates. push ( Candidate {
690
+ source : CandidateSource :: AliasBound ,
691
+ result,
692
+ candidate_usages : CandidateUsages :: default ( ) ,
693
+ } ) ;
665
694
}
666
695
return ;
667
696
}
@@ -959,7 +988,7 @@ where
959
988
// Even when a trait bound has been proven using a where-bound, we
960
989
// still need to consider alias-bounds for normalization, see
961
990
// `tests/ui/next-solver/alias-bound-shadowed-by-env.rs`.
962
- let mut candidates: Vec < _ > = self
991
+ let ( mut candidates, _ ) = self
963
992
. assemble_and_evaluate_candidates ( goal, AssembleCandidatesFrom :: EnvAndBounds ) ;
964
993
965
994
// We still need to prefer where-bounds over alias-bounds however.
@@ -972,23 +1001,20 @@ where
972
1001
return inject_normalize_to_rigid_candidate ( self ) ;
973
1002
}
974
1003
975
- if let Some ( response) = self . try_merge_candidates ( & candidates) {
1004
+ if let Some ( ( response, _ ) ) = self . try_merge_candidates ( & candidates) {
976
1005
Ok ( response)
977
1006
} else {
978
1007
self . flounder ( & candidates)
979
1008
}
980
1009
}
981
1010
TraitGoalProvenVia :: Misc => {
982
- let mut candidates =
1011
+ let ( mut candidates, _ ) =
983
1012
self . assemble_and_evaluate_candidates ( goal, AssembleCandidatesFrom :: All ) ;
984
1013
985
1014
// Prefer "orphaned" param-env normalization predicates, which are used
986
1015
// (for example, and ideally only) when proving item bounds for an impl.
987
- let candidates_from_env: Vec < _ > = candidates
988
- . extract_if ( .., |c| matches ! ( c. source, CandidateSource :: ParamEnv ( _) ) )
989
- . collect ( ) ;
990
- if let Some ( response) = self . try_merge_candidates ( & candidates_from_env) {
991
- return Ok ( response) ;
1016
+ if candidates. iter ( ) . any ( |c| matches ! ( c. source, CandidateSource :: ParamEnv ( _) ) ) {
1017
+ candidates. retain ( |c| matches ! ( c. source, CandidateSource :: ParamEnv ( _) ) ) ;
992
1018
}
993
1019
994
1020
// We drop specialized impls to allow normalization via a final impl here. In case
@@ -997,7 +1023,7 @@ where
997
1023
// means we can just ignore inference constraints and don't have to special-case
998
1024
// constraining the normalized-to `term`.
999
1025
self . filter_specialized_impls ( AllowInferenceConstraints :: Yes , & mut candidates) ;
1000
- if let Some ( response) = self . try_merge_candidates ( & candidates) {
1026
+ if let Some ( ( response, _ ) ) = self . try_merge_candidates ( & candidates) {
1001
1027
Ok ( response)
1002
1028
} else {
1003
1029
self . flounder ( & candidates)
0 commit comments