gracelang / minigrace

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

Bad error message with multiple inheritance #302

Closed KimBruce closed 4 years ago

KimBruce commented 4 years ago

Consider the following incorrect program:

class a {
    method m(n) {print "in a"}
}

class b {
    method m1(p) {print "in b"}
}

class c {
    inherit a
       alias am(n) = m(n)
    inherit b
       alias bm(n) = m1(n)
    print "in c"

    method n {
        am(2)
        bm(3)
    }
}

c.n

The mistake is that we are inheriting from two classes, which is illegal. The minimal correction for this is to change one of the two "inherit" statements to "use" (changing a class to a trait is technically optional, but not enforced). The problem is the error message:

Syntax error: unknown method 'am(_)'; this may be a spelling mistake, or an attempt to access a method in another scope
    in "testalias" (line 17, column 9-13)

There should be a check to rule out the multiple inheritance, rather than just silently dropping the first inherit statement.

apblack commented 4 years ago

Fixed in commit ba9524d, which also adds a test.

There is just one superclass slot in the parse node for an object, so the second inherit statement was simply overwriting the first. The fix is to check that there is no existing inherit statement before storing the one that has just been parsed.