Repository: rust
Issue: Unused trait bound with lifetime causes compile error
Labels: A-trait-system,T-compiler

## Summary

### What happens
* Unused trait bound causes compile error.
    + In non-(3c) case, `Into::<Arc<B>>::into()` is completely unused, but `&'a B: Into<Arc<B>>` causes compile error.
    + I think this may be a compiler issue.
* With either of these small modifications, they successfully compile.
    + Changing `&a B: Into<Arc<B>>` to `for<'b> &'b B: Into<Arc<B>>`.
    + Changing `&self` to `&'a self` (or `wrap: &OwnedWrap<B>` to `wrap: &'a OwnedWrap<B>`).

### Expected behavior
* Unused trait bound does not cause compile error.
* With `where B: 'a + ToOwned + ?Sized, &'a B: Into<Arc<B>>`,
  `let b: &B = owned.borrow();` successfully compiles.
    + I think `let b: &B = owned.borrow();` does not require `b` to outlive lifetime of `&self` nor `'a`,
      so it should not be error.
    + `owned.borrow().into()` might be error though, but I think it is OK because `owned.borrow()` does not outlive `'a` but `Into<Arc<B>>` requires `'a &B`.

## Meta
* Compiler version: 1.28.0 (stable)

## Code to reproduce

### Simplified cases 1: in impl
Basic code: https://play.rust-lang.org/?gist=40cb0fa71f3bd32091fbc3e144db2de1&version=stable&mode=debug&edition=2015

* error
    + [(1a-2a-3a)](https://play.rust-lang.org/?gist=40cb0fa71f3bd32091fbc3e144db2de1&version=stable&mode=debug&edition=2015)
    + [(1a-2a-3c)](https://play.rust-lang.org/?gist=a84952cfc70e90b715c2fd988c289fe5&version=stable&mode=debug&edition=2015)
* success
    + [(1a-2b-3a)](https://play.rust-lang.org/?gist=8580e01db6f3376f46b091ba91e23334&version=stable&mode=debug&edition=2015)
    + [(1a-2b-3b)](https://play.rust-lang.org/?gist=f0d26a7b0552e3409dc122d82adb37d7&version=stable&mode=debug&edition=2015)
    + [(1a-2b-3c)](https://play.rust-lang.org/?gist=4a26d538074cb7f5d8b174718be5120d&version=stable&mode=debug&edition=2015)
    + [(1b-2a-3a)](https://play.rust-lang.org/?gist=478d6a6871decf303d3a3ddc6d71a920&version=stable&mode=debug&edition=2015)
    + [(1b-2a-3b)](https://play.rust-lang.org/?gist=ef3e72d0063a547e8f37a58f400c588a&version=stable&mode=debug&edition=2015)
    + [(1b-2a-3c)](https://play.rust-lang.org/?gist=153ec65bf83f5a9b64f17d3f59eace95&version=stable&mode=debug&edition=2015)
    + [(1b-2b-3a)](https://play.rust-lang.org/?gist=99f3bfc45e08f14bd9a48053a9a0329b&version=stable&mode=debug&edition=2015)
    + [(1b-2b-3b)](https://play.rust-lang.org/?gist=88f575113591395593c1974378a4e2da&version=stable&mode=debug&edition=2015)
    + [(1b-2b-3c)](https://play.rust-lang.org/?gist=d341d369cbd70b4003c28b16cb5b16fe&version=stable&mode=debug&edition=2015)
* intermediate
    + [(1a-2a-3b)](https://play.rust-lang.org/?gist=49512f2650dab965c3cb808c5f5adc5b&version=stable&mode=debug&edition=2015)
        - Compiles without NLL, but fails to compile with NLL.
        - I filed the difference as another issue. See #53151.

```rust
//#![feature(nll)]

use std::borrow::Borrow;
use std::sync::Arc;

struct OwnedWrap<B: ToOwned + ?Sized>(<B as ToOwned>::Owned);

impl<'a, B> OwnedWrap<B>
where
    B: 'a + ToOwned + ?Sized,
    &'a B: Into<Arc<B>>, // (1a) does not compile.
    //for<'b> &'b B: Into<Arc<B>>, // (1b) successfully compiles.
{
    pub fn to_arc(&self) -> Arc<B> { // (2a) does not compile.
    //pub fn to_shared(&'a self) -> Arc<B> { // (2b) `successfully compiles.
        let OwnedWrap(ref owned) = *self;
        let b: &B = owned.borrow(); // (3a) does not compile.
        //let _: &B = owned.borrow(); // (3b) error with NLL, compiles without NLL.

        //owned.borrow().into() // (3c) What I want to do.
        unimplemented!() // comment in when using 3c.
    }
}

fn main() {}
```

Error for (1a-2a-3a) without NLL:
```
   Compiling playground v0.0.1 (file:///playground)
error[E0495]: cannot infer an appropriate lifetime for pattern due to conflicting requirements
  --> src/main.rs:16:23
   |
16 |         let OwnedWrap(ref owned) = *self;
   |                       ^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 14:5...
  --> src/main.rs:14:5
   |
14 | /     pub fn to_arc(&self) -> Arc<B> { // (2a) does not compile.
15 | |     //pub fn to_shared(&'a self) -> Arc<B> { // (2b) `successfully compiles.
16 | |         let OwnedWrap(ref owned) = *self;
17 | |         let b: &B = owned.borrow(); // (3a) does not compile.
...  |
21 | |         unimplemented!() // comment in when using 3c.
22 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:16:23
   |
16 |         let OwnedWrap(ref owned) = *self;
   |                       ^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 8:1...
  --> src/main.rs:8:1
   |
8  | / impl<'a, B> OwnedWrap<B>
9  | | where
10 | |     B: 'a + ToOwned + ?Sized,
11 | |     &'a B: Into<Arc<B>>, // (1a) does not compile.
...  |
22 | |     }
23 | | }
   | |_^
   = note: ...so that the types are compatible:
           expected std::marker::Sized
              found std::marker::Sized

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
error: Could not compile `playground`.
```

Error for (1a2a-3a) with NLL:
```
   Compiling playground v0.0.1 (file:///playground)
warning: unused variable: `b`
  --> src/main.rs:17:13
   |
17 |         let b: &B = owned.borrow(); // (3a) does not compile.
   |             ^ help: consider using `_b` instead
   |
   = note: #[warn(unused_variables)] on by default

error: unsatisfied lifetime constraints
  --> src/main.rs:17:21
   |
8  | impl<'a, B> OwnedWrap<B>
   |      -- lifetime `'a` defined here
...
14 |     pub fn to_arc(&self) -> Arc<B> { // (2a) does not compile.
   |                   - let's call the lifetime of this reference `'1`
...
17 |         let b: &B = owned.borrow(); // (3a) does not compile.
   |                     ^^^^^^^^^^^^^^ requires that `'1` must outlive `'a`

error: aborting due to previous error

error: Could not compile `playground`.
```

### Simplified cases 2: toplevel fn
Basic code: https://play.rust-lang.org/?gist=649f1a00835b68d4b51ef4a78a5ac857&version=stable&mode=debug&edition=2015

* error
    + [(1a-2a-3a)](https://play.rust-lang.org/?gist=649f1a00835b68d4b51ef4a78a5ac857&version=stable&mode=debug&edition=2015)
    + [(1a-2a-3c)](https://play.rust-lang.org/?gist=b78e9efa43378b31516f24d4a086c019&version=stable&mode=debug&edition=2015)
* success
    + [(1a-2b-3a)](https://play.rust-lang.org/?gist=3724d4a9dab3766058205d4d02c90551&version=stable&mode=debug&edition=2015)
    + [(1a-2b-3b)](https://play.rust-lang.org/?gist=c5626d3ce41dd45ee9a054fad4742a8f&version=stable&mode=debug&edition=2015)
    + [(1a-2b-3c)](https://play.rust-lang.org/?gist=9a29cee74978f19db85c47118d6923b5&version=stable&mode=debug&edition=2015)
    + [(1b-2a-3a)](https://play.rust-lang.org/?gist=836f203fd40be5479ee88c5b3dda01b2&version=stable&mode=debug&edition=2015)
    + [(1b-2a-3b)](https://play.rust-lang.org/?gist=a5b8399be501b3997a5374e263baba61&version=stable&mode=debug&edition=2015)
    + [(1b-2a-3c)](https://play.rust-lang.org/?gist=24f32e715adc916c8e48128418e63225&version=stable&mode=debug&edition=2015)
    + [(1b-2b-3a)](https://play.rust-lang.org/?gist=608735af16df8b30cde89797db546fd5&version=stable&mode=debug&edition=2015)
    + [(1b-2b-3b)](https://play.rust-lang.org/?gist=51168e483c7a5fe6fe06db9ccbbc4a60&version=stable&mode=debug&edition=2015)
    + [(1b-2b-3c)](https://play.rust-lang.org/?gist=e839d72c8906e64c13e5e3a2125b62e4&version=stable&mode=debug&edition=2015)
* intermediate
    + [(1a-2a-3b)](https://play.rust-lang.org/?gist=0ba71d2ee8ab9306953d3ebe02d9d11d&version=stable&mode=debug&edition=2015)
        - Compiles without NLL, but fails to compile with NLL.
        - I filed the difference as another issue. See #53151.

```rust
//#![feature(nll)]

use std::borrow::Borrow;
use std::sync::Arc;

struct OwnedWrap<B: ToOwned + ?Sized>(<B as ToOwned>::Owned);

fn to_arc<'a, B>(wrap: &OwnedWrap<B>) -> Arc<B> // (2a) does not compile.
//fn to_arc<'a, B>(wrap: &'a OwnedWrap<B>) -> Arc<B> // (2b) successfully compiles.
where
    B: 'a + ToOwned + ?Sized,
    &'a B: Into<Arc<B>>, // (1a) does not compile.
    //for<'b> &'b B: Into<Arc<B>>, // (1b) successfully compiles.
{
        let OwnedWrap(ref owned) = wrap;
        let b: &B = owned.borrow(); // (3a) does not compile.
        //let _: &B = owned.borrow(); // (3b) error with NLL, compiles without NLL.

        //owned.borrow().into() // (3c) What I want to do.
        unimplemented!() // comment out when using 3c.

}

fn main() {}
```


### FYI: What I've wanted to do
This code is more meaningful than simplified cases.
https://play.rust-lang.org/?gist=2b304fda2af1102dff60b72b5681a945&version=nightly&mode=debug&edition=2015