Skip to content

Commit f5323d6

Browse files
Auto merge of #145714 - lcnr:dedup-obligations, r=<try>
fulfill: dedup newly added obligations
2 parents 6ba0ce4 + 6cab005 commit f5323d6

File tree

8 files changed

+20
-60
lines changed

8 files changed

+20
-60
lines changed

compiler/rustc_trait_selection/src/solve/fulfill.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::marker::PhantomData;
22
use std::mem;
33
use std::ops::ControlFlow;
44

5+
use rustc_data_structures::fx::FxHashSet;
56
use rustc_data_structures::thinvec::ExtractIf;
67
use rustc_hir::def_id::LocalDefId;
78
use rustc_infer::infer::InferCtxt;
@@ -19,7 +20,7 @@ use rustc_next_trait_solver::solve::{
1920
};
2021
use rustc_span::Span;
2122
use thin_vec::ThinVec;
22-
use tracing::instrument;
23+
use tracing::{debug, instrument};
2324

2425
use self::derive_errors::*;
2526
use super::Certainty;
@@ -57,6 +58,7 @@ pub struct FulfillmentCtxt<'tcx, E: 'tcx> {
5758

5859
#[derive(Default, Debug)]
5960
struct ObligationStorage<'tcx> {
61+
dedup: FxHashSet<(ty::ParamEnv<'tcx>, ty::Predicate<'tcx>)>,
6062
/// Obligations which resulted in an overflow in fulfillment itself.
6163
///
6264
/// We cannot eagerly return these as error so we instead store them here
@@ -67,7 +69,14 @@ struct ObligationStorage<'tcx> {
6769
}
6870

6971
impl<'tcx> ObligationStorage<'tcx> {
70-
fn register(
72+
fn register(&mut self, obligation: PredicateObligation<'tcx>) {
73+
if self.dedup.insert((obligation.param_env, obligation.predicate)) {
74+
self.pending.push((obligation, None));
75+
} else {
76+
debug!("skipping already registered obligation: {obligation:?}");
77+
}
78+
}
79+
fn readd_pending(
7180
&mut self,
7281
obligation: PredicateObligation<'tcx>,
7382
stalled_on: Option<GoalStalledOn<TyCtxt<'tcx>>>,
@@ -161,7 +170,7 @@ where
161170
obligation: PredicateObligation<'tcx>,
162171
) {
163172
assert_eq!(self.usable_in_snapshot, infcx.num_open_snapshots());
164-
self.obligations.register(obligation, None);
173+
self.obligations.register(obligation);
165174
}
166175

167176
fn collect_remaining_errors(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<E> {
@@ -205,7 +214,7 @@ where
205214
// Only goals proven via the trait solver should be region dependent.
206215
Certainty::Yes => {}
207216
Certainty::Maybe(_) => {
208-
self.obligations.register(obligation, None);
217+
self.obligations.readd_pending(obligation, None);
209218
}
210219
}
211220
continue;
@@ -256,7 +265,7 @@ where
256265
infcx.push_hir_typeck_potentially_region_dependent_goal(obligation);
257266
}
258267
}
259-
Certainty::Maybe(_) => self.obligations.register(obligation, stalled_on),
268+
Certainty::Maybe(_) => self.obligations.readd_pending(obligation, stalled_on),
260269
}
261270
}
262271

tests/ui/const-generics/issues/issue-88119.stderr

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,6 @@ note: required by a bound in `<&T as ConstName>`
3030
LL | [(); name_len::<T>()]:,
3131
| ^^^^^^^^^^^^^^^^^^^^^ required by this bound in `<&T as ConstName>`
3232

33-
error[E0275]: overflow evaluating the requirement `[(); name_len::<T>()] well-formed`
34-
--> $DIR/issue-88119.rs:21:10
35-
|
36-
LL | [(); name_len::<T>()]:,
37-
| ^^^^^^^^^^^^^^^
38-
|
39-
note: required by a bound in `<&T as ConstName>`
40-
--> $DIR/issue-88119.rs:21:5
41-
|
42-
LL | [(); name_len::<T>()]:,
43-
| ^^^^^^^^^^^^^^^^^^^^^ required by this bound in `<&T as ConstName>`
44-
4533
error[E0275]: overflow evaluating the requirement `&mut T: [const] ConstName`
4634
--> $DIR/issue-88119.rs:26:49
4735
|
@@ -66,18 +54,6 @@ note: required by a bound in `<&mut T as ConstName>`
6654
LL | [(); name_len::<T>()]:,
6755
| ^^^^^^^^^^^^^^^^^^^^^ required by this bound in `<&mut T as ConstName>`
6856

69-
error[E0275]: overflow evaluating the requirement `[(); name_len::<T>()] well-formed`
70-
--> $DIR/issue-88119.rs:28:10
71-
|
72-
LL | [(); name_len::<T>()]:,
73-
| ^^^^^^^^^^^^^^^
74-
|
75-
note: required by a bound in `<&mut T as ConstName>`
76-
--> $DIR/issue-88119.rs:28:5
77-
|
78-
LL | [(); name_len::<T>()]:,
79-
| ^^^^^^^^^^^^^^^^^^^^^ required by this bound in `<&mut T as ConstName>`
80-
8157
error[E0275]: overflow evaluating the requirement `&&mut u8: ConstName`
8258
--> $DIR/issue-88119.rs:33:35
8359
|
@@ -90,6 +66,6 @@ error[E0275]: overflow evaluating the requirement `&mut &u8: ConstName`
9066
LL | pub const ICE_2: &'static [u8] = <&mut &u8 as ConstName>::NAME_BYTES;
9167
| ^^^^^^^^
9268

93-
error: aborting due to 11 previous errors
69+
error: aborting due to 9 previous errors
9470

9571
For more information about this error, try `rustc --explain E0275`.

tests/ui/traits/const-traits/assoc-type-const-bound-usage-fail-2.next.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ error[E0277]: the trait bound `U: [const] Other` is not satisfied
44
LL | T::Assoc::<U>::func();
55
| ^^^^^^^^^^^^^
66

7-
error[E0277]: the trait bound `U: [const] Other` is not satisfied
8-
--> $DIR/assoc-type-const-bound-usage-fail-2.rs:26:5
9-
|
10-
LL | <T as Trait>::Assoc::<U>::func();
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
148

159
For more information about this error, try `rustc --explain E0277`.

tests/ui/traits/const-traits/assoc-type-const-bound-usage-fail-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const fn fails<T: [const] Trait, U: Other>() {
2424
T::Assoc::<U>::func();
2525
//~^ ERROR the trait bound `U: [const] Other` is not satisfied
2626
<T as Trait>::Assoc::<U>::func();
27-
//~^ ERROR the trait bound `U: [const] Other` is not satisfied
27+
//[current]~^ ERROR the trait bound `U: [const] Other` is not satisfied
2828
}
2929

3030
const fn works<T: [const] Trait, U: [const] Other>() {

tests/ui/traits/const-traits/assoc-type-const-bound-usage-fail.next.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ error[E0277]: the trait bound `T: [const] Trait` is not satisfied
44
LL | T::Assoc::func();
55
| ^^^^^^^^
66

7-
error[E0277]: the trait bound `T: [const] Trait` is not satisfied
8-
--> $DIR/assoc-type-const-bound-usage-fail.rs:19:5
9-
|
10-
LL | <T as Trait>::Assoc::func();
11-
| ^^^^^^^^^^^^^^^^^^^
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
148

159
For more information about this error, try `rustc --explain E0277`.

tests/ui/traits/const-traits/assoc-type-const-bound-usage-fail.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const fn unqualified<T: Trait>() {
1717
T::Assoc::func();
1818
//~^ ERROR the trait bound `T: [const] Trait` is not satisfied
1919
<T as Trait>::Assoc::func();
20-
//~^ ERROR the trait bound `T: [const] Trait` is not satisfied
20+
//[current]~^ ERROR the trait bound `T: [const] Trait` is not satisfied
2121
}
2222

2323
const fn works<T: [const] Trait>() {

tests/ui/traits/const-traits/trait-where-clause-const.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const fn test1<T: [const] Foo + Bar>() {
2121
T::b();
2222
//~^ ERROR the trait bound
2323
T::c::<T>();
24-
//~^ ERROR the trait bound
2524
}
2625

2726
const fn test2<T: [const] Foo + [const] Bar>() {

tests/ui/traits/const-traits/trait-where-clause-const.stderr

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@ note: required by a bound in `Foo::b`
1010
LL | fn b() where Self: [const] Bar;
1111
| ^^^^^^^^^^^ required by this bound in `Foo::b`
1212

13-
error[E0277]: the trait bound `T: [const] Bar` is not satisfied
14-
--> $DIR/trait-where-clause-const.rs:23:12
15-
|
16-
LL | T::c::<T>();
17-
| ^
18-
|
19-
note: required by a bound in `Foo::c`
20-
--> $DIR/trait-where-clause-const.rs:16:13
21-
|
22-
LL | fn c<T: [const] Bar>();
23-
| ^^^^^^^^^^^ required by this bound in `Foo::c`
24-
25-
error: aborting due to 2 previous errors
13+
error: aborting due to 1 previous error
2614

2715
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)