Skip to content

A couple of confusing diagnostics with &str slices #26319

Closed
@durka

Description

@durka

https://play.rust-lang.org/?gist=089ca2d7214f6761051c&version=stable

let s = "abc";
let t = if true { s[..2] } else { s };       // confusing error message
let u: &str = if true { s[..2] } else { s }; // clear error message
let v = s[..2];                              // strange error message
let w: &str = s[..2];                        // clear error message

These are four attempts to take a slice of a &str, in two pairs. They are all wrong because indexing a &str returns a str, so you need an extra &. That's not the point. The point is without type annotations, the error diagnostics seem like they could use some improvement.

Especially the first one -- it sounds like it is saying my expression ought to be a str, but it already is. I think it's actually saying the associated type Index::Output ought to be a str, but the way the diagnostic is written, it sounds backwards.

Can either of these diagnostics be improved?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions