rust/src/test
bors 32fb4dcdd7 Auto merge of #69707 - estebank:impl-trait-missing-bounds, r=Centril
Handle `impl Trait` where `Trait` has an assoc type with missing bounds

When encountering a type parameter that needs more bounds the trivial case is `T` `where T: Bound`, but it can also be an `impl Trait` param that needs to be decomposed to a type param for cleaner code. For example, given

```rust
fn foo(constraints: impl Iterator) {
    for constraint in constraints {
        println!("{:?}", constraint);
    }
}
```

the previous output was

```
error[E0277]: `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug`
 --> src/main.rs:3:26
  |
1 | fn foo(constraints: impl Iterator) {
  |                                    - help: consider further restricting the associated type: `where <impl Iterator as std::iter::Iterator>::Item: std::fmt::Debug`
2 |     for constraint in constraints {
3 |         println!("{:?}", constraint);
  |                          ^^^^^^^^^^ `<impl Iterator as std::iter::Iterator>::Item` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
  |
  = help: the trait `std::fmt::Debug` is not implemented for `<impl Iterator as std::iter::Iterator>::Item`
  = note: required by `std::fmt::Debug::fmt`
```

which is incorrect as `where <impl Iterator as std::iter::Iterator>::Item: std::fmt::Debug` is not valid syntax nor would it restrict the positional `impl Iterator` parameter if it were.

The output being introduced is

```
error[E0277]: `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug`
 --> src/main.rs:3:26
  |
3 |         println!("{:?}", constraint);
  |                          ^^^^^^^^^^ `<impl Iterator as std::iter::Iterator>::Item` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
  |
  = help: the trait `std::fmt::Debug` is not implemented for `<impl Iterator as std::iter::Iterator>::Item`
  = note: required by `std::fmt::Debug::fmt`
help: introduce a type parameter with a trait bound instead of using `impl Trait`
   |
LL | fn foo<T: Iterator>(constraints: T) where <T as std::iter::Iterator>::Item: std::fmt::Debug  {
   |       ^^^^^^^^^^^^^              ^  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

This suggestion is correct and lead the user in the right direction: because you have an associated type restriction you can no longer use `impl Trait`, the only reasonable alternative is to introduce a named type parameter, bound by `Trait` and with a `where` binding on the associated type for the new type parameter `as Trait` for the missing bound.

*Ideally*, we would want to suggest something like the following, but that is not valid syntax today

```
error[E0277]: `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug`
 --> src/main.rs:3:26
  |
3 |         println!("{:?}", constraint);
  |                          ^^^^^^^^^^ `<impl Iterator as std::iter::Iterator>::Item` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
  |
  = help: the trait `std::fmt::Debug` is not implemented for `<impl Iterator as std::iter::Iterator>::Item`
  = note: required by `std::fmt::Debug::fmt`
help: introduce a type parameter with a trait bound instead of using `impl Trait`
   |
LL | fn foo(constraints: impl Iterator<Item: std::fmt::Debug>) {
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

Fix #69638.
2020-04-12 06:32:45 +00:00
..
assembly
auxiliary
codegen Add hash of source files in debug info 2020-04-02 14:13:19 -07:00
codegen-units Keep codegen units unmerged when building compiler builtins 2020-04-06 00:00:00 +00:00
compile-fail Update tests to use llvm_asm! 2020-03-26 15:49:22 +00:00
debuginfo Test and fix gdb pretty printing again 2020-03-26 23:09:20 +00:00
incremental update tests 2020-03-31 19:01:49 +02:00
mir-opt Use write!-style syntax for MIR assert terminator 2020-04-11 13:27:51 +02:00
pretty Update tests to use llvm_asm! 2020-03-26 15:49:22 +00:00
run-fail
run-make
run-make-fulldeps Auto merge of #71031 - Dylan-DPC:rollup-zr8hh86, r=Dylan-DPC 2020-04-11 21:19:28 +00:00
run-pass-valgrind
rustdoc rustdoc: Don't try to load source files from external crates 2020-04-08 18:35:17 +01:00
rustdoc-js
rustdoc-js-std Add tests for new of variables 2020-03-16 18:30:26 +01:00
rustdoc-ui rustc: Add a warning count upon completion 2020-04-11 16:15:24 +02:00
rustfix
ui Auto merge of #69707 - estebank:impl-trait-missing-bounds, r=Centril 2020-04-12 06:32:45 +00:00
ui-fulldeps rustc: Add a warning count upon completion 2020-04-11 16:15:24 +02:00
COMPILER_TESTS.md rust-lang.github.io/rustc-dev-guide -> rustc-dev-guide.rust-lang.org 2020-03-10 17:08:18 -03:00