Skip to content

bug: E4001 should hint when undefined variable matches a named return value #1559

@SchoolyB

Description

@SchoolyB

Description

When a function has named return values and the programmer uses one of those names without declaring it, E4001 fires with the generic "undefined variable" message. The user has to cross-reference the separate W2011 warning to understand why. E4001 should detect this case and provide a targeted hint.

Repro

do getValue(x int) -> (result int) {
    if x > 0 {
        mut result int = 42
    }
    return result
}

do main() {
    println(getValue(1))
}

Current output:

error[E4001]: undefined variable 'result'
  --> test.ez:5:12
   |
  5 |     return result
   |            ^

Expected output:

error[E4001]: undefined variable 'result'
  --> test.ez:5:12
   |
  5 |     return result
   |            ^
   = hint: 'result' is a named return value in the function signature — did you forget to declare 'mut result int' at function scope?

Where to look

ezc/src/typechecker/typechecker.c — the code path that emits E4001. When the undefined identifier matches one of the enclosing function's named return value identifiers (tc->current_return_names), append a hint pointing the user to declare the variable explicitly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingerror-messagesRelated to improving error messagestypecheckerRelated to type checking and validation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions