Skip to content

Commit cfcde24

Browse files
committed
Auto merge of rust-lang#111754 - lcnr:recursion-depth, r=matthewjasper
fix recursion depth handling after confirmation fixes rust-lang#111729 I think having to use `Obligation::with_depth` correctly everywhere is very hard because e.g. the nested obligations from `eq` currently do not have the correct obligation depth. The new solver [completely removes `recursion_depth` from obligations](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/traits/solve/struct.Goal.html) and instead tracks the depth in the solver itself which is far easier to get right. Moving the old solver towards this shouldn't be that hard but is probably somewhat annoying. r? `@matthewjasper`
2 parents 2fe47b9 + c5ec1b8 commit cfcde24

File tree

5 files changed

+101
-4
lines changed

5 files changed

+101
-4
lines changed

compiler/rustc_middle/src/traits/mod.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -701,9 +701,9 @@ impl<'tcx, N> ImplSource<'tcx, N> {
701701
}
702702

703703
pub fn borrow_nested_obligations(&self) -> &[N] {
704-
match &self {
705-
ImplSource::UserDefined(i) => &i.nested[..],
706-
ImplSource::Param(n, _) => &n,
704+
match self {
705+
ImplSource::UserDefined(i) => &i.nested,
706+
ImplSource::Param(n, _) => n,
707707
ImplSource::Builtin(i) => &i.nested,
708708
ImplSource::AutoImpl(d) => &d.nested,
709709
ImplSource::Closure(c) => &c.nested,
@@ -717,6 +717,23 @@ impl<'tcx, N> ImplSource<'tcx, N> {
717717
}
718718
}
719719

720+
pub fn borrow_nested_obligations_mut(&mut self) -> &mut [N] {
721+
match self {
722+
ImplSource::UserDefined(i) => &mut i.nested,
723+
ImplSource::Param(n, _) => n,
724+
ImplSource::Builtin(i) => &mut i.nested,
725+
ImplSource::AutoImpl(d) => &mut d.nested,
726+
ImplSource::Closure(c) => &mut c.nested,
727+
ImplSource::Generator(c) => &mut c.nested,
728+
ImplSource::Future(c) => &mut c.nested,
729+
ImplSource::Object(d) => &mut d.nested,
730+
ImplSource::FnPointer(d) => &mut d.nested,
731+
ImplSource::TraitAlias(d) => &mut d.nested,
732+
ImplSource::TraitUpcasting(d) => &mut d.nested,
733+
ImplSource::ConstDestruct(i) => &mut i.nested,
734+
}
735+
}
736+
720737
pub fn map<M, F>(self, f: F) -> ImplSource<'tcx, M>
721738
where
722739
F: FnMut(N) -> M,

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+6
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
132132
}
133133
};
134134

135+
// The obligations returned by confirmation are recursively evaluated
136+
// so we need to make sure they have the correct depth.
137+
for subobligation in impl_src.borrow_nested_obligations_mut() {
138+
subobligation.set_depth_from_parent(obligation.recursion_depth);
139+
}
140+
135141
if !obligation.predicate.is_const_if_const() {
136142
// normalize nested predicates according to parent predicate's constness.
137143
impl_src = impl_src.map(|mut o| {

tests/ui/traits/cycle-cache-err-60010.stderr

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
error[E0275]: overflow evaluating the requirement `RootDatabase: RefUnwindSafe`
1+
error[E0275]: overflow evaluating the requirement `SalsaStorage: RefUnwindSafe`
22
--> $DIR/cycle-cache-err-60010.rs:27:13
33
|
44
LL | _parse: <ParseQuery as Query<RootDatabase>>::Data,
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7+
note: required because it appears within the type `PhantomData<SalsaStorage>`
8+
--> $SRC_DIR/core/src/marker.rs:LL:COL
9+
note: required because it appears within the type `Unique<SalsaStorage>`
10+
--> $SRC_DIR/core/src/ptr/unique.rs:LL:COL
11+
note: required because it appears within the type `Box<SalsaStorage>`
12+
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
13+
note: required because it appears within the type `Runtime<RootDatabase>`
14+
--> $DIR/cycle-cache-err-60010.rs:23:8
15+
|
16+
LL | struct Runtime<DB: Database> {
17+
| ^^^^^^^
18+
note: required because it appears within the type `RootDatabase`
19+
--> $DIR/cycle-cache-err-60010.rs:20:8
20+
|
21+
LL | struct RootDatabase {
22+
| ^^^^^^^^^^^^
723
note: required for `RootDatabase` to implement `SourceDatabase`
824
--> $DIR/cycle-cache-err-60010.rs:44:9
925
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//~ ERROR overflow
2+
// A regression test for #111729 checking that we correctly
3+
// track recursion depth for obligations returned by confirmation.
4+
use std::panic::RefUnwindSafe;
5+
6+
trait Database {
7+
type Storage;
8+
}
9+
trait Query<DB> {
10+
type Data;
11+
}
12+
struct ParseQuery;
13+
struct RootDatabase {
14+
_runtime: Runtime<RootDatabase>,
15+
}
16+
17+
impl<T: RefUnwindSafe> Database for T {
18+
type Storage = SalsaStorage;
19+
}
20+
impl Database for RootDatabase {
21+
type Storage = SalsaStorage;
22+
}
23+
24+
struct Runtime<DB: Database> {
25+
_storage: Box<DB::Storage>,
26+
}
27+
struct SalsaStorage {
28+
_parse: <ParseQuery as Query<RootDatabase>>::Data,
29+
}
30+
31+
impl<DB: Database> Query<DB> for ParseQuery {
32+
type Data = RootDatabase;
33+
}
34+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0275]: overflow evaluating the requirement `Runtime<RootDatabase>: RefUnwindSafe`
2+
|
3+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`cycle_via_builtin_auto_trait_impl`)
4+
note: required because it appears within the type `RootDatabase`
5+
--> $DIR/cycle-via-builtin-auto-trait-impl.rs:13:8
6+
|
7+
LL | struct RootDatabase {
8+
| ^^^^^^^^^^^^
9+
note: required for `RootDatabase` to implement `Database`
10+
--> $DIR/cycle-via-builtin-auto-trait-impl.rs:17:24
11+
|
12+
LL | impl<T: RefUnwindSafe> Database for T {
13+
| ------------- ^^^^^^^^ ^
14+
| |
15+
| unsatisfied trait bound introduced here
16+
note: required because it appears within the type `Runtime<RootDatabase>`
17+
--> $DIR/cycle-via-builtin-auto-trait-impl.rs:24:8
18+
|
19+
LL | struct Runtime<DB: Database> {
20+
| ^^^^^^^
21+
22+
error: aborting due to previous error
23+
24+
For more information about this error, try `rustc --explain E0275`.

0 commit comments

Comments
 (0)