Search code examples
mps

I update type of a variable (e.g. Foo a; -> Bar b) how MPS updates x.field references to the new type?


TL;DR: I know MPS 2017.2 does not update field references. The question is how to get it right, so DSL users would not have to retype all the program in case of a single "variable type change"

Here's an example for MPS's base language:

public class Foo { 
  public int x; 
  public int y; 
} 

public class Bar { 
  public long x; 
  public long z; 
} 

public void test() { 
  Foo a; 
  a.x = 1; // "x" points to the field of class Foo
  a.y = 1; 
}

If I update Foo with Bar in Foo a;, then the test code would look like the same

public void test() { 
  Bar a; 
  a.x = 1; // "x" still points to the field of class Foo
  a.y = 1; // Of course this reference is now invalid, however MPS does not underline that
}

If I update the type of variable a to Bar, then the code in test method would still reference fields of Foo. Of course, check model identifies the broken reference, however I wonder what is the expected way to solve that kind of DSL issues in MPS?

Should "on update" scripts find all the "field usages" and update model accordingly? Should "field type updates" be forbidden and ask user confirmation? (e.g. some sort of refactoring or whatever intention)

I'm building 61131 ST language in MPS, so I'm looking into "static typed language" kind of DSL.


Solution

  • In my opinion this is mostly a bug in MPS. MPS should similar to the typesystem also track the dependencies of scoping rules and the reevaluate them when something changes. MPS for some reason doesn't know that there is a dependency from you dot operation (the field access) to the operand the local variable. So when you change the type of the local variable it won't reevaluate the scope of the operation. If you press F5 it will reevaluate and show the error, but this is usually not the desired behaviour. I think this should get solved in MPS in a generic way.

    Usually MPS does a good job on rebinding references based on the name if they become out of scope but the user has to press F5 to make this happen.

    Other than that we are suffering the same problem as you are that these errors aren't presented to the user the type of a variable gets changed. Even baseLanguage has this problem.

    I have just put up a feature request into the MPS issue tracker, feel free to vote for it and/or add your comments in there.

    https://youtrack.jetbrains.com/issue/MPS-27328