gracelang / minigrace

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

Error compiling match(_)case(_) #332

Closed KimBruce closed 3 years ago

KimBruce commented 3 years ago

Compiler error in identifierResolution.grace on line 244. It is apparently calling method uid on "done". This was raised by compiling the same code that generated the last compiler error.

Error message:

Internal compiler error at line 0 of native code. NoSuchMethod: no method uid on done (defined in module built-in library).
NoSuchMethod: no method uid on done (defined in module built-in library).
  raised from serializeVariable(_)withName(_)in(_) at line 244 of identifierresolution
  requested from block.apply(_,_) at line 181 of identifierresolution
  requested from block.apply(_,_) at line 324 of scope
  requested from nameDictionary(_).keysAndValuesDo(_) at line 1247 of collections
  requested from objectScope.localAndReusedNamesAndValuesDo(_)filteringOut(_) at line 322 of scope
  requested from objectScope.localAndReusedNamesAndValuesDo(_) at line 765 of scope
  requested from block.apply at line 1 of identifierresolution
  requested from while(_)do(_) at line 113 of intrinsic
  requested from generateGctForModule(_) at line 173 of identifierresolution
  requested from writeGctForModule(_) at line 125 of identifierresolution
  requested from compile(_,_,_,_) at line 1717 of genjs
  requested from block.apply at line 82 of compiler
  requested from compileInputFile at line 23 of compiler
Compilation terminated.
    in "minigrace"
apblack commented 3 years ago

Please provide the code that generates this error

KimBruce commented 3 years ago

After lots of experimentation, here is pretty minimal code that generates the problem:

class pieceOn (colorNum: Number) {

    def current: Number = match(colorNum)
        case {1 -> 2}
        case {2 -> 3}
        case {3 -> 4}
        case {4 -> 5}

}

The error goes away if you replace the "def" by "var". I suspect it is an initialization timing problem with match of the right hand side.

Here is the error message again:

Internal compiler error at line 0 of native code. NoSuchMethod: no method uid on done (defined in module built-in library).
NoSuchMethod: no method uid on done (defined in module built-in library).
  raised from serializeVariable(_)withName(_)in(_) at line 244 of identifierresolution
  requested from block.apply(_,_) at line 181 of identifierresolution
  requested from block.apply(_,_) at line 324 of scope
  requested from nameDictionary(_).keysAndValuesDo(_) at line 1247 of collections
  requested from objectScope.localAndReusedNamesAndValuesDo(_)filteringOut(_) at line 322 of scope
  requested from objectScope.localAndReusedNamesAndValuesDo(_) at line 765 of scope
  requested from block.apply at line 1 of identifierresolution
  requested from while(_)do(_) at line 113 of intrinsic
  requested from generateGctForModule(_) at line 173 of identifierresolution
  requested from writeGctForModule(_) at line 125 of identifierresolution
  requested from compile(_,_,_,_) at line 1717 of genjs
  requested from block.apply at line 82 of compiler
  requested from compileInputFile at line 23 of compiler
Compilation terminated.
    in "minigrace"
apblack commented 3 years ago

Thank you! Although it's nice to have minimal code that provokes the error, just linking to the file in a repo that contains any code that provokes the error is ok. Not having the code makes it impossible to debug.

This is not an initialization problem. It's a compiler bug arising when trying to construct the scope for current. When current is a var, no attempt is made to construct its scope, because the scope can be different after each assignment.

KimBruce commented 3 years ago

The reason it took time is it was part of a complex program involving 5 files and lots of interconnections. Interestingly when I started cutting it down I got a few different (but likely related) problems. Hopefully they’ll all go away when this is fixed.

Thanks for looking at this!

apblack commented 3 years ago

This is fixed in commit cc7ac00 (minigrace version 5056)

The problem was forgetting to return the calculated result in the attributeScope method on matchCase AST nodes. A type-checker would have caught this.