Skip to content

Result[Unit, E] function ending in an assignment gets no implicit Ok(()): "return type mismatch" #1296

Closed

Description

@ehartford

Found rewriting `examples/json-parser` under #1150 ruling 4 (`mut fn bump() -> Result[Unit, JsonError]: self.current = self.tokenizer.next_token()?`).

§10 / docs/with_for_ai.md §3: "For `Result[Unit, E]`, ending the function normally returns `Ok(())`." A function whose last statement is a call (`print(...)`) gets the implicit `Ok(())`; one whose last statement is an assignment or compound assignment does not:

```
error E = Bad
type P { cur: Option[i32] = None, n: i32 = 0 }
fn next(n: i32) -> Result[Option[i32], E]:
if n > 3: return Err(.Bad)
Some(n)
extend P:
mut fn bump() -> Result[Unit, E]:
self.cur = next(self.n)? // error: return type mismatch
mut fn bump2() -> Result[Unit, E]:
let c = next(self.n)?
self.cur = c // error: return type mismatch
mut fn bump3() -> Result[Unit, E]:
self.n += 1 // error: return type mismatch
```

Expected: all three compile and return `Ok(())`. Installed `with v0.15.2.1-g478b26481`. Likely the D43 tail inference treats an assignment tail as a value of the assigned type rather than as a statement ending the function.

The example spells an explicit `Ok(())` after the assignment until this lands.

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