Skip to content

Reports "not all control paths return a value" when that's not true #1905

Description

@jsternberg

The following code fragment returns the following from the compiler:

fn get_input<T: copy>(default: T, f: fn(str) -> T) -> T {
    alt stdin().read_line() {
      "" { default }
      line { f(line) }
    }
}

fn yes_no_prompt(prompt: str, default: bool) -> bool {
    while true {
        print(prompt);
        let input = get_input(some(default)) {|x|
            alt str::to_lower(x) {
              "y" { some(true) }
              "n" { some(false) }
              _ { none }
            }
        };

        alt input {
          some(x) { ret x; }
          none { println("Invalid input"); }
        }
    };
}
hello.rs:52:53: 68:1 error: In function yes_no_prompt, not all control paths return a value
hello.rs:52 fn yes_no_prompt(prompt: str, default: bool) -> bool {
hello.rs:53     while true {
hello.rs:54         print(prompt);
hello.rs:55         let input = get_input(some(default)) {|x|
hello.rs:56             alt str::to_lower(x) {
hello.rs:57               "y" { some(true) }
            ...
hello.rs:52:48: 52:52 error: see declared return type of 'bool'
hello.rs:52 fn yes_no_prompt(prompt: str, default: bool) -> bool {
                                                            ^~~~

The control paths for this function should be fine. Putting "fail" at the end causes the compiler to work fine, but I haven't been able to find an input that causes it to fail.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions