66
77use std:: fmt:: Debug ;
88
9- use rustc_data_structures:: fx:: FxIndexSet ;
9+ use rustc_data_structures:: fx:: { FxHashSet , FxIndexSet } ;
1010use rustc_errors:: { Diag , EmissionGuarantee } ;
1111use rustc_hir:: def:: DefKind ;
1212use rustc_hir:: def_id:: DefId ;
@@ -116,28 +116,39 @@ pub fn overlapping_impls(
116116 return None ;
117117 }
118118
119- let _overlap_with_bad_diagnostics = overlap (
120- tcx,
121- TrackAmbiguityCauses :: No ,
122- skip_leak_check,
123- impl1_def_id,
124- impl2_def_id,
125- overlap_mode,
126- ) ?;
127-
128- // In the case where we detect an error, run the check again, but
129- // this time tracking intercrate ambiguity causes for better
130- // diagnostics. (These take time and can lead to false errors.)
131- let overlap = overlap (
132- tcx,
133- TrackAmbiguityCauses :: Yes ,
134- skip_leak_check,
135- impl1_def_id,
136- impl2_def_id,
137- overlap_mode,
138- )
139- . unwrap ( ) ;
140- Some ( overlap)
119+ if tcx. next_trait_solver_in_coherence ( ) {
120+ overlap (
121+ tcx,
122+ TrackAmbiguityCauses :: Yes ,
123+ skip_leak_check,
124+ impl1_def_id,
125+ impl2_def_id,
126+ overlap_mode,
127+ )
128+ } else {
129+ let _overlap_with_bad_diagnostics = overlap (
130+ tcx,
131+ TrackAmbiguityCauses :: No ,
132+ skip_leak_check,
133+ impl1_def_id,
134+ impl2_def_id,
135+ overlap_mode,
136+ ) ?;
137+
138+ // In the case where we detect an error, run the check again, but
139+ // this time tracking intercrate ambiguity causes for better
140+ // diagnostics. (These take time and can lead to false errors.)
141+ let overlap = overlap (
142+ tcx,
143+ TrackAmbiguityCauses :: Yes ,
144+ skip_leak_check,
145+ impl1_def_id,
146+ impl2_def_id,
147+ overlap_mode,
148+ )
149+ . unwrap ( ) ;
150+ Some ( overlap)
151+ }
141152}
142153
143154fn fresh_impl_header < ' tcx > ( infcx : & InferCtxt < ' tcx > , impl_def_id : DefId ) -> ty:: ImplHeader < ' tcx > {
@@ -615,6 +626,7 @@ fn compute_intercrate_ambiguity_causes<'tcx>(
615626}
616627
617628struct AmbiguityCausesVisitor < ' a , ' tcx > {
629+ cache : FxHashSet < Goal < ' tcx , ty:: Predicate < ' tcx > > > ,
618630 causes : & ' a mut FxIndexSet < IntercrateAmbiguityCause < ' tcx > > ,
619631}
620632
@@ -624,6 +636,10 @@ impl<'a, 'tcx> ProofTreeVisitor<'tcx> for AmbiguityCausesVisitor<'a, 'tcx> {
624636 }
625637
626638 fn visit_goal ( & mut self , goal : & InspectGoal < ' _ , ' tcx > ) {
639+ if !self . cache . insert ( goal. goal ( ) ) {
640+ return ;
641+ }
642+
627643 let infcx = goal. infcx ( ) ;
628644 for cand in goal. candidates ( ) {
629645 cand. visit_nested_in_probe ( self ) ;
@@ -748,5 +764,10 @@ fn search_ambiguity_causes<'tcx>(
748764 goal : Goal < ' tcx , ty:: Predicate < ' tcx > > ,
749765 causes : & mut FxIndexSet < IntercrateAmbiguityCause < ' tcx > > ,
750766) {
751- infcx. probe ( |_| infcx. visit_proof_tree ( goal, & mut AmbiguityCausesVisitor { causes } ) ) ;
767+ infcx. probe ( |_| {
768+ infcx. visit_proof_tree ( goal, & mut AmbiguityCausesVisitor {
769+ cache : Default :: default ( ) ,
770+ causes,
771+ } )
772+ } ) ;
752773}
0 commit comments