Skip to content

Enhance compiler errors with return types #45871

Closed
@ghost

Description

I want to return an object containing a specific type

I tried this code:

fn get_file_parser() {
    let file = File::open("/Users/me/dev/rust/foobar/examples/books.xml").unwrap();
    let file = BufReader::new(file);
    EventReader::new(file)
}

I expected to see this happen:

This could be a better suggestion

fn get_file_parser() -> xml::EventReader<std::io::BufReader<std::fs::File>> {

Instead, this happened:

error[E0308]: mismatched types
  --> src/lib.rs:23:5
   |
23 |     EventReader::new(file)
   |     ^^^^^^^^^^^^^^^^^^^^^^ expected (), found struct `xml::EventReader`
   |
   = note: expected type `()`
              found type `xml::EventReader<std::io::BufReader<std::fs::File>>`
help: try adding a semicolon
   |
23 |     EventReader::new(file);
   |                           ^
help: try adding a return type
   |
20 | fn get_file_parser<T: std::io::Read>() -> xml::EventReader<_> {
   |                                        ^^^^^^^^^^^^^^^^^^^^^^

Meta

rustc --version --verbose:

Backtrace:
rustc 1.21.0 (3b72af9 2017-10-09)
binary: rustc
commit-hash: 3b72af9
commit-date: 2017-10-09
host: x86_64-apple-darwin
release: 1.21.0
LLVM version: 4.0

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.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions