amanjpro / languages-a-la-carte

Sana a modular compiler framework
BSD 3-Clause "New" or "Revised" License
6 stars 0 forks source link

Compiled constant expressions #1

Closed amanjpro closed 8 years ago

amanjpro commented 8 years ago

Compiled constant expressions are not seen by the compiler during typer. For example if we compile the following class:

package test;

class A {
  final static int KONSTANT = 2;
}

Then we compile the following class separately with the above class in the classpath:

import test.A;

class B {
   short konstant = A.KONSTANT;      // fails

   int m(short k) {
      switch(k) {
         case A.KONSTANT:
            return 2;   // fails
         case 2:
            return 3;   // should be an error, but the compiler cannot detect it
         default:
            return 4;
      }
   }
}

The rhs part of the field definition fails, as A.KONSTANT is of type int, even though it is a constant expression and within the range of short. The first case of the switch statement in the method m also fails, because the typer cannot see the case guard as a constant expression. The second case, is an erroneous case, as the guard is already defined by the previous case, but the compiler fails to detect it.

The reason behind all the aforementioned problems are the same, which is the compiled classes are not constant folded, the fix is easy by adding an attribute to symbols like in classes and store rhs of compiled fields if they are constant expressions.

amanjpro commented 8 years ago

Closing, commit 6407b4f fixes it