Skip to content

Enhance compiler errors with return types #45871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Nov 8, 2017 · 2 comments
Closed

Enhance compiler errors with return types #45871

ghost opened this issue Nov 8, 2017 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. WG-diagnostics Working group: Diagnostics

Comments

@ghost
Copy link

ghost commented Nov 8, 2017

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

@durka
Copy link
Contributor

durka commented Nov 8, 2017

What specifically would make the suggestion better?

@ghost
Copy link
Author

ghost commented Nov 8, 2017

An improvement could be, to propose the correct type, <std::io::BufReader<std::fs::File> instead of <_> , since this is known to the compiler.
Another thing might be, if we have 2 different suggestion, they could be marked as such.
I really like the compiler and the hint it provides, so this is a small enhancement, which will especially help newcomers

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Nov 9, 2017
@estebank estebank added the WG-diagnostics Working group: Diagnostics label Dec 9, 2017
bors added a commit that referenced this issue Dec 11, 2017
Resolve type on return type suggestion

Partially address #45871.
@estebank estebank closed this as completed Apr 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

2 participants