8000 Runtime faults must be rescuable: walker cannot rescue div by zero, BEAM reasons inconsistent · Issue #704 · kexhq/kex · GitHub
[go: up one dir, main page]

Skip to content

Runtime faults must be rescuable: walker cannot rescue div by zero, BEAM reasons inconsistent #704

Open

Description

@akoskovacs
Issue body actions

Decision: runtime faults (division by zero, float overflow / NaN, …) must be catchable by trying/rescue and by a function-level rescue, consistently on both backends. Today BEAM catches them with inconsistent reason values, and the walker doesn't catch them at all.

let zero() -> Integer = 0

main do
  trying do
    IO.printLine("int: ${22 / zero()}")
  rescue
    reason => IO.printLine("rescued int: ${reason}")
  end
  trying do
    IO.printLine("float: ${1.0 / (zero() * 1.0)}")
  rescue
    reason => IO.printLine("rescued float: ${reason}")
  end
  trying do
    IO.printLine("sqrt: ${Math.sqrt(-1.0)}")
  rescue
    reason => IO.printLine("rescued sqrt: ${reason}")
  end
  IO.printLine("after")
end

BEAM (OTP 29):

rescued int: runtime error: Division by zero
rescued float: r.kex:10:34: runtime error: Division by zero
rescued sqrt: :badarith
after

Walker (-R):

Internal error: r.kex:5:31: runtime error: Division by zero

(exit 1; nothing is rescued)

Work implied:

  • Walker: route runtime faults through the same failure path rescue already handles, instead of aborting.
  • Both backends: give the same reason for the same fault, always with file:line:col. No raw Erlang terms: Math.sqrt(-1.0) gives :badarith, although src/stdlib/math.kex documents raises: Math.sqrt: undefined result (NaN).
  • Unhandled faults in user code shouldn't be labelled Internal error. Note that spec/backend_arithmetic_errors.expected currently pins that prefix.
  • Specs: trying/rescue around each fault kind, on both backends. spec/backend_arithmetic_errors.kex covers only uncaught faults.
  • Docs: docs/error-handling.md describes rescue only for .try failures.

Open questions:

  • Scope: arithmetic, surely. What about bad index, no matching clause, very deep recursion, timeouts?
  • What type is reason? Today it's a string or a raw atom. A structured error (e.g. ArithmeticError { kind, location, message }) would let rescue arms match on the kind of failure, as Crystal's rescue ex : DivisionByZeroError does.

Background: the original aim was no exceptions at all, only return values. That's not achievable on BEAM: arithmetic raises badarith, and BEAM floats can't be inf/NaN. Even Gleam, which has no exceptions, keeps panic. Model this leads to: Optional/Result for expected failures, opt-in checked arithmetic (#705), and remaining faults as rescuable panics.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0