Skip to content

Propagate recursion depth in collect_predicates_for_types #111736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2409,9 +2409,10 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
)
});

let obligation = Obligation::new(
let obligation = Obligation::with_depth(
self.tcx(),
cause.clone(),
recursion_depth,
param_env,
ty::TraitRef::new(self.tcx(), trait_def_id, [normalized_ty]),
);
Expand Down
18 changes: 17 additions & 1 deletion tests/ui/traits/cycle-cache-err-60010.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
error[E0275]: overflow evaluating the requirement `RootDatabase: RefUnwindSafe`
error[E0275]: overflow evaluating the requirement `SalsaStorage: RefUnwindSafe`
--> $DIR/cycle-cache-err-60010.rs:27:13
|
LL | _parse: <ParseQuery as Query<RootDatabase>>::Data,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: required because it appears within the type `PhantomData<SalsaStorage>`
--> $SRC_DIR/core/src/marker.rs:LL:COL
note: required because it appears within the type `Unique<SalsaStorage>`
--> $SRC_DIR/core/src/ptr/unique.rs:LL:COL
note: required because it appears within the type `Box<SalsaStorage>`
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
note: required because it appears within the type `Runtime<RootDatabase>`
--> $DIR/cycle-cache-err-60010.rs:23:8
|
LL | struct Runtime<DB: Database> {
| ^^^^^^^
note: required because it appears within the type `RootDatabase`
--> $DIR/cycle-cache-err-60010.rs:20:8
|
LL | struct RootDatabase {
| ^^^^^^^^^^^^
note: required for `RootDatabase` to implement `SourceDatabase`
--> $DIR/cycle-cache-err-60010.rs:44:9
|
Expand Down
74 changes: 74 additions & 0 deletions tests/ui/traits/cycle-recursion-limit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//~ ERROR overflow
// Test that we properly detect the cycle amongst the traits
// here and report an error.

use std::panic::RefUnwindSafe;

trait Database {
type Storage;
}
trait HasQueryGroup {}
trait Query<DB> {
type Data;
}
trait SourceDatabase {
fn parse(&self) {
loop {}
}
}

struct ParseQuery;
struct RootDatabase {
_runtime: Runtime<RootDatabase>,
}
struct Runtime<DB: Database> {
_storage: Box<DB::Storage>,
}
struct SalsaStorage {
_parse: <ParseQuery as Query<RootDatabase>>::Data,
}

impl Database for RootDatabase {
// This would also be an error if we didn't abort compilation on the error
// above.
type Storage = SalsaStorage;
}
impl HasQueryGroup for RootDatabase {}
impl<DB> Query<DB> for ParseQuery
where
DB: SourceDatabase,
DB: Database,
{
type Data = RootDatabase;
}
impl<T> Database for T
where
T: RefUnwindSafe,
T: HasQueryGroup,
{
}

pub(crate) fn goto_implementation(db: &RootDatabase) -> u32 {
// This is not satisfied:
//
// - `RootDatabase: SourceDatabase`
// - requires `RootDatabase: RefUnwindSafe` + `RootDatabase: HasQueryGroup`
// - `RootDatabase: RefUnwindSafe`
// - requires `Runtime<RootDatabase>: RefUnwindSafe`
// - `Runtime<RootDatabase>: RefUnwindSafe`
// - requires `DB::Storage: RefUnwindSafe` (`SalsaStorage: RefUnwindSafe`)
// - `SalsaStorage: RefUnwindSafe`
// - requires `<ParseQuery as Query<RootDatabase>>::Data: RefUnwindSafe`,
// which means `ParseQuery: Query<RootDatabase>`
// - `ParseQuery: Query<RootDatabase>`
// - requires `RootDatabase: SourceDatabase`,
// - `RootDatabase: SourceDatabase` is already on the stack, so we have a
// cycle with non-coinductive participants
//
// we used to fail to report an error here because we got the
// caching wrong.
SourceDatabase::parse(db);
22
}

fn main() {}
25 changes: 25 additions & 0 deletions tests/ui/traits/cycle-recursion-limit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error[E0275]: overflow evaluating the requirement `Runtime<RootDatabase>: RefUnwindSafe`
|
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`cycle_recursion_limit`)
note: required because it appears within the type `RootDatabase`
--> $DIR/cycle-recursion-limit.rs:21:8
|
LL | struct RootDatabase {
| ^^^^^^^^^^^^
note: required for `RootDatabase` to implement `Database`
--> $DIR/cycle-recursion-limit.rs:44:9
|
LL | impl<T> Database for T
| ^^^^^^^^ ^
LL | where
LL | T: RefUnwindSafe,
| ------------- unsatisfied trait bound introduced here
note: required because it appears within the type `Runtime<RootDatabase>`
--> $DIR/cycle-recursion-limit.rs:24:8
|
LL | struct Runtime<DB: Database> {
| ^^^^^^^

error: aborting due to previous error

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