gracelang / minigrace

Self-hosting compiler for the Grace programming language
39 stars 22 forks source link

Type Errors do not give helpfull error messages #280

Closed IsaacOscar closed 5 years ago

IsaacOscar commented 5 years ago

Error messages given for type errors are not very usefull, e.g:

TypeError: result of method foo(_) does not have type Bar.
It is missing method bar.

This dosn't tell me what it is that was passed to the foo(_) method is trying to return.

I have "fixed" this by adding the indicated line to "/usr/lib/grace/modules/gracelib.js":

function raiseTypeError(msg, type, value) {
    var diff;
    if (GraceDone === value) {
        diff = " — it is `done`.";
    } else {
        var mm = do_import("mirrors", gracecode_mirrors);
        try {
             var tc = callmethod(mm, "loadDynamicModule(1)", [1], new GraceString("typeComparison"));
             var missing = callmethod(tc, "methodsIn(1)missingFrom(1)", [1, 1], type, value)._value;
             var s = (missing.includes(" ")) ? "s " : " ";
             diff = ".\nIt is missing method" + s + missing + "." +
/*ADDED--------->*/"\nThe object was created by \"" + value.className + "\" in module \"" + value.definitionModule + "\" (line " + value.definitionLine + ").";
        } catch (ex) {
             // if something goes wrong while generating the message, just give up
        }
        if (! diff) { diff = ""; }
    }
    var ex = new GraceExceptionPacket(TypeErrorObject,
                                      new GraceString(msg + diff));
    throw ex;
}

I havn't added it to the source code as there are 9 files definining that function, and I don't know which is the appropriate one to modify.

Now it will also print

The object was created by "baz(_)" in module "Baz" (line 3).

Indicating "what" the object is (i.e. a Baz). This is very helpfull for debugging grace programs!

apblack commented 5 years ago

This is a good suggestion. There is already a JavaScript function in gracelib.js called describe that attempts to build a useful description of an arbitrary Grace object. Do you think that it should add the definition module and line number? This function could then be called from raiseTypeError.

The master version of gracelib.js is in the js directory. The other copies are made as part of the build process.

apblack commented 5 years ago

Solved by 79adeec9b8eee0a9472c9b6329202408916f6ec9