mozilla/rust — Issue #1905: Reports "not all control paths return a value" when that's not true
Author: jsternberg
Created: 2012-02-26 18:42:04+00:00
State: open

The following code fragment returns the following from the compiler:

```rust
fn get_input(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.