Skip to content

Don't suggest dereferencing on &dyn T where T is a trait #115569

Closed
@typesanitizer

Description

@typesanitizer

Code

fn use_iterator<I>(itr: I) where I: IntoIterator<Item = i32> {}

fn pass_iterator<I>(i: &dyn IntoIterator<Item = i32, IntoIter = I>)
where I: Iterator<Item = i32> {
    use_iterator(i);
}


### Current output

```Shell
error[E0277]: `&dyn IntoIterator<IntoIter = I, Item = i32>` is not an iterator
 --> src/main.rs:5:18
  |
5 |     use_iterator(i);
  |     ------------ ^ `&dyn IntoIterator<IntoIter = I, Item = i32>` is not an iterator
  |     |
  |     required by a bound introduced by this call
  |
  = help: the trait `Iterator` is not implemented for `&dyn IntoIterator<IntoIter = I, Item = i32>`
  = note: required for `&dyn IntoIterator<IntoIter = I, Item = i32>` to implement `IntoIterator`
note: required by a bound in `use_iterator`
 --> src/main.rs:1:37
  |
1 | fn use_iterator<I>(itr: I) where I: IntoIterator<Item = i32> {}
  |                                     ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `use_iterator`
help: consider dereferencing here
  |
5 |     use_iterator(*i);
  |                  +


### Desired output

```Shell
I'm not 100% sure, but I can't think of any situation under which `*` on `&dyn` is actually sensible given that `&dyn` is dynamically sized. So that suggestion should at least be removed.

Rationale and extra context

No response

Other cases

No response

Anything else?

Playground link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=84f3066368cc498af5c078514f84525a

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions