gracelang / minigrace

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

Strange and inconsistent behaviour when using _ as a method name/identifier #322

Open IsaacOscar opened 4 years ago

IsaacOscar commented 4 years ago

I noticed that the following program is accepted:

def _ = 1
def _ = 2

But the following is not:

method _ { 1 }
method _ { 2 }

It produces the following error:

a.grace[2:8]: Syntax error: '_' cannot be redeclared because it is already declared as a method on line 1
   1: method _ { 1 }
   2: method _ { 2 }
-------------^

The same also applies to interface { _; _}

I also note that the following code is not a syntax error, but produces a runtime error:

def _ is public = 1
self._

When run

NoSuchMethod: no method _ on the "a" module (defined at a:1).  Did you mean __1?
  raised from module initialization at a:2
minigrace: program a exited with code 3.

(writing self.__1 does work)

After some thought, the only uses I can see for _ is as a parameter name (of a method, block, trait, etc.) or as the RHS of an import (import "foo" as _). I think all other uses should be syntax errors.

apblack commented 4 years ago

You have certainly found some bugs here, but have also raised an interesting language design issue, which we should resolve before expending effort on the way that _ is compiled.