Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1a372d6

Browse files
committedJan 17, 2025
Auto merge of rust-lang#135618 - lcnr:coherence-unknown, r=<try>
add cache to `AmbiguityCausesVisitor` fixes rust-lang#135457, alternative to rust-lang#135524. cc https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/new-solver.20hang.20.23135457 r? `@compiler-errors`
2 parents 0c2c096 + 94bf8f0 commit 1a372d6

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed
 

‎compiler/rustc_trait_selection/src/traits/coherence.rs

+45-24
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
use std::fmt::Debug;
88

9-
use rustc_data_structures::fx::FxIndexSet;
9+
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
1010
use rustc_errors::{Diag, EmissionGuarantee};
1111
use rustc_hir::def::DefKind;
1212
use 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

143154
fn 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

617628
struct 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
}

‎tests/ui/coherence/occurs-check/associated-type.next.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTy { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), "'a")], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit), .. }
22
WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTy { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), "'a")], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit), .. }
3-
WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTy { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), "'a")], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit), .. }
4-
WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTy { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), "'a")], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit), .. }
53
error[E0119]: conflicting implementations of trait `Overlap<for<'a> fn(&'a (), ())>` for type `for<'a> fn(&'a (), ())`
64
--> $DIR/associated-type.rs:32:1
75
|

‎tests/ui/coherence/occurs-check/associated-type.old.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTy { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), "'a")], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit), .. }
22
WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTy { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), "'a")], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit), .. }
3-
WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTy { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), "'a")], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit), .. }
4-
WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTy { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), "'a")], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit), .. }
53
error[E0119]: conflicting implementations of trait `Overlap<for<'a> fn(&'a (), ())>` for type `for<'a> fn(&'a (), ())`
64
--> $DIR/associated-type.rs:32:1
75
|

0 commit comments

Comments
 (0)
Failed to load comments.