Skip to content

Confusing error mesage #52284

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
mmrath opened this issue Jul 12, 2018 · 3 comments
Closed

Confusing error mesage #52284

mmrath opened this issue Jul 12, 2018 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mmrath
Copy link

mmrath commented Jul 12, 2018

Source

    fn main() {
        println!("Hello world");
        let arr1 = [10, 15, 20, 25];
        let arr2 = [1, 5, 9, 13, 17, 21, 25];
        let med = median(&arr1, &arr2);
        println!("{:?}", med);
    }

    fn median(arr1: &[i32], arr2: &[i32]) -> Result<f64, String> {
        use std::cmp;
        if arr1.len() == 0 && arr2.len() == 0 {
            Err("No elements".to_owned())
        }
        let x = arr1.len();
        let y = arr2.len();

        if x > y {
            return median(arr2, arr1);
        }

        let mut low: usize = 0;
        let mut high = x;
        while low <= high {
            let part_x = (low + high) / 2;
            let part_y = (x + y + 1) / 2 - part_x;

            let max_x = if part_x == 0 {
                i32::min_value()
            } else {
                arr1[part_x - 1]
            };
            let min_x = if part_x == x {
                i32::max_value()
            } else {
                arr1[part_x]
            };

            let max_y = if part_y == 0 {
                i32::min_value()
            } else {
                arr2[part_y - 1]
            };
            let min_y = if part_y == y {
                i32::max_value()
            } else {
                arr2[part_y]
            };

            if max_x <= min_y && max_y <= min_x {
                if (x + y) % 2 == 0 {
                    return Ok((cmp::max(max_x, max_y) + cmp::min(min_x, min_y)) as f64 / 2.0);
                } else {
                    return Ok(cmp::max(max_x, max_y) as f64);
                }
            } else if max_x > min_y {
                high = part_x - 1;
            } else {
                low = part_x + 1;
            }
        }
        Err("Median not found".to_owned())
    }]]

Link to playground: https://play.rust-lang.org/?gist=4f80f043a2f29bf2810ecc8f75575f8d&version=stable&mode=debug&edition=2015

Current error message:

error[E0308]: mismatched types
  --> src/main.rs:12:9
   |
12 |         Err("No elements".to_owned())
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: try adding a semicolon: `;`
   |         |
   |         expected (), found enum `std::result::Result`
   |
   = note: expected type `()`
			  found type `std::result::Result<_, std::string::String>`

Expected

The compiler should probably tell me that I am missing a return statement.

@oli-obk oli-obk added the A-diagnostics Area: Messages for errors, warnings, and lints label Jul 12, 2018
@oli-obk
Copy link
Contributor

oli-obk commented Jul 12, 2018

Yea, if the return type matches the type that mismatched against () then we should probably not suggest a semicolon.

We should probably also stop suggesting the addition of a semicolon on expressions like enum variant constructors and similar side effect free expressions

@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 26, 2019
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Oct 2, 2019
@estebank estebank added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Feb 3, 2021
@JohnTitor
Copy link
Member

Current output (nightly):

error[E0308]: mismatched types
  --> src/main.rs:12:9
   |
11 | /     if arr1.len() == 0 && arr2.len() == 0 {
12 | |         Err("No elements".to_owned())
   | |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found enum `Result`
13 | |     }
   | |_____- expected this to be `()`
   |
   = note: expected unit type `()`
                   found enum `Result<_, String>`
help: consider using a semicolon here
   |
12 |         Err("No elements".to_owned());
   |                                      ^
help: consider using a semicolon here
   |
13 |     };
   |      ^
help: you might have meant to return this value
   |
12 |         return Err("No elements".to_owned());
   |         ^^^^^^                              ^

Now we have an additional suggestion with return.

@oli-obk
Copy link
Contributor

oli-obk commented Mar 1, 2021

That looks much more helpful already. I think we can close this and revisit if it comes up again.

@oli-obk oli-obk closed this as completed Mar 1, 2021
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants