Skip to content

Confusing error message for lifetime flowing from input to output with different syntax #142515

Closed
@amab8901

Description

@amab8901

Code

fn main() {
    println!("Hello, world!");

    let instance = Structy::default();
    dbg!(&instance.iter());
}

#[derive(Debug, Clone, Default)]
pub(crate) struct Structy {
    pub(crate) vecy: Vec<u16>,
}

impl Structy {
    pub(crate) fn iter(&self) -> std::slice::Iter<u16> {
        self.vecy.iter()
    }
}

Current output

❯ cargo +nightly careful run
Preparing a careful sysroot (target: x86_64-unknown-linux-gnu)... done
   Compiling nice v0.1.0 (...)
warning: lifetime flowing from input to output with different syntax can be confusing
  --> src/main.rs:14:24
   |
14 |     pub(crate) fn iter(&self) -> std::slice::Iter<u16> {
   |                        ^^^^^     --------------------- the lifetime gets resolved as `'_`
   |                        |
   |                        this lifetime flows to the output
   |
   = note: `#[warn(mismatched_lifetime_syntaxes)]` on by default
help: one option is to remove the lifetime for references and use the anonymous lifetime for paths
   |
14 |     pub(crate) fn iter(&self) -> std::slice::Iter<'_, u16> {
   |                                                   +++

warning: `nice` (bin "nice") generated 1 warning (run `cargo fix --bin "nice"` to apply 1 suggestion)

Desired output

none

Rationale and extra context

run this command in CLI to trigger bug:

cargo +nightly careful run

I posted this bug originally at cargo careful but the maintainer said the issue was with rustc.

Other cases

Rust Version

rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-discussionCategory: Discussion or questions that doesn't represent real issues.L-mismatched_lifetime_syntaxesLint: mismatched_lifetime_syntaxesT-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