Skip to content

Error message mentions impl trait even though no traits are involved #84160

Closed
@MikailBag

Description

@MikailBag

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=0aed05fe97333e35dc688bc76eabd8ad

use std::path::Path;

fn path_starts_with<'a>(path: &'a Path) -> Option<&'a Path> {
    if false {
        return None;
    }
    return Path::new("/");
}

The current output is:

// ... standard error about type mismatch
= note: to return `impl Trait`, all returned values must be of the same type
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
  

The error message should not message impl Trait, because it has nothing to do with this code (from user PoV)

If I change the code to the following:

use std::path::Path;

fn path_starts_with<'a>(/*no parameter here anymore*/) -> Option<&'a Path> {
    if false {
        return None;
    }
    return Path::new("/");
}

Confusing message goes away

Metadata

Metadata

Assignees

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