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

Infinite recursion error instead of trait bound error #40827

Closed
dnorman opened this issue Mar 25, 2017 · 5 comments · Fixed by #53255
Closed

Infinite recursion error instead of trait bound error #40827

dnorman opened this issue Mar 25, 2017 · 5 comments · Fixed by #53255
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dnorman
Copy link

dnorman commented Mar 25, 2017

Observed behavior:
Receiving the following error when attempting to compile the sample program.

Expected behavior:
Indicate that there is a trait bound error.


rustc 1.16.0 (30cf806ef 2017-03-10)
error[E0275]: overflow evaluating the requirement `TransmitterInternal: std::marker::Send`
  --> <anon>:22:16
   |
22 | fn g(x: Memo) {f(x)}
   |                ^
   |
   = note: consider adding a `#![recursion_limit="128"]` attribute to your crate
   = note: required because of the requirements on the impl of `std::marker::Sync` for `std::sync::Arc<TransmitterInternal>`
   = note: required because it appears within the type `SlabRef`
   = note: required because of the requirements on the impl of `std::marker::Sync` for `std::ptr::Unique<SlabRef>`
   = note: required because it appears within the type `alloc::raw_vec::RawVec<SlabRef>`
   = note: required because it appears within the type `std::vec::Vec<SlabRef>`
   = note: required because it appears within the type `TransmitterInternal`
   = note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Arc<TransmitterInternal>`
   = note: required because it appears within the type `SlabRef`
   = note: required because of the requirements on the impl of `std::marker::Send` for `std::ptr::Unique<SlabRef>`
   = note: required because it appears within the type `alloc::raw_vec::RawVec<SlabRef>`
   = note: required because it appears within the type `std::vec::Vec<SlabRef>`
   = note: required because it appears within the type `TransmitterInternal`
   = note: required because of the requirements on the impl of `std::marker::Sync` for `std::sync::Arc<TransmitterInternal>`
   = note: required because it appears within the type `SlabRef`
...

Minimal reproduction case:

https://is.gd/KgD5Tk
Many thanks to the tireless efforts of @stephaneyfx in creating the repro above!

Bonus round
I haven't yet figured out a minimal repro for this second part, but if I'm interpreting correctly, it's possible that a variant of this endless recursion error does not require a trait bound error to be triggered. (noob warning: it's possible that the omission of +Send+Sync in 4ab0fdf is actually a trait bounds error, but I don't presently understand why)

Non-minimal repro:
https://travis-ci.org/dnorman/unbase/jobs/209643435#L240

Minimal change that fixed it:
dnorman/unbase@47c68bf
https://travis-ci.org/dnorman/unbase/jobs/209649602#L228

@stephaneyfx
Copy link
Contributor

This is a simplified example reproducing the issue (playground).

use std::rc::Rc;
use std::sync::Arc;

// Rc<_>: !Send
// => Bar: !Send
// => Arc<Bar>: !Send
// => Foo: !Send
//
// Shouldn't the compiler come to the same conclusion instead of entering this
// infinite recursion?
struct Foo(Arc<Bar>);

enum Bar {
    A(Rc<Foo>),
    B(Option<Foo>),
}

fn f<T: Send>(_: T) {}

fn main() {
    f(Foo(Arc::new(Bar::B(None))));
}
rustc 1.16.0 (30cf806ef 2017-03-10)
error[E0275]: overflow evaluating the requirement `Foo: std::marker::Send`
  --> <anon>:21:5
   |
21 |     f(Foo(Arc::new(Bar::B(None))));
   |     ^
   |
   = note: consider adding a `#![recursion_limit="128"]` attribute to your crate
   = note: required because it appears within the type `std::option::Option<Foo>`
   = note: required because it appears within the type `Bar`
   = note: required because of the requirements on the impl of `std::marker::Sync` for `std::sync::Arc<Bar>`
   = note: required because it appears within the type `Foo`
   = note: required because it appears within the type `std::option::Option<Foo>`
   = note: required because it appears within the type `Bar`
   = note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Arc<Bar>`
   = note: required because it appears within the type `Foo`
...

@stephaneyfx
Copy link
Contributor

Note that if Rc is removed (or replaced by Arc, for example), the compiler does not enter this infinite recursion and deduces Foo is Send (playground).

@Mark-Simulacrum Mark-Simulacrum added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 20, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 27, 2017
@dnorman
Copy link
Author

dnorman commented Aug 18, 2017

This is the case I was referring to @aturon :)

@aturon
Copy link
Member

aturon commented Aug 18, 2017

@dnorman Thanks! Fascinating bug...

@orium
Copy link
Member

orium commented Jul 23, 2018

I think I have hit the same bug with

use std::rc::Rc;
use std::sync::Arc;

struct List {
    value: Rc<i32>,
    next:  Option<Arc<List>>
}

fn is_send() {
    let _: Box<Send> = Box::new(List { value: Rc::new(4), next: None });
}
error[E0275]: overflow evaluating the requirement `std::sync::Arc<List>: std::marker::Send`
  --> src/lib.rs:12:24
   |
12 |     let _: Box<Send> = Box::new(List { value: Rc::new(4), next: None });
   |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: consider adding a `#![recursion_limit="128"]` attribute to your crate
   = note: required because it appears within the type `std::option::Option<std::sync::Arc<List>>`
   = note: required because it appears within the type `List`
   = note: required because of the requirements on the impl of `std::marker::Sync` for `std::sync::Arc<List>`
   = note: required because it appears within the type `std::option::Option<std::sync::Arc<List>>`

I did some digging around in the trait resolver and I believe what causes it is pointed by @nikomatsakis in #30533 (comment):

  1. If you find that a new obligation is a duplicate of one already in the tree, the proper processing is:
    • if that other location is your parent, you should abort with a cycle error (or accept it, if coinductive)
    • if that other location is not an ancestor, you can safely ignore the new obligation

If this is truly the cause of the bug I will give it a go and try to fix it, but I would really appreciate some mentoring instructions. @nikomatsakis can you give me a few pointers?

orium added a commit to orium/rust that referenced this issue Sep 30, 2018
bors added a commit that referenced this issue Sep 30, 2018
Add a per-tree error cache to the obligation forest

This implements part of what @nikomatsakis mentioned in  #30533 (comment):

> 1. If you find that a new obligation is a duplicate of one already in the tree, the proper processing is:
>      * if that other location is your parent, you should abort with a cycle error (or accept it, if coinductive)
>      * if that other location is not an ancestor, you can safely ignore the new obligation

In particular it implements the "if that other location is your parent accept it, if coinductive" part.  This fixes #40827.

I have to say that I'm not 100% confident that this is rock solid.  This is my first pull request 🎉, and I didn't know anything about the trait resolver before this.  In particular I'm not totally sure that comparing predicates is enough (for instance, do we need to compare `param_env` as well?).  Also, I'm not sure what @nikomatsakis mentions [here](#30977 (comment)), but it might be something that affects this PR:

> In particular, I am wary of getting things wrong around inference variables! We can always add things to the set in their current state, and if unifications occur then the obligation is just kind of out-of-date, but I want to be sure we don't accidentally fail to notice that something is our ancestor. I decided this was subtle enough to merit its own PR.

Anyway, go ahead and review 🙂.

Ref #30977.

# Performance

We are now copying vectors around, so I decided to do some benchmarking.  A simple benchmark shows that this does not seem to affect performance in a measurable way:

I ran `cargo clean && cargo build` 20 times on actix-web (84b27db) and these are the results:

```text
rustc master:

            Mean        Std.Dev.    Min         Median      Max
real        66.637      2.996       57.220      67.714      69.314
user        307.293     14.741      258.093     312.209     320.702
sys         12.524      0.653       10.499      12.726      13.193

rustc fix-bug-overflow-send:

            Mean        Std.Dev.    Min         Median      Max
real        66.297      4.310       53.532      67.516      70.348
user        306.812     22.371      236.917     314.748     326.229
sys         12.757      0.952       9.671       13.125      13.544
```

I will do a more comprehensive benchmark (compiling rustc stage1) and post the results.

r? @nikomatsakis, @nnethercote

PS: It is better to review this commit-by-commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants