source-academy / java-slang

Implementation of the Java language in TypeScript
Apache License 2.0
0 stars 0 forks source link

Type checker: more detailed type errors needed #53

Open martin-henz opened 2 months ago

martin-henz commented 2 months ago

Example:

class Test {
    public static void main(String[] args) {
    int x = 0;
        x = x * true;
    }
}

javac gives:

Test.java:6: error: bad operand types for binary operator '*'
        x = x * true;
              ^
  first type:  int
  second type: boolean
1 error

Source Academy gives:

Error: bad operand

A few improvements possible:

bryanlohxz commented 2 months ago

I think I will first make sure all errors include location, then look at including the offending character/characters in errors since the error message that I am returning already has a pointer to the general location.

An example here:

image
bryanlohxz commented 2 months ago

At the same time, source academy frontend has a SourceError type that defaults to printing the start and end of the offending part of the program in some kind of format, I am thinking of supporting SourceError as well ontop of the above error message.

xyliew25 commented 2 months ago

I am thinking of supporting SourceError as well ontop of the above error message.

Just to tap on, ECE attempted to support SourceError already, although still missing location info, can check it out here. Perhaps it's a good idea in the long run to share these errors among all components of java-slang, e.g., type checker, compiler, JVM, ECE, as I believe these errors are indifferent to the different components and this improves output consistency as well.