Auto merge of #53255 - orium:fix-bug-overflow-send, r=arielb1

Add a per-tree error cache to the obligation forest

This implements part of what @nikomatsakis mentioned in  https://github.com/rust-lang/rust/pull/30533#issuecomment-170705871:

> 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](https://github.com/rust-lang/rust/issues/30977#issue-127091096), 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.
This commit is contained in:
bors 2018-09-30 19:41:07 +00:00
commit fc403ad987
10 changed files with 183 additions and 49 deletions

View file

@ -0,0 +1,27 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::rc::Rc;
use std::sync::Arc;
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))));
//~^ ERROR E0277
//~| ERROR E0277
}

View file

@ -0,0 +1,35 @@
error[E0277]: `std::rc::Rc<Foo>` cannot be sent between threads safely
--> $DIR/issue-40827.rs:24:5
|
LL | f(Foo(Arc::new(Bar::B(None))));
| ^ `std::rc::Rc<Foo>` cannot be sent between threads safely
|
= help: within `Bar`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<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`
note: required by `f`
--> $DIR/issue-40827.rs:21:1
|
LL | fn f<T: Send>(_: T) {}
| ^^^^^^^^^^^^^^^^^^^
error[E0277]: `std::rc::Rc<Foo>` cannot be shared between threads safely
--> $DIR/issue-40827.rs:24:5
|
LL | f(Foo(Arc::new(Bar::B(None))));
| ^ `std::rc::Rc<Foo>` cannot be shared between threads safely
|
= help: within `Bar`, the trait `std::marker::Sync` is not implemented for `std::rc::Rc<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`
note: required by `f`
--> $DIR/issue-40827.rs:21:1
|
LL | fn f<T: Send>(_: T) {}
| ^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0277`.

View file

@ -23,5 +23,7 @@ pub struct Bar {
}
fn main() {
let _: AssertSync<Foo> = unimplemented!(); //~ ERROR E0275
let _: AssertSync<Foo> = unimplemented!();
//~^ ERROR E0277
//~| ERROR E0277
}

View file

@ -1,15 +1,33 @@
error[E0275]: overflow evaluating the requirement `Foo: std::marker::Sync`
error[E0277]: `*const Bar` cannot be shared between threads safely
--> $DIR/recursive-requirements.rs:26:12
|
LL | let _: AssertSync<Foo> = unimplemented!(); //~ ERROR E0275
| ^^^^^^^^^^^^^^^
LL | let _: AssertSync<Foo> = unimplemented!();
| ^^^^^^^^^^^^^^^ `*const Bar` cannot be shared between threads safely
|
= help: consider adding a `#![recursion_limit="128"]` attribute to your crate
= note: required because it appears within the type `std::marker::PhantomData<Foo>`
= help: within `Foo`, the trait `std::marker::Sync` is not implemented for `*const Bar`
= note: required because it appears within the type `Foo`
note: required by `AssertSync`
--> $DIR/recursive-requirements.rs:13:1
|
LL | struct AssertSync<T: Sync>(PhantomData<T>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0277]: `*const Foo` cannot be shared between threads safely
--> $DIR/recursive-requirements.rs:26:12
|
LL | let _: AssertSync<Foo> = unimplemented!();
| ^^^^^^^^^^^^^^^ `*const Foo` cannot be shared between threads safely
|
= help: within `Foo`, the trait `std::marker::Sync` is not implemented for `*const Foo`
= note: required because it appears within the type `Bar`
= note: required because it appears within the type `std::marker::PhantomData<Bar>`
= note: required because it appears within the type `Foo`
note: required by `AssertSync`
--> $DIR/recursive-requirements.rs:13:1
|
LL | struct AssertSync<T: Sync>(PhantomData<T>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0275`.
For more information about this error, try `rustc --explain E0277`.