Skip to content

Improve Error-tracking for Standard Library functions #4678

Description

@StachuDotNet

The aim here is to improve trace-ability of errors in stdlib fns.

DvalSource is used throughout Dark's Runtime to track the source of Incomplete or Errors.
It has two values - SourceID of tlid * id and SourceNone (implying we don't have context of the source).
The Interpreter currently does a good job of tracking the sources of Dvals.

When a SourceID is returned (rather than a SourceNone), the Editor does a great job of highlighting the source of an Error:

The stdlib fns, however, do a poor job of tracking the source of errors - all stdlib functions that return a DError currently simply return a DError(SourceNone, "message") without context of which bit of code (often a fn arg) caused the error. I believe this is a limitation of our standard library functions simply not having the 'id's of each of their params.

The definition of BuiltInFnSig is
type BuiltInFnSig = (ExecutionState * List<Dval>) -> DvalTask
and a standard library function demonstrating this limitation may be found in Date::parse:

    { name = fn "Date" "parse" 0
      parameters = [ Param.make "s" TStr "" ]
      returnType = TDate
      description = "Parses a string representing a date and time..."
      fn =
        (function
        | _, [ DStr s ] ->
          match ocamlCompatibleDateParser s with
          | Error () -> Ply(DError(SourceNone, "Invalid date format"))
          | Ok d -> Ply(DDate d)
        | _ -> incorrectArgs ())
      sqlSpec = NotQueryable
      previewable = Pure
      deprecated = ReplacedBy(fn "Date" "parse" 1) }

If we updated BuiltInFnSig to instead look like
BuiltInFnSig = (ExecutionState * List<id * Dval>) -> DvalTask
then this function definition could instead be

      fn =
        (function
        | state, [ (sID, DStr s) ] ->
          match ocamlCompatibleDateParser s with
          | Error () -> Ply(DError(SourceID(state.tlid, sID), "Invalid date format"))
          | Ok d -> Ply(DDate d)
        | _ -> incorrectArgs ())

In order to support such, many things would have to change in the Interpreter (just a bunch of id-tracking and type changes) - I started going down this path, but got lost. This is me recording my thoughts/progress, to see if this makes sense to pursue after a break. What do you think?

Beyond this limitation with Standard Library functions, we also over-use SourceNone, or could seemingly choose a "better" (more precise) id to reference - those can be handled separately.

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