Skip to content

E0495 shows the same found and expected #87109

Closed
@MarkTanashchuk

Description

@MarkTanashchuk

Given the following code, the current output in Rust Analyzer is:

cannot infer an appropriate lifetime for autoref due to conflicting requirements
  expected `std::str::Chars<'_>`
     found `std::str::Chars<'_>`(rustc E0495)
lib.rs(6, 20): first, the lifetime cannot outlive the anonymous lifetime defined on the method body at 6:20...
lib.rs(8, 20): ...so that reference does not outlive borrowed content
lib.rs(5, 16): but, the lifetime must be valid for the lifetime `'_` as defined on the impl at 5:16...
lib.rs(8, 20): ...so that the expression is assignable

And in the playground:

error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
 --> src/main.rs:8:27
  |
8 |             chars: source.chars()
  |                           ^^^^^
  |
note: first, the lifetime cannot outlive the anonymous lifetime defined on the method body at 6:20...
 --> src/main.rs:6:20
  |
6 |     fn new(source: &str) -> Self {
  |                    ^^^^
note: ...so that reference does not outlive borrowed content
 --> src/main.rs:8:20
  |
8 |             chars: source.chars()
  |                    ^^^^^^
note: but, the lifetime must be valid for the lifetime `'_` as defined on the impl at 5:16...
 --> src/main.rs:5:16
  |
5 | impl SomeChars<'_> {
  |                ^^
note: ...so that the expression is assignable
 --> src/main.rs:8:20
  |
8 |             chars: source.chars()
  |                    ^^^^^^^^^^^^^^
  = note: expected `Chars<'_>`
             found `Chars<'_>`

I think it would be helpful to add a tip that brings the code to something like in this playground and, if possible, make the Chars<'_> in 'found'/'expected' different

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