Skip to content

E0308 makes incorrect proposal on range reference, missing parantheses #73553

Closed
@phorward

Description

@phorward

Hi there!

With this code (here's a playground)

type Range = std::ops::Range<usize>;

fn demo(r: &Range) {
    println!("{:?}", r);
}

fn tell(x: usize) -> usize {
    x
}

fn main() {
    demo(tell(1)..tell(10)); // here, it is reported wrongly!
    demo(1..10); // here, it is reported correctly!
}

The compiler reports this error message with a hint

error[E0308]: mismatched types
  --> src/main.rs:13:10
   |
13 |     demo(tell(1)..tell(10)); // here, it is reported wrongly!
   |          ^^^^^^^^^^^^^^^^^
   |          |
   |          expected reference, found struct `std::ops::Range`
   |          help: consider borrowing here: `&tell(1)..tell(10)`
   |
   = note: expected reference `&std::ops::Range<usize>`
                 found struct `std::ops::Range<usize>`

but the hint is incorrect due to missing parantheses in this case. The correct answer would be

   |          help: consider borrowing here: `&(tell(1)..tell(10))`

It works correctly when the second line in main is executed, where the range is provided directly.

Meta

rustc --version --verbose:

1.44.1

This issue has been assigned to @ayazhafiz via this comment.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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