Skip to content
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

fix: prevent infinity evaluate predicate for auto-trait #111985

Closed
wants to merge 1 commit into from
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
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}
}
}

ty::Adt(def, _) => {
let ty = def.sized_constraint(self.tcx()).0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still breaks if you changed the definitions from:

struct A<T>(B<T>);
//~^ ERROR: recursive types `A` and `B` have infinite size
struct B<T>(A<A<T>>);

to

struct A<T>(B<T>, ());
//~^ ERROR: recursive types `A` and `B` have infinite size
struct B<T>(A<A<T>>, ());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried it and there seems to be no panic here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, I've misinterpreted why you're using sized_constraint here.

Checking that sized_constraint matches [ty::Error] doesn't seem like the right way to check that the struct is cyclical, and after that, not registering an AutoImplCandidate if a type is cyclical also seems like the wrong way to avoid an overflow in the solver.

The correct solution is probably to not ICE on the overflow at all, and instead treat it as ambiguity, but that is (as I said above) a bit delicate...

Copy link
Contributor Author

@bvanjoi bvanjoi May 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct solution is probably to not ICE on the overflow at all, and instead treat it as ambiguity

Haha, this is a method I never thought of, because when I look at this part of the code, it seems to be all set up for throwing an error after an overflow.

not registering an AutoImplCandidate if a type is cyclical also seems like the wrong way to avoid an overflow in the solver.

can we write it like this:

 if ty.len() == 1 && matches!(ty[0].kind(), ty::Error(_)) {
    make_overflow_error()                       
 }
candidates.vec.push(AutoImplCandidate)

It should eventually throw the following error:

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's okay to be emitting an overflow error there. Typically we avoid emitting errors from within the trait solver itself (except for pretty specific cases).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we make the following changes:

struct A<T>(B<T>);
struct B<T>(A<T>);

it will not cause a panic because the code has been checked for cycles in the check_evaluation_cycle function (see here).
Therefore, could we add a similar function called check_evaluation_overflow to check if the current stack has an infinite evaluation process?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that that's right either. I think we should probably just make predicate_may_hold_fatal not actually ICE. But then there's only one callsite for predicate_may_hold_fatal anyways, so it's probably best to just inline it and delay a bug if we have overflow.

if !(ty.len() == 1 && matches!(ty[0].kind(), ty::Error(_))) {
candidates.vec.push(AutoImplCandidate)
}
}
_ => candidates.vec.push(AutoImplCandidate),
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/traits/issue-105231.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct A<T>(B<T>);
//~^ ERROR: recursive types `A` and `B` have infinite size
struct B<T>(A<A<T>>);
trait Foo {}
impl<T> Foo for T where T: Send {}
impl Foo for B<u8> {}

fn main() {}
19 changes: 19 additions & 0 deletions tests/ui/traits/issue-105231.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0072]: recursive types `A` and `B` have infinite size
--> $DIR/issue-105231.rs:1:1
|
LL | struct A<T>(B<T>);
| ^^^^^^^^^^^ ---- recursive without indirection
LL |
LL | struct B<T>(A<A<T>>);
| ^^^^^^^^^^^ ------- recursive without indirection
|
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
|
LL ~ struct A<T>(Box<B<T>>);
LL |
LL ~ struct B<T>(Box<A<A<T>>>);
|

error: aborting due to previous error

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